i guess you could use ClassGen.replaceField or JavaClass.setFields

[EMAIL PROTECTED] wrote:
I've been using the lib for some time now and it works nicely, but I ran
into an issue regarding fields. When modifying methods I tend to do it
like this:

JavaClass jc = Repository.lookupClass(args[0]);

ClassGen _cg = new ClassGen(jc);

ConstantPoolGen _cp = _cg.getConstantPool();

InstructionFactory _factory = new InstructionFactory(_cg, _cp);


String _classname = "ClassX";


Method methods[] = _cg.getMethods();

for (int i = 0; i < methods.length; i++)

{
   MethodGen mg = new MethodGen(methods[i], _classname, _cp);
   InstructionList il = mg.getInstructionList();

//operate on mg (possible through il)

mg.setMaxLocals();


mg.setMaxStack();


methods[i] = mg.getMethod();

   il.dispose();
}
_cg.setMethods(methods)

I was expecting the same when altering fields. (In my case I'd like to
change field access modifiers from public/protected/package to private.)

Field fields[] = _cg.getFields();
for (int i = 0; i < fields.length; i++)
{
    FieldGen fg = new FieldGen(fields[i], _cp);

//operate on fg

    fields[i] = fg.getField();
}
_cg.setFields(fields); // <<<--- ???

However the ClassGen.setFields method doesn't exist!? Why not? So I have
to remove the field explicitly first using ClassGen.removeField(Field) and
add it using ClassGen.addField(Field). Is there a good reason for this?

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to