Skip to content

Commit

Permalink
Make reflection Java 12+ compatible (GTNewHorizons#517)
Browse files Browse the repository at this point in the history
  • Loading branch information
eigenraven authored Jan 24, 2023
1 parent 7366dea commit 0f46044
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 65 deletions.
11 changes: 8 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//version: 1673027205
//version: 1674409054
/*
DO NOT CHANGE THIS FILE!
Also, you may replace this file at any time if there is an update available.
Expand Down Expand Up @@ -144,6 +144,7 @@ propertyDefaultIfUnset("modrinthProjectId", "")
propertyDefaultIfUnset("modrinthRelations", "")
propertyDefaultIfUnset("curseForgeProjectId", "")
propertyDefaultIfUnset("curseForgeRelations", "")
propertyDefaultIfUnset("minimizeShadowedDependencies", true)

String javaSourceDir = "src/main/java/"
String scalaSourceDir = "src/main/scala/"
Expand Down Expand Up @@ -411,7 +412,9 @@ shadowJar {
attributes(getManifestAttributes())
}

minimize() // This will only allow shading for actually used classes
if (minimizeShadowedDependencies.toBoolean()) {
minimize() // This will only allow shading for actually used classes
}
configurations = [
project.configurations.shadowImplementation,
project.configurations.shadowCompile
Expand Down Expand Up @@ -554,7 +557,9 @@ task shadowDevJar(type: ShadowJar) {
attributes(getManifestAttributes())
}

minimize() // This will only allow shading for actually used classes
if (minimizeShadowedDependencies.toBoolean()) {
minimize() // This will only allow shading for actually used classes
}
configurations = [
project.configurations.shadowImplementation,
project.configurations.shadowCompile
Expand Down
3 changes: 2 additions & 1 deletion dependencies.gradle
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
dependencies {
compile('com.github.GTNewHorizons:GTNHLib:0.0.10:dev')
compile('com.github.GTNewHorizons:GT5-Unofficial:5.09.41.232:dev')
compile("com.github.GTNewHorizons:StructureLib:1.2.0-beta.2:dev")
compile("com.github.GTNewHorizons:ModularUI:1.0.46:dev") {transitive=false}
compile("com.github.GTNewHorizons:NotEnoughItems:2.3.20-GTNH:dev")
compile('com.github.GTNewHorizons:CodeChickenCore:1.1.7:dev')
compile('com.github.GTNewHorizons:CodeChickenLib:1.1.5.6:dev')
compile('com.github.GTNewHorizons:CodeChickenLib:1.1.5.7:dev')
compile('net.industrial-craft:industrialcraft-2:2.2.828-experimental:dev')
compile('com.github.GTNewHorizons:NewHorizonsCoreMod:1.9.125:dev')

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/gtPlusPlus/GTplusplus.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
name = CORE.name,
version = CORE.VERSION,
dependencies =
"required-after:Forge; after:TConstruct; after:PlayerAPI; after:dreamcraft; after:IC2; after:ihl; after:psychedelicraft; required-after:gregtech; after:Forestry; after:MagicBees; after:CoFHCore; after:Growthcraft; after:Railcraft; after:CompactWindmills; after:ForbiddenMagic; after:MorePlanet; after:PneumaticCraft; after:ExtraUtilities; after:Thaumcraft; after:rftools; after:simplyjetpacks; after:BigReactors; after:EnderIO; after:tectech; after:GTRedtech; after:beyondrealitycore; after:OpenBlocks; after:IC2NuclearControl; after:TGregworks; after:StevesCarts; after:xreliquary;")
"required-after:Forge; after:TConstruct; after:PlayerAPI; after:dreamcraft; after:IC2; after:ihl; after:psychedelicraft; required-after:gregtech; after:Forestry; after:MagicBees; after:CoFHCore; after:Growthcraft; after:Railcraft; after:CompactWindmills; after:ForbiddenMagic; after:MorePlanet; after:PneumaticCraft; after:ExtraUtilities; after:Thaumcraft; after:rftools; after:simplyjetpacks; after:BigReactors; after:EnderIO; after:tectech; after:GTRedtech; after:beyondrealitycore; after:OpenBlocks; after:IC2NuclearControl; after:TGregworks; after:StevesCarts; after:xreliquary; required-after:gtnhlib@[0.0.10,)")
public class GTplusplus implements ActionListener {

public static enum INIT_PHASE {
Expand Down
67 changes: 16 additions & 51 deletions src/main/java/gtPlusPlus/core/util/reflect/ReflectionUtils.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gtPlusPlus.core.util.reflect;

import com.google.common.reflect.ClassPath;
import com.gtnewhorizon.gtnhlib.reflect.Fields;
import gtPlusPlus.api.objects.Logger;
import gtPlusPlus.core.util.data.StringUtils;
import java.io.IOException;
Expand All @@ -20,12 +21,14 @@
import java.util.Map;
import org.apache.commons.lang3.ArrayUtils;

@SuppressWarnings({"unchecked", "rawtypes"})
public class ReflectionUtils {

public static Map<String, Class<?>> mCachedClasses = new LinkedHashMap<String, Class<?>>();
public static Map<String, CachedMethod> mCachedMethods = new LinkedHashMap<String, CachedMethod>();
public static Map<String, CachedField> mCachedFields = new LinkedHashMap<String, CachedField>();
public static Map<String, CachedConstructor> mCachedConstructors = new LinkedHashMap<String, CachedConstructor>();
public static Map<String, Class<?>> mCachedClasses = new LinkedHashMap<>();
public static Map<String, CachedMethod> mCachedMethods = new LinkedHashMap<>();
public static Map<String, CachedField> mCachedFields = new LinkedHashMap<>();
public static Map<String, CachedConstructor> mCachedConstructors = new LinkedHashMap<>();
public static Map<Field, Fields.ClassFields.Field> mCachedFieldAccessors = new LinkedHashMap<>();

private static class CachedConstructor {

Expand Down Expand Up @@ -78,6 +81,11 @@ public boolean type() {
}
}

private static Fields.ClassFields.Field cacheAccessor(Field f) {
return mCachedFieldAccessors.computeIfAbsent(f, (field) -> Fields.ofClass(field.getDeclaringClass())
.getUntypedField(Fields.LookupType.DECLARED_IN_HIERARCHY, field.getName()));
}

private static boolean cacheClass(Class<?> aClass) {
if (aClass == null) {
return false;
Expand Down Expand Up @@ -448,29 +456,12 @@ public static void setFinalFieldValue(Class<?> clazz, Field field, Object newVal

@Deprecated
public static void setFinalStatic(Field field, Object newValue) throws Exception {
field.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
field.set(null, newValue);
cacheAccessor(field).setValue(null, newValue);
}

public static void setByte(Object clazz, String fieldName, byte newValue) throws Exception {
Field nameField = getField(clazz.getClass(), fieldName);
nameField.setAccessible(true);
int modifiers = nameField.getModifiers();
Field modifierField = nameField.getClass().getDeclaredField("modifiers");
modifiers = modifiers & ~Modifier.FINAL;
modifierField.setAccessible(true);
modifierField.setInt(nameField, modifiers);
// Utils.LOG_INFO("O-"+(byte) nameField.get(clazz) + " | "+newValue);
nameField.setByte(clazz, newValue);
// Utils.LOG_INFO("N-"+(byte) nameField.get(clazz));

/*final Field fieldA = getField(clazz.getClass(), fieldName);
fieldA.setAccessible(true);
fieldA.setByte(clazz, newValue);*/

cacheAccessor(nameField).setValue(null, newValue);
}

public static boolean invoke(Object objectInstance, String methodName, Class[] parameters, Object[] values) {
Expand Down Expand Up @@ -808,11 +799,6 @@ private static Method getMethod_Internal(Class<?> aClass, String aMethodName, Cl
m = aClass.getDeclaredMethod(aMethodName, aTypes);
if (m != null) {
m.setAccessible(true);
int modifiers = m.getModifiers();
Field modifierField = m.getClass().getDeclaredField("modifiers");
modifiers = modifiers & ~Modifier.FINAL;
modifierField.setAccessible(true);
modifierField.setInt(m, modifiers);
}
} catch (Throwable t) {
Logger.REFLECTION("Method: Internal Lookup Failed: " + aMethodName);
Expand All @@ -834,11 +820,6 @@ private static Constructor<?> getConstructor_Internal(Class<?> aClass, Class<?>.
c = aClass.getDeclaredConstructor(aTypes);
if (c != null) {
c.setAccessible(true);
int modifiers = c.getModifiers();
Field modifierField = c.getClass().getDeclaredField("modifiers");
modifiers = modifiers & ~Modifier.FINAL;
modifierField.setAccessible(true);
modifierField.setInt(c, modifiers);
}
} catch (Throwable t) {
Logger.REFLECTION("Constructor: Internal Lookup Failed: " + aClass.getName());
Expand All @@ -859,14 +840,9 @@ private static Constructor<?> getConstructorRecursively(Class<?> aClass, Class<?
Constructor<?> c = aClass.getConstructor(aTypes);
if (c != null) {
c.setAccessible(true);
int modifiers = c.getModifiers();
Field modifierField = c.getClass().getDeclaredField("modifiers");
modifiers = modifiers & ~Modifier.FINAL;
modifierField.setAccessible(true);
modifierField.setInt(c, modifiers);
}
return c;
} catch (final NoSuchMethodException | IllegalArgumentException | IllegalAccessException e) {
} catch (final NoSuchMethodException | IllegalArgumentException e) {
final Class<?> superClass = aClass.getSuperclass();
if (superClass == null || superClass == Object.class) {
throw e;
Expand Down Expand Up @@ -1018,18 +994,7 @@ private static Class<?> getClass_Internal(String string) {
* Set the value of a field reflectively.
*/
private static void setFieldValue_Internal(Object owner, Field field, Object value) throws Exception {
makeModifiable(field);
field.set(owner, value);
}

/**
* Force the field to be modifiable and accessible.
*/
private static void makeModifiable(Field nameField) throws Exception {
nameField.setAccessible(true);
Field modifiers = getField(Field.class, "modifiers");
modifiers.setAccessible(true);
modifiers.setInt(nameField, nameField.getModifiers() & ~Modifier.FINAL);
cacheAccessor(field).setValue(owner, value);
}

public static boolean doesFieldExist(String clazz, String string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static gregtech.api.enums.GT_Values.V;

import com.gtnewhorizon.gtnhlib.reflect.Fields;
import gregtech.api.enums.GT_Values;
import gregtech.api.enums.Textures;
import gregtech.api.interfaces.ITexture;
Expand All @@ -13,13 +14,10 @@
import gtPlusPlus.core.lib.CORE;
import gtPlusPlus.core.util.math.MathUtils;
import gtPlusPlus.core.util.minecraft.PlayerUtils;
import gtPlusPlus.core.util.reflect.ReflectionUtils;
import gtPlusPlus.core.util.sys.KeyboardUtils;
import gtPlusPlus.xmod.gregtech.common.blocks.textures.TexturesGtBlock;
import gtPlusPlus.xmod.gregtech.common.blocks.textures.TexturesGtBlock.CustomIcon;
import gtPlusPlus.xmod.gregtech.common.tileentities.storage.GregtechMetaEnergyBuffer;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
Expand Down Expand Up @@ -208,6 +206,8 @@ public void loadNBTData(NBTTagCompound aNBT) {
super.loadNBTData(aNBT);
}

private static Fields.ClassFields<GregtechMetaCreativeEnergyBuffer>.Field<ITexture[][][]> mTexturesAccessor;

@Override
public void onScrewdriverRightClick(byte aSide, EntityPlayer aPlayer, float aX, float aY, float aZ) {
if (KeyboardUtils.isShiftKeyDown()) {
Expand All @@ -220,18 +220,17 @@ public void onScrewdriverRightClick(byte aSide, EntityPlayer aPlayer, float aX,
}
this.markDirty();
try {
Field field = ReflectionUtils.getField(this.getClass(), "mTextures");
field.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
if (mTexturesAccessor == null) {
mTexturesAccessor = Fields.ofClass(GregtechMetaCreativeEnergyBuffer.class)
.getField(Fields.LookupType.PUBLIC, "mTextures", ITexture[][][].class);
}
ITexture[][][] V = getTextureSet(null);
if (V != null) {
Logger.REFLECTION("Got Valid Textures.");
if (this.getBaseMetaTileEntity().isClientSide()) {
Logger.REFLECTION("Clientside Call.");
Logger.REFLECTION("Refreshing Textures on buffer.");
field.set(this, V);
mTexturesAccessor.setValue(this, V);
Logger.REFLECTION("Refreshed Textures on buffer.");
} else {
Logger.REFLECTION("Serverside Call.");
Expand Down

0 comments on commit 0f46044

Please sign in to comment.