8
8
import java .util .Map ;
9
9
10
10
import chylex .hee .system .logging .Log ;
11
+ import com .gtnewhorizon .gtnhlib .reflect .Fields ;
11
12
import cpw .mods .fml .relauncher .CoreModManager ;
12
13
import cpw .mods .fml .relauncher .ReflectionHelper ;
13
14
import net .minecraft .launchwrapper .Launch ;
14
15
16
+ @ SuppressWarnings ({"unchecked" , "rawtypes" })
15
17
public class ReflectionUtils {
16
18
17
19
public static Boolean obf ;
@@ -78,25 +80,25 @@ public static String getCorrectFieldName(String aFieldName) {
78
80
79
81
80
82
private static class CachedField {
81
- private final Field FIELD ;
83
+ private final Fields . ClassFields . Field FIELD ;
82
84
83
- public CachedField (Field aField ) {
85
+ public CachedField (Fields . ClassFields . Field aField ) {
84
86
FIELD = aField ;
85
87
}
86
88
87
- public Field get () {
89
+ public Fields . ClassFields . Field get () {
88
90
return FIELD ;
89
91
}
90
92
}
91
93
92
- private static boolean cacheField (Class <?> aClass , Field aField ) {
94
+ private static boolean cacheField (Class <?> aClass , Fields . ClassFields . Field aField ) {
93
95
if (aField == null ) {
94
96
return false ;
95
97
}
96
- CachedField y = mCachedFields .get (aClass .getName ()+"." +aField .getName ());
98
+ CachedField y = mCachedFields .get (aClass .getName ()+"." +aField .javaField . getName ());
97
99
if (y == null ) {
98
- Log .debug ("Caching Field: " +aClass .getName ()+"." +aField .getName ());
99
- mCachedFields .put (aClass .getName ()+"." +aField .getName (), new CachedField (aField ));
100
+ Log .debug ("Caching Field: " +aClass .getName ()+"." +aField .javaField . getName ());
101
+ mCachedFields .put (aClass .getName ()+"." +aField .javaField . getName (), new CachedField (aField ));
100
102
return true ;
101
103
}
102
104
return false ;
@@ -108,22 +110,19 @@ private static boolean cacheField(Class<?> aClass, Field aField) {
108
110
* @param aFieldName - Field name in {@link String} form.
109
111
* @return - Valid, non-final, {@link Field} object, or {@link null}.
110
112
*/
111
- public static Field getField (final Class <?> aClass , String aFieldName ) {
113
+ public static Fields . ClassFields . Field getField (final Class <?> aClass , String aFieldName ) {
112
114
if (aClass == null || aFieldName == null || aFieldName .length () <= 0 ) {
113
115
return null ;
114
116
}
115
117
aFieldName = getCorrectFieldName (aFieldName );
116
118
Log .debug ("Getting Field: " +aFieldName );
117
119
CachedField y = mCachedFields .get (aClass .getName ()+"." +aFieldName );
118
120
if (y == null ) {
119
- Field u ;
120
- try {
121
- u = getField_Internal (aClass , aFieldName );
122
- if (u != null ) {
123
- cacheField (aClass , u );
124
- return u ;
125
- }
126
- } catch (NoSuchFieldException e ) {
121
+ Fields .ClassFields .Field u ;
122
+ u = getField_Internal (aClass , aFieldName );
123
+ if (u != null ) {
124
+ cacheField (aClass , u );
125
+ return u ;
127
126
}
128
127
return null ;
129
128
@@ -138,74 +137,46 @@ public static Field getField(final Class<?> aClass, String aFieldName) {
138
137
* @param aFieldName - Field name in {@link String} form.
139
138
* @return - Valid, non-final, {@link Field} object, or {@link null}.
140
139
*/
141
- public static Field getField (final Object aInstance , String aFieldName ) {
140
+ public static Fields . ClassFields . Field getField (final Object aInstance , String aFieldName ) {
142
141
return getField (aInstance .getClass (), aFieldName );
143
142
}
144
143
145
144
146
- @ SuppressWarnings ("unchecked" )
147
145
public static <T > T getFieldValue (final Object aInstance , String aFieldName ) {
148
146
aFieldName = getCorrectFieldName (aFieldName );
149
147
Log .debug ("Getting Field Value: " +aFieldName );
150
148
try {
151
- return (T ) getField (aInstance , aFieldName ).get (aInstance );
152
- } catch (IllegalArgumentException | IllegalAccessException e ) {
149
+ return (T ) getField (aInstance , aFieldName ).getValue (aInstance );
150
+ } catch (ClassCastException e ) {
153
151
return null ;
154
152
}
155
153
}
156
154
157
155
public static void setFieldValue (final Object aInstance , String aFieldName , Object aNewValue ) {
158
- setFieldValue (aInstance , aFieldName , aInstance instanceof Class ? true : false , aNewValue );
156
+ setFieldValue (aInstance , aFieldName , aInstance instanceof Class , aNewValue );
159
157
}
160
158
161
- public static void setFieldValue (final Object aInstance , String aFieldName , boolean aStatic , Object aNewValue ) {
162
- Field f ;
159
+ public static void setFieldValue (final Object aInstance , String aFieldName , boolean aStatic , Object aNewValue ) {
160
+ Fields . ClassFields . Field f ;
163
161
aFieldName = getCorrectFieldName (aFieldName );
164
162
Log .debug ("Setting Field: " +aFieldName );
165
163
try {
166
164
if (aInstance instanceof Class && aStatic ) {
167
165
f = getField ((Class ) aInstance , aFieldName );
168
- makeFieldAccessible (f );
169
- f .set (null , aNewValue );
166
+ f .setValue (null , aNewValue );
170
167
}
171
168
else {
172
169
f = getField (aInstance , aFieldName );
173
- makeFieldAccessible (f );
174
- f .set (aInstance , aNewValue );
170
+ f .setValue (aInstance , aNewValue );
175
171
176
- }
177
- } catch (IllegalArgumentException | IllegalAccessException e ) {
172
+ }
173
+ } catch (ClassCastException e ) {
178
174
e .printStackTrace ();
179
175
}
180
176
}
181
177
182
-
183
- private static Field getField_Internal (final Class <?> clazz , final String fieldName ) throws NoSuchFieldException {
184
- try {
185
- Field k = clazz .getDeclaredField (fieldName );
186
- makeFieldAccessible (k );
187
- return k ;
188
- } catch (final NoSuchFieldException e ) {
189
- final Class <?> superClass = clazz .getSuperclass ();
190
- if (superClass == null ) {
191
- throw e ;
192
- }
193
- Log .debug ("Checking Super: " +superClass .getCanonicalName ());
194
- return getField_Internal (superClass , fieldName );
195
- }
196
- }
197
-
198
- public static void makeFieldAccessible (final Field field ) {
199
- field .setAccessible (true );
200
- if (Modifier .isFinal (field .getModifiers ())){
201
- try {
202
- Unfinalizer .unfinalizeField (field );
203
- }
204
- catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException e ) {
205
- e .printStackTrace ();
206
- }
207
- }
178
+ private static Fields .ClassFields .Field getField_Internal (final Class <?> clazz , final String fieldName ) {
179
+ return Fields .ofClass (clazz ).getUntypedField (Fields .LookupType .DECLARED_IN_HIERARCHY , fieldName );
208
180
}
209
181
210
-
211
182
}
0 commit comments