I have code like below:
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?
Think what you are looking for the the method getReturnType()
of the object java.lang.reflect.Method
public Class getReturnType()
Returns a Class object that represents the formal return type of the method represented by this Method object.
Returns:the return type for the method this object represents
Do this in your for loop:
for (Method method : methods) {
if (isGetter(method)) {
String type = String.valueOf(method.getReturnType());
System.out.println("getter: " + method + ". Return type: " + type);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.