获取调用者Class和method、反射获取get方法、获取注解信息

时间:2022-10-01 18:06:40

 

调用者Class 及 method

StackTraceElement stacks[] = Thread.currentThread().getStackTrace();
        for (StackTraceElement stack : stacks) {
            String className = stack.getClassName();
            if (className.contains("com.upsoft")
                    && className.contains("Controller")) {
                className = className.substring(className.lastIndexOf(".") + 1,
                        className.length());
                String methodName = stack.getMethodName();
            }
        }

 

反射(get)

// 通过反射 遍历实体所有属性,找出发生改变的字段及修改前和修改后的值
        Field[] fields = entity.getClass().getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {

            changeColumn = new ColumnChangeBean();
            String attrName = fields[i].getName();
            Class<?> attrType = fields[i].getType();
            Method method = getGetMethod(entity.getClass(), attrName);

            try {

                Object oldValue = null;
                Object newValue = null;

                // 通过反射获取 oldValue 和 newValue 的值
                if (oldEntity != null) {
                    oldValue = method.invoke(oldEntity, new Object[0]);
                }
                newValue = method.invoke(entity, new Object[0]);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    private Method getGetMethod(Class objectClass, String fieldName) {
        StringBuffer sb = new StringBuffer();
        sb.append("get");
        sb.append(fieldName.substring(0, 1).toUpperCase());
        sb.append(fieldName.substring(1));
        try {
            return objectClass.getMethod(sb.toString());
        } catch (Exception e) {
        }
        return null;
    }

 反射(set)

public static void invokeSet(Object o, String fieldName, Object value) {       
  
    Method method = getSetMethod(o.getClass(), fieldName);       
  
    try {       
  
        method.invoke(o, new Object[] { value });       
  
    } catch (Exception e) {       
  
        e.printStackTrace();       
  
    }       
  
}



public static Method getSetMethod(Class objectClass, String fieldName) {       
  
    try {       
  
        Class[] parameterTypes = new Class[1];       
  
        Field field = objectClass.getDeclaredField(fieldName);       
  
        parameterTypes[0] = field.getType();       
  
        StringBuffer sb = new StringBuffer();       
  
        sb.append("set");       
  
        sb.append(fieldName.substring(0, 1).toUpperCase());       
  
        sb.append(fieldName.substring(1));       
  
        Method method = objectClass.getMethod(sb.toString(), parameterTypes);       
  
        return method;       
  
    } catch (Exception e) {       
  
        e.printStackTrace();       
  
    }       
  
    return null;       
  
} 

 

 

获取类注解信息

Annotation[] annotations = entity.getClass().getAnnotations();
        for (int i = 0; i < annotations.length; i++) {
            String annoStr = annotations[i].toString();
            if (annoStr.contains("@javax.persistence.Table")) {
                annoStr = annoStr.substring(annoStr.lastIndexOf("name=") + 5);
                while (annoStr.contains(",")) {
                    annoStr = annoStr.substring(0, annoStr.lastIndexOf(","));
                }
                tableName = annoStr;
            }
        }

获取方法、属性注解

Field[] fields = entity.getClass().getDeclaredFields();

        // 遍历Bean属性
        for (int i = 0; i < fields.length; i++) {
            Field field = fields[i];
            String attrName = field.getName();
            // 获取属性对应的get方法
            Method method = getGetMethod(entity.getClass(), attrName);

            Annotation[] attrAnnotations = field.getAnnotations();
            Annotation[] methodAnnotations = method.getAnnotations();

        }