Class ReflectionUtilities

java.lang.Object
de.gurkenlabs.litiengine.util.ReflectionUtilities

public final class ReflectionUtilities
extends java.lang.Object
  • Method Summary

    Modifier and TypeMethodDescription
    static java.util.List<java.lang.reflect.Field>getAllFields​(java.util.List<java.lang.reflect.Field> fields, java.lang.Class<?> type)
    Recursively gets all fields of the specified type, respecting parent classes.
    static <T> TgetDefaultValue​(java.lang.Class<T> clazz) 
    static java.util.Collection<java.lang.reflect.Method>getEvents​(java.lang.Class<?> type)
    Gets the events for the specified type.
    static <T> java.lang.reflect.FieldgetField​(java.lang.Class<T> cls, java.lang.String fieldName) 
    static <T> java.lang.reflect.FieldgetField​(java.lang.Class<T> cls, java.lang.String fieldName, boolean recursive) 
    static java.lang.reflect.MethodgetMethod​(java.lang.String name, java.lang.Class<?> type, java.lang.Class<?>... parameterTypes)
    Recursively gets a method by the the specified name respecting the parent classes and the parameters of the declaration.
    static java.util.List<java.lang.reflect.Method>getMethodsAnnotatedWith​(java.lang.Class<?> type, java.lang.Class<? extends java.lang.annotation.Annotation> annotation) 
    static <T> java.lang.reflect.MethodgetSetter​(java.lang.Class<T> cls, java.lang.String fieldName) 
    static <T> java.util.Collection<java.lang.reflect.Method>getSetters​(java.lang.Class<T> cls) 
    static <V> VgetStaticValue​(java.lang.Class<?> cls, java.lang.String fieldName) 
    static <T,​ C> booleanisWrapperType​(java.lang.Class<T> primitive, java.lang.Class<C> potentialWrapper) 
    static <T> booleansetEnumPropertyValue​(java.lang.Class<T> cls, java.lang.Object instance, java.lang.reflect.Field field, java.lang.String propertyName, java.lang.String value) 
    static <T> booleansetFieldValue​(java.lang.Class<T> cls, java.lang.Object instance, java.lang.String fieldName, java.lang.String value) 
    static <T,​ C> booleansetValue​(java.lang.Class<C> cls, java.lang.Object instance, java.lang.String fieldName, T value) 

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • getField

      public static <T> java.lang.reflect.Field getField​(java.lang.Class<T> cls, java.lang.String fieldName)
    • getField

      public static <T> java.lang.reflect.Field getField​(java.lang.Class<T> cls, java.lang.String fieldName, boolean recursive)
    • getStaticValue

      public static <V> V getStaticValue​(java.lang.Class<?> cls, java.lang.String fieldName)
    • getAllFields

      public static java.util.List<java.lang.reflect.Field> getAllFields​(java.util.List<java.lang.reflect.Field> fields, java.lang.Class<?> type)
      Recursively gets all fields of the specified type, respecting parent classes.
      Parameters:
      fields - The list containing all fields.
      type - The type to retrieve the fields from.
      Returns:
      All fields of the specified type, including the fields of the parent classes.
    • getMethod

      public static java.lang.reflect.Method getMethod​(java.lang.String name, java.lang.Class<?> type, java.lang.Class<?>... parameterTypes)
      Recursively gets a method by the the specified name respecting the parent classes and the parameters of the declaration.
      Parameters:
      name - The name of the method.
      type - The type on which to search for the method.
      parameterTypes - The types of the parameters defined by the method declaration.
      Returns:
      The found method or null if no such method exists.
    • setValue

      public static <T,​ C> boolean setValue​(java.lang.Class<C> cls, java.lang.Object instance, java.lang.String fieldName, T value)
    • setEnumPropertyValue

      public static <T> boolean setEnumPropertyValue​(java.lang.Class<T> cls, java.lang.Object instance, java.lang.reflect.Field field, java.lang.String propertyName, java.lang.String value)
    • getSetter

      public static <T> java.lang.reflect.Method getSetter​(java.lang.Class<T> cls, java.lang.String fieldName)
    • getSetters

      public static <T> java.util.Collection<java.lang.reflect.Method> getSetters​(java.lang.Class<T> cls)
    • isWrapperType

      public static <T,​ C> boolean isWrapperType​(java.lang.Class<T> primitive, java.lang.Class<C> potentialWrapper)
    • setFieldValue

      public static <T> boolean setFieldValue​(java.lang.Class<T> cls, java.lang.Object instance, java.lang.String fieldName, java.lang.String value)
    • getMethodsAnnotatedWith

      public static java.util.List<java.lang.reflect.Method> getMethodsAnnotatedWith​(java.lang.Class<?> type, java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
    • getEvents

      public static java.util.Collection<java.lang.reflect.Method> getEvents​(java.lang.Class<?> type)
      Gets the events for the specified type.

      This will search for all methods that have a parameter of type EventListener and match the LITIENGINE's naming conventions for event subscription (i.e. the method name starts with one of the prefixes "add" or "on".

      Parameters:
      type - The type to inspect the events on.
      Returns:
      All methods on the specified type that are considered to be events.
      See Also:
      EventListener
    • getDefaultValue

      public static <T> T getDefaultValue​(java.lang.Class<T> clazz)