Skip to content

Commit 2fea02e

Browse files
committed
Switch the reflection wrapper to java 17-compatible gtnhlib
1 parent 6caac2a commit 2fea02e

File tree

3 files changed

+32
-82
lines changed

3 files changed

+32
-82
lines changed

src/main/java/chylex/hee/block/BlockReplaceHelper.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.lang.reflect.Method;
44
import java.util.Map;
55

6+
import com.gtnewhorizon.gtnhlib.reflect.Fields;
67
import net.minecraft.block.Block;
78
import net.minecraft.init.Blocks;
89
import net.minecraft.item.Item;
@@ -11,7 +12,6 @@
1112
import chylex.hee.system.logging.Log;
1213
import chylex.hee.system.logging.Stopwatch;
1314
import chylex.hee.system.util.ReflectionUtils;
14-
import chylex.hee.system.util.Unfinalizer;
1515
import cpw.mods.fml.common.registry.FMLControlledNamespacedRegistry;
1616
import cpw.mods.fml.common.registry.GameData;
1717

@@ -23,11 +23,13 @@ public static void replaceBlock(Block toReplace, Block replacement){
2323
Exception exception = null;
2424

2525
try{
26+
final Fields.ClassFields blocksFields = Fields.ofClass(Blocks.class);
2627
for(Field blockField:Blocks.class.getFields()){
2728
if (Block.class.isAssignableFrom(blockField.getType())){
2829
Block block = (Block)blockField.get(null);
2930

3031
if (block == toReplace){
32+
final Fields.ClassFields.Field fieldAccessor = blocksFields.getUntypedField(Fields.LookupType.PUBLIC, blockField.getName());
3133
String registryName = Block.blockRegistry.getNameForObject(block);
3234
int id = Block.getIdFromBlock(block);
3335

@@ -50,10 +52,9 @@ public static void replaceBlock(Block toReplace, Block replacement){
5052
Log.debug("Set new value for registryObjects: registryObjects. For object registryBlocks.");
5153
ReflectionUtils.setFieldValue(registryBlocks, "underlyingIntegerMap", underlyingIntegerMap);
5254
Log.debug("Set new value for underlyingIntegerMap: underlyingIntegerMap. For object registryBlocks.");
53-
54-
ReflectionUtils.makeFieldAccessible(blockField);
55+
5556
Log.debug("Made "+blockField.getName()+" accessible.");
56-
blockField.set(null,replacement);
57+
fieldAccessor.setValue(null,replacement);
5758
Log.debug("Set "+blockField.getName()+" to "+replacement.getUnlocalizedName()+".");
5859

5960
Method delegateNameMethod = replacement.delegate.getClass().getDeclaredMethod("setName",String.class);

src/main/java/chylex/hee/system/util/ReflectionUtils.java

Lines changed: 27 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
import java.util.Map;
99

1010
import chylex.hee.system.logging.Log;
11+
import com.gtnewhorizon.gtnhlib.reflect.Fields;
1112
import cpw.mods.fml.relauncher.CoreModManager;
1213
import cpw.mods.fml.relauncher.ReflectionHelper;
1314
import net.minecraft.launchwrapper.Launch;
1415

16+
@SuppressWarnings({"unchecked", "rawtypes"})
1517
public class ReflectionUtils {
1618

1719
public static Boolean obf;
@@ -78,25 +80,25 @@ public static String getCorrectFieldName(String aFieldName) {
7880

7981

8082
private static class CachedField {
81-
private final Field FIELD;
83+
private final Fields.ClassFields.Field FIELD;
8284

83-
public CachedField(Field aField) {
85+
public CachedField(Fields.ClassFields.Field aField) {
8486
FIELD = aField;
8587
}
8688

87-
public Field get() {
89+
public Fields.ClassFields.Field get() {
8890
return FIELD;
8991
}
9092
}
9193

92-
private static boolean cacheField(Class<?> aClass, Field aField) {
94+
private static boolean cacheField(Class<?> aClass, Fields.ClassFields.Field aField) {
9395
if (aField == null) {
9496
return false;
9597
}
96-
CachedField y = mCachedFields.get(aClass.getName()+"."+aField.getName());
98+
CachedField y = mCachedFields.get(aClass.getName()+"."+aField.javaField.getName());
9799
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));
100102
return true;
101103
}
102104
return false;
@@ -108,22 +110,19 @@ private static boolean cacheField(Class<?> aClass, Field aField) {
108110
* @param aFieldName - Field name in {@link String} form.
109111
* @return - Valid, non-final, {@link Field} object, or {@link null}.
110112
*/
111-
public static Field getField(final Class<?> aClass, String aFieldName) {
113+
public static Fields.ClassFields.Field getField(final Class<?> aClass, String aFieldName) {
112114
if (aClass == null || aFieldName == null || aFieldName.length() <= 0) {
113115
return null;
114116
}
115117
aFieldName = getCorrectFieldName(aFieldName);
116118
Log.debug("Getting Field: "+aFieldName);
117119
CachedField y = mCachedFields.get(aClass.getName()+"."+aFieldName);
118120
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;
127126
}
128127
return null;
129128

@@ -138,74 +137,46 @@ public static Field getField(final Class<?> aClass, String aFieldName) {
138137
* @param aFieldName - Field name in {@link String} form.
139138
* @return - Valid, non-final, {@link Field} object, or {@link null}.
140139
*/
141-
public static Field getField(final Object aInstance, String aFieldName) {
140+
public static Fields.ClassFields.Field getField(final Object aInstance, String aFieldName) {
142141
return getField(aInstance.getClass(), aFieldName);
143142
}
144143

145144

146-
@SuppressWarnings("unchecked")
147145
public static <T> T getFieldValue(final Object aInstance, String aFieldName) {
148146
aFieldName = getCorrectFieldName(aFieldName);
149147
Log.debug("Getting Field Value: "+aFieldName);
150148
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) {
153151
return null;
154152
}
155153
}
156154

157155
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);
159157
}
160158

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;
163161
aFieldName = getCorrectFieldName(aFieldName);
164162
Log.debug("Setting Field: "+aFieldName);
165163
try {
166164
if (aInstance instanceof Class && aStatic) {
167165
f = getField((Class) aInstance, aFieldName);
168-
makeFieldAccessible(f);
169-
f.set(null, aNewValue);
166+
f.setValue(null, aNewValue);
170167
}
171168
else {
172169
f = getField(aInstance, aFieldName);
173-
makeFieldAccessible(f);
174-
f.set(aInstance, aNewValue);
170+
f.setValue(aInstance, aNewValue);
175171

176-
}
177-
} catch (IllegalArgumentException | IllegalAccessException e) {
172+
}
173+
} catch (ClassCastException e) {
178174
e.printStackTrace();
179175
}
180176
}
181177

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);
208180
}
209181

210-
211182
}

src/main/java/chylex/hee/system/util/Unfinalizer.java

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)