Package net.minecraftforge.fml.util
Class ObfuscationReflectionHelper
java.lang.Object
net.minecraftforge.fml.util.ObfuscationReflectionHelper
Some reflection helper code.
This may not work properly in Java 9 with its new, more restrictive, reflection management.
As such, if issues are encountered, please report them and we can see what we can do to expand
the compatibility.
In other cases, AccessTransformers may be used.
All field and method names should be passed in as SRG names, and this will automatically resolve if MCP mappings are detected.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
static class
static class
static class
-
Field Summary
Modifier and TypeFieldDescriptionprivate static org.apache.logging.log4j.Logger
private static org.apache.logging.log4j.Marker
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> @NotNull Constructor<T>
findConstructor(@NotNull Class<T> clazz, @NotNull Class<?>... parameterTypes)
Finds a constructor with the specified parameter types in the given class and makes it accessible.static <T> @NotNull Field
Finds a field with the specified name in the given class and makes it accessible.static @NotNull Method
findMethod(@NotNull Class<?> clazz, @NotNull String methodName, @NotNull Class<?>... parameterTypes)
Finds a method with the specified name and parameters in the given class and makes it accessible.static <T, E> T
getPrivateValue(Class<? super E> classToAccess, E instance, String fieldName)
Gets the value a field with the specified name in the given class.static @NotNull String
Remaps a name using the SRG naming functionstatic <T, E> void
setPrivateValue(@NotNull Class<? super T> classToAccess, T instance, E value, @NotNull String fieldName)
Sets the value a field with the specified name in the given class.
-
Field Details
-
LOGGER
private static final org.apache.logging.log4j.Logger LOGGER -
REFLECTION
private static final org.apache.logging.log4j.Marker REFLECTION
-
-
Constructor Details
-
ObfuscationReflectionHelper
public ObfuscationReflectionHelper()
-
-
Method Details
-
remapName
@NotNull public static @NotNull String remapName(cpw.mods.modlauncher.api.INameMappingService.Domain domain, String name)Remaps a name using the SRG naming function- Parameters:
domain
- TheINameMappingService.Domain
to use to remap the name.name
- The name to try and remap.- Returns:
- The remapped name, or the original name if it couldn't be remapped.
-
getPrivateValue
@Nullable public static <T, E> T getPrivateValue(Class<? super E> classToAccess, E instance, String fieldName)Gets the value a field with the specified name in the given class. Note: For performance, usefindField(Class, String)
if you are getting the value more than once.Throws an exception if the field is not found or the value of the field cannot be gotten.
- Type Parameters:
T
- The type of the value.E
- The type of theclassToAccess
.- Parameters:
classToAccess
- The class to find the field on.instance
- The instance of theclassToAccess
.fieldName
- The SRG (unmapped) name of the field to find (e.g. "f_46443_").- Returns:
- The value of the field with the specified name in the
classToAccess
. - Throws:
ObfuscationReflectionHelper.UnableToAccessFieldException
- If there was a problem getting the field.ObfuscationReflectionHelper.UnableToAccessFieldException
- If there was a problem getting the value.
-
setPrivateValue
public static <T, E> void setPrivateValue(@NotNull @NotNull Class<? super T> classToAccess, @NotNull T instance, @Nullable E value, @NotNull @NotNull String fieldName)Sets the value a field with the specified name in the given class. Note: For performance, usefindField(Class, String)
if you are setting the value more than once.Throws an exception if the field is not found or the value of the field cannot be set.
- Type Parameters:
T
- The type of the value.E
- The type of theclassToAccess
.- Parameters:
classToAccess
- The class to find the field on.instance
- The instance of theclassToAccess
.value
- The new value for the fieldfieldName
- The name of the field in theclassToAccess
.- Throws:
ObfuscationReflectionHelper.UnableToFindFieldException
- If there was a problem getting the field.ObfuscationReflectionHelper.UnableToAccessFieldException
- If there was a problem setting the value of the field.
-
findMethod
@NotNull public static @NotNull Method findMethod(@NotNull @NotNull Class<?> clazz, @NotNull @NotNull String methodName, @NotNull @NotNull Class<?>... parameterTypes)Finds a method with the specified name and parameters in the given class and makes it accessible. Note: For performance, store the returned value and avoid calling this repeatedly.Throws an exception if the method is not found.
- Parameters:
clazz
- The class to find the method on.methodName
- The SRG (unmapped) name of the method to find (e.g. "m_5776_").parameterTypes
- The parameter types of the method to find.- Returns:
- The method with the specified name and parameters in the given class.
- Throws:
NullPointerException
- Ifclazz
is null.NullPointerException
- IfmethodName
is null.IllegalArgumentException
- IfmethodName
is empty.NullPointerException
- IfparameterTypes
is null.ObfuscationReflectionHelper.UnableToFindMethodException
- If the method could not be found.
-
findConstructor
@NotNull public static <T> @NotNull Constructor<T> findConstructor(@NotNull @NotNull Class<T> clazz, @NotNull @NotNull Class<?>... parameterTypes)Finds a constructor with the specified parameter types in the given class and makes it accessible. Note: For performance, store the returned value and avoid calling this repeatedly.Throws an exception if the constructor is not found.
- Type Parameters:
T
- The type.- Parameters:
clazz
- The class to find the constructor in.parameterTypes
- The parameter types of the constructor.- Returns:
- The constructor with the specified parameters in the given class.
- Throws:
NullPointerException
- Ifclazz
is null.NullPointerException
- IfparameterTypes
is null.ObfuscationReflectionHelper.UnknownConstructorException
- If the constructor could not be found.
-
findField
@NotNull public static <T> @NotNull Field findField(@NotNull @NotNull Class<? super T> clazz, @NotNull @NotNull String fieldName)Finds a field with the specified name in the given class and makes it accessible. Note: For performance, store the returned value and avoid calling this repeatedly.Throws an exception if the field is not found.
- Type Parameters:
T
- The type.- Parameters:
clazz
- The class to find the field on.fieldName
- The SRG (unmapped) name of the field to find (e.g. "f_46443_").- Returns:
- The constructor with the specified parameters in the given class.
- Throws:
NullPointerException
- Ifclazz
is null.NullPointerException
- IffieldName
is null.IllegalArgumentException
- IffieldName
is empty.ObfuscationReflectionHelper.UnableToFindFieldException
- If the field could not be found.
-