Class FuzzyReflection

java.lang.Object
com.comphenix.protocol.reflect.FuzzyReflection

public class FuzzyReflection extends Object
Retrieves fields and methods by signature, not just name.
  • Constructor Details

    • FuzzyReflection

      public FuzzyReflection(Class<?> source, boolean forceAccess)
  • Method Details

    • fromClass

      public static FuzzyReflection fromClass(Class<?> source)
      Retrieves a fuzzy reflection instance from a given class.
      Parameters:
      source - - the class we'll use.
      Returns:
      A fuzzy reflection instance.
    • fromClass

      public static FuzzyReflection fromClass(Class<?> source, boolean forceAccess)
      Retrieves a fuzzy reflection instance from a given class.
      Parameters:
      source - - the class we'll use.
      forceAccess - - whether to override scope restrictions.
      Returns:
      A fuzzy reflection instance.
    • fromObject

      public static FuzzyReflection fromObject(Object reference)
      Retrieves a fuzzy reflection instance from an object.
      Parameters:
      reference - - the object we'll use.
      Returns:
      A fuzzy reflection instance that uses the class of the given object.
    • fromObject

      public static FuzzyReflection fromObject(Object reference, boolean forceAccess)
      Retrieves a fuzzy reflection instance from an object.
      Parameters:
      reference - - the object we'll use.
      forceAccess - - whether to override scope restrictions.
      Returns:
      A fuzzy reflection instance that uses the class of the given object.
    • getFieldValue

      public static <T> T getFieldValue(Object instance, Class<T> fieldClass, boolean forceAccess)
      Retrieve the value of the first field of the given type.
      Type Parameters:
      T - Type
      Parameters:
      instance - - the instance to retrieve from.
      fieldClass - - type of the field to retrieve.
      forceAccess - - whether to look for private and protected fields.
      Returns:
      The value of that field.
      Throws:
      IllegalArgumentException - If the field cannot be found.
    • combineArrays

      @SafeVarargs public static <T> Set<T> combineArrays(T[]... arrays)
    • getSource

      public Class<?> getSource()
      Retrieves the underlying class.
      Returns:
      The underlying class.
    • isForceAccess

      public boolean isForceAccess()
      Retrieves whether or not not to override any scope restrictions.
      Returns:
      TRUE if we override scope, FALSE otherwise.
    • getSingleton

      public Object getSingleton()
      Retrieve the singleton instance of a class, from a method or field.
      Returns:
      The singleton instance.
      Throws:
      IllegalStateException - If the class has no singleton.
    • getMethod

      public Method getMethod(AbstractFuzzyMatcher<MethodInfo> matcher)
      Retrieve the first method that matches.

      ForceAccess must be TRUE in order for this method to access private, protected and package level method.

      Parameters:
      matcher - - the matcher to use.
      Returns:
      The first method that satisfies the given matcher.
      Throws:
      IllegalArgumentException - If the method cannot be found.
    • getMethod

      public Method getMethod(AbstractFuzzyMatcher<MethodInfo> matcher, String preferred)
      Retrieve a method that matches. If there are multiple methods that match, the first one with the preferred name is selected.

      ForceAccess must be TRUE in order for this method to access private, protected and package level method.

      Parameters:
      matcher - - the matcher to use.
      preferred - - the preferred name, null for no preference.
      Returns:
      The first method that satisfies the given matcher.
      Throws:
      IllegalArgumentException - If the method cannot be found.
    • getMethodByName

      public Method getMethodByName(String nameRegex)
      Retrieves a method by looking at its name.
      Parameters:
      nameRegex - - regular expression that will match method names.
      Returns:
      The first method that satisfies the regular expression.
      Throws:
      IllegalArgumentException - If the method cannot be found.
    • getMethodByParameters

      public Method getMethodByParameters(String name, Class<?>... args)
      Retrieves a method by looking at the parameter types only.
      Parameters:
      name - - potential name of the method. Only used by the error mechanism.
      args - - parameter types of the method to find.
      Returns:
      The first method that satisfies the parameter types.
      Throws:
      IllegalArgumentException - If the method cannot be found.
    • getMethodByReturnTypeAndParameters

      public Method getMethodByReturnTypeAndParameters(String name, Class<?> returnType, Class<?>... args)
      Retrieves a method by looking at the parameter types and return type only.
      Parameters:
      name - - potential name of the method. Only used by the error mechanism.
      returnType - - return type of the method to find.
      args - - parameter types of the method to find.
      Returns:
      The first method that satisfies the parameter types.
      Throws:
      IllegalArgumentException - If the method cannot be found.
    • getMethodList

      public List<Method> getMethodList(AbstractFuzzyMatcher<MethodInfo> matcher)
      Retrieve a list of every method that matches the given matcher.

      ForceAccess must be TRUE in order for this method to access private, protected and package level methods.

      Parameters:
      matcher - - the matcher to apply.
      Returns:
      List of found methods.
    • getMethodListByParameters

      public List<Method> getMethodListByParameters(Class<?> returnType, Class<?>... args)
      Retrieves every method that has the given parameter types and return type.
      Parameters:
      returnType - - return type of the method to find.
      args - - parameter types of the method to find.
      Returns:
      Every method that satisfies the given constraints.
    • getField

      public Field getField(AbstractFuzzyMatcher<Field> matcher)
      Retrieve the first field that matches.

      ForceAccess must be TRUE in order for this method to access private, protected and package level fields.

      Parameters:
      matcher - - the matcher to use.
      Returns:
      The first method that satisfies the given matcher.
      Throws:
      IllegalArgumentException - If the method cannot be found.
    • getFieldByName

      public Field getFieldByName(String nameRegex)
      Retrieves a field by name.
      Parameters:
      nameRegex - - regular expression that will match a field name.
      Returns:
      The first field to match the given expression.
      Throws:
      IllegalArgumentException - If the field cannot be found.
    • getFieldByType

      public Field getFieldByType(String name, Class<?> type)
      Retrieves the first field with a type equal to or more specific to the given type.
      Parameters:
      name - - name the field probably is given. This will only be used in the error message.
      type - - type of the field to find.
      Returns:
      The first field with a type that is an instance of the given type.
    • getFieldListByType

      public List<Field> getFieldListByType(Class<?> type)
      Retrieves every field with a type equal to or more specific to the given type.
      Parameters:
      type - - type of the fields to find.
      Returns:
      Every field with a type that is an instance of the given type.
    • getParameterizedField

      public Field getParameterizedField(Class<?> fieldType, Class<?>... params)
      Retrieves a field with a given type and parameters. This is most useful when dealing with Collections.
      Parameters:
      fieldType - Type of the field
      params - Variable length array of type parameters
      Returns:
      The field
      Throws:
      IllegalArgumentException - If the field cannot be found
    • getFieldList

      public List<Field> getFieldList(AbstractFuzzyMatcher<Field> matcher)
      Retrieve a list of every field that matches the given matcher.

      ForceAccess must be TRUE in order for this method to access private, protected and package level fields.

      Parameters:
      matcher - - the matcher to apply.
      Returns:
      List of found fields.
    • getFieldByType

      public Field getFieldByType(String typeRegex)
      Retrieves a field by type.

      Note that the type is matched using the full canonical representation, i.e.:

      • java.util.List
      • net.comphenix.xp.ExperienceMod
      Parameters:
      typeRegex - - regular expression that will match the field type.
      Returns:
      The first field with a type that matches the given regular expression.
      Throws:
      IllegalArgumentException - If the field cannot be found.
    • getConstructor

      public Constructor<?> getConstructor(AbstractFuzzyMatcher<MethodInfo> matcher)
      Retrieve the first constructor that matches.

      ForceAccess must be TRUE in order for this method to access private, protected and package level constructors.

      Parameters:
      matcher - - the matcher to use.
      Returns:
      The first constructor that satisfies the given matcher.
      Throws:
      IllegalArgumentException - If the constructor cannot be found.
    • getConstructorList

      public List<Constructor<?>> getConstructorList(AbstractFuzzyMatcher<MethodInfo> matcher)
      Retrieve a list of every constructor that matches the given matcher.

      ForceAccess must be TRUE in order for this method to access private, protected and package level constructors.

      Parameters:
      matcher - - the matcher to apply.
      Returns:
      List of found constructors.
    • getFields

      public Set<Field> getFields()
      Retrieves all private and public fields in declared order.

      Private, protected and package fields are ignored if forceAccess is FALSE.

      Returns:
      Every field.
    • getDeclaredFields

      public Set<Field> getDeclaredFields(Class<?> excludeClass)
      Retrieves all private and public fields, up until a certain superclass.
      Parameters:
      excludeClass - - the class (and its superclasses) to exclude from the search.
      Returns:
      Every such declared field.
    • getMethods

      public Set<Method> getMethods()
      Retrieves all private and public methods in declared order (after JDK 1.5).

      Private, protected and package methods are ignored if forceAccess is FALSE.

      Returns:
      Every method.
    • getConstructors

      public Set<Constructor<?>> getConstructors()
      Retrieves all private and public constructors in declared order (after JDK 1.5).

      Private, protected and package constructors are ignored if forceAccess is FALSE.

      Returns:
      Every constructor.