Given the standard naming convention, make sure there is a method with "get" or "set" with the class field name prefix:
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, replace getDeclaredFields() and getDeclaredMethods() with getFields() and getMethods() .
source share