diff options
author | Alexdoru <57050655+Alexdoru@users.noreply.github.com> | 2024-09-28 13:25:01 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-28 11:25:01 +0000 |
commit | 86f1765b171f4cc6f163b8027d1330f4e5094e2d (patch) | |
tree | b6e293bd3c976558fc4e4b7190e4d342c2ddc555 /src/main/java/gtPlusPlus/core | |
parent | 6b1f145f5028f1bc92cf478e5963224e7d94b5cd (diff) | |
download | GT5-Unofficial-86f1765b171f4cc6f163b8027d1330f4e5094e2d.tar.gz GT5-Unofficial-86f1765b171f4cc6f163b8027d1330f4e5094e2d.tar.bz2 GT5-Unofficial-86f1765b171f4cc6f163b8027d1330f4e5094e2d.zip |
Remove more reflection + reorganize mixin accessors packages (#3260)
Co-authored-by: Martin Robertz <dream-master@gmx.net>
Co-authored-by: boubou19 <miisterunknown@gmail.com>
Diffstat (limited to 'src/main/java/gtPlusPlus/core')
4 files changed, 5 insertions, 417 deletions
diff --git a/src/main/java/gtPlusPlus/core/config/ASMConfiguration.java b/src/main/java/gtPlusPlus/core/config/ASMConfiguration.java index a348998ef0..63d65b32c9 100644 --- a/src/main/java/gtPlusPlus/core/config/ASMConfiguration.java +++ b/src/main/java/gtPlusPlus/core/config/ASMConfiguration.java @@ -11,7 +11,6 @@ import gregtech.api.enums.Mods; public class ASMConfiguration { public static Debug debug = new Debug(); - public static General general = new General(); // Debug @Config.Comment("Debug section") @@ -27,14 +26,4 @@ public class ASMConfiguration { } - @Config.Comment("General section") - public static class General { - // General Features - - @Config.Comment("Set to a value greater than 0 to reduce the ticks taken to delay between BGM tracks. Acceptable Values are 1-32767, where 0 is disabled. Vanilla Uses 12,000 & 24,000. 200 is 10s.") - @Config.DefaultInt(0) - @Config.RangeInt(min = 0, max = Short.MAX_VALUE) - public int enableWatchdogBGM; - - } } diff --git a/src/main/java/gtPlusPlus/core/handler/PacketHandler.java b/src/main/java/gtPlusPlus/core/handler/PacketHandler.java index 9c5d6b25d2..504afc55d8 100644 --- a/src/main/java/gtPlusPlus/core/handler/PacketHandler.java +++ b/src/main/java/gtPlusPlus/core/handler/PacketHandler.java @@ -9,12 +9,9 @@ import cpw.mods.fml.common.network.NetworkRegistry; import cpw.mods.fml.common.network.simpleimpl.IMessage; import cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper; import cpw.mods.fml.relauncher.Side; -import gtPlusPlus.api.objects.Logger; import gtPlusPlus.core.network.handler.AbstractClientMessageHandler; -import gtPlusPlus.core.network.packet.AbstractPacket; import gtPlusPlus.core.network.packet.PacketVolumetricFlaskGui; import gtPlusPlus.core.network.packet.PacketVolumetricFlaskGui2; -import gtPlusPlus.core.util.reflect.ReflectionUtils; public class PacketHandler { @@ -37,14 +34,6 @@ public class PacketHandler { private static void registerMessage(Class handlerClass, Class messageClass, Side side) { INSTANCE.registerMessage(handlerClass, messageClass, packetId++, side); - if (AbstractPacket.class.isInstance(messageClass.getClass())) { - AbstractPacket aPacket = ReflectionUtils.createNewInstanceFromConstructor( - ReflectionUtils.getConstructor(messageClass, new Class[] {}), - new Object[] {}); - if (aPacket != null) { - Logger.INFO("Registered Packet: " + aPacket.getPacketName()); - } - } } /** diff --git a/src/main/java/gtPlusPlus/core/handler/events/PlayerSleepEventHandler.java b/src/main/java/gtPlusPlus/core/handler/events/PlayerSleepEventHandler.java index 33649b4ff2..41755d6abc 100644 --- a/src/main/java/gtPlusPlus/core/handler/events/PlayerSleepEventHandler.java +++ b/src/main/java/gtPlusPlus/core/handler/events/PlayerSleepEventHandler.java @@ -11,7 +11,7 @@ import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.event.entity.player.PlayerWakeUpEvent; import cpw.mods.fml.common.eventhandler.SubscribeEvent; -import gregtech.mixin.interfaces.PotionExt; +import gregtech.mixin.interfaces.accessors.PotionAccessor; import gtPlusPlus.core.util.math.MathUtils; public class PlayerSleepEventHandler { @@ -43,7 +43,7 @@ public class PlayerSleepEventHandler { final List<Integer> potionToRemove = new ArrayList<>(); for (PotionEffect potionEffect : player.getActivePotionEffects()) { final Potion potion = Potion.potionTypes[potionEffect.getPotionID()]; - if (potion instanceof PotionExt && ((PotionExt) potion).gt5u$isBadEffect()) { + if (potion instanceof PotionAccessor && ((PotionAccessor) potion).gt5u$isBadEffect()) { potionToRemove.add(potion.id); } } diff --git a/src/main/java/gtPlusPlus/core/util/reflect/ReflectionUtils.java b/src/main/java/gtPlusPlus/core/util/reflect/ReflectionUtils.java index 40b32fed86..5dd9f9f426 100644 --- a/src/main/java/gtPlusPlus/core/util/reflect/ReflectionUtils.java +++ b/src/main/java/gtPlusPlus/core/util/reflect/ReflectionUtils.java @@ -1,56 +1,19 @@ package gtPlusPlus.core.util.reflect; -import java.io.IOException; -import java.lang.reflect.Constructor; import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; import java.lang.reflect.Modifier; -import java.util.LinkedHashMap; +import java.util.HashMap; import java.util.Map; -import org.apache.commons.lang3.ArrayUtils; - -import com.google.common.reflect.ClassPath; import com.gtnewhorizon.gtnhlib.reflect.Fields; import gtPlusPlus.api.objects.Logger; -import gtPlusPlus.core.util.data.StringUtils; @SuppressWarnings({ "unchecked", "rawtypes" }) public class ReflectionUtils { - 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 { - - private final Constructor<?> METHOD; - - public CachedConstructor(Constructor<?> aCons) { - METHOD = aCons; - } - - public Constructor<?> get() { - return METHOD; - } - } - - private static class CachedMethod { - - private final Method METHOD; - - public CachedMethod(Method aMethod, boolean isStatic) { - METHOD = aMethod; - } - - public Method get() { - return METHOD; - } - - } + public static Map<String, CachedField> mCachedFields = new HashMap<>(); + public static Map<Field, Fields.ClassFields.Field> mCachedFieldAccessors = new HashMap<>(); private static class CachedField { @@ -73,22 +36,6 @@ public class ReflectionUtils { .getUntypedField(Fields.LookupType.DECLARED_IN_HIERARCHY, field.getName())); } - private static boolean cacheMethod(Class<?> aClass, Method aMethod) { - if (aMethod == null) { - return false; - } - boolean isStatic = Modifier.isStatic(aMethod.getModifiers()); - CachedMethod y = mCachedMethods - .get(aClass.getName() + "." + aMethod.getName() + "." + ArrayUtils.toString(aMethod.getParameterTypes())); - if (y == null) { - mCachedMethods.put( - aClass.getName() + "." + aMethod.getName() + "." + ArrayUtils.toString(aMethod.getParameterTypes()), - new CachedMethod(aMethod, isStatic)); - return true; - } - return false; - } - private static boolean cacheField(Class<?> aClass, Field aField) { if (aField == null) { return false; @@ -102,85 +49,6 @@ public class ReflectionUtils { return false; } - private static void cacheConstructor(Class<?> aClass, Constructor<?> aConstructor) { - if (aConstructor == null) { - return; - } - mCachedConstructors.computeIfAbsent( - aClass.getName() + "." + ArrayUtils.toString(aConstructor.getParameterTypes()), - k -> new CachedConstructor(aConstructor)); - } - - /** - * Returns a cached {@link Constructor} object. - * - * @param aClass - Class containing the Constructor. - * @param aTypes - Varags Class Types for objects constructor. - * @return - Valid, non-final, {@link Method} object, or {@link null}. - */ - public static Constructor<?> getConstructor(Class<?> aClass, Class<?>... aTypes) { - if (aClass == null || aTypes == null) { - return null; - } - - String aMethodKey = ArrayUtils.toString(aTypes); - // Logger.REFLECTION("Looking up method in cache: "+(aClass.getName()+"."+aMethodName + "." + aMethodKey)); - CachedConstructor y = mCachedConstructors.get(aClass.getName() + "." + aMethodKey); - if (y == null) { - Constructor<?> u = getConstructor_Internal(aClass, aTypes); - if (u != null) { - Logger.REFLECTION("Caching Constructor: " + aClass.getName() + "." + aMethodKey); - cacheConstructor(aClass, u); - return u; - } else { - return null; - } - } else { - return y.get(); - } - } - - /** - * Returns a cached {@link Method} object. Wraps {@link #getMethod(Class, String, Class...)}. - * - * @param aObject - Object containing the Method. - * @param aMethodName - Method's name in {@link String} form. - * @param aTypes - Class Array of Types for {@link Method}'s constructor. - * @return - Valid, non-final, {@link Method} object, or {@link null}. - */ - public static Method getMethod(Object aObject, String aMethodName, Class[] aTypes) { - return getMethod(aObject.getClass(), aMethodName, aTypes); - } - - /** - * Returns a cached {@link Method} object. - * - * @param aClass - Class containing the Method. - * @param aMethodName - Method's name in {@link String} form. - * @param aTypes - Varags Class Types for {@link Method}'s constructor. - * @return - Valid, non-final, {@link Method} object, or {@link null}. - */ - public static Method getMethod(Class<?> aClass, String aMethodName, Class<?>... aTypes) { - if (aClass == null || aMethodName == null || aMethodName.length() <= 0) { - return null; - } - String aMethodKey = ArrayUtils.toString(aTypes); - // Logger.REFLECTION("Looking up method in cache: "+(aClass.getName()+"."+aMethodName + "." + aMethodKey)); - CachedMethod y = mCachedMethods.get(aClass.getName() + "." + aMethodName + "." + aMethodKey); - if (y == null) { - Method u = getMethod_Internal(aClass, aMethodName, aTypes); - if (u != null) { - Logger.REFLECTION("Caching Method: " + aMethodName + "." + aMethodKey); - cacheMethod(aClass, u); - return u; - } else { - return null; - } - } else { - return y.get(); - } - } - /** * Returns a cached {@link Field} object. * @@ -237,79 +105,6 @@ public class ReflectionUtils { } } - public static void makeMethodAccessible(final Method field) { - if (!Modifier.isPublic(field.getModifiers()) || !Modifier.isPublic( - field.getDeclaringClass() - .getModifiers())) { - field.setAccessible(true); - } - } - - /** - * Get the method name for a depth in call stack. <br /> - * Utility function - * - * @param depth depth in the call stack (0 means current method, 1 means call method, ...) - * @return Method name - */ - public static String getMethodName(final int depth) { - final StackTraceElement[] ste = new Throwable().getStackTrace(); - // System. out.println(ste[ste.length-depth].getClassName()+"#"+ste[ste.length-depth].getMethodName()); - if (ste.length < depth) { - return "No valid stack."; - } - return ste[depth + 1].getMethodName(); - } - - /** - * - * @param aPackageName - The full {@link Package} name in {@link String} form. - * @return - {@link Boolean} object. True if loaded > 0 classes. - */ - public static boolean dynamicallyLoadClassesInPackage(String aPackageName) { - ClassLoader classLoader = ReflectionUtils.class.getClassLoader(); - int loaded = 0; - try { - ClassPath path = ClassPath.from(classLoader); - for (ClassPath.ClassInfo info : path.getTopLevelClassesRecursive(aPackageName)) { - Class<?> clazz = Class.forName(info.getName(), true, classLoader); - if (clazz != null) { - loaded++; - Logger.INFO("Found " + clazz.getCanonicalName() + ". [" + loaded + "]"); - } - } - } catch (ClassNotFoundException | IOException e) { - - } - - return loaded > 0; - } - - public static boolean setField(final Object object, final String fieldName, final Object fieldValue) { - Class<?> clazz; - if (object instanceof Class) { - clazz = (Class<?>) object; - } else { - clazz = object.getClass(); - } - while (clazz != null) { - try { - final Field field = getField(clazz, fieldName); - if (field != null) { - setFieldValue_Internal(object, field, fieldValue); - return true; - } - } catch (final NoSuchFieldException e) { - Logger.REFLECTION("setField(" + object + ", " + fieldName + ") failed."); - clazz = clazz.getSuperclass(); - } catch (final Exception e) { - Logger.REFLECTION("setField(" + object + ", " + fieldName + ") failed."); - throw new IllegalStateException(e); - } - } - return false; - } - public static boolean setField(final Object object, final Field field, final Object fieldValue) { if (field == null) return false; Class<?> clazz; @@ -336,51 +131,6 @@ public class ReflectionUtils { return false; } - /** - * Allows to change the state of an immutable instance. Huh?!? - */ - public static void setFinalFieldValue(Class<?> clazz, String fieldName, Object newValue) { - Field nameField = getField(clazz, fieldName); - try { - setFieldValue_Internal(clazz, nameField, newValue); - } catch (Throwable t) { - t.printStackTrace(); - } - } - - public static void setByte(Object clazz, String fieldName, byte newValue) { - Field nameField = getField(clazz.getClass(), fieldName); - cacheAccessor(nameField).setValue(null, newValue); - } - - public static boolean invokeVoid(Object objectInstance, String methodName, Class[] parameters, Object[] values) { - if (objectInstance == null || methodName == null || parameters == null || values == null) { - return false; - } - Class<?> mLocalClass = (objectInstance instanceof Class ? (Class<?>) objectInstance - : objectInstance.getClass()); - Logger.REFLECTION( - "Trying to invoke " + methodName + " on an instance of " + mLocalClass.getCanonicalName() + "."); - try { - Method mInvokingMethod = mLocalClass.getDeclaredMethod(methodName, parameters); - if (mInvokingMethod != null) { - Logger.REFLECTION(methodName + " was not null."); - mInvokingMethod.invoke(objectInstance, values); - Logger.REFLECTION("Successfully invoked " + methodName + "."); - return true; - } else { - Logger.REFLECTION(methodName + " is null."); - } - } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException - | InvocationTargetException e) { - Logger.REFLECTION( - "Failed to Dynamically invoke " + methodName + " on an object of type: " + mLocalClass.getName()); - } - - Logger.REFLECTION("Invoke failed or did something wrong."); - return false; - } - /* * Below Code block is used for determining generic types associated with type<E> */ @@ -407,108 +157,6 @@ public class ReflectionUtils { } } - private static Method getMethod_Internal(Class<?> aClass, String aMethodName, Class<?>... aTypes) { - Method m = null; - try { - Logger.REFLECTION("Method: Internal Lookup: " + aMethodName); - m = aClass.getDeclaredMethod(aMethodName, aTypes); - if (m != null) { - m.setAccessible(true); - } - } catch (Throwable t) { - Logger.REFLECTION("Method: Internal Lookup Failed: " + aMethodName); - try { - m = getMethodRecursively(aClass, aMethodName); - } catch (NoSuchMethodException e) { - Logger.REFLECTION("Unable to find method '" + aMethodName + "'"); - e.printStackTrace(); - dumpClassInfo(aClass); - } - } - return m; - } - - private static Constructor<?> getConstructor_Internal(Class<?> aClass, Class<?>... aTypes) { - Constructor<?> c = null; - try { - Logger.REFLECTION("Constructor: Internal Lookup: " + aClass.getName()); - c = aClass.getDeclaredConstructor(aTypes); - if (c != null) { - c.setAccessible(true); - } - } catch (Throwable t) { - Logger.REFLECTION("Constructor: Internal Lookup Failed: " + aClass.getName()); - try { - c = getConstructorRecursively(aClass, aTypes); - } catch (Exception e) { - Logger.REFLECTION("Unable to find method '" + aClass.getName() + "'"); - e.printStackTrace(); - dumpClassInfo(aClass); - } - } - return c; - } - - private static Constructor<?> getConstructorRecursively(Class<?> aClass, Class<?>... aTypes) throws Exception { - try { - Logger.REFLECTION("Constructor: Recursion Lookup: " + aClass.getName()); - Constructor<?> c = aClass.getConstructor(aTypes); - if (c != null) { - c.setAccessible(true); - } - return c; - } catch (final NoSuchMethodException | IllegalArgumentException e) { - final Class<?> superClass = aClass.getSuperclass(); - if (superClass == null || superClass == Object.class) { - throw e; - } - return getConstructor_Internal(superClass, aTypes); - } - } - - private static Method getMethodRecursively(final Class<?> clazz, final String aMethodName) - throws NoSuchMethodException { - try { - Logger.REFLECTION("Method: Recursion Lookup: " + aMethodName); - Method k = clazz.getDeclaredMethod(aMethodName); - makeMethodAccessible(k); - return k; - } catch (final NoSuchMethodException e) { - final Class<?> superClass = clazz.getSuperclass(); - if (superClass == null || superClass == Object.class) { - throw e; - } - return getMethod_Internal(superClass, aMethodName); - } - } - - private static void dumpClassInfo(Class<?> aClass) { - Logger.INFO( - "We ran into an error processing reflection in " + aClass.getName() + ", dumping all data for debugging."); - // Get the methods - Method[] methods = aClass.getDeclaredMethods(); - Field[] fields = aClass.getDeclaredFields(); - Constructor[] consts = aClass.getDeclaredConstructors(); - - Logger.INFO("Dumping all Methods."); - for (Method method : methods) { - System.out - .println(method.getName() + " | " + StringUtils.getDataStringFromArray(method.getParameterTypes())); - } - Logger.INFO("Dumping all Fields."); - for (Field f : fields) { - System.out.println(f.getName()); - } - Logger.INFO("Dumping all Constructors."); - for (Constructor<?> c : consts) { - System.out.println( - c.getName() + " | " - + c.getParameterCount() - + " | " - + StringUtils.getDataStringFromArray(c.getParameterTypes())); - } - } - /** * * Set the value of a field reflectively. @@ -517,19 +165,6 @@ public class ReflectionUtils { cacheAccessor(field).setValue(owner, value); } - public static boolean doesFieldExist(Class<?> clazz, String string) { - if (clazz != null) { - if (ReflectionUtils.getField(clazz, string) != null) { - return true; - } - } - return false; - } - - public static <T> T getFieldValue(Field field) { - return getFieldValue(field, null); - } - public static <T> T getFieldValue(Field field, Object instance) { try { return (T) field.get(instance); @@ -537,29 +172,4 @@ public class ReflectionUtils { return null; } - public static <T> T createNewInstanceFromConstructor(Constructor aConstructor, Object[] aArgs) { - T aInstance; - try { - aInstance = (T) aConstructor.newInstance(aArgs); - return aInstance; - } catch (InstantiationException | IllegalAccessException | IllegalArgumentException - | InvocationTargetException e) { - e.printStackTrace(); - } - return null; - } - - public static Enum getEnum(Class<Enum> sgtbees, String name) { - if (sgtbees.isEnum()) { - Object[] aValues = sgtbees.getEnumConstants(); - for (Object o : aValues) { - if (o.toString() - .toLowerCase() - .equals(name.toLowerCase())) { - return (Enum) o; - } - } - } - return null; - } } |