Class ClassReflection


  • public final class ClassReflection
    extends java.lang.Object
    Utilities for Class reflection.
    • Constructor Summary

      Constructors 
      Constructor Description
      ClassReflection()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.Class forName​(java.lang.String name)
      Returns the Class object associated with the class or interface with the supplied string name.
      static Annotation getAnnotation​(java.lang.Class c, java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
      Returns an Annotation object reflecting the annotation provided, or null if this class doesn't have such an annotation.
      static Annotation[] getAnnotations​(java.lang.Class c)
      Returns an array of Annotation objects reflecting all annotations declared by the supplied class, and inherited from its superclass.
      static java.lang.Class getComponentType​(java.lang.Class c)
      Returns the Class representing the component type of an array.
      static Constructor getConstructor​(java.lang.Class c, java.lang.Class... parameterTypes)
      Returns a Constructor that represents the public constructor for the supplied class which takes the supplied parameter types.
      static Constructor[] getConstructors​(java.lang.Class c)
      Returns an array of Constructor containing the public constructors of the class represented by the supplied Class.
      static Annotation getDeclaredAnnotation​(java.lang.Class c, java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
      Returns an Annotation object reflecting the annotation provided, or null if this class doesn't have such an annotation.
      static Annotation[] getDeclaredAnnotations​(java.lang.Class c)
      Returns an array of Annotation objects reflecting all annotations declared by the supplied class, or an empty array if there are none.
      static Constructor getDeclaredConstructor​(java.lang.Class c, java.lang.Class... parameterTypes)
      Returns a Constructor that represents the constructor for the supplied class which takes the supplied parameter types.
      static Field getDeclaredField​(java.lang.Class c, java.lang.String name)
      Returns a Field that represents the specified declared field for the supplied class.
      static Field[] getDeclaredFields​(java.lang.Class c)
      Returns an array of Field objects reflecting all the fields declared by the supplied class.
      static Method getDeclaredMethod​(java.lang.Class c, java.lang.String name, java.lang.Class... parameterTypes)
      Returns a Method that represents the method declared by the supplied class which takes the supplied parameter types.
      static Method[] getDeclaredMethods​(java.lang.Class c)
      Returns an array of Method containing the methods declared by the class represented by the supplied Class.
      static java.lang.Object[] getEnumConstants​(java.lang.Class c)
      Returns the elements of this enum class or null if this Class object does not represent an enum type.
      static Field getField​(java.lang.Class c, java.lang.String name)
      Returns a Field that represents the specified public member field for the supplied class.
      static Field[] getFields​(java.lang.Class c)
      Returns an array of Field containing the public fields of the class represented by the supplied Class.
      static java.lang.Class[] getInterfaces​(java.lang.Class c)  
      static Method getMethod​(java.lang.Class c, java.lang.String name, java.lang.Class... parameterTypes)
      Returns a Method that represents the public member method for the supplied class which takes the supplied parameter types.
      static Method[] getMethods​(java.lang.Class c)
      Returns an array of Method containing the public member methods of the class represented by the supplied Class.
      static java.lang.String getSimpleName​(java.lang.Class c)
      Returns the simple name of the underlying class as supplied in the source code.
      static boolean isAbstract​(java.lang.Class c)
      Determines if the supplied Class object represents an abstract type.
      static boolean isAnnotation​(java.lang.Class c)
      Determines if the supplied Class object represents an annotation type.
      static boolean isAnnotationPresent​(java.lang.Class c, java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
      Returns true if the supplied class includes an annotation of the given type.
      static boolean isArray​(java.lang.Class c)
      Determines if the supplied Class object represents an array class.
      static boolean isAssignableFrom​(java.lang.Class c1, java.lang.Class c2)
      Determines if the class or interface represented by first Class parameter is either the same as, or is a superclass or superinterface of, the class or interface represented by the second Class parameter.
      static boolean isEnum​(java.lang.Class c)
      Determines if the supplied Class object represents an enum type.
      static boolean isInstance​(java.lang.Class c, java.lang.Object obj)
      Determines if the supplied Object is assignment-compatible with the object represented by supplied Class.
      static boolean isInterface​(java.lang.Class c)
      Determines if the supplied Class object represents an interface type.
      static boolean isMemberClass​(java.lang.Class c)
      Returns true if the class or interface represented by the supplied Class is a member class.
      static boolean isPrimitive​(java.lang.Class c)
      Determines if the supplied Class object represents a primitive type.
      static boolean isStaticClass​(java.lang.Class c)
      Returns true if the class or interface represented by the supplied Class is a static class.
      static <T> T newInstance​(java.lang.Class<T> c)
      Creates a new instance of the class represented by the supplied Class.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ClassReflection

        public ClassReflection()
    • Method Detail

      • forName

        public static java.lang.Class forName​(java.lang.String name)
                                       throws ReflectionException
        Returns the Class object associated with the class or interface with the supplied string name.
        Throws:
        ReflectionException
      • getSimpleName

        public static java.lang.String getSimpleName​(java.lang.Class c)
        Returns the simple name of the underlying class as supplied in the source code.
      • isInstance

        public static boolean isInstance​(java.lang.Class c,
                                         java.lang.Object obj)
        Determines if the supplied Object is assignment-compatible with the object represented by supplied Class.
      • isAssignableFrom

        public static boolean isAssignableFrom​(java.lang.Class c1,
                                               java.lang.Class c2)
        Determines if the class or interface represented by first Class parameter is either the same as, or is a superclass or superinterface of, the class or interface represented by the second Class parameter.
      • isMemberClass

        public static boolean isMemberClass​(java.lang.Class c)
        Returns true if the class or interface represented by the supplied Class is a member class.
      • isStaticClass

        public static boolean isStaticClass​(java.lang.Class c)
        Returns true if the class or interface represented by the supplied Class is a static class.
      • isArray

        public static boolean isArray​(java.lang.Class c)
        Determines if the supplied Class object represents an array class.
      • isPrimitive

        public static boolean isPrimitive​(java.lang.Class c)
        Determines if the supplied Class object represents a primitive type.
      • isEnum

        public static boolean isEnum​(java.lang.Class c)
        Determines if the supplied Class object represents an enum type.
      • isAnnotation

        public static boolean isAnnotation​(java.lang.Class c)
        Determines if the supplied Class object represents an annotation type.
      • isInterface

        public static boolean isInterface​(java.lang.Class c)
        Determines if the supplied Class object represents an interface type.
      • isAbstract

        public static boolean isAbstract​(java.lang.Class c)
        Determines if the supplied Class object represents an abstract type.
      • newInstance

        public static <T> T newInstance​(java.lang.Class<T> c)
                                 throws ReflectionException
        Creates a new instance of the class represented by the supplied Class.
        Throws:
        ReflectionException
      • getComponentType

        public static java.lang.Class getComponentType​(java.lang.Class c)
        Returns the Class representing the component type of an array. If this class does not represent an array class this method returns null.
      • getConstructors

        public static Constructor[] getConstructors​(java.lang.Class c)
        Returns an array of Constructor containing the public constructors of the class represented by the supplied Class.
      • getDeclaredConstructor

        public static Constructor getDeclaredConstructor​(java.lang.Class c,
                                                         java.lang.Class... parameterTypes)
                                                  throws ReflectionException
        Returns a Constructor that represents the constructor for the supplied class which takes the supplied parameter types.
        Throws:
        ReflectionException
      • getEnumConstants

        public static java.lang.Object[] getEnumConstants​(java.lang.Class c)
        Returns the elements of this enum class or null if this Class object does not represent an enum type.
      • getMethods

        public static Method[] getMethods​(java.lang.Class c)
        Returns an array of Method containing the public member methods of the class represented by the supplied Class.
      • getMethod

        public static Method getMethod​(java.lang.Class c,
                                       java.lang.String name,
                                       java.lang.Class... parameterTypes)
                                throws ReflectionException
        Returns a Method that represents the public member method for the supplied class which takes the supplied parameter types.
        Throws:
        ReflectionException
      • getDeclaredMethods

        public static Method[] getDeclaredMethods​(java.lang.Class c)
        Returns an array of Method containing the methods declared by the class represented by the supplied Class.
      • getDeclaredMethod

        public static Method getDeclaredMethod​(java.lang.Class c,
                                               java.lang.String name,
                                               java.lang.Class... parameterTypes)
                                        throws ReflectionException
        Returns a Method that represents the method declared by the supplied class which takes the supplied parameter types.
        Throws:
        ReflectionException
      • getFields

        public static Field[] getFields​(java.lang.Class c)
        Returns an array of Field containing the public fields of the class represented by the supplied Class.
      • getDeclaredFields

        public static Field[] getDeclaredFields​(java.lang.Class c)
        Returns an array of Field objects reflecting all the fields declared by the supplied class.
      • getDeclaredField

        public static Field getDeclaredField​(java.lang.Class c,
                                             java.lang.String name)
                                      throws ReflectionException
        Returns a Field that represents the specified declared field for the supplied class.
        Throws:
        ReflectionException
      • isAnnotationPresent

        public static boolean isAnnotationPresent​(java.lang.Class c,
                                                  java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
        Returns true if the supplied class includes an annotation of the given type.
      • getAnnotations

        public static Annotation[] getAnnotations​(java.lang.Class c)
        Returns an array of Annotation objects reflecting all annotations declared by the supplied class, and inherited from its superclass. Returns an empty array if there are none.
      • getAnnotation

        public static Annotation getAnnotation​(java.lang.Class c,
                                               java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
        Returns an Annotation object reflecting the annotation provided, or null if this class doesn't have such an annotation. This is a convenience function if the caller knows already which annotation type he's looking for.
      • getDeclaredAnnotations

        public static Annotation[] getDeclaredAnnotations​(java.lang.Class c)
        Returns an array of Annotation objects reflecting all annotations declared by the supplied class, or an empty array if there are none. Does not include inherited annotations.
      • getDeclaredAnnotation

        public static Annotation getDeclaredAnnotation​(java.lang.Class c,
                                                       java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
        Returns an Annotation object reflecting the annotation provided, or null if this class doesn't have such an annotation. This is a convenience function if the caller knows already which annotation type he's looking for.
      • getInterfaces

        public static java.lang.Class[] getInterfaces​(java.lang.Class c)