From 11eed6afb54d3f6f4b8ca387f475beacab53fa16 Mon Sep 17 00:00:00 2001 From: wh1t3P1g Date: Thu, 20 Jun 2024 22:55:38 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96ReflectionHelper?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ysomap/core/util/ReflectionHelper.java | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/ysomap/core/util/ReflectionHelper.java b/core/src/main/java/ysomap/core/util/ReflectionHelper.java index 9c7e5bd..5dd20e1 100755 --- a/core/src/main/java/ysomap/core/util/ReflectionHelper.java +++ b/core/src/main/java/ysomap/core/util/ReflectionHelper.java @@ -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); } @@ -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); } @@ -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(); } @@ -144,13 +156,13 @@ public static T createWithoutConstructor ( Class classToInstantiate ) public static 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(); } @@ -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; } }