I have to write a function
public static int[] countGettersandSetters (String classname)
to count the number of get and set methods and return as an array where index 0 are sets and index 1 are gets.
Can anyone give me a high level approach to how to solve this problem?
public static int[] countGettersandSetters (String className)
int[] count = new int[2];
Method[] methods = Class.forName(className).getDeclaredMethods();
for (Method method : methods) {
if (method.getName().startsWith("set")) {
count[0]++;
} else if (method.getName().startsWith("get") ||
method.getName().startsWith("is")) { // to incl. boolean properties
count[1]++;
}
}
return count;
}
For a concise tutorial on Reflection API take a look here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With