优化类加载

This commit is contained in:
zhou-hao
2020-08-19 10:52:34 +08:00
parent 6fbf3af50f
commit 23db12b697
6 changed files with 38 additions and 37 deletions

View File

@@ -62,7 +62,7 @@ public class EasyormRepositoryRegistrar implements ImportBeanDefinitionRegistrar
for (Resource resource : resourcePatternResolver.getResources(path)) {
MetadataReader reader = metadataReaderFactory.getMetadataReader(resource);
String className = reader.getClassMetadata().getClassName();
Class entityType = Class.forName(className);
Class<?> entityType = org.springframework.util.ClassUtils.forName(className,null);
if (Arrays.stream(anno)
.noneMatch(ann -> AnnotationUtils.findAnnotation(entityType, ann) != null)) {
continue;

View File

@@ -44,7 +44,7 @@ public class MapperEntityFactory implements EntityFactory, BeanFactory {
private static final DefaultMapperFactory DEFAULT_MAPPER_FACTORY = clazz -> {
String simpleClassName = clazz.getPackage().getName().concat(".Simple").concat(clazz.getSimpleName());
try {
return defaultMapper(Class.forName(simpleClassName));
return defaultMapper(org.springframework.util.ClassUtils.forName(simpleClassName,null));
} catch (ClassNotFoundException ignore) {
// throw new NotFoundException(e.getMessage());
}

View File

@@ -33,7 +33,7 @@ public final class FastBeanCopier {
private static final PropertyUtilsBean propertyUtils = BeanUtilsBean.getInstance().getPropertyUtils();
private static final Map<Class, Class> wrapperClassMapping = new HashMap<>();
private static final Map<Class<?>, Class<?>> wrapperClassMapping = new HashMap<>();
@SuppressWarnings("all")
public static final Class[] EMPTY_CLASS_ARRAY = new Class[0];
@@ -63,6 +63,7 @@ public final class FastBeanCopier {
BEAN_FACTORY = new BeanFactory() {
@Override
@SneakyThrows
@SuppressWarnings("all")
public <T> T newInstance(Class<T> beanType) {
return beanType == Map.class ? (T) new HashMap<>() : beanType.newInstance();
}
@@ -114,14 +115,14 @@ public final class FastBeanCopier {
return target;
}
static Class getUserClass(Object object) {
static Class<?> getUserClass(Object object) {
if (object instanceof Map) {
return Map.class;
}
Class type = ClassUtils.getUserClass(object);
Class<?> type = ClassUtils.getUserClass(object);
if (java.lang.reflect.Proxy.isProxyClass(type)) {
Class[] interfaces= type.getInterfaces();
Class<?>[] interfaces= type.getInterfaces();
return interfaces[0];
}
@@ -129,8 +130,8 @@ public final class FastBeanCopier {
}
public static Copier getCopier(Object source, Object target, boolean autoCreate) {
Class sourceType = getUserClass(source);
Class targetType = getUserClass(target);
Class<?> sourceType = getUserClass(source);
Class<?> targetType = getUserClass(target);
CacheKey key = createCacheKey(sourceType, targetType);
if (autoCreate) {
return CACHE.computeIfAbsent(key, k -> createCopier(sourceType, targetType));
@@ -140,11 +141,11 @@ public final class FastBeanCopier {
}
private static CacheKey createCacheKey(Class source, Class target) {
private static CacheKey createCacheKey(Class<?> source, Class<?> target) {
return new CacheKey(source, target);
}
public static Copier createCopier(Class source, Class target) {
public static Copier createCopier(Class<?> source, Class<?> target) {
String sourceName = source.getName();
String tartName = target.getName();
if (sourceName.startsWith("package ")) {
@@ -173,7 +174,7 @@ public final class FastBeanCopier {
}
}
private static Map<String, ClassProperty> createProperty(Class type) {
private static Map<String, ClassProperty> createProperty(Class<?> type) {
List<String> fieldNames = Arrays.stream(type.getDeclaredFields())
.map(Field::getName).collect(Collectors.toList());
@@ -192,7 +193,7 @@ public final class FastBeanCopier {
.collect(Collectors.toMap(ClassProperty::getName, Function.identity(), (k, k2) -> k, LinkedHashMap::new));
}
private static String createCopierCode(Class source, Class target) {
private static String createCopierCode(Class<?> source, Class<?> target) {
Map<String, ClassProperty> sourceProperties = null;
Map<String, ClassProperty> targetProperties = null;
@@ -259,16 +260,16 @@ public final class FastBeanCopier {
protected String writeMethodName;
@Getter
protected BiFunction<Class, Class, String> getter;
protected BiFunction<Class<?>, Class<?>, String> getter;
@Getter
protected BiFunction<Class, String, String> setter;
protected BiFunction<Class<?>, String, String> setter;
@Getter
protected Class type;
protected Class<?> type;
@Getter
protected Class beanType;
protected Class<?> beanType;
public String getReadMethod() {
return readMethodName + "()";
@@ -282,7 +283,7 @@ public final class FastBeanCopier {
return getTypeName(type);
}
public String getTypeName(Class type) {
public String getTypeName(Class<?> type) {
String targetTypeName = type.getName();
if (type.isArray()) {
targetTypeName = type.getComponentType().getName() + "[]";
@@ -294,7 +295,7 @@ public final class FastBeanCopier {
return isPrimitive(getType());
}
public boolean isPrimitive(Class type) {
public boolean isPrimitive(Class<?> type) {
return type.isPrimitive();
}
@@ -302,11 +303,11 @@ public final class FastBeanCopier {
return isWrapper(getType());
}
public boolean isWrapper(Class type) {
return wrapperClassMapping.values().contains(type);
public boolean isWrapper(Class<?> type) {
return wrapperClassMapping.containsValue(type);
}
protected Class getPrimitiveType(Class type) {
protected Class<?> getPrimitiveType(Class<?> type) {
return wrapperClassMapping.entrySet().stream()
.filter(entry -> entry.getValue() == type)
.map(Map.Entry::getKey)
@@ -314,7 +315,7 @@ public final class FastBeanCopier {
.orElse(null);
}
protected Class getWrapperType() {
protected Class<?> getWrapperType() {
return wrapperClassMapping.get(type);
}
@@ -322,7 +323,7 @@ public final class FastBeanCopier {
return getWrapperType().getSimpleName().concat(".valueOf(").concat(getter).concat(")");
}
public BiFunction<Class, Class, String> createGetterFunction() {
public BiFunction<Class<?>, Class<?>, String> createGetterFunction() {
return (targetBeanType, targetType) -> {
String getterCode = "$$__source." + getReadMethod();
@@ -349,9 +350,9 @@ public final class FastBeanCopier {
if (targetType != getType()) {
if (isPrimitive(targetType)) {
boolean sourceIsWrapper = isWrapper();
Class targetWrapperClass = wrapperClassMapping.get(targetType);
Class<?> targetWrapperClass = wrapperClassMapping.get(targetType);
Class sourcePrimitive = getPrimitiveType(getType());
Class<?> sourcePrimitive = getPrimitiveType(getType());
//目标字段是基本数据类型,源字段是包装器类型
// source.getField().intValue();
if (sourceIsWrapper) {
@@ -416,15 +417,15 @@ public final class FastBeanCopier {
};
}
public BiFunction<Class, String, String> createSetterFunction(Function<String, String> settingNameSupplier) {
public BiFunction<Class<?>, String, String> createSetterFunction(Function<String, String> settingNameSupplier) {
return (sourceType, paramGetter) -> settingNameSupplier.apply(paramGetter);
}
public String generateGetter(Class targetBeanType, Class targetType) {
public String generateGetter(Class<?> targetBeanType, Class<?> targetType) {
return getGetter().apply(targetBeanType, targetType);
}
public String generateSetter(Class targetType, String getter) {
public String generateSetter(Class<?> targetType, String getter) {
return getSetter().apply(targetType, getter);
}
}
@@ -474,7 +475,7 @@ public final class FastBeanCopier {
this.beanFactory = beanFactory;
}
public Collection newCollection(Class targetClass) {
public Collection<?> newCollection(Class<?> targetClass) {
if (targetClass == List.class) {
return new ArrayList<>();
@@ -484,7 +485,7 @@ public final class FastBeanCopier {
return new LinkedList<>();
} else {
try {
return (Collection) targetClass.newInstance();
return (Collection<?>) targetClass.newInstance();
} catch (Exception e) {
throw new UnsupportedOperationException("不支持的类型:" + targetClass, e);
}
@@ -602,9 +603,9 @@ public final class FastBeanCopier {
@AllArgsConstructor
public static class CacheKey {
private Class targetType;
private final Class<?> targetType;
private Class sourceType;
private final Class<?> sourceType;
@Override
public boolean equals(Object obj) {

View File

@@ -45,10 +45,9 @@ public class Proxy<I> {
throw new NullPointerException("superClass can not be null");
}
this.superClass = superClass;
ClassPool classPool = new ClassPool(true);
ClassPool classPool = ClassPool.getDefault();
ClassPath classPath = new ClassClassPath(this.getClass());
classPool.insertClassPath(classPath);
classPool.insertClassPath(new ClassClassPath(this.getClass()));
classPool.insertClassPath(new LoaderClassPath(ClassUtils.getDefaultClassLoader()));
if (classPathString != null) {

View File

@@ -7,6 +7,7 @@ import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.hswebframework.web.bean.FastBeanCopier;
import org.hswebframework.web.datasource.config.DynamicDataSourceConfig;
import org.springframework.util.ClassUtils;
import javax.sql.XADataSource;
import java.sql.SQLException;
@@ -42,7 +43,7 @@ public class AtomikosDataSourceConfig extends DynamicDataSourceConfig {
xaProperties.entrySet().forEach(entry -> entry.setValue(String.valueOf(entry.getValue())));
}
//fix #87
XADataSource dataSource = (XADataSource) Class.forName(getXaDataSourceClassName()).newInstance();
XADataSource dataSource = (XADataSource) ClassUtils.forName(getXaDataSourceClassName(),null).newInstance();
FastBeanCopier.copy(xaProperties, dataSource);
atomikosDataSourceBean.setXaDataSource(dataSource);

View File

@@ -41,7 +41,7 @@ public class DictionaryProperties {
try {
MetadataReader reader = metadataReaderFactory.getMetadataReader(resource);
String name = reader.getClassMetadata().getClassName();
Class clazz = Class.forName(name);
Class<?> clazz = ClassUtils.forName(name,null);
if (clazz.isEnum() && EnumDict.class.isAssignableFrom(clazz)) {
classes.add(clazz);
}