Generate a code from a java class that uses another java class (BCEL) - bcel

I am trying to generate(and modify) the code of an output class from another class using the ByteCode Engineering Library (by Apache) .
String class_name = c_gen.getClassName();
Method[] Methods = c_gen.getMethods();
for (int i=0;i<Methods.length;i++)
{
MethodGen m_gen = new MethodGen(Methods[i], class_name, cpg);
InstructionList il = m_gen.getInstructionList();
InstructionHandle h;
il.insert(h,factory.createInvoke("ClassName","printSomething", Type.VOID,new Type[]{Type.STRING}, INVOKESTATIC));
}
so I am trying to call printSomething from ClassName for every method.The problem is that I don't know how to actually pass the string argument to the method printSomething

You will need to push the string argument on the stack before the invokestatic. This is done with the LDC opcode. Something like:
il.insert( new LDC(cpg.addString("MyString")));
The outline looks like this:
JavaClass clazz = Repository.lookupClass( class_name );
ClassGen c_gen = new ClassGen( clazz );
ConstantPoolGen cpg = new ConstantPoolGen( clazz.getConstantPool() );
InstructionFactory factory = new InstructionFactory( c_gen, cpg );
Methods [] methods = clazz.getMethods();
for ( int i = 0; i < methods.length; i ++ )
{
if ( m.isAbstract() || m.isNative() || .... )
continue;
MethodGen m_gen = new MethodGen( methods[i], class_name, cpg );
InstructionList il = m_gen.getInstructionList();
il.insert( factory.createInvoke("ClassName", "printSomething",
Type.VOID, new Type[]{Type.STRING}, INVOKESTATIC) );
il.insert( factory.createPush( "StringToPrint" ) );
methods[i] = m_gen.getMethod();
}
clazz.setConstantPool( cpg.getFinalConstantPool() );
clazz.setMethods( methods ); // might be redundant
clazz.dump( new File( .... ) );
A few notes:
Since we're inserting, every insert will prepend to the method. This is why we first insert the instruction opcode, and then the arguments (in reverse), so that the actual sequence will be ldc #stringref; invokestatic #methodref.
We need to replace the ConstantPool and the Methods with our modified versions of them.

Related

Scoping classes created in Lua using Luabind

I am aware that Lua classes can be created using the OO system that Luabind exposes to Lua:
http://www.rasterbar.com/products/luabind/docs.html#defining-classes-in-lua
class 'lua_testclass'
function lua_testclass:__init(name)
self.name = name
end
function lua_testclass:print()
print(self.name)
end
a = lua_testclass('example')
a:print()
However I am unable to figure out how to scope the class within another namespace so I can do the following:
a = MyScope.lua_testclass('example')
a:print()
Anyone has a idea. I do not want my classes to pollute the global namespace in Lua.
Luabind's class function will always pollute the global table. However, you can clean up after it:
function newclass(name)
oldglobal = _G[name]
class(name)
cls = _G[name]
_G[name] = oldglobal
return cls
end
Then you would use it like this:
MyScope.lua_testclass = newclass 'lua_testclass'
Analogous to local mod = require 'mod' you have to spell the name of the class twice, but you could easily build another function on top of this that could be used like setclass(MyScope, 'lua_testclass'), automatically putting the class into MyScope:
function setclass(scope, name) scope[name] = newclass(name) end
Disclaimer: All this code is entirely untested.
I did mine a little differently, but it's generally the same concept. Mine doesn't create the class, but rather just moves it. I also implemented it on the C++ side.
To implement what I did in Lua, you would write:
function moveClass(name)
oldGlobal = _G[name]
_G[name] = nil
return oldGlobal
end
To implement it in C++, you would write:
luabind::module(lua) [
luabind::def("moveClass", +[](lua_State * lua, std::string name) {
// In the case the class does not exist, this will just
// remove nil and return nil. That essentially does nothing.
luabind::object oldGlobal = luabind::globals(lua)[name];
luabind::globals(lua)[name] = luabind::nil;
return oldGlobal;
})
];
So now if you were to use that to move a class you created, you would do this:
class 'MyClass'
myTable = {}
myTable.MyClass = moveClass 'MyClass'
As an extra note, if you want the moveClass function to give an error in the case that the class you are trying to move does not exist, use luabind::type(oldGlobal) == LUA_TNIL to determine if the class existed or not.
Example:
luabind::module(lua) [
luabind::def("moveClass", +[](lua_State * lua, std::string name) {
luabind::object oldGlobal = luabind::globals(lua)[name];
if (luabind::type(oldGlobal) == LUA_TNIL) {
throw std::runtime_error("Class does not exist.");
}
luabind::globals(lua)[name] = luabind::nil;
return oldGlobal;
})
];

Mixin overshadowing

Let's say i have code like this :
class A{ foo() => "A";}
class B{ foo() => "B";}
class C{ foo() => "C";}
class Mix extends A with B,C {
foo() => "MIX";
bar() => super.foo();
}
class MessABC = Object with A,B,C;
class MessBCA = Object with B,C,A;
class MessCAB = Object with C,A,B;
void main() {
Mix mix = new Mix();
MessABC mABC = new MessABC();
MessBCA mBCA = new MessBCA();
MessCAB mCAB = new MessCAB();
print("Mix.foo = ${mix.foo()} Mix.bar = ${mix.bar()} \n"
"mABC.foo = ${mABC.foo()} \n"
"mBCA.foo = ${mBCA.foo()} \n"
"mCAB.foo = ${mCAB.foo()} \n");
}
The output
Mix.foo = MIX Mix.bar = C
mABC.foo = C
mBCA.foo = A
mCAB.foo = B
On the mix object I can call both Mix.foo and C.foo with the super (actually i expected A.foo)
But can i somehow call A.foo and B.foo ?
Also i am a bit confused by the with semantics. A,B,C Looks like "one rank enumeration" but the order is important. And "Dart Editor" doesn't warn me about the name collision. Perhaps i can enable this warning?
To me it feels like something that makes you confused and error-prone.
Mixin application is processed from left to right.
The superclass of Object with A,B,C is Object with A,B, which again has the superclasses Object with A and then Object.
The overriding rules for mixins are the same as for non-mixin superclasses. Your C.foo in Mix overrides the ones inherited from A and B, so Mix.bar's super.foo() only reaches the C.foo, and there is no way to access A.foo or B.foo.
Overriding is perfectly ok, and doesn't cause any warnings, as long as the methods have compatible signatures.
The most thorough description of mixins is still https://www.dartlang.org/articles/mixins/

Issues with setting a property

Below is the gist of the system i'm having issues with. I seem to understand self, ., and :. I just seem to be missing something. What's happening is that when it calls "Object:setSomeObjectIsAttachedTo()" if I simply print "self" i'll get a table address printed. If i go a step further and try to print "self.someObject" i get nil, which shouldn't happen because in Object it has a key someObject which was created at the start in "Object.new(args)" of course if tried to go a step further it wouldn't even be able to go there since its nil. Please Help!!
Object File
Object = {};
ObjectMeta = {__index = Object};
function Object.new(args)
Obj = {};
Object.someObject = OtherObject.new(args)
return setmetatable(Obj,ObjectMeta );
end
function Object:setSomeObjectIsAttachedTo()
--OtherObject instance Should set its attached property to
--This instance of Object
self.someObject.ObjectImAttachedTo = self;
end
--Calls after new to set the ObjectImAttachedTo Property, So it isnt nil
Object:setSomeObjectIsAttachedTo();
return Object;
OtherObject File
OtherObject = {};
OtherObjectMeta = {__index = OtherObject};
function OtherObject.new(args)
Obj = {};
Obj.ObjectImAttachedTo =nil;
return setmetatable(Obj,ObjectMeta );
end
return Object;
UPDATE
Scene
Scene = {};
ObjectContainer = {};
function Scene.new()
end
function Scene.addObjects()
local Object= require "Object"
local StartX = 50;
local StartY = 20;
local counter = 0;
for i=0, 17 do
ObjectContainer[i] = Object.new({x=StartX,y=StartY});
end
end
Scene.addObjects();
return Scene
end
The table Object does not have a field named someObject, though instances returned by Object.new() do have that field. These are two different tables, roughly corresponding to a class and one of its instances.
ADDENDUM
With the revised code, Object doesn't have the field someObject until you call Object.new(). So, you must call Object.new() before you call Object:setSomeObjectIsAttachedTo();. Note that OtherObject.new() must be defined before you can call Object.new().
ADDENDUM2 in answer to question "so what would you suggest i do to fix this?"
function Object.new(args)
Obj = {};
Obj.someObject = OtherObject.new(args)
Obj.someObject.ObjectImAttachedTo = Obj;
return setmetatable(Obj,ObjectMeta );
end
and get rid of Object:setSomeObjectIsAttachedTo();

How to dynamically generate variables in Action Script 2.0

I have a for loop in action script which I'm trying to use to dynamically create variable.
Example
for( i = 0 ; i &lt 3 ; i++)
{
var MyVar+i = i;
}
after this for loop runs, i would like to have 3 variables named MyVar1, MyVar2, MyVar3. I know the code above will give you a syntax error, but that is just to illustrate what I am trying to do. Any takers?
The primary reason i'm doing this is because I'm having scope problems noted here in this other unanswered Action Script question: How to pass variables into inline functions in Action Script 2
Thanks!
I could be wrong (I haven't done AS2 for a long while), but I think you can do this using array syntax:
for( i = 0 ; i < 3 ; i++)
{
this["myVar"+i] = i;
}
and then for variable access:
var foo = this["myVar0"] //etc
First answer is correct, but if you make the class dynamic (ie. new members can be created dynamically) ...
dynamic class ClassName { // etc. }
... then you can reference the variable in normal syntax:
var foo = this.myVar0;
You won't be able to access the variable at all without 'this' whether the class is dynamic or not.

Actionscript 2 functions

I'm an experienced programmer but just starting out with Flash/Actionscript. I'm working on a project that for certain reasons requires me to use Actionscript 2 rather than 3.
When I run the following (I just put it in frame one of a new flash project), the output is a 3 rather than a 1 ? I need it to be a 1.
Why does the scope of the 'ii' variable continue between loops?
var fs:Array = new Array();
for (var i = 0; i < 3; i++){
var ii = i + 1;
fs[i] = function(){
trace(ii);
}
}
fs[0]();
Unfortunately, AS2 is not that kind of language; it doesn't have that kind of closure. Functions aren't exactly first-class citizens in AS2, and one of the results of that is that a function doesn't retain its own scope, it has to be associated with some scope when it's called (usually the same scope where the function itself is defined, unless you use a function's call or apply methods).
Then when the function is executed, the scope of variables inside it is just the scope of wherever it happened to be called - in your case, the scope outside your loop. This is also why you can do things like this:
function foo() {
trace( this.value );
}
objA = { value:"A" };
objB = { value:"B" };
foo.apply( objA ); // A
foo.apply( objB ); // B
objA.foo = foo;
objB.foo = foo;
objA.foo(); // A
objB.foo(); // B
If you're used to true OO languages that looks very strange, and the reason is that AS2 is ultimately a prototyped language. Everything that looks object-oriented is just a coincidence. ;D
Unfortunately Actionscript 2.0 does not have a strong scope... especially on the time line.
var fs:Array = new Array();
for (var i = 0; i < 3; i++){
var ii = i + 1;
fs[i] = function(){
trace(ii);
}
}
fs[0]();
trace("out of scope: " + ii + "... but still works");
I came up with a kind of strage solution to my own problem:
var fs:Array = new Array();
for (var i = 0; i < 3; i++){
var ii = i + 1;
f = function(j){
return function(){
trace(j);
};
};
fs[i] = f(ii);
}
fs[0](); //1
fs[1](); //2
fs[2](); //3

Resources