jakarta-bcel-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dweim...@students.cs.uu.nl
Subject change field
Date Sat, 20 Mar 2004 13:32:50 GMT
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)



   methods[i] = mg.getMethod();


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: bcel-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bcel-user-help@jakarta.apache.org

View raw message