private static void generateSetters(Class<?> clazz) { Method[] methods = clazz.getMethods(); for (Method m : methods) { if (m.getName().startsWith("set") && m.getParameterTypes().length == 1){ System.out.println("bean." + m.getName() + "(\"abc\");"); } } }
will print:
bean.setName(“abc”);
bean.setUserId(“abc”);
You can put it inside eclipse as a code template:
${:import(java.lang.reflect.Method)} public static void main(String args[]) { Class<?> clazz = ${Bean}.class; String object = "${bean}"; Method[] methods = clazz.getMethods(); for (Method m : methods) { if (m.getName().startsWith("set") && m.getParameterTypes().length == 1) { System.out.println(object + "." + m.getName() + "(\"abc\");"); } } }
In intellij you can create a live template:
public static void main(String args[]) { Class<?> clazz = $Bean$.class; String object = "$bean$"; java.lang.reflect.Method[] methods = clazz.getMethods(); for (java.lang.reflect.Method m : methods) { if (m.getName().startsWith("set") && m.getParameterTypes().length == 1) { System.out.println(object + "." + m.getName() + "(\"abc\");"); } } }