mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
remove methods
This commit is contained in:
parent
9268dc220c
commit
6c8fb79b38
@ -2,9 +2,8 @@ package cn.hutool.core.classloader;
|
||||
|
||||
import cn.hutool.core.exceptions.UtilException;
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.reflect.ClassUtil;
|
||||
import cn.hutool.core.reflect.ReflectUtil;
|
||||
import cn.hutool.core.net.URLUtil;
|
||||
import cn.hutool.core.reflect.ReflectUtil;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
@ -54,7 +53,7 @@ public class JarClassLoader extends URLClassLoader {
|
||||
*/
|
||||
public static void loadJar(final URLClassLoader loader, final File jarFile) throws UtilException {
|
||||
try {
|
||||
final Method method = ClassUtil.getDeclaredMethod(URLClassLoader.class, "addURL", URL.class);
|
||||
final Method method = ReflectUtil.getMethod(URLClassLoader.class, "addURL", URL.class);
|
||||
if (null != method) {
|
||||
method.setAccessible(true);
|
||||
final List<File> jars = loopJar(jarFile);
|
||||
@ -94,7 +93,7 @@ public class JarClassLoader extends URLClassLoader {
|
||||
* @param urls 被加载的URL
|
||||
*/
|
||||
public JarClassLoader(final URL[] urls) {
|
||||
super(urls, ClassUtil.getClassLoader());
|
||||
super(urls, ClassLoaderUtil.getClassLoader());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,7 +1,6 @@
|
||||
package cn.hutool.core.comparator;
|
||||
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.reflect.ClassUtil;
|
||||
import cn.hutool.core.reflect.ReflectUtil;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
|
||||
@ -56,7 +55,7 @@ public class FieldComparator<T> extends FuncComparator<T> {
|
||||
* @return 非null字段
|
||||
*/
|
||||
private static Field getNonNullField(final Class<?> beanClass, final String fieldName) {
|
||||
final Field field = ClassUtil.getDeclaredField(beanClass, fieldName);
|
||||
final Field field = ReflectUtil.getField(beanClass, fieldName);
|
||||
if (field == null) {
|
||||
throw new IllegalArgumentException(StrUtil.format("Field [{}] not found in Class [{}]", fieldName, beanClass.getName()));
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
package cn.hutool.core.comparator;
|
||||
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.reflect.ClassUtil;
|
||||
import cn.hutool.core.reflect.ReflectUtil;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
@ -36,7 +36,7 @@ public class FieldsComparator<T> extends NullComparator<T> {
|
||||
super(nullGreater, (a, b) -> {
|
||||
Field field;
|
||||
for (final String fieldName : fieldNames) {
|
||||
field = ClassUtil.getDeclaredField(beanClass, fieldName);
|
||||
field = ReflectUtil.getField(beanClass, fieldName);
|
||||
Assert.notNull(field, "Field [{}] not found in Class [{}]", fieldName, beanClass.getName());
|
||||
final int compare = new FieldComparator<>(field).compare(a, b);
|
||||
if (0 != compare) {
|
||||
|
@ -1,11 +1,11 @@
|
||||
package cn.hutool.core.io.resource;
|
||||
|
||||
import cn.hutool.core.classloader.ClassLoaderUtil;
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.reflect.ClassUtil;
|
||||
import cn.hutool.core.util.ObjUtil;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
import cn.hutool.core.net.URLUtil;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
import cn.hutool.core.util.ObjUtil;
|
||||
|
||||
import java.net.URL;
|
||||
|
||||
@ -69,7 +69,7 @@ public class ClassPathResource extends UrlResource {
|
||||
this.path = path;
|
||||
this.name = StrUtil.isBlank(path) ? null : FileUtil.getName(path);
|
||||
|
||||
this.classLoader = ObjUtil.defaultIfNull(classLoader, ClassUtil::getClassLoader);
|
||||
this.classLoader = ObjUtil.defaultIfNull(classLoader, ClassLoaderUtil::getClassLoader);
|
||||
this.clazz = clazz;
|
||||
initUrl();
|
||||
}
|
||||
|
@ -8,9 +8,10 @@ import cn.hutool.core.io.IORuntimeException;
|
||||
import cn.hutool.core.io.resource.ResourceUtil;
|
||||
import cn.hutool.core.net.URLDecoder;
|
||||
import cn.hutool.core.net.URLUtil;
|
||||
import cn.hutool.core.reflect.ClassUtil;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
import cn.hutool.core.util.*;
|
||||
import cn.hutool.core.util.CharUtil;
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.core.util.SystemUtil;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
@ -279,7 +280,7 @@ public class ClassScanner implements Serializable {
|
||||
* 扫描Java指定的ClassPath路径
|
||||
*/
|
||||
private void scanJavaClassPaths() {
|
||||
final String[] javaClassPaths = ClassUtil.getJavaClassPaths();
|
||||
final String[] javaClassPaths = SystemUtil.getJavaClassPaths();
|
||||
for (String classPath : javaClassPaths) {
|
||||
// bug修复,由于路径中空格和中文导致的Jar找不到
|
||||
classPath = URLDecoder.decode(classPath, CharsetUtil.defaultCharset());
|
||||
|
@ -5,12 +5,10 @@ import cn.hutool.core.classloader.ClassLoaderUtil;
|
||||
import cn.hutool.core.convert.BasicType;
|
||||
import cn.hutool.core.exceptions.UtilException;
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.io.IORuntimeException;
|
||||
import cn.hutool.core.io.resource.ResourceUtil;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.lang.ClassScanner;
|
||||
import cn.hutool.core.lang.Singleton;
|
||||
import cn.hutool.core.lang.func.Filter;
|
||||
import cn.hutool.core.net.URLDecoder;
|
||||
import cn.hutool.core.net.URLUtil;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
@ -20,7 +18,6 @@ import cn.hutool.core.util.CharsetUtil;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.lang.reflect.Type;
|
||||
@ -245,158 +242,6 @@ public class ClassUtil {
|
||||
return ClassScanner.scanPackage(packageName, classFilter);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------------------- Method
|
||||
|
||||
/**
|
||||
* 获得指定类中的Public方法名<br>
|
||||
* 去重重载的方法
|
||||
*
|
||||
* @param clazz 类
|
||||
* @return 方法名Set
|
||||
*/
|
||||
public static Set<String> getPublicMethodNames(final Class<?> clazz) {
|
||||
return ReflectUtil.getPublicMethodNames(clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得本类及其父类所有Public方法
|
||||
*
|
||||
* @param clazz 查找方法的类
|
||||
* @return 过滤后的方法列表
|
||||
*/
|
||||
public static Method[] getPublicMethods(final Class<?> clazz) {
|
||||
return ReflectUtil.getPublicMethods(clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得指定类过滤后的Public方法列表
|
||||
*
|
||||
* @param clazz 查找方法的类
|
||||
* @param filter 过滤器
|
||||
* @return 过滤后的方法列表
|
||||
*/
|
||||
public static List<Method> getPublicMethods(final Class<?> clazz, final Filter<Method> filter) {
|
||||
return ReflectUtil.getPublicMethods(clazz, filter);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得指定类过滤后的Public方法列表
|
||||
*
|
||||
* @param clazz 查找方法的类
|
||||
* @param excludeMethods 不包括的方法
|
||||
* @return 过滤后的方法列表
|
||||
*/
|
||||
public static List<Method> getPublicMethods(final Class<?> clazz, final Method... excludeMethods) {
|
||||
return ReflectUtil.getPublicMethods(clazz, excludeMethods);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得指定类过滤后的Public方法列表
|
||||
*
|
||||
* @param clazz 查找方法的类
|
||||
* @param excludeMethodNames 不包括的方法名列表
|
||||
* @return 过滤后的方法列表
|
||||
*/
|
||||
public static List<Method> getPublicMethods(final Class<?> clazz, final String... excludeMethodNames) {
|
||||
return ReflectUtil.getPublicMethods(clazz, excludeMethodNames);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查找指定Public方法 如果找不到对应的方法或方法不为public的则返回{@code null}
|
||||
*
|
||||
* @param clazz 类
|
||||
* @param methodName 方法名
|
||||
* @param paramTypes 参数类型
|
||||
* @return 方法
|
||||
* @throws SecurityException 无权访问抛出异常
|
||||
*/
|
||||
public static Method getPublicMethod(final Class<?> clazz, final String methodName, final Class<?>... paramTypes) throws SecurityException {
|
||||
return ReflectUtil.getPublicMethod(clazz, methodName, paramTypes);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得指定类中的Public方法名<br>
|
||||
* 去重重载的方法
|
||||
*
|
||||
* @param clazz 类
|
||||
* @return 方法名Set
|
||||
*/
|
||||
public static Set<String> getDeclaredMethodNames(final Class<?> clazz) {
|
||||
return ReflectUtil.getMethodNames(clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得声明的所有方法,包括本类及其父类和接口的所有方法和Object类的方法
|
||||
*
|
||||
* @param clazz 类
|
||||
* @return 方法数组
|
||||
*/
|
||||
public static Method[] getDeclaredMethods(final Class<?> clazz) {
|
||||
return ReflectUtil.getMethods(clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查找指定对象中的所有方法(包括非public方法),也包括父对象和Object类的方法
|
||||
*
|
||||
* @param obj 被查找的对象
|
||||
* @param methodName 方法名
|
||||
* @param args 参数
|
||||
* @return 方法
|
||||
* @throws SecurityException 无访问权限抛出异常
|
||||
*/
|
||||
public static Method getDeclaredMethodOfObj(final Object obj, final String methodName, final Object... args) throws SecurityException {
|
||||
return getDeclaredMethod(obj.getClass(), methodName, getClasses(args));
|
||||
}
|
||||
|
||||
/**
|
||||
* 查找指定类中的所有方法(包括非public方法),也包括父类和Object类的方法 找不到方法会返回{@code null}
|
||||
*
|
||||
* @param clazz 被查找的类
|
||||
* @param methodName 方法名
|
||||
* @param parameterTypes 参数类型
|
||||
* @return 方法
|
||||
* @throws SecurityException 无访问权限抛出异常
|
||||
*/
|
||||
public static Method getDeclaredMethod(final Class<?> clazz, final String methodName, final Class<?>... parameterTypes) throws SecurityException {
|
||||
return ReflectUtil.getMethod(clazz, methodName, parameterTypes);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------------------- Field
|
||||
|
||||
/**
|
||||
* 查找指定类中的所有字段(包括非public字段), 字段不存在则返回{@code null}
|
||||
*
|
||||
* @param clazz 被查找字段的类
|
||||
* @param fieldName 字段名
|
||||
* @return 字段
|
||||
* @throws SecurityException 安全异常
|
||||
*/
|
||||
public static Field getDeclaredField(final Class<?> clazz, final String fieldName) throws SecurityException {
|
||||
if (null == clazz || StrUtil.isBlank(fieldName)) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return clazz.getDeclaredField(fieldName);
|
||||
} catch (final NoSuchFieldException e) {
|
||||
// e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查找指定类中的所有字段(包括非public字段)
|
||||
*
|
||||
* @param clazz 被查找字段的类
|
||||
* @return 字段
|
||||
* @throws SecurityException 安全异常
|
||||
*/
|
||||
public static Field[] getDeclaredFields(final Class<?> clazz) throws SecurityException {
|
||||
if (null == clazz) {
|
||||
return null;
|
||||
}
|
||||
return clazz.getDeclaredFields();
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------------------- Classpath
|
||||
|
||||
/**
|
||||
@ -441,7 +286,7 @@ public class ClassUtil {
|
||||
final String packagePath = packageName.replace(StrUtil.DOT, StrUtil.SLASH);
|
||||
final Enumeration<URL> resources;
|
||||
try {
|
||||
resources = getClassLoader().getResources(packagePath);
|
||||
resources = ClassLoaderUtil.getClassLoader().getResources(packagePath);
|
||||
} catch (final IOException e) {
|
||||
throw new UtilException(e, "Loading classPath [{}] error!", packagePath);
|
||||
}
|
||||
@ -472,99 +317,11 @@ public class ClassUtil {
|
||||
* @since 3.2.1
|
||||
*/
|
||||
public static String getClassPath(final boolean isEncoded) {
|
||||
final URL classPathURL = getClassPathURL();
|
||||
final URL classPathURL = ResourceUtil.getResource(StrUtil.EMPTY);
|
||||
final String url = isEncoded ? classPathURL.getPath() : URLUtil.getDecodedPath(classPathURL);
|
||||
return FileUtil.normalize(url);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得ClassPath URL
|
||||
*
|
||||
* @return ClassPath URL
|
||||
*/
|
||||
public static URL getClassPathURL() {
|
||||
return getResourceURL(StrUtil.EMPTY);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得资源的URL<br>
|
||||
* 路径用/分隔,例如:
|
||||
*
|
||||
* <pre>
|
||||
* config/a/db.config
|
||||
* spring/xml/test.xml
|
||||
* </pre>
|
||||
*
|
||||
* @param resource 资源(相对Classpath的路径)
|
||||
* @return 资源URL
|
||||
* @see ResourceUtil#getResource(String)
|
||||
*/
|
||||
public static URL getResourceURL(final String resource) throws IORuntimeException {
|
||||
return ResourceUtil.getResource(resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定路径下的资源列表<br>
|
||||
* 路径格式必须为目录格式,用/分隔,例如:
|
||||
*
|
||||
* <pre>
|
||||
* config/a
|
||||
* spring/xml
|
||||
* </pre>
|
||||
*
|
||||
* @param resource 资源路径
|
||||
* @return 资源列表
|
||||
* @see ResourceUtil#getResources(String)
|
||||
*/
|
||||
public static List<URL> getResources(final String resource) {
|
||||
return ResourceUtil.getResources(resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得资源相对路径对应的URL
|
||||
*
|
||||
* @param resource 资源相对路径
|
||||
* @param baseClass 基准Class,获得的相对路径相对于此Class所在路径,如果为{@code null}则相对ClassPath
|
||||
* @return {@link URL}
|
||||
* @see ResourceUtil#getResource(String, Class)
|
||||
*/
|
||||
public static URL getResourceUrl(final String resource, final Class<?> baseClass) {
|
||||
return ResourceUtil.getResource(resource, baseClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 获得Java ClassPath路径,不包括 jre
|
||||
*/
|
||||
public static String[] getJavaClassPaths() {
|
||||
return System.getProperty("java.class.path").split(System.getProperty("path.separator"));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前线程的{@link ClassLoader}
|
||||
*
|
||||
* @return 当前线程的class loader
|
||||
* @see ClassLoaderUtil#getClassLoader()
|
||||
*/
|
||||
public static ClassLoader getContextClassLoader() {
|
||||
return ClassLoaderUtil.getContextClassLoader();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取{@link ClassLoader}<br>
|
||||
* 获取顺序如下:<br>
|
||||
*
|
||||
* <pre>
|
||||
* 1、获取当前线程的ContextClassLoader
|
||||
* 2、获取{@link ClassLoaderUtil}类对应的ClassLoader
|
||||
* 3、获取系统ClassLoader({@link ClassLoader#getSystemClassLoader()})
|
||||
* </pre>
|
||||
*
|
||||
* @return 类加载器
|
||||
*/
|
||||
public static ClassLoader getClassLoader() {
|
||||
return ClassLoaderUtil.getClassLoader();
|
||||
}
|
||||
|
||||
/**
|
||||
* 比较判断types1和types2两组类,如果types1中所有的类都与types2对应位置的类相同,或者是其父类或接口,则返回{@code true}
|
||||
*
|
||||
@ -703,7 +460,7 @@ public class ClassUtil {
|
||||
public static <T> T invoke(final String className, final String methodName, final boolean isSingleton, final Object... args) {
|
||||
final Class<Object> clazz = loadClass(className);
|
||||
try {
|
||||
final Method method = getDeclaredMethod(clazz, methodName, getClasses(args));
|
||||
final Method method = ReflectUtil.getMethod(clazz, methodName, getClasses(args));
|
||||
if (null == method) {
|
||||
throw new NoSuchMethodException(StrUtil.format("No such method: [{}]", methodName));
|
||||
}
|
||||
|
@ -396,26 +396,19 @@ public class ReflectUtil {
|
||||
*
|
||||
* @param clazz 查找方法的类
|
||||
* @param filter 过滤器
|
||||
* @return 过滤后的方法列表
|
||||
* @return 过滤后的方法数组
|
||||
*/
|
||||
public static List<Method> getPublicMethods(final Class<?> clazz, final Filter<Method> filter) {
|
||||
public static Method[] getPublicMethods(final Class<?> clazz, final Filter<Method> filter) {
|
||||
if (null == clazz) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final Method[] methods = getPublicMethods(clazz);
|
||||
final List<Method> methodList;
|
||||
if (null != filter) {
|
||||
methodList = new ArrayList<>();
|
||||
for (final Method method : methods) {
|
||||
if (filter.accept(method)) {
|
||||
methodList.add(method);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
methodList = CollUtil.newArrayList(methods);
|
||||
if(null == filter){
|
||||
return methods;
|
||||
}
|
||||
return methodList;
|
||||
|
||||
return ArrayUtil.filter(methods, filter);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -425,7 +418,7 @@ public class ReflectUtil {
|
||||
* @param excludeMethods 不包括的方法
|
||||
* @return 过滤后的方法列表
|
||||
*/
|
||||
public static List<Method> getPublicMethods(final Class<?> clazz, final Method... excludeMethods) {
|
||||
public static Method[] getPublicMethods(final Class<?> clazz, final Method... excludeMethods) {
|
||||
final HashSet<Method> excludeMethodSet = CollUtil.newHashSet(excludeMethods);
|
||||
return getPublicMethods(clazz, method -> false == excludeMethodSet.contains(method));
|
||||
}
|
||||
@ -435,9 +428,9 @@ public class ReflectUtil {
|
||||
*
|
||||
* @param clazz 查找方法的类
|
||||
* @param excludeMethodNames 不包括的方法名列表
|
||||
* @return 过滤后的方法列表
|
||||
* @return 过滤后的方法数组
|
||||
*/
|
||||
public static List<Method> getPublicMethods(final Class<?> clazz, final String... excludeMethodNames) {
|
||||
public static Method[] getPublicMethods(final Class<?> clazz, final String... excludeMethodNames) {
|
||||
final HashSet<String> excludeMethodNameSet = CollUtil.newHashSet(excludeMethodNames);
|
||||
return getPublicMethods(clazz, method -> false == excludeMethodNameSet.contains(method.getName()));
|
||||
}
|
||||
|
@ -144,4 +144,13 @@ public class SystemUtil {
|
||||
System.setProperty(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得Java ClassPath路径,不包括 jre
|
||||
*
|
||||
* @return Java ClassPath路径,不包括 jre
|
||||
*/
|
||||
public static String[] getJavaClassPaths() {
|
||||
return get("java.class.path").split(get("path.separator"));
|
||||
}
|
||||
}
|
||||
|
@ -4,8 +4,6 @@ import cn.hutool.core.reflect.ClassUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
@ -25,73 +23,6 @@ public class ClassUtilTest {
|
||||
Assert.assertEquals("ClassUtil", simpleClassName);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
static class TestClass {
|
||||
private String privateField;
|
||||
protected String field;
|
||||
|
||||
private void privateMethod() {
|
||||
}
|
||||
|
||||
public void publicMethod() {
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings({"unused", "InnerClassMayBeStatic"})
|
||||
class TestSubClass extends TestClass {
|
||||
private String subField;
|
||||
|
||||
private void privateSubMethod() {
|
||||
}
|
||||
|
||||
public void publicSubMethod() {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getPublicMethod() {
|
||||
final Method superPublicMethod = ClassUtil.getPublicMethod(TestSubClass.class, "publicMethod");
|
||||
Assert.assertNotNull(superPublicMethod);
|
||||
final Method superPrivateMethod = ClassUtil.getPublicMethod(TestSubClass.class, "privateMethod");
|
||||
Assert.assertNull(superPrivateMethod);
|
||||
|
||||
final Method publicMethod = ClassUtil.getPublicMethod(TestSubClass.class, "publicSubMethod");
|
||||
Assert.assertNotNull(publicMethod);
|
||||
final Method privateMethod = ClassUtil.getPublicMethod(TestSubClass.class, "privateSubMethod");
|
||||
Assert.assertNull(privateMethod);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getDeclaredMethod() {
|
||||
final Method noMethod = ClassUtil.getDeclaredMethod(TestSubClass.class, "noMethod");
|
||||
Assert.assertNull(noMethod);
|
||||
|
||||
final Method privateMethod = ClassUtil.getDeclaredMethod(TestSubClass.class, "privateMethod");
|
||||
Assert.assertNotNull(privateMethod);
|
||||
final Method publicMethod = ClassUtil.getDeclaredMethod(TestSubClass.class, "publicMethod");
|
||||
Assert.assertNotNull(publicMethod);
|
||||
|
||||
final Method publicSubMethod = ClassUtil.getDeclaredMethod(TestSubClass.class, "publicSubMethod");
|
||||
Assert.assertNotNull(publicSubMethod);
|
||||
final Method privateSubMethod = ClassUtil.getDeclaredMethod(TestSubClass.class, "privateSubMethod");
|
||||
Assert.assertNotNull(privateSubMethod);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getDeclaredField() {
|
||||
final Field noField = ClassUtil.getDeclaredField(TestSubClass.class, "noField");
|
||||
Assert.assertNull(noField);
|
||||
|
||||
// 获取不到父类字段
|
||||
final Field field = ClassUtil.getDeclaredField(TestSubClass.class, "field");
|
||||
Assert.assertNull(field);
|
||||
|
||||
final Field subField = ClassUtil.getDeclaredField(TestSubClass.class, "subField");
|
||||
Assert.assertNotNull(subField);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getClassPathTest() {
|
||||
final String classPath = ClassUtil.getClassPath();
|
||||
|
@ -4,11 +4,10 @@ import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.date.TimeInterval;
|
||||
import cn.hutool.core.date.Week;
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.core.lang.test.bean.ExamInfoDict;
|
||||
import cn.hutool.core.reflect.ClassUtil;
|
||||
import cn.hutool.core.reflect.ReflectUtil;
|
||||
import cn.hutool.core.lang.test.bean.ExamInfoDict;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
import cn.hutool.core.util.ClassUtilTest.TestSubClass;
|
||||
import lombok.Data;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
@ -88,14 +87,14 @@ public class ReflectUtilTest {
|
||||
|
||||
@Test
|
||||
public void setFieldTest() {
|
||||
final TestClass testClass = new TestClass();
|
||||
final AClass testClass = new AClass();
|
||||
ReflectUtil.setFieldValue(testClass, "a", "111");
|
||||
Assert.assertEquals(111, testClass.getA());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void invokeTest() {
|
||||
final TestClass testClass = new TestClass();
|
||||
final AClass testClass = new AClass();
|
||||
ReflectUtil.invoke(testClass, "setA", 10);
|
||||
Assert.assertEquals(10, testClass.getA());
|
||||
}
|
||||
@ -108,7 +107,7 @@ public class ReflectUtilTest {
|
||||
}
|
||||
|
||||
@Data
|
||||
static class TestClass {
|
||||
static class AClass {
|
||||
private int a;
|
||||
}
|
||||
|
||||
@ -220,6 +219,7 @@ public class ReflectUtilTest {
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("AbstractMethodOverridesAbstractMethod")
|
||||
interface TestInterface2 extends TestInterface1 {
|
||||
@Override
|
||||
void getB();
|
||||
@ -271,4 +271,71 @@ public class ReflectUtilTest {
|
||||
final int[] intArray = ReflectUtil.newInstanceIfPossible(int[].class);
|
||||
Assert.assertArrayEquals(new int[0], intArray);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getPublicMethod() {
|
||||
final Method superPublicMethod = ReflectUtil.getPublicMethod(TestSubClass.class, "publicMethod");
|
||||
Assert.assertNotNull(superPublicMethod);
|
||||
final Method superPrivateMethod = ReflectUtil.getPublicMethod(TestSubClass.class, "privateMethod");
|
||||
Assert.assertNull(superPrivateMethod);
|
||||
|
||||
final Method publicMethod = ReflectUtil.getPublicMethod(TestSubClass.class, "publicSubMethod");
|
||||
Assert.assertNotNull(publicMethod);
|
||||
final Method privateMethod = ReflectUtil.getPublicMethod(TestSubClass.class, "privateSubMethod");
|
||||
Assert.assertNull(privateMethod);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getDeclaredMethod() {
|
||||
final Method noMethod = ReflectUtil.getMethod(TestSubClass.class, "noMethod");
|
||||
Assert.assertNull(noMethod);
|
||||
|
||||
final Method privateMethod = ReflectUtil.getMethod(TestSubClass.class, "privateMethod");
|
||||
Assert.assertNotNull(privateMethod);
|
||||
final Method publicMethod = ReflectUtil.getMethod(TestSubClass.class, "publicMethod");
|
||||
Assert.assertNotNull(publicMethod);
|
||||
|
||||
final Method publicSubMethod = ReflectUtil.getMethod(TestSubClass.class, "publicSubMethod");
|
||||
Assert.assertNotNull(publicSubMethod);
|
||||
final Method privateSubMethod = ReflectUtil.getMethod(TestSubClass.class, "privateSubMethod");
|
||||
Assert.assertNotNull(privateSubMethod);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getDeclaredField() {
|
||||
final Field noField = ReflectUtil.getField(TestSubClass.class, "noField");
|
||||
Assert.assertNull(noField);
|
||||
|
||||
// 获取不到父类字段
|
||||
final Field field = ReflectUtil.getField(TestSubClass.class, "field");
|
||||
Assert.assertNull(field);
|
||||
|
||||
final Field subField = ReflectUtil.getField(TestSubClass.class, "subField");
|
||||
Assert.assertNotNull(subField);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
static class TestClass {
|
||||
private String privateField;
|
||||
protected String field;
|
||||
|
||||
private void privateMethod() {
|
||||
}
|
||||
|
||||
public void publicMethod() {
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings({"InnerClassMayBeStatic", "unused"})
|
||||
class TestSubClass extends TestClass {
|
||||
private String subField;
|
||||
|
||||
private void privateSubMethod() {
|
||||
}
|
||||
|
||||
public void publicSubMethod() {
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user