Skip to content

Commit 79af0ea

Browse files
committed
统一初始化异常的描述
1 parent 075a682 commit 79af0ea

File tree

3 files changed

+30
-39
lines changed

3 files changed

+30
-39
lines changed

snack3/src/main/java/org/noear/snack/core/Options.java

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,14 @@ public Options sub(Feature... features) {
8888

8989
/**
9090
* 获取特性码
91-
* */
91+
*/
9292
public final int getFeatures() {
9393
return features;
9494
}
9595

9696
/**
9797
* 重新设置特性码
98-
* */
98+
*/
9999
public final void setFeatures(Feature... features) {
100100
this.features = Feature.of(features);
101101
}
@@ -217,30 +217,21 @@ public void setClassLoader(ClassLoader classLoader) {
217217
this.classLoader = classLoader;
218218
}
219219

220-
private final Map<String, Class<?>> clzCached = new ConcurrentHashMap<>();
221-
222-
public Class<?> loadClass( String clzName) {
220+
public Class<?> loadClass(String clzName) {
223221
if (StringUtil.isEmpty(clzName)) {
224222
return null;
225223
}
226224

227225
try {
228-
Class<?> clz = clzCached.get(clzName);
229-
if (clz == null) {
230-
if(classLoader == null) {
231-
clz = Class.forName(clzName);
232-
}else{
233-
clz = classLoader.loadClass(clzName);
234-
}
235-
236-
clzCached.put(clzName, clz);
226+
if (classLoader == null) {
227+
return Class.forName(clzName);
228+
} else {
229+
return classLoader.loadClass(clzName);
237230
}
238-
239-
return clz;
240231
} catch (RuntimeException e) {
241232
throw e;
242233
} catch (Throwable e) {
243234
throw new SnackException("Failed to load class: " + clzName, e);
244235
}
245236
}
246-
}
237+
}

snack3/src/main/java/org/noear/snack/core/utils/BeanUtil.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.noear.snack.exception.SnackException;
44

55
import java.io.Reader;
6+
import java.lang.reflect.Constructor;
67
import java.sql.Clob;
78
import java.util.*;
89
import java.util.concurrent.ConcurrentHashMap;
@@ -16,7 +17,7 @@ public class BeanUtil {
1617

1718
/**
1819
* @deprecated 3.2.55
19-
* */
20+
*/
2021
@Deprecated
2122
public static Class<?> loadClass(String clzName) {
2223
if (StringUtil.isEmpty(clzName)) {
@@ -77,15 +78,27 @@ public static String clobToString(Clob clob) {
7778
return text;
7879
}
7980

80-
public static Object newInstance(Class<?> clz) {
81+
public static Object newInstance(Class<?> clz) throws SnackException {
8182
try {
8283
if (clz.isInterface()) {
8384
return null;
8485
} else {
8586
return clz.getDeclaredConstructor().newInstance();
8687
}
8788
} catch (Throwable e) {
88-
throw new SnackException("The instantiation failed, class: " + clz.getName(), e);
89+
throw new SnackException("Instantiation failed: " + clz.getName(), e);
90+
}
91+
}
92+
93+
public static Object newInstance(Constructor constructor, Object[] args) throws SnackException {
94+
if (constructor == null) {
95+
throw new IllegalArgumentException("constructor is null");
96+
}
97+
98+
try {
99+
return constructor.newInstance(args);
100+
} catch (Throwable e) {
101+
throw new SnackException("Instantiation failed: " + constructor.getDeclaringClass().getName(), e);
89102
}
90103
}
91104
}

snack3/src/main/java/org/noear/snack/to/ObjectToer.java

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ private Object analyse(Context ctx, ONode o, Object rst, Class<?> clz, Type type
104104

105105
return analyseVal(ctx, o.nodeData(), clz);
106106
case Object:
107-
//clz = getTypeByNode(ctx, o, clz);
108107
o.remove(ctx.options.getTypePropertyName());//尝试移除类型内容
109108

110109
if (Properties.class.isAssignableFrom(clz)) {
@@ -626,13 +625,7 @@ public Object analyseBean(Context ctx, ONode o, Object rst, Class<?> clz, Type t
626625
}
627626
}
628627

629-
try {
630-
rst = clzWrap.recordConstructor().newInstance(argsV);
631-
} catch (IllegalArgumentException e) {
632-
throw new IllegalArgumentException("the constructor missing parameters: " + clz.getName(), e);
633-
} catch (Throwable e) {
634-
throw new SnackException("The instantiation failed, class: " + clz.getName(), e);
635-
}
628+
rst = BeanUtil.newInstance(clzWrap.recordConstructor(), argsV);
636629
} else {
637630
//排除字段
638631
Set<String> excNames = null;
@@ -662,13 +655,7 @@ public Object analyseBean(Context ctx, ONode o, Object rst, Class<?> clz, Type t
662655
}
663656
}
664657

665-
try {
666-
rst = clzWrap.recordConstructor().newInstance(argsV);
667-
} catch (IllegalArgumentException e) {
668-
throw new IllegalArgumentException("The constructor missing parameters: " + clz.getName(), e);
669-
} catch (Throwable e) {
670-
throw new SnackException("The instantiation failed, class: " + clz.getName(), e);
671-
}
658+
rst = BeanUtil.newInstance(clzWrap.recordConstructor(), argsV);
672659
}
673660
}
674661

@@ -720,7 +707,7 @@ public Object analyseBean(Context ctx, ONode o, Object rst, Class<?> clz, Type t
720707
return rst;
721708
}
722709

723-
private void setValueForMethod(Context ctx, ONode o, Object rst, Map<String, Type> genericInfo, String name, Method method) throws Exception {
710+
private void setValueForMethod(Context ctx, ONode o, Object rst, Map<String, Type> genericInfo, String name, Method method) throws Exception {
724711
Class<?> fieldT = method.getParameterTypes()[0];
725712

726713
Object val = analyseBeanOfValue(name, fieldT, null, ctx, o, null, genericInfo);
@@ -735,7 +722,7 @@ private void setValueForMethod(Context ctx, ONode o, Object rst, Map<String, Ty
735722
method.invoke(rst, val);
736723
}
737724

738-
private void setValueForField(Context ctx, ONode o, Object rst, Map<String, Type> genericInfo,FieldWrap f, boolean useSetter, boolean useGetter, Set<String> excNames) throws Exception {
725+
private void setValueForField(Context ctx, ONode o, Object rst, Map<String, Type> genericInfo, FieldWrap f, boolean useSetter, boolean useGetter, Set<String> excNames) throws Exception {
739726
if (f.isDeserialize() == false) {
740727
//不做序列化
741728
return;
@@ -813,9 +800,9 @@ private Object analyseBeanOfValue(String fieldK, Class fieldT, Type fieldGt, Con
813800
}
814801

815802
if (fieldK == null) { // key为空,代表扁平化处理
816-
return analyse(ctx, o, rst, fieldT, fieldGt, genericInfo);
803+
return analyse(ctx, o, rst, fieldT, fieldGt, genericInfo);
817804
}
818-
805+
819806
return analyse(ctx, o.get(fieldK), rst, fieldT, fieldGt, genericInfo);
820807
}
821808

0 commit comments

Comments
 (0)