Commit 247ac523 by chengzhuoshen

移除掉spring-core依赖

parent fd4db0c7
...@@ -70,11 +70,6 @@ ...@@ -70,11 +70,6 @@
<version>1.9</version> <version>1.9</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>com.brilliance.swift</groupId> <groupId>com.brilliance.swift</groupId>
<artifactId>eibs-container</artifactId> <artifactId>eibs-container</artifactId>
<version>3.5.0</version> <version>3.5.0</version>
......
package com.brilliance.swift.spi;
import com.brilliance.swift.util.ClassUtil;
import com.brilliance.swift.util.PropertyUtil;
import com.brilliance.swift.util.ReflectionUtil;
import com.brilliance.swift.util.StringUtil;
import org.springframework.core.OrderComparator;
import org.springframework.util.Assert;
import java.io.IOException;
import java.net.URL;
import java.util.*;
import java.util.stream.Collectors;
/**
* spi加载类
*/
public class BeSpiLoader {
public static final String SERVICES_RESOURCE_LOCATION = "META-INF/be-esb.services";
public static final String COMMA_DELIMITED = ",";
static final com.brilliance.swift.spi.ConcurrentReferenceMap<ClassLoader, Map<String, List<String>>> cache = new ConcurrentReferenceMap<ClassLoader, Map<String, List<String>>>();
private BeSpiLoader() {
}
public static <T> List<T> loadBeServices(Class<T> serviceType, ClassLoader classLoader) {
return loadBeServices(serviceType, classLoader, null, null);
}
public static <T> List<T> loadBeServices(Class<T> serviceType, ClassLoader classLoader, Object[] args, Class[] argsTypes) {
Assert.notNull(serviceType, "'serviceType' must not be null");
ClassLoader classLoaderToUse = classLoader;
if (classLoaderToUse == null) {
classLoaderToUse = BeSpiLoader.class.getClassLoader();
}
List<String> serviceImplementationNames = loadServiceNames(serviceType, classLoaderToUse);
List<T> result = new ArrayList<>(serviceImplementationNames.size());
for (String serviceImplementationName : serviceImplementationNames) {
result.add(instantiateService(serviceImplementationName, serviceType, classLoaderToUse, args, argsTypes));
}
OrderComparator.sort(result);
return result;
}
public static List<String> loadServiceNames(Class<?> serviceType, ClassLoader classLoader) {
ClassLoader classLoaderToUse = classLoader;
if (classLoaderToUse == null) {
classLoaderToUse = BeSpiLoader.class.getClassLoader();
}
String serviceTypeName = serviceType.getName();
return loadBeServices(classLoaderToUse).getOrDefault(serviceTypeName, Collections.emptyList());
}
private static Map<String, List<String>> loadBeServices(ClassLoader classLoader) {
Map<String, List<String>> result = cache.get(classLoader);
if (result != null) {
return result;
}
try {
Enumeration<URL> urls = classLoader.getResources(SERVICES_RESOURCE_LOCATION);
result = new HashMap<String, List<String>>();
while (urls.hasMoreElements()) {
URL url = urls.nextElement();
Properties properties = PropertyUtil.loadProperties(url);
for (Map.Entry<?, ?> entry : properties.entrySet()) {
String serviceTypeName = ((String) entry.getKey()).trim();
String[] serviceImplementationNames = ((String) entry.getValue()).split(COMMA_DELIMITED);
for (String serviceImplementationName : serviceImplementationNames) {
if(StringUtil.isEmpty(serviceImplementationName)){
continue;
}
result.computeIfAbsent(serviceTypeName, key -> new ArrayList<>())
.add(serviceImplementationName.trim());
}
}
}
// 去重,并且转换为不能修改得list,按key为classLoader缓存到map
result.replaceAll((serviceType, implementations) -> implementations.stream().distinct()
.collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList)));
cache.put(classLoader, result);
} catch (IOException ex) {
throw new IllegalArgumentException("Unable to load services from location [" +
SERVICES_RESOURCE_LOCATION + "]", ex);
}
return result;
}
@SuppressWarnings("unchecked")
private static <T> T instantiateService(String serviceImplementationName, Class<T> serviceType,
ClassLoader classLoader, Object[] args, Class[] argsTypes) {
try {
Class<?> serviceImplementationClass = ClassUtil.forName(serviceImplementationName, classLoader);
if (!serviceType.isAssignableFrom(serviceImplementationClass)) {
throw new IllegalArgumentException(
"Class [" + serviceImplementationName + "] is not assignable to type [" + serviceType.getName() + "]");
}
if (args == null) {
return (T) ReflectionUtil.accessibleConstructor(serviceImplementationClass).newInstance();
} else {
try{
return (T) ReflectionUtil.accessibleConstructor(serviceImplementationClass, argsTypes).newInstance(args);
}catch (NoSuchMethodException e){
//尝试采用无参构造函数创建实例
return (T) ReflectionUtil.accessibleConstructor(serviceImplementationClass).newInstance();
}
}
} catch (Throwable ex) {
throw new IllegalArgumentException(
"Unable to instantiate class [" + serviceImplementationName + "] for type [" + serviceType.getName() + "]",
ex);
}
}
}
package com.brilliance.swift.spi; package com.brilliance.swift.spi;
import com.brilliance.swift.util.ObjectUtil; import com.brilliance.swift.util.ObjectUtil;
import org.springframework.util.Assert;
import java.lang.ref.Reference; import java.lang.ref.Reference;
import java.lang.ref.SoftReference; import java.lang.ref.SoftReference;
...@@ -45,7 +44,6 @@ public class ConcurrentReferenceMap<T, S> { ...@@ -45,7 +44,6 @@ public class ConcurrentReferenceMap<T, S> {
} }
public S get(Object key) { public S get(Object key) {
Assert.notNull(key, "key must not be null");
Reference<S> sReference = cache.get(key); Reference<S> sReference = cache.get(key);
if (ObjectUtil.isNull(sReference)) { if (ObjectUtil.isNull(sReference)) {
return null; return null;
...@@ -59,8 +57,6 @@ public class ConcurrentReferenceMap<T, S> { ...@@ -59,8 +57,6 @@ public class ConcurrentReferenceMap<T, S> {
} }
public S put(T key, S value) { public S put(T key, S value) {
Assert.notNull(key, "key must not be null");
Assert.notNull(value, "value must not be null");
Reference<S> oldref = null; Reference<S> oldref = null;
if (value instanceof Reference) { if (value instanceof Reference) {
oldref = cache.put(key, (Reference<S>) value); oldref = cache.put(key, (Reference<S>) value);
......
...@@ -18,7 +18,6 @@ package com.brilliance.swift.util; ...@@ -18,7 +18,6 @@ package com.brilliance.swift.util;
import com.brilliance.swift.exception.CommonRuntimeException; import com.brilliance.swift.exception.CommonRuntimeException;
import org.apache.commons.lang.ClassUtils; import org.apache.commons.lang.ClassUtils;
import org.springframework.util.Assert;
import java.io.Closeable; import java.io.Closeable;
import java.io.Externalizable; import java.io.Externalizable;
...@@ -158,9 +157,7 @@ public abstract class ClassUtil { ...@@ -158,9 +157,7 @@ public abstract class ClassUtil {
* @throws ClassNotFoundException 如果类没有被发现 * @throws ClassNotFoundException 如果类没有被发现
* @throws LinkageError 如果类文件不能被加载 * @throws LinkageError 如果类文件不能被加载
*/ */
public static Class<?> forName(String name, ClassLoader classLoader) public static Class<?> forName(String name, ClassLoader classLoader) {
{
Assert.notNull(name, "Name must not be null");
Class<?> clazz = resolvePrimitiveClassName(name); Class<?> clazz = resolvePrimitiveClassName(name);
if (clazz == null) { if (clazz == null) {
clazz = commonClassCache.get(name); clazz = commonClassCache.get(name);
...@@ -257,8 +254,6 @@ public abstract class ClassUtil { ...@@ -257,8 +254,6 @@ public abstract class ClassUtil {
* @return 如果右侧类型可以赋值给左侧类型,则返回true * @return 如果右侧类型可以赋值给左侧类型,则返回true
*/ */
public static boolean isAssignable(Class<?> lhsType, Class<?> rhsType) { public static boolean isAssignable(Class<?> lhsType, Class<?> rhsType) {
Assert.notNull(lhsType, "Left-hand side type must not be null");
Assert.notNull(rhsType, "Right-hand side type must not be null");
if (lhsType.isAssignableFrom(rhsType)) { if (lhsType.isAssignableFrom(rhsType)) {
return true; return true;
} }
...@@ -275,7 +270,6 @@ public abstract class ClassUtil { ...@@ -275,7 +270,6 @@ public abstract class ClassUtil {
* 如果给定的类是基础类型则返回相应的基础类型的包装器类型 * 如果给定的类是基础类型则返回相应的基础类型的包装器类型
*/ */
public static Class<?> resolvePrimitiveIfNecessary(Class<?> clazz) { public static Class<?> resolvePrimitiveIfNecessary(Class<?> clazz) {
Assert.notNull(clazz, "Class must not be null");
return (clazz.isPrimitive() && clazz != void.class ? primitiveTypeToWrapperMap.get(clazz) : clazz); return (clazz.isPrimitive() && clazz != void.class ? primitiveTypeToWrapperMap.get(clazz) : clazz);
} }
...@@ -283,7 +277,6 @@ public abstract class ClassUtil { ...@@ -283,7 +277,6 @@ public abstract class ClassUtil {
* 获取类型全限定名 * 获取类型全限定名
*/ */
public static String getQualifiedName(Class<?> clazz) { public static String getQualifiedName(Class<?> clazz) {
Assert.notNull(clazz, "Class must not be null");
return clazz.getTypeName(); return clazz.getTypeName();
} }
...@@ -293,7 +286,6 @@ public abstract class ClassUtil { ...@@ -293,7 +286,6 @@ public abstract class ClassUtil {
* 参考{@link Class#getSimpleName()} * 参考{@link Class#getSimpleName()}
*/ */
public static String getSimpleName(Class<?> clazz) { public static String getSimpleName(Class<?> clazz) {
Assert.notNull(clazz, "Class must not be null");
return clazz.getSimpleName(); return clazz.getSimpleName();
} }
...@@ -322,7 +314,6 @@ public abstract class ClassUtil { ...@@ -322,7 +314,6 @@ public abstract class ClassUtil {
* 如果类本身是一个接口,它将作为唯一接口数组返回 * 如果类本身是一个接口,它将作为唯一接口数组返回
*/ */
public static Set<Class<?>> getAllInterfacesForClassAsSet(Class<?> clazz, ClassLoader classLoader) { public static Set<Class<?>> getAllInterfacesForClassAsSet(Class<?> clazz, ClassLoader classLoader) {
Assert.notNull(clazz, "Class must not be null");
if (clazz.isInterface() && isVisible(clazz, classLoader)) { if (clazz.isInterface() && isVisible(clazz, classLoader)) {
return Collections.singleton(clazz); return Collections.singleton(clazz);
} }
...@@ -363,8 +354,6 @@ public abstract class ClassUtil { ...@@ -363,8 +354,6 @@ public abstract class ClassUtil {
* @return 方法,如果未找到,则为null * @return 方法,如果未找到,则为null
*/ */
public static Method getMethodIfAvailable(Class<?> clazz, String methodName, Class<?>... paramTypes) { public static Method getMethodIfAvailable(Class<?> clazz, String methodName, Class<?>... paramTypes) {
Assert.notNull(clazz, "Class must not be null");
Assert.notNull(methodName, "Method name must not be null");
if (paramTypes != null) { if (paramTypes != null) {
try { try {
return clazz.getMethod(methodName, paramTypes); return clazz.getMethod(methodName, paramTypes);
...@@ -396,8 +385,6 @@ public abstract class ClassUtil { ...@@ -396,8 +385,6 @@ public abstract class ClassUtil {
* @throws IllegalArgumentException 如果方法名称为空或clazz为空 * @throws IllegalArgumentException 如果方法名称为空或clazz为空
*/ */
public static Method getStaticMethod(Class<?> clazz, String methodName, Class<?>... args) { public static Method getStaticMethod(Class<?> clazz, String methodName, Class<?>... args) {
Assert.notNull(clazz, "Class must not be null");
Assert.notNull(methodName, "Method name must not be null");
try { try {
Method method = clazz.getMethod(methodName, args); Method method = clazz.getMethod(methodName, args);
return Modifier.isStatic(method.getModifiers()) ? method : null; return Modifier.isStatic(method.getModifiers()) ? method : null;
...@@ -414,7 +401,6 @@ public abstract class ClassUtil { ...@@ -414,7 +401,6 @@ public abstract class ClassUtil {
* @return 构造函数,或者如果没有找到返回{@code null} * @return 构造函数,或者如果没有找到返回{@code null}
*/ */
public static <T> Constructor<T> getConstructorIfAvailable(Class<T> clazz, Class<?>... paramTypes) { public static <T> Constructor<T> getConstructorIfAvailable(Class<T> clazz, Class<?>... paramTypes) {
Assert.notNull(clazz, "Class must not be null");
try { try {
return clazz.getConstructor(paramTypes); return clazz.getConstructor(paramTypes);
} catch (NoSuchMethodException ex) { } catch (NoSuchMethodException ex) {
...@@ -442,7 +428,6 @@ public abstract class ClassUtil { ...@@ -442,7 +428,6 @@ public abstract class ClassUtil {
* @param value 应该分配给目标类型的值 * @param value 应该分配给目标类型的值
*/ */
public static boolean isAssignableValue(Class<?> type, Object value) { public static boolean isAssignableValue(Class<?> type, Object value) {
Assert.notNull(type, "Type must not be null");
return (value != null ? isAssignable(type, value.getClass()) : !type.isPrimitive()); return (value != null ? isAssignable(type, value.getClass()) : !type.isPrimitive());
} }
} }
...@@ -3,7 +3,6 @@ package com.brilliance.swift.util; ...@@ -3,7 +3,6 @@ package com.brilliance.swift.util;
import com.brilliance.swift.exception.IORuntimeException; import com.brilliance.swift.exception.IORuntimeException;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.springframework.util.Assert;
import java.io.File; import java.io.File;
import java.io.FileFilter; import java.io.FileFilter;
...@@ -21,7 +20,6 @@ public class FileUtil { ...@@ -21,7 +20,6 @@ public class FileUtil {
public static final String URL_PROTOCOL_FILE = "file"; public static final String URL_PROTOCOL_FILE = "file";
public static URL getURL(String resourceLocation) { public static URL getURL(String resourceLocation) {
Assert.notNull(resourceLocation, "Resource location must not be null");
if (resourceLocation.startsWith(CLASSPATH_URL_PREFIX)) { if (resourceLocation.startsWith(CLASSPATH_URL_PREFIX)) {
String path = resourceLocation.substring(CLASSPATH_URL_PREFIX.length()); String path = resourceLocation.substring(CLASSPATH_URL_PREFIX.length());
ClassLoader cl = ClassUtil.getDefaultClassLoader(); ClassLoader cl = ClassUtil.getDefaultClassLoader();
...@@ -58,19 +56,15 @@ public class FileUtil { ...@@ -58,19 +56,15 @@ public class FileUtil {
} }
public static File getFile(String path) { public static File getFile(String path) {
Assert.notNull(path, "Resource path must not be null");
return getFile(getURL(path)); return getFile(getURL(path));
} }
public static File getFile(String rootPath, String childPath) { public static File getFile(String rootPath, String childPath) {
Assert.notNull(rootPath, "Resource rootPath must not be null");
Assert.notNull(childPath, "Resource childPath must not be null");
File rootFile = getFile(rootPath); File rootFile = getFile(rootPath);
return new File(rootFile, childPath); return new File(rootFile, childPath);
} }
public static File getFile(URL resourceUrl) { public static File getFile(URL resourceUrl) {
Assert.notNull(resourceUrl, "Resource URL must not be null");
if (!URL_PROTOCOL_FILE.equals(resourceUrl.getProtocol())) { if (!URL_PROTOCOL_FILE.equals(resourceUrl.getProtocol())) {
throw new IllegalArgumentException("url cannot be resolved to absolute file path " + throw new IllegalArgumentException("url cannot be resolved to absolute file path " +
"because it does not reside in the file system: " + resourceUrl); "because it does not reside in the file system: " + resourceUrl);
......
...@@ -16,7 +16,6 @@ ...@@ -16,7 +16,6 @@
package com.brilliance.swift.util; package com.brilliance.swift.util;
import org.springframework.util.Assert;
import java.lang.reflect.Array; import java.lang.reflect.Array;
import java.util.Arrays; import java.util.Arrays;
...@@ -111,7 +110,6 @@ public class ObjectUtil { ...@@ -111,7 +110,6 @@ public class ObjectUtil {
return null; return null;
} }
Object result = optional.get(); Object result = optional.get();
Assert.isTrue(!(result instanceof Optional), "不支持多层Optional使用");
return result; return result;
} }
return obj; return obj;
......
...@@ -18,7 +18,6 @@ package com.brilliance.swift.util; ...@@ -18,7 +18,6 @@ package com.brilliance.swift.util;
import com.brilliance.swift.exception.InstantiateException; import com.brilliance.swift.exception.InstantiateException;
import com.brilliance.swift.spi.ConcurrentReferenceMap; import com.brilliance.swift.spi.ConcurrentReferenceMap;
import org.springframework.util.Assert;
import java.lang.reflect.*; import java.lang.reflect.*;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -164,8 +163,6 @@ public class ReflectionUtil { ...@@ -164,8 +163,6 @@ public class ReflectionUtil {
* @param dest 目标对象 * @param dest 目标对象
*/ */
public static void shallowCopyFieldState(final Object src, final Object dest) { public static void shallowCopyFieldState(final Object src, final Object dest) {
Assert.notNull(src, "Source for field copy cannot be null");
Assert.notNull(dest, "Destination for field copy cannot be null");
if (!src.getClass().isAssignableFrom(dest.getClass())) { if (!src.getClass().isAssignableFrom(dest.getClass())) {
throw new IllegalArgumentException("Destination class [" + dest.getClass().getName() + throw new IllegalArgumentException("Destination class [" + dest.getClass().getName() +
"] must be same or subclass as source class [" + src.getClass().getName() + "]"); "] must be same or subclass as source class [" + src.getClass().getName() + "]");
...@@ -361,8 +358,6 @@ public class ReflectionUtil { ...@@ -361,8 +358,6 @@ public class ReflectionUtil {
* @return Method 对象,如果没有找到则为null * @return Method 对象,如果没有找到则为null
*/ */
public static Method findMethod(Class<?> clazz, String name, Class<?>... paramTypes) { public static Method findMethod(Class<?> clazz, String name, Class<?>... paramTypes) {
Assert.notNull(clazz, "Class must not be null");
Assert.notNull(name, "Method name must not be null");
Class<?> searchType = clazz; Class<?> searchType = clazz;
while (searchType != null) { while (searchType != null) {
Method[] methods = (searchType.isInterface() ? searchType.getMethods() : getDeclaredMethods(searchType)); Method[] methods = (searchType.isInterface() ? searchType.getMethods() : getDeclaredMethods(searchType));
...@@ -378,7 +373,6 @@ public class ReflectionUtil { ...@@ -378,7 +373,6 @@ public class ReflectionUtil {
} }
private static Field[] getDeclaredFields(Class<?> clazz) { private static Field[] getDeclaredFields(Class<?> clazz) {
Assert.notNull(clazz, "Class must not be null");
Field[] result = declaredFieldsCache.get(clazz); Field[] result = declaredFieldsCache.get(clazz);
if (result == null) { if (result == null) {
try { try {
...@@ -393,7 +387,6 @@ public class ReflectionUtil { ...@@ -393,7 +387,6 @@ public class ReflectionUtil {
} }
private static Method[] getDeclaredMethods(Class<?> clazz, boolean defensive) { private static Method[] getDeclaredMethods(Class<?> clazz, boolean defensive) {
Assert.notNull(clazz, "Class must not be null");
Method[] result = declaredMethodsCache.get(clazz); Method[] result = declaredMethodsCache.get(clazz);
if (result == null) { if (result == null) {
try { try {
...@@ -435,7 +428,6 @@ public class ReflectionUtil { ...@@ -435,7 +428,6 @@ public class ReflectionUtil {
} }
public static <T> T instantiateClass(Constructor<T> ctor, Object... args) { public static <T> T instantiateClass(Constructor<T> ctor, Object... args) {
Assert.notNull(ctor, "Constructor must not be null");
try { try {
ReflectionUtil.makeAccessible(ctor); ReflectionUtil.makeAccessible(ctor);
return ctor.newInstance(args); return ctor.newInstance(args);
...@@ -490,7 +482,6 @@ public class ReflectionUtil { ...@@ -490,7 +482,6 @@ public class ReflectionUtil {
* @throws IllegalArgumentException 如果 MethodFilter 参数为null * @throws IllegalArgumentException 如果 MethodFilter 参数为null
*/ */
default MethodFilter and(MethodFilter next) { default MethodFilter and(MethodFilter next) {
Assert.notNull(next, "Next MethodFilter must not be null");
return method -> matches(method) && next.matches(method); return method -> matches(method) && next.matches(method);
} }
} }
...@@ -513,7 +504,6 @@ public class ReflectionUtil { ...@@ -513,7 +504,6 @@ public class ReflectionUtil {
* @throws IllegalArgumentException 如果 FieldFilter 参数为null * @throws IllegalArgumentException 如果 FieldFilter 参数为null
*/ */
default FieldFilter and(FieldFilter next) { default FieldFilter and(FieldFilter next) {
Assert.notNull(next, "Next FieldFilter must not be null");
return field -> matches(field) && next.matches(field); return field -> matches(field) && next.matches(field);
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment