aboutsummaryrefslogtreecommitdiff
path: root/src/Java/gtPlusPlus/core/util/reflect
diff options
context:
space:
mode:
author‭huajijam <strhuaji@gmail.com>2019-05-17 23:26:41 +0800
committer‭huajijam <strhuaji@gmail.com>2019-05-17 23:26:41 +0800
commit5920c6db3ff62d89ba41db4eda59cc6d70ddce3b (patch)
treed4d6d5036e8cb95d2232066f0ee257f59b5aa967 /src/Java/gtPlusPlus/core/util/reflect
parent787c31758d2ea3259f8da9c5c7ffd4b28429eb9a (diff)
parentcacc2915ef0039e3460817e4f116d44cf8e44e5f (diff)
downloadGT5-Unofficial-5920c6db3ff62d89ba41db4eda59cc6d70ddce3b.tar.gz
GT5-Unofficial-5920c6db3ff62d89ba41db4eda59cc6d70ddce3b.tar.bz2
GT5-Unofficial-5920c6db3ff62d89ba41db4eda59cc6d70ddce3b.zip
Automatic synchronization
Diffstat (limited to 'src/Java/gtPlusPlus/core/util/reflect')
-rw-r--r--src/Java/gtPlusPlus/core/util/reflect/AddGregtechRecipe.java61
-rw-r--r--src/Java/gtPlusPlus/core/util/reflect/ReflectionUtils.java232
2 files changed, 265 insertions, 28 deletions
diff --git a/src/Java/gtPlusPlus/core/util/reflect/AddGregtechRecipe.java b/src/Java/gtPlusPlus/core/util/reflect/AddGregtechRecipe.java
index fcb8c42197..0b22ea3acc 100644
--- a/src/Java/gtPlusPlus/core/util/reflect/AddGregtechRecipe.java
+++ b/src/Java/gtPlusPlus/core/util/reflect/AddGregtechRecipe.java
@@ -7,13 +7,72 @@ import net.minecraft.item.ItemStack;
import gregtech.api.enums.GT_Values;
import gregtech.api.interfaces.internal.IGT_RecipeAdder;
-
+import gregtech.api.util.GT_Recipe;
+import gregtech.api.util.GT_Utility;
import gtPlusPlus.core.lib.CORE;
+import gtPlusPlus.core.recipe.common.CI;
import gtPlusPlus.core.util.minecraft.FluidUtils;
import gtPlusPlus.core.util.minecraft.ItemUtils;
import net.minecraftforge.fluids.FluidStack;
public final class AddGregtechRecipe {
+
+
+ public static boolean importPyroRecipe(GT_Recipe aRecipe) {
+
+ int aModifiedTime = (int) (aRecipe.mDuration * 0.8);
+
+ if (aRecipe.mInputs == null || aRecipe.mFluidInputs == null || aRecipe.mFluidOutputs == null || aRecipe.mOutputs == null) {
+ return false;
+ }
+
+ if (aRecipe.mInputs.length > 2 || aRecipe.mFluidInputs.length > 1 || aRecipe.mFluidOutputs.length > 1 || aRecipe.mOutputs.length > 1) {
+ return false;
+ }
+ else if (aRecipe.mInputs.length <= 0 || aRecipe.mFluidInputs.length <= 0 || aRecipe.mFluidOutputs.length <= 0 || aRecipe.mOutputs.length <= 0) {
+ return false;
+ }
+
+ int aCircuitNumber = -1;
+ int aItemSlot = -1;
+
+ int aSlot = 0;
+ for (ItemStack a : aRecipe.mInputs) {
+ if (a != null && a.getItem() != CI.getNumberedCircuit(1).getItem()) {
+ aItemSlot = aSlot;
+ }
+ else {
+ aSlot++;
+ }
+ }
+
+ for (int i=0;i<25;i++) {
+ ItemStack aTest = CI.getNumberedCircuit(i);
+ for (ItemStack a : aRecipe.mInputs) {
+ if (a != null && GT_Utility.areStacksEqual(a, aTest)) {
+ aCircuitNumber = i;
+ break;
+ }
+ }
+ }
+
+ if (aCircuitNumber < 0) {
+ return false;
+ }
+
+
+ return CORE.RA.addCokeOvenRecipe(
+ aRecipe.mInputs[aItemSlot],
+ ItemUtils.getGregtechCircuit(aCircuitNumber),
+ aRecipe.mFluidInputs[0],
+ aRecipe.mFluidOutputs[0],
+ aRecipe.mOutputs[0],
+ aModifiedTime,
+ aRecipe.mEUt);
+
+
+ }
+
public static boolean addCokeAndPyrolyseRecipes(
ItemStack input1, int circuitNumber,
diff --git a/src/Java/gtPlusPlus/core/util/reflect/ReflectionUtils.java b/src/Java/gtPlusPlus/core/util/reflect/ReflectionUtils.java
index 722a4f3ff7..2371753fe6 100644
--- a/src/Java/gtPlusPlus/core/util/reflect/ReflectionUtils.java
+++ b/src/Java/gtPlusPlus/core/util/reflect/ReflectionUtils.java
@@ -3,10 +3,17 @@ package gtPlusPlus.core.util.reflect;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
+import java.lang.reflect.GenericDeclaration;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
+import java.lang.reflect.TypeVariable;
+import java.util.HashMap;
import java.util.LinkedHashMap;
+import java.util.LinkedList;
+import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.ArrayUtils;
@@ -133,8 +140,8 @@ public class ReflectionUtils {
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.
@@ -193,7 +200,7 @@ public class ReflectionUtils {
return y.get();
}
}
-
+
/**
* Returns a cached {@link Field} object.
* @param aInstance - {@link Object} to get the field instance from.
@@ -219,6 +226,20 @@ public class ReflectionUtils {
return isClassPresent(classname);
}
+
+ /**
+ * Returns the class of the objects type parameter
+ * @param o - Object to examine paramters on
+ * @return - a Class<?> or null
+ */
+ public static Class<?> getTypeOfGenericObject(Object o) {
+ Class<?> aTypeParam = findSuperClassParameterType(o, o.getClass(), 0);
+ if (aTypeParam == null) {
+ aTypeParam = findSubClassParameterType(o, o.getClass(), 0);
+ }
+ return aTypeParam;
+ }
+
public static void makeFieldAccessible(final Field field) {
if (!Modifier.isPublic(field.getModifiers()) ||
!Modifier.isPublic(field.getDeclaringClass().getModifiers()))
@@ -271,6 +292,7 @@ public class ReflectionUtils {
return loaded > 0;
}
+
public static boolean setField(final Object object, final String fieldName, final Object fieldValue) {
@@ -291,15 +313,42 @@ public class ReflectionUtils {
}
}
return false;
+
+
+ }
+
+ public static boolean setField(final Object object, final Field field, final Object fieldValue) {
+ Class<?> clazz = object.getClass();
+ while (clazz != null) {
+ try {
+ final Field field2 = getField(clazz, field.getName());
+ if (field2 != null) {
+ setFieldValue_Internal(object, field, fieldValue);
+ return true;
+ }
+ } catch (final NoSuchFieldException e) {
+ Logger.REFLECTION("setField("+object.toString()+", "+field.getName()+") failed.");
+ clazz = clazz.getSuperclass();
+ } catch (final Exception e) {
+ Logger.REFLECTION("setField("+object.toString()+", "+field.getName()+") failed.");
+ throw new IllegalStateException(e);
+ }
+ }
+ return false;
}
/**
* Allows to change the state of an immutable instance. Huh?!?
*/
- public static void setFinalFieldValue(Class<?> clazz, String fieldName, Object newValue) throws Exception {
+ public static void setFinalFieldValue(Class<?> clazz, String fieldName, Object newValue) {
Field nameField = getField(clazz, fieldName);
- setFieldValue_Internal(clazz, nameField, newValue);
+ try {
+ setFieldValue_Internal(clazz, nameField, newValue);
+ }
+ catch (Throwable t) {
+ t.printStackTrace();
+ }
}
@Deprecated
@@ -414,13 +463,27 @@ public class ReflectionUtils {
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ /*
+ * Internal Magic that probably should not get exposed.
+ */
+
@@ -429,11 +492,126 @@ public class ReflectionUtils {
+ /*
+ *
+ * Below Code block is used for determining generic types associated with type<E>
+ *
+ */
+
+
+ //https://xebia.com/blog/acessing-generic-types-at-runtime-in-java/
+ //https://www.javacodegeeks.com/2013/12/advanced-java-generics-retreiving-generic-type-arguments.html
+ public static Class<?> findSuperClassParameterType(Object instance, Class<?> classOfInterest, int parameterIndex) {
+ Class<?> subClass = instance.getClass();
+ while (classOfInterest != subClass.getSuperclass()) {
+ // instance.getClass() is no subclass of classOfInterest or instance is a direct instance of classOfInterest
+ subClass = subClass.getSuperclass();
+ if (subClass == null) {
+ return null;
+ }
+ }
+ ParameterizedType parameterizedType = (ParameterizedType) subClass.getGenericSuperclass();
+ Class<?> aReturn;
+ aReturn = (Class<?>) parameterizedType.getActualTypeArguments()[parameterIndex];
+ return aReturn;
+ }
+
+ public static Class<?> findSubClassParameterType(Object instance, Class<?> classOfInterest, int parameterIndex) {
+ Map<Type, Type> typeMap = new HashMap<Type, Type>();
+ Class<?> instanceClass = instance.getClass();
+ while (classOfInterest != instanceClass.getSuperclass()) {
+ extractTypeArguments(typeMap, instanceClass);
+ instanceClass = instanceClass.getSuperclass();
+ if (instanceClass == null) {
+ return null;
+ }
+ }
+
+ ParameterizedType parameterizedType = (ParameterizedType) instanceClass.getGenericSuperclass();
+ Type actualType = parameterizedType.getActualTypeArguments()[parameterIndex];
+ if (typeMap.containsKey(actualType)) {
+ actualType = typeMap.get(actualType);
+ }
+ if (actualType instanceof Class) {
+ return (Class<?>) actualType;
+ } else if (actualType instanceof TypeVariable) {
+ return browseNestedTypes(instance, (TypeVariable<?>) actualType);
+ } else {
+ return null;
+ }
+ }
+
+ private static void extractTypeArguments(Map<Type, Type> typeMap, Class<?> clazz) {
+ Type genericSuperclass = clazz.getGenericSuperclass();
+ if (!(genericSuperclass instanceof ParameterizedType)) {
+ return;
+ }
+
+ ParameterizedType parameterizedType = (ParameterizedType) genericSuperclass;
+ Type[] typeParameter = ((Class<?>) parameterizedType.getRawType()).getTypeParameters();
+ Type[] actualTypeArgument = parameterizedType.getActualTypeArguments();
+ for (int i = 0; i < typeParameter.length; i++) {
+ if(typeMap.containsKey(actualTypeArgument[i])) {
+ actualTypeArgument[i] = typeMap.get(actualTypeArgument[i]);
+ }
+ typeMap.put(typeParameter[i], actualTypeArgument[i]);
+ }
+ }
+
+ private static Class<?> browseNestedTypes(Object instance, TypeVariable<?> actualType) {
+ Class<?> instanceClass = instance.getClass();
+ List<Class<?>> nestedOuterTypes = new LinkedList<Class<?>>();
+ for (Class<?> enclosingClass = instanceClass
+ .getEnclosingClass(); enclosingClass != null; enclosingClass = enclosingClass.getEnclosingClass()) {
+ try {
+ Field this$0 = instanceClass.getDeclaredField("this$0");
+ Object outerInstance = this$0.get(instance);
+ Class<?> outerClass = outerInstance.getClass();
+ nestedOuterTypes.add(outerClass);
+ Map<Type, Type> outerTypeMap = new HashMap<Type, Type>();
+ extractTypeArguments(outerTypeMap, outerClass);
+ for (Map.Entry<Type, Type> entry : outerTypeMap.entrySet()) {
+ if (!(entry.getKey() instanceof TypeVariable)) {
+ continue;
+ }
+ TypeVariable<?> foundType = (TypeVariable<?>) entry.getKey();
+ if (foundType.getName().equals(actualType.getName())
+ && isInnerClass(foundType.getGenericDeclaration(), actualType.getGenericDeclaration())) {
+ if (entry.getValue() instanceof Class) {
+ return (Class<?>) entry.getValue();
+ }
+ actualType = (TypeVariable<?>) entry.getValue();
+ }
+ }
+ } catch (NoSuchFieldException | IllegalAccessException e) {
+
+ }
+
+ }
+ return null;
+ }
+
+ private static boolean isInnerClass(GenericDeclaration outerDeclaration, GenericDeclaration innerDeclaration) {
+ if (!(outerDeclaration instanceof Class) || !(innerDeclaration instanceof Class)) {
+ return false;
+ }
+ Class<?> outerClass = (Class<?>) outerDeclaration;
+ Class<?> innerClass = (Class<?>) innerDeclaration;
+ while ((innerClass = innerClass.getEnclosingClass()) != null) {
+ if (innerClass == outerClass) {
+ return true;
+ }
+ }
+ return false;
+ }
/*
- * Internal Magic that probably should not get exposed.
- */
+ *
+ * End of Generics Block
+ *
+ */
+
private static Field getField_Internal(final Class<?> clazz, final String fieldName) throws NoSuchFieldException {
@@ -525,26 +703,26 @@ public class ReflectionUtils {
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();
+ 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()));
- }
+ 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());
- }
+ 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()));
- }
+ for (Constructor c : consts) {
+ System.out.println(c.getName()+" | "+c.getParameterCount()+" | "+StringUtils.getDataStringFromArray(c.getParameterTypes()));
+ }
}
private static Class<?> getNonPublicClass(final String className) {