com.luues.util.classUtil.ClassUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of commons-util Show documentation
Show all versions of commons-util Show documentation
A Simple Tool Operations Class
package com.luues.util.classUtil;
import org.apache.logging.log4j.util.Strings;
import org.reflections.Reflections;
import org.reflections.scanners.SubTypesScanner;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Set;
/**
* Created by Mr-Wu on 2019/12/28.
*/
public class ClassUtil {
public static Set> getSubTypesOf(String mypackage, Class tclass){
Reflections reflections = new Reflections(Strings.isBlank(mypackage) ? "" : mypackage);
Set> subTypes = reflections.getSubTypesOf(tclass);
return subTypes;
}
public static Set> getTypesAnnotatedWith(Class extends Annotation> tClass){
Reflections reflections = new Reflections("");
return reflections.getTypesAnnotatedWith(tClass);
}
public static Set> getAllClass(String mypackage){
Reflections reflections = new Reflections(Strings.isBlank(mypackage) ? "" : mypackage, new SubTypesScanner(false));
return reflections.getSubTypesOf(Object.class);
}
/**
* 该方法是用于相同对象不同属性值的合并,如果两个相同对象中同一属性都有值,那么sourceBean中的值会覆盖tagetBean重点的值
* @param sourceBean 被提取的对象bean
* @param targetBean 用于合并的对象bean
* @return targetBean,合并后的对象
*/
public static T combineSydwCore(T sourceBean,T targetBean){
Class sourceBeanClass = sourceBean.getClass();
Class targetBeanClass = targetBean.getClass();
Field[] sourceFields = sourceBeanClass.getDeclaredFields();
Field[] targetFields = targetBeanClass.getDeclaredFields();
for(int i=0; i