197 lines
5.3 KiB
Java
197 lines
5.3 KiB
Java
package com.luqj.util.reflect;
|
|
|
|
import com.luqj.util.assertion.Assert;
|
|
import com.luqj.util.log.LogUtil;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import java.lang.reflect.Field;
|
|
import java.lang.reflect.Method;
|
|
import java.lang.reflect.Modifier;
|
|
|
|
public abstract class GSetterUtil {
|
|
private static Logger _log = LoggerFactory.getLogger(GSetterUtil.class);
|
|
|
|
public static final String PREFIX_GETTER = "get";
|
|
|
|
public static final String PREFIX_SETTER = "set";
|
|
|
|
|
|
public static Object getValueByGetter(Object obj,String fieldName){
|
|
Field f = ClassUtils.getDeclaredFields(obj.getClass(), fieldName);
|
|
Assert.notNull(f, obj.getClass()+",找不到指定的属性["+fieldName+"]");
|
|
return getValueByGetter(obj, f);
|
|
}
|
|
|
|
/**
|
|
* 判断属性是否可操作,是否有公共getter,setter方法
|
|
* @return
|
|
*/
|
|
public static boolean isOpAbleField(Class<?> type,Field f){
|
|
return getGetter(type, f) != null && getSetter(type, f) != null;
|
|
}
|
|
|
|
/**
|
|
* 判断指定的对象属性是否存在getter方法
|
|
* @param f
|
|
* @return
|
|
*/
|
|
public static boolean existGetter(Class<?> type,Field f){
|
|
return getGetter(type, f) != null;
|
|
}
|
|
|
|
/**
|
|
* 判断指定的对象属性是否存在setter方法
|
|
* @param f
|
|
* @return
|
|
*/
|
|
public static boolean existSetter(Class<?> type,Field f){
|
|
return getSetter(type, f) != null;
|
|
}
|
|
|
|
public static void setValueBySetter(Object obj,Field f,Object paramValue){
|
|
setValueBySetter(obj, f.getName(), paramValue,f.getType());
|
|
}
|
|
|
|
public static void setValueBySetter(Object obj,String fieldName,Object paramValue,Class<?> paramType){
|
|
Method m = getSetter(obj.getClass(),fieldName,paramType);
|
|
Assert.notNull(m,obj.getClass().getSimpleName()+"属性["+fieldName+"]不存在setter方法");
|
|
try {
|
|
m.invoke(obj,paramValue);
|
|
} catch (Throwable e) {
|
|
|
|
_log.error(LogUtil.exceptionMsg("["+fieldName+"]set方法"+m.getName()+"设置出现异常", e),e);
|
|
}
|
|
}
|
|
|
|
public static Object getValueByGetter(Object obj,Field f){
|
|
Method m = getGetter(obj.getClass(), f);
|
|
Assert.notNull(m,"属性["+f.getName()+"]不存在getter方法");
|
|
try {
|
|
return m.invoke(obj);
|
|
} catch (Throwable e) {
|
|
|
|
_log.error(LogUtil.exceptionMsg(f+"set方法"+m.getName()+"设置出现异常", e));
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static String getGetterMethodName(String fieldName){
|
|
Assert.hasText(fieldName,"属性名必须有内容!");
|
|
return PREFIX_GETTER + upperFirstChar(fieldName);
|
|
}
|
|
|
|
public static String getSetterMethodName(String fieldName){
|
|
Assert.hasText(fieldName,"属性名必须有内容!");
|
|
return PREFIX_SETTER + upperFirstChar(fieldName);
|
|
}
|
|
|
|
public static String upperFirstChar(String fieldName){
|
|
return fieldName.substring(0,1).toUpperCase()
|
|
+ fieldName.substring(1,fieldName.length());
|
|
}
|
|
|
|
public static String lowerFirstChar(String fieldName){
|
|
return fieldName.substring(0,1).toLowerCase()
|
|
+ fieldName.substring(1,fieldName.length());
|
|
}
|
|
|
|
|
|
public static boolean isGetter(Method method){
|
|
return method.getModifiers() == Modifier.PUBLIC
|
|
&& method.getName().length() > PREFIX_GETTER.length()
|
|
&& method.getName().startsWith(PREFIX_GETTER)
|
|
&& method.getParameterTypes().length == 0;
|
|
}
|
|
|
|
public static boolean isSetter(Method method){
|
|
return (method.getModifiers() & Modifier.PUBLIC) == 1
|
|
&& method.getName().length() > PREFIX_SETTER.length()
|
|
&& method.getName().startsWith(PREFIX_SETTER)
|
|
&& method.getParameterTypes().length == 1;
|
|
}
|
|
|
|
/**
|
|
* 获取setter方法,如果找不到或达不到要求返回NULL
|
|
* @param type
|
|
* @param f
|
|
* @return
|
|
*/
|
|
public static Method getSetter(Class<?> type,String fieldName,Class<?> fieldType){
|
|
try {
|
|
Method m = type.getMethod(getSetterMethodName(fieldName), fieldType);
|
|
if(isSetter(m)){
|
|
return m;
|
|
}else{
|
|
return null;
|
|
}
|
|
} catch (Exception e) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* 获取setter方法,如果找不到或达不到要求返回NULL
|
|
* @param type
|
|
* @param f
|
|
* @return
|
|
*/
|
|
public static Method getSetter(Class<?> type,Field f){
|
|
return getSetter(type, f.getName(), f.getType());
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* 获取getter方法,如果找不到或达不到要求返回NULL
|
|
* @param type
|
|
* @param f
|
|
* @return
|
|
*/
|
|
public static Method getGetter(Class<?> type,Field f){
|
|
try {
|
|
Method m = type.getMethod(getGetterMethodName(f.getName()));
|
|
if(isGetter(m)){
|
|
return m;
|
|
}else{
|
|
return null;
|
|
}
|
|
} catch (Exception e) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* 根据setter方法获取属性名
|
|
* @param method
|
|
* @return
|
|
*/
|
|
public static String getFieldNameBySetter(Method method){
|
|
//get开头且没有参数
|
|
Assert.notNull(method,"方法不允许为NULL");
|
|
Assert.logic(isSetter(method),
|
|
method.getName()+"["+method.getParameterTypes().length+"]不是JAVA标准set方法");
|
|
String fieldName = method.getName().substring(PREFIX_SETTER.length());
|
|
fieldName = lowerFirstChar(fieldName);
|
|
return fieldName;
|
|
}
|
|
|
|
/**
|
|
* 根据getter方法获取属性名
|
|
* @param method
|
|
* @return
|
|
*/
|
|
public static String getFieldNameByGetter(Method method){
|
|
//get开头且没有参数
|
|
Assert.notNull(method,"方法不允许为NULL");
|
|
Assert.logic(isGetter(method),
|
|
method.getName()+"["+method.getParameterTypes().length+"]不是JAVA标准get方法");
|
|
String fieldName = method.getName().substring(PREFIX_GETTER.length());
|
|
fieldName = lowerFirstChar(fieldName);
|
|
return fieldName;
|
|
}
|
|
|
|
}
|