java - Adding new method to a class through reflection -
this question has answer here:
- can java class add method @ runtime? 9 answers
is possible add method class through reflection in java??
public class basedomain { public basedomain(){ field[] fields = this.getclass().getdeclaredfields(); for(int i=0; i<fields.length; i++){ string field = fields[i].tostring(); string settermethod = "public void set" + field.tolowercase(); //now want add method class. } } }
no, not through reflection.
reflection asks classes , members, can change fields cannot create new ones. cannot add new methods.
you can use a bytecode manipulation library add methods classes; why want to?
you can't call methods anyway except via reflection not exist @ compile time.
maybe take @ project lombok - annotation preprocessor can add methods classes @ compile time. add getters , setters automagically long classes correctly annotated.
Comments
Post a Comment