jeudi 23 juillet 2015

How to print a type of getter

I have a code like bellow:

public static void main(String[] args) throws Exception {

printGettersSetters(PodmiotSzukajQueryFormSet.class);

}

public static void printGettersSetters(Class aClass) {
Method[] methods = aClass.getMethods();

for (Method method : methods) {
  if (isGetter(method)) {
    System.out.println("getter: " + method);

  }
}
}

public static boolean isGetter(Method method) {
if (!method.getName().startsWith("getSomething")) {
  return false;
}
if (method.getParameterTypes().length != 0)
  return false;
if (void.class.equals(method.getReturnType()))
  return false;
return true;
}

output:

getter: public package.package.Class package.package.Class.getSomething()

How can I get a type of that getter, in this example: "Class" but in other examples "String" , "int" etc.

--edit it is possible with .getModifiers() but it returns int. How can I return String?





Aucun commentaire:

Enregistrer un commentaire