I have a class X with maybe 100 String in it and I want to do a function that mock an object of this class for all the setters which begins by "setTop".
For the moment I did this :
public void setFtoMethods(Class aClass){
Methods[] methods = aClass.getMethods();
for(Method method : methods){
if(method.getName().startsWith("setTop")){
method.invoke ....
}
}
}
And I don't know how to do now and I'm not pretty sure I can fill all these setters like that. In my environment I can't use frameworks and I'm in Java 6.
You CANNOT fill the setters because they are methods (functionallities), not values itselfs. But...
You CAN fill the value of the attributes (fields) of the class that corresponds to the getter.
Imagine you have a class:
Taking:
getTopXXXwill correspond to fieldtopXXX)You can get only needed fields with reflection in this way:
OUTPUT:
Now, do whatever you need inside the
if (field.getName().startsWith("top")) {instead of aSystem.out.