mirror of
https://github.com/GTNewHorizons/ModTweaker.git
synced 2026-11-16 01:55:11 +00:00
[ci skip] spotlessApply with the new settings
This commit is contained in:
1 parent
0cc3fa06c2
commit
655636b7e9
231 files changed
+10024
-8585
No files matched your search
@@ -5,46 +5,53 @@ import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
|
||||
public class ReflectionHelper {
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new instance with the provided constructor and arguments.
|
||||
*
|
||||
* @param constructor The constructor of the class for which a new instance should be created.
|
||||
* @param arguments Array of objects to be passed as arguments to the constructor call.
|
||||
* @param arguments Array of objects to be passed as arguments to the constructor call.
|
||||
* @return A new object created by calling the constructor.
|
||||
*/
|
||||
public static <T> T getInstance(Constructor<T> constructor, Object... arguments) {
|
||||
try {
|
||||
return constructor.newInstance(arguments);
|
||||
} catch (Exception e) { LogHelper.logError("Exception creating instance of " + constructor.getClass().getName(), e); }
|
||||
} catch (Exception e) {
|
||||
LogHelper.logError("Exception creating instance of " + constructor.getClass().getName(), e);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Obtains the constructor for the named class identified by the parameter types.
|
||||
* @param className The fully qualified name of the class.
|
||||
*
|
||||
* @param className The fully qualified name of the class.
|
||||
* @param argumentTypes Parameter types to identify the constructor.
|
||||
* @return Constructor object representing the declared constructor for the parameter types.
|
||||
*/
|
||||
public static Constructor<?> getConstructor(final String className, final Class<?>... argumentTypes) {
|
||||
if(className == null || argumentTypes == null) {
|
||||
if (className == null || argumentTypes == null) {
|
||||
LogHelper.logError("The provided class name or arguments can't be null.");
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
Class<?> clazz = Class.forName(className);
|
||||
Constructor<?> constructor = clazz.getDeclaredConstructor(argumentTypes);
|
||||
constructor.setAccessible(true);
|
||||
return constructor;
|
||||
} catch (Exception ex) { LogHelper.logError("Exception getting constructor of " + className, ex); }
|
||||
} catch (Exception ex) {
|
||||
LogHelper.logError("Exception getting constructor of " + className, ex);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Obtains the constructor for the class identified by the parameter types.
|
||||
* @param clazz The Class for which the constructor should be obtained.
|
||||
*
|
||||
* @param clazz The Class for which the constructor should be obtained.
|
||||
* @param argumentTypes Parameter types to identify the constructor.
|
||||
* @return Constructor object representing the declared constructor for the parameter types.
|
||||
*/
|
||||
@@ -53,16 +60,19 @@ public class ReflectionHelper {
|
||||
Constructor<T> constructor = clazz.getDeclaredConstructor(types);
|
||||
constructor.setAccessible(true);
|
||||
return constructor;
|
||||
} catch (Exception ex) { LogHelper.logError("Exception getting constructor of " + clazz.getName(), ex); }
|
||||
} catch (Exception ex) {
|
||||
LogHelper.logError("Exception getting constructor of " + clazz.getName(), ex);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value of a private field for an object instance.
|
||||
* @param object An object instance from which the field value is to be extracted.
|
||||
* @param fieldNames A list of field names for which the value should be extracted.
|
||||
* The functions returns value of the first field found.
|
||||
*
|
||||
* @param object An object instance from which the field value is to be extracted.
|
||||
* @param fieldNames A list of field names for which the value should be extracted. The functions returns value of
|
||||
* the first field found.
|
||||
* @return The value of the provided field name.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
@@ -73,19 +83,22 @@ public class ReflectionHelper {
|
||||
Field result = cls.getDeclaredField(field);
|
||||
result.setAccessible(true);
|
||||
return (T) result.get(object);
|
||||
} catch (Exception ex) { LogHelper.logError("Exception in getObject()", ex); }
|
||||
} catch (Exception ex) {
|
||||
LogHelper.logError("Exception in getObject()", ex);
|
||||
}
|
||||
}
|
||||
|
||||
LogHelper.logError("Could not retrieve any object for the provided field names.");
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value of a private final field for an object instance and removes the final modifier.
|
||||
* @param object An object instance from which the field value is to be extracted.
|
||||
* @param fieldNames A list of field names for which the value should be extracted.
|
||||
* The functions returns value of the first field found.
|
||||
*
|
||||
* @param object An object instance from which the field value is to be extracted.
|
||||
* @param fieldNames A list of field names for which the value should be extracted. The functions returns value of
|
||||
* the first field found.
|
||||
* @return The value of the provided field name.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
@@ -99,17 +112,20 @@ public class ReflectionHelper {
|
||||
modifiersField.setAccessible(true);
|
||||
modifiersField.setInt(result, result.getModifiers() & ~Modifier.FINAL);
|
||||
return (T) result.get(object);
|
||||
} catch (Exception ex) { LogHelper.logError("Exception in getFinalObject()", ex); }
|
||||
} catch (Exception ex) {
|
||||
LogHelper.logError("Exception in getFinalObject()", ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
LogHelper.logError("Could not retrieve any final object for the provided field names.");
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value of a private static field for a class.
|
||||
* @param clazz The class for which the field value is to be extracted.
|
||||
*
|
||||
* @param clazz The class for which the field value is to be extracted.
|
||||
* @param fieldNames A list of field names for which the value should be extracted.
|
||||
* @return The value of the provided field name.
|
||||
*/
|
||||
@@ -120,17 +136,20 @@ public class ReflectionHelper {
|
||||
Field result = clazz.getDeclaredField(field);
|
||||
result.setAccessible(true);
|
||||
return (T) result.get(null);
|
||||
} catch (Exception e) { LogHelper.logError("Exception in getStaticObject()", e); }
|
||||
} catch (Exception e) {
|
||||
LogHelper.logError("Exception in getStaticObject()", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
LogHelper.logError("Could not retrieve any static object for the provided field names.");
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the value of a private static field for a class.
|
||||
* @param className The fully qualified name of the class.
|
||||
*
|
||||
* @param className The fully qualified name of the class.
|
||||
* @param fieldNames A list of field names for which the value should be extracted.
|
||||
* @return The value of the provided field name.
|
||||
*/
|
||||
@@ -138,10 +157,12 @@ public class ReflectionHelper {
|
||||
try {
|
||||
Class<?> clazz = Class.forName(className);
|
||||
return getStaticObject(clazz, fieldNames);
|
||||
} catch (ClassNotFoundException e) { LogHelper.logError("Exception in getStaticObject()", e); }
|
||||
|
||||
} catch (ClassNotFoundException e) {
|
||||
LogHelper.logError("Exception in getStaticObject()", e);
|
||||
}
|
||||
|
||||
LogHelper.logError("Could not retrieve any static object for the provided field names.");
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -150,11 +171,13 @@ public class ReflectionHelper {
|
||||
Field f = cls.getDeclaredField(field);
|
||||
f.setAccessible(true);
|
||||
f.setInt(null, var);
|
||||
} catch (Exception e) { LogHelper.logError("Exception in setPrivateValue()", e); }
|
||||
} catch (Exception e) {
|
||||
LogHelper.logError("Exception in setPrivateValue()", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
public static void setPrivateValue( Class cls, Object o, String field, Object var) {
|
||||
public static void setPrivateValue(Class cls, Object o, String field, Object var) {
|
||||
cpw.mods.fml.relauncher.ReflectionHelper.setPrivateValue(cls, o, var, field);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user