import com.framework.emt.system.domain.exception.ExceptionItem;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Method;

public class GetAllSetMethods {

    public static void main(String[] args) {
        ExceptionItem entity = new ExceptionItem();
        getAllSetMethods(entity);
        //ExceptionItem entity = new ExceptionItem();
        //Class<?> clazz = entity.getClass();
        //while (clazz != null) {
        //    Field[] fields = clazz.getDeclaredFields();
        //    for (Field field : fields) {
        //        field.setAccessible(true);
        //        String fieldName = field.getName();
        //        System.out.println("this." + fieldName + " = null;");
        //    }
        //    clazz = clazz.getSuperclass();
        //}
    }

    public static void getAllSetMethods(Object object) {
        Class<?> clazz = object.getClass();
        Method[] methods = clazz.getMethods();
        String entityName = initialLowerCase(clazz.getSimpleName());
        for (Method method : methods) {
            if (isSetMethod(method)) {
                System.out.println(entityName + "." + method.getName() + "();");
            }
        }
    }

    public static boolean isSetMethod(Method method) {
        String methodName = method.getName();
        return methodName.startsWith("set") && methodName.length() > 3;
    }

    public static String initialLowerCase(String str) {
        if (StringUtils.isBlank(str)) {
            return str;
        }
        return Character.toLowerCase(str.charAt(0)) + str.substring(1);
    }

}

拿去,不用谢

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐