Generate all the setter invocation statements of a javabean

    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\");");
            }
        }

    }

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.