Skip to content

Commit

Permalink
优化ReflectionHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
wh1t3p1g committed Jun 20, 2024
1 parent 23677fc commit 11eed6a
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions core/src/main/java/ysomap/core/util/ReflectionHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ public static Field getField(final Class<?> clazz, final String fieldName) {
}

public static void setFieldValue(final Object obj, final String fieldName, final Object value) throws Exception {
final Field field = getField(obj.getClass(), fieldName);
Field field = null;
if(obj instanceof Class){
field = getField((Class<?>) obj, fieldName);
}else{
field = getField(obj.getClass(), fieldName);
}

if(field != null) {
field.set(obj, value);
}
Expand All @@ -49,7 +55,13 @@ public static void setStaticFieldValue(final Class<?> clazz, final String fieldN
}

public static Object getFieldValue(final Object obj, final String fieldName) throws Exception {
final Field field = getField(obj.getClass(), fieldName);
Field field = null;
if(obj instanceof Class){
field = getField((Class<?>) obj, fieldName);
}else{
field = getField(obj.getClass(), fieldName);
}

return field.get(obj);
}

Expand Down Expand Up @@ -109,7 +121,7 @@ public static void checkClassFieldsNotNull(Object obj) throws ArgumentsNotComple
public static void checkValueNotNull(Object obj, String field) throws ArgumentsNotCompleteException {
Object fvalue = null;
try {
fvalue = ReflectionHelper.getFieldValue(obj, field);
fvalue = getFieldValue(obj, field);
} catch (Exception e) {
e.printStackTrace();
}
Expand Down Expand Up @@ -144,13 +156,13 @@ public static <T> T createWithoutConstructor ( Class<T> classToInstantiate )


public static <T> T set(T thisObj, String key, Object value) throws Exception {
ReflectionHelper.setFieldValue(thisObj, key, value);
setFieldValue(thisObj, key, value);
return thisObj;
}

public static String get(Object thisObj, String key) {
try {
Object obj = ReflectionHelper.getFieldValue(thisObj, key);
Object obj = getFieldValue(thisObj, key);
if(obj != null){
return obj.toString();
}
Expand All @@ -162,6 +174,6 @@ public static String get(Object thisObj, String key) {
}

public static boolean has(Object thisObj, String key) {
return ReflectionHelper.getField(thisObj.getClass(), key) != null;
return getField(thisObj.getClass(), key) != null;
}
}

0 comments on commit 11eed6a

Please sign in to comment.