Skip to content

Commit

Permalink
add JSONConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
looly committed Oct 25, 2018
1 parent 55c8159 commit 1ade28c
Show file tree
Hide file tree
Showing 10 changed files with 499 additions and 195 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* 【extra】 Ftp增加setMode方法(issue#INPMZ@Gitee)
* 【core】 IdUtil增加fastUUID和fastSimpleUUID方法(issue#INU37@Gitee)
* 【core】 DateUtil增加formatChineseDate方法(issue#INT6I@Gitee)
* 【core】 ClassUtil中部分方法迁移至ReflectUtil
* 【json】 新增JSONConfig,统一JSON配置,并添加可选的自定义输出日期格式支持

### Bug修复
* 【core】 修复ImageUtil文件流未关闭问题(感谢@【西安】追寻)
Expand Down
134 changes: 52 additions & 82 deletions hutool-core/src/main/java/cn/hutool/core/util/ClassUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@
import java.lang.reflect.Type;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.BasicType;
import cn.hutool.core.exceptions.UtilException;
import cn.hutool.core.io.FileUtil;
Expand All @@ -30,7 +28,7 @@
/**
* 类工具类 <br>
*
* @author xiaoleilu
* @author xiaoleilu
*
*/
public class ClassUtil {
Expand Down Expand Up @@ -84,7 +82,7 @@ public static String getClassName(Class<?> clazz, boolean isSimple) {
}
return isSimple ? clazz.getSimpleName() : clazz.getName();
}

/**
* 获取完整类名的短格式如:<br>
* cn.hutool.core.util.StrUtil -》c.h.c.u.StrUtil
Expand All @@ -95,14 +93,14 @@ public static String getClassName(Class<?> clazz, boolean isSimple) {
*/
public static String getShortClassName(String className) {
final List<String> packages = StrUtil.split(className, CharUtil.DOT);
if(null == packages || packages.size() < 2) {
if (null == packages || packages.size() < 2) {
return className;
}

final int size = packages.size();
final StringBuilder result = StrUtil.builder();
result.append(packages.get(0).charAt(0));
for(int i = 1; i < size - 1; i++) {
for (int i = 1; i < size - 1; i++) {
result.append(CharUtil.DOT).append(packages.get(i).charAt(0));
}
result.append(CharUtil.DOT).append(packages.get(size - 1));
Expand Down Expand Up @@ -213,13 +211,7 @@ public static Set<Class<?>> scanPackage(String packageName, Filter<Class<?>> cla
* @return 方法名Set
*/
public static Set<String> getPublicMethodNames(Class<?> clazz) {
HashSet<String> methodSet = new HashSet<String>();
Method[] methodArray = getPublicMethods(clazz);
for (Method method : methodArray) {
String methodName = method.getName();
methodSet.add(methodName);
}
return methodSet;
return ReflectUtil.getPublicMethodNames(clazz);
}

/**
Expand All @@ -229,7 +221,7 @@ public static Set<String> getPublicMethodNames(Class<?> clazz) {
* @return 过滤后的方法列表
*/
public static Method[] getPublicMethods(Class<?> clazz) {
return clazz.getMethods();
return ReflectUtil.getPublicMethods(clazz);
}

/**
Expand All @@ -240,23 +232,7 @@ public static Method[] getPublicMethods(Class<?> clazz) {
* @return 过滤后的方法列表
*/
public static List<Method> getPublicMethods(Class<?> clazz, Filter<Method> filter) {
if (null == clazz) {
return null;
}

Method[] methods = getPublicMethods(clazz);
List<Method> methodList;
if (null != filter) {
methodList = new ArrayList<>();
for (Method method : methods) {
if (filter.accept(method)) {
methodList.add(method);
}
}
} else {
methodList = CollectionUtil.newArrayList(methods);
}
return methodList;
return ReflectUtil.getPublicMethods(clazz, filter);
}

/**
Expand All @@ -267,13 +243,7 @@ public static List<Method> getPublicMethods(Class<?> clazz, Filter<Method> filte
* @return 过滤后的方法列表
*/
public static List<Method> getPublicMethods(Class<?> clazz, Method... excludeMethods) {
final HashSet<Method> excludeMethodSet = CollectionUtil.newHashSet(excludeMethods);
return getPublicMethods(clazz, new Filter<Method>(){
@Override
public boolean accept(Method method) {
return false == excludeMethodSet.contains(method);
}
});
return ReflectUtil.getPublicMethods(clazz, excludeMethods);
}

/**
Expand All @@ -284,13 +254,7 @@ public boolean accept(Method method) {
* @return 过滤后的方法列表
*/
public static List<Method> getPublicMethods(Class<?> clazz, String... excludeMethodNames) {
final HashSet<String> excludeMethodNameSet = CollectionUtil.newHashSet(excludeMethodNames);
return getPublicMethods(clazz, new Filter<Method>(){
@Override
public boolean accept(Method method) {
return false == excludeMethodNameSet.contains(method.getName());
}
});
return getPublicMethods(clazz, excludeMethodNames);
}

/**
Expand All @@ -303,11 +267,7 @@ public boolean accept(Method method) {
* @throws SecurityException 无权访问抛出异常
*/
public static Method getPublicMethod(Class<?> clazz, String methodName, Class<?>... paramTypes) throws SecurityException {
try {
return clazz.getMethod(methodName, paramTypes);
} catch (NoSuchMethodException ex) {
return null;
}
return ReflectUtil.getPublicMethod(clazz, methodName, paramTypes);
}

/**
Expand Down Expand Up @@ -401,7 +361,7 @@ public static Field[] getDeclaredFields(Class<?> clazz) throws SecurityException
public static Set<String> getClassPathResources() {
return getClassPathResources(false);
}

/**
* 获得ClassPath
*
Expand All @@ -412,7 +372,7 @@ public static Set<String> getClassPathResources() {
public static Set<String> getClassPathResources(boolean isDecode) {
return getClassPaths(StrUtil.EMPTY, isDecode);
}

/**
* 获得ClassPath,不解码路径中的特殊字符(例如空格和中文)
*
Expand Down Expand Up @@ -447,7 +407,7 @@ public static Set<String> getClassPaths(String packageName, boolean isDecode) {
}
return paths;
}

/**
* 获得ClassPath,将编码后的中文路径解码为原字符<br>
* 这个ClassPath路径会文件路径被标准化处理
Expand Down Expand Up @@ -570,25 +530,25 @@ public static boolean isAllAssignableFrom(Class<?>[] types1, Class<?>[] types2)
if (ArrayUtil.isEmpty(types1) && ArrayUtil.isEmpty(types2)) {
return true;
}
if(null == types1 || null == types2) {
//任何一个为null不相等(之前已判断两个都为null的情况)
if (null == types1 || null == types2) {
// 任何一个为null不相等(之前已判断两个都为null的情况)
return false;
}
if (types1.length != types2.length) {
return false;
}

Class<?> type1;
Class<?> type2;
for (int i = 0; i < types1.length; i++) {
type1 = types1[i];
type2 = types2[i];
if(isBasicType(type1) && isBasicType(type2)) {
//原始类型和包装类型存在不一致情况
if(BasicType.unWrap(type1) != BasicType.unWrap(type2)) {
if (isBasicType(type1) && isBasicType(type2)) {
// 原始类型和包装类型存在不一致情况
if (BasicType.unWrap(type1) != BasicType.unWrap(type2)) {
return false;
}
}else if (false == type1.isAssignableFrom(type2)) {
} else if (false == type1.isAssignableFrom(type2)) {
return false;
}
}
Expand Down Expand Up @@ -627,8 +587,7 @@ public static <T> Class<T> loadClass(String className) {
* 非单例模式,如果是非静态方法,每次创建一个新对象
*
* @param <T> 对象类型
* @param classNameDotMethodName 类名和方法名表达式,类名与方法名用<code>.</code>或<code>#</code>连接
* 例如:com.xiaoleilu.hutool.StrUtil.isEmpty 或 com.xiaoleilu.hutool.StrUtil#isEmpty
* @param classNameDotMethodName 类名和方法名表达式,类名与方法名用<code>.</code>或<code>#</code>连接 例如:com.xiaoleilu.hutool.StrUtil.isEmpty 或 com.xiaoleilu.hutool.StrUtil#isEmpty
* @param args 参数,必须严格对应指定方法的参数类型和数量
* @return 返回结果
*/
Expand Down Expand Up @@ -761,8 +720,15 @@ public static boolean isSimpleTypeOrArray(Class<?> clazz) {
* @return 是否为简单值类型
*/
public static boolean isSimpleValueType(Class<?> clazz) {
return isBasicType(clazz) || clazz.isEnum() || CharSequence.class.isAssignableFrom(clazz) || Number.class.isAssignableFrom(clazz) || Date.class.isAssignableFrom(clazz) || clazz
.equals(URI.class) || clazz.equals(URL.class) || clazz.equals(Locale.class) || clazz.equals(Class.class);
return isBasicType(clazz) //
|| clazz.isEnum() //
|| CharSequence.class.isAssignableFrom(clazz) //
|| Number.class.isAssignableFrom(clazz) //
|| Date.class.isAssignableFrom(clazz) //
|| clazz.equals(URI.class) //
|| clazz.equals(URL.class) //
|| clazz.equals(Locale.class) //
|| clazz.equals(Class.class);//
}

/**
Expand Down Expand Up @@ -884,6 +850,7 @@ public static boolean isAbstract(Class<?> clazz) {
/**
* 是否为标准的类<br>
* 这个类必须:
*
* <pre>
* 1、非接口
* 2、非抽象类
Expand All @@ -906,9 +873,10 @@ public static boolean isNormalClass(Class<?> clazz) {
&& false == clazz.isSynthetic() //
&& false == clazz.isPrimitive();//
}

/**
* 判断类是否为枚举类型
*
* @param clazz 类
* @return 是否为枚举类型
* @since 3.2.0
Expand Down Expand Up @@ -973,10 +941,11 @@ public static String getPackage(Class<?> clazz) {
public static String getPackagePath(Class<?> clazz) {
return getPackage(clazz).replace(StrUtil.C_DOT, StrUtil.C_SLASH);
}

/**
* 获取指定类型分的默认值<br>
* 默认值规则为:
*
* <pre>
* 1、如果为原始类型,返回0
* 2、非原始类型返回{@code null}
Expand All @@ -987,38 +956,39 @@ public static String getPackagePath(Class<?> clazz) {
* @since 3.0.8
*/
public static Object getDefaultValue(Class<?> clazz) {
if(clazz.isPrimitive()) {
if(long.class == clazz) {
if (clazz.isPrimitive()) {
if (long.class == clazz) {
return 0L;
}else if(int.class == clazz) {
} else if (int.class == clazz) {
return 0;
}else if(short.class == clazz) {
return (short)0;
}else if(char.class == clazz) {
return (char)0;
}else if(byte.class == clazz) {
return (byte)0;
}else if(double.class == clazz) {
} else if (short.class == clazz) {
return (short) 0;
} else if (char.class == clazz) {
return (char) 0;
} else if (byte.class == clazz) {
return (byte) 0;
} else if (double.class == clazz) {
return 0D;
}else if(float.class == clazz) {
} else if (float.class == clazz) {
return 0f;
}else if(boolean.class == clazz) {
} else if (boolean.class == clazz) {
return false;
}
}

return null;
}

/**
* 获得默认值列表
*
* @param classes 值类型
* @return 默认值列表
* @since 3.0.9
*/
public static Object[] getDefaultValues(Class<?>... classes) {
final Object[] values = new Object[classes.length];
for(int i = 0; i < classes.length; i++) {
for (int i = 0; i < classes.length; i++) {
values[i] = getDefaultValue(classes[i]);
}
return values;
Expand Down
Loading

0 comments on commit 1ade28c

Please sign in to comment.