14

Is it possible to somehow iterate over every method of an object, with name starting with "get"? I want to compare two very complex custom objects, that have fields consisting of data structures based on other custom objects. What I want to do is to get a hashcode of the result of every get method, and compare if they are equal for every field.

Sorry if it is not very understandable, if you have questions please ask. Thanks for any help and suggestions

I thought of something like that:

for(method m : gettersOfMyClass){ 
boolean same = object1.m.hashCode() == object2.m.hashCode() 
} 
hakre
  • 193,403
  • 52
  • 435
  • 836
LucasSeveryn
  • 5,984
  • 8
  • 38
  • 65
  • so you want to do something like get***() ..write?.. – Ahmad Jun 27 '12 at 10:34
  • 1
    ...Are you trying to build a reflective implementation of `hashCode()` or `equals`? Is there a reason you can't just have your IDE generate them? – Louis Wasserman Jun 27 '12 at 10:34
  • getters - http://stackoverflow.com/questions/8524011/java-reflection-how-can-i-get-the-all-getter-methods-of-a-java-class-and-invoke – ant Jun 27 '12 at 10:36
  • Why not just iterate over all of the methods and check if the method name starts with get? – John Kane Jun 27 '12 at 10:36
  • For your 1st question - yes, through reflection. But I don't think it's a good practice. – iozee Jun 27 '12 at 10:37
  • I don't think equals will work in this case... This is first time I'm attempting such thing so I thought hashcode would work... – LucasSeveryn Jun 27 '12 at 10:40
  • You'd probably want to check that the name has more than three letters and the method has no parameters, is public, not abstract, not static, not native, not void, and possibly handle a boolean "is"-getter as well. – COME FROM Jun 27 '12 at 12:00

2 Answers2

30

Yes, it is possible, and in fact it's quite simple:

public static void main(String[] args) throws Exception {
  final Object o = "";
  for (Method m : o.getClass().getMethods()) {
    if (m.getName().startsWith("get") && m.getParameterTypes().length == 0) {
      final Object r = m.invoke(o);
      // do your thing with r
    }
  }
}
Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • Hi, would you care to explain what this line does: final Object r = m.invoke(o); – LucasSeveryn Jun 27 '12 at 10:44
  • 1
    It invokes the method `m` on instance `o` and assigns the result to `r`. It does the equivalent of `Object r = o.getXyz()`, in a case where `m.getName()` returns `getXyz`. – Marko Topolnik Jun 27 '12 at 10:48
3

Looks like some thing to deal with reflex concept. Reverse engineering the Object

May be this is what you need

Sample Class :

class Syndrome{
public void getMethod1(){}
public void getMethod2(){}
public void getMethod3(){}
public void getMethod4(){}
}

Main Method:

Syndrome syndrome = new Syndrome();

Method[] methods = syndrome.getClass().getMethods();

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

if( methods[index].getName().contains( "get")){
    // Do something here
}

}
madhairsilence
  • 3,787
  • 2
  • 35
  • 76