来一个demo
Category category = new Category();
Class<? extends Category> aClass = category.getClass();
Method[] methods = aClass.getMethods();
for (Method method : methods) {
System.out.println(method.getName());
if("setId".equals(method.getName())){
try {
//传参的时候类型要传对,不然抛异常的
method.invoke(category, 11l);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}
}
//查看是否设置成功
System.out.println(category.getId());
打印的结果
通过属性名反射获取方法调用
import org.springframework.util.ObjectUtils;
import org.springframework.util.ReflectionUtils;
import java.lang.reflect.Method;
import java.lang.reflect.Field;
import java.beans.PropertyDescriptor;
private boolean checkSingleParam(Object o, String propertyName){
if(Objects.isNull(o)){
return false;
}
Field companyNameField = ReflectionUtils.findField(o.getClass(), propertyName);
try {
PropertyDescriptor propertyDescriptor = new PropertyDescriptor(companyNameField.getName(), o.getClass());
//获取get方法
Method method = propertyDescriptor.getReadMethod();
//Method method = propertyDescriptor.getWriteMethod(); set方法
if(Objects.isNull(method)){
log.error("未找到方法class:{},propertyName:{}", o.getClass(), propertyName);
return false;
}
Object result = null;
try {
result = method.invoke(o);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
if(Objects.isNull(result)){
return false;
}
} catch (IntrospectionException e) {
e.printStackTrace();
return false;
}
return true;
}
通过JoinPoint获取参数
post或者put方法获取请求体中参数
joinPoint.getArgs()
方法名称
String className = joinPoint.getTarget().getClass().getName();
String methodName = joinPoint.getSignature().getName();
反射手动copy属性
public static void copy(Object originVO, Object destVO) throws InvocationTargetException, IllegalAccessException {
List<Field> allFilelds = Lists.newArrayList();
for(Class<?> currentClass = originVO.getClass() ;currentClass != Object.class; currentClass = currentClass.getSuperclass() ){
Field[] declaredFields = currentClass.getDeclaredFields();
allFilelds.addAll(Arrays.asList(declaredFields));
}
for (Field field : allFilelds) {
if(!field.isAccessible()){
field.setAccessible(Boolean.TRUE);
}
PropertyDescriptor origin = getPropertyDescriptor(field.getName(), originVO.getClass());
PropertyDescriptor dest = getPropertyDescriptor(field.getName(), destVO.getClass());
if(Objects.isNull(origin) || Objects.isNull(dest)){
continue;
}
//获取get方法
Method method = origin.getReadMethod();
Object readValue = method.invoke(originVO);
if(Objects.isNull(readValue)){
continue;
}
Method writeMethod = dest.getWriteMethod();
writeMethod.invoke(destVO, readValue);
}
}
private static PropertyDescriptor getPropertyDescriptor(String fileldName, Class<?> currentClass){
try {
PropertyDescriptor origin = new PropertyDescriptor(fileldName, currentClass);
return origin;
} catch (IntrospectionException e) {
if(Object.class.equals(currentClass)){
return null;
}
log.error("getPropertyDescriptor() called with parameters => 【fileldName = {}】, 【currentClass = {}】, exception = 【{}】", fileldName, currentClass, e);
return getPropertyDescriptor(fileldName, currentClass.getSuperclass());
}
}
获取class的所有方法(含父类)
public static Field[] getFieldsDirectly(Class<?> beanClass, boolean withSuperClassFields) throws SecurityException {
Assert.notNull(beanClass);
Field[] allFields = null;
Class<?> searchType = beanClass;
Field[] declaredFields;
while (searchType != null) {
declaredFields = searchType.getDeclaredFields();
if (null == allFields) {
allFields = declaredFields;
} else {
//allFields = ArrayUtil.append(allFields, declaredFields);
if(declaredFields.length > 0){
Field[] tmp = new Field[allFields.length + declaredFields.length];
System.arraycopy(allFields, 0, tmp, 0, allFields.length);
System.arraycopy(declaredFields, 0, tmp, allFields.length, declaredFields.length);
allFields = tmp;
}
}
searchType = withSuperClassFields ? searchType.getSuperclass() : null;
}
return allFields;
}