public class Being {
}
Class cls = Class.forName("Being");
/* Adds a public integer field named "num" */
cls.setField(new Field("num", Integer.TYPE));
/* ...or something like that, maybe even: */
Field num = cls.addField();
num.setInt(22);
public class Being {
}
Class cls = Class.forName("Being");
/* Adds a public integer field named "num" */
cls.setField(new Field("num", Integer.TYPE));
/* ...or something like that, maybe even: */
Field num = cls.addField();
num.setInt(22);
Quote:Original post by AAA
consider using a map?
Quote:Original post by AAA
To the best of my knowledge you can't do that and it wouldn't make much sense to be able to do it. Reflection does exactly what the name suggests, reflect.
Can you imagine instantiating a class object, then adding fields to its class? What would that even mean?
Quote:Original post by Antheus
BCEL. The other way is to invoke javac itself.
Although this type of code generation has yet to convince me of its benefits from application design perspective.
class DynaFieldBean { Map<String, Field> fields = new HasMap<String, Field>(); public Field createFieldWithName(String fieldName) { Field f = new Field(); fields.put(fieldName, f); return f; } public Field getField(String fieldName) { Field f = fields.get(fieldName); if (f == null) { f = createFieldWithName(fieldName); } return f; }}
Quote:Original post by Antheus
BCEL. The other way is to invoke javac itself.
Although this type of code generation has yet to convince me of its benefits from application design perspective.