4

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?

2
  • Use Java Reflection - docs.oracle.com/javase/tutorial/reflect Commented Aug 5, 2013 at 3:14
  • 2
    Would you have to consider is methods for booleans? Commented Aug 5, 2013 at 3:32

3 Answers 3

10
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.

Sign up to request clarification or add additional context in comments.

5 Comments

I could be wrong, haven't tested it, but I believe this will only return the methods for the current class and not any declared by it's parent classes...
@MadProgrammer, that's correct. getDeclaredMethods() excludes inherited methods.
@DownVoter, The idea is to give OP a push in the right direction. Not churn out production class code.
the setter must have exactly 1 parameter and the getter should not has any parameter.and your answer didn't consider that.
@J.Rush, Please, note that the methods are not being invoked here. So, how many arguments they take is immaterial. And, the other answers here that check for field names as well are incorrect because JavaBean properties do not require the underlying field names to match with their getters/setters.
1
public static int[] countGettersandSetters(String c) throws ClassNotFoundException {
    int[] count = new int[2];
    Class<?> classs = Class.forName(c);
    Field[] fields = classs.getDeclaredFields();
    for (Field field : fields) {
        String name = field.getName();
        try {
            classs.getMethod("set"+name.substring(0, 1).toUpperCase()+name.substring(1) , null);
            count[0]++;
        } catch (NoSuchMethodException e) {
        }
        try {
            classs.getMethod("get"+name.substring(0, 1).toUpperCase()+name.substring(1), field.getType());
            count[1]++;
        } catch (NoSuchMethodException e) {
        }
    }
    return count;
}

1 Comment

JavaBean properties do not require the underlying field names to match their getters/setters. So, the check is unnecessary and may potentially report an incorrect number of property methods.
0

Considering the standard naming convention, check that there exists a method with "get" or "set" prefixed to the name of a field of the class:

public static int[] count(Class<? extends Object> c) {
    int[] counts = new int[2];

    Field[] fields = c.getDeclaredFields();
    Method[] methods = c.getDeclaredMethods();

    Set<String> fieldNames = new HashSet<String>();
    List<String> methodNames = new ArrayList<String>();

    for (Field f : fields){
        fieldNames.add(f.getName().toLowerCase());
    }

    for (Method m : methods){
        methodNames.add(m.getName().toLowerCase());
    }

    for (String name : methodNames){
        if(name.startsWith("get") && fieldNames.contains(name.substring(3))){
            counts[0]++;
        }else if(name.startsWith("set") && fieldNames.contains(name.substring(3))){
            counts[1]++;
        }
    }

    return counts;
}

If you want to get all inherited getters/setters as well, replace getDeclaredFields() and getDeclaredMethods() with getFields() and getMethods().

1 Comment

JavaBean properties do not require the underlying field names to match their getters/setters. So, the check is unnecessary and may potentially report an incorrect number of property methods.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.