Class Classes


  • public class Classes
    extends java.lang.Object
    Utilities for manipulating Java classes.
    Author:
    Garret Wilson
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String CLASS_NAME_EXTENSION
      The name extension for Java class files.
      static java.util.Comparator<NameValuePair<java.lang.Class<?>,​java.lang.Integer>> CONCRETE_CLASS_HEIGHT_COMPARATOR
      A comparator that sorts ancestor classes primarily in terms of height (distance from a descendant class), secondarily in terms of concreteness (concrete class, abstract class, and then interface), and tertiarily by class name.
      static java.lang.String GET_GETTER_PREFIX
      The getter prefix "get".
      static java.util.regex.Pattern GETTER_METHOD_NAME_PATTERN
      The pattern recognizing a getter method name: "get" or "is" followed by any other characters (assuming they are Java characters), with the prefix in matching group 1 and the property name in matching group 2.
      static java.lang.String IS_GETTER_PREFIX
      The getter prefix "is".
      static java.util.Set<java.lang.Class<?>> PRIMITIVE_WRAPPER_CLASSES
      The set of classes that wrap primitive types.
      static char RESOURCE_PATH_SEPARATOR
      The slash character ('/') that separates components in a resource path.
      static java.lang.String SET_SETTER_PREFIX
      The getter prefix "set".
      static java.util.regex.Pattern SETTER_METHOD_NAME_PATTERN
      The pattern recognizing a setter method name: "set" followed by any other characters (assuming they are Java characters), with the prefix in matching group 1 and the property name in matching group 2.
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static java.lang.Class<?> asClass​(java.net.URI resourceURI)
      Determines the Java class represented by the given URI.
      static java.net.URI createJavaURI​(java.lang.Class<?> objectClass)
      Creates a Java URI for a Java class using the "java" scheme in the form java:/com/example/package/Class.
      static java.net.URI createJavaURI​(java.lang.String objectClassName)
      Creates a Java URI for a named Java class using the "java" scheme in the form java:/com/example/package/Class.
      static java.util.Collection<java.lang.reflect.Method> gatherAccessibleMethods​(java.lang.Class<?> objectClass)
      Gathers all declared methods in this class and its ancestors that could potentially be accessible to the class, including all public and protected methods from the class and all its parents.
      protected static void gatherAccessibleMethods​(java.lang.Class<?> objectClass, java.util.Map<MethodSignature,​java.lang.reflect.Method> methods, boolean includePrivateMethods)
      Gathers all declared methods in this class and its ancestors that could potentially be accessible to the class, including all public and protected methods from the class and all its parents.
      static java.util.List<java.lang.Class<?>> getAncestorClasses​(java.lang.Class<?> objectClass)
      Determines all super classes and interfaces of the given class, including the given class itself.
      protected static <R> void getAncestorClasses​(java.lang.Class<? extends R> objectClass, int height, java.lang.Class<R> rootClass, boolean includeSuperClasses, boolean includeAbstract, boolean includeInterfaces, java.util.Map<java.lang.Class<? extends R>,​NameValuePair<java.lang.Class<? extends R>,​java.lang.Integer>> classHeightMap)
      Determines super classes and interfaces of the given class.
      static <R> java.util.List<java.lang.Class<? extends R>> getAncestorClasses​(java.lang.Class<? extends R> objectClass, java.lang.Class<R> rootClass)
      Determines all super classes and interfaces of the given class, including the given class itself, up to and including the given class.
      static <R> java.util.List<java.lang.Class<? extends R>> getAncestorClasses​(java.lang.Class<? extends R> objectClass, java.lang.Class<R> rootClass, boolean includeThisClass, boolean includeSuperClasses, boolean includeAbstract, boolean includeInterfaces, java.util.Comparator<NameValuePair<java.lang.Class<? extends R>,​java.lang.Integer>> comparator)
      Determines all super classes and interfaces of the given class.
      protected static <T> java.lang.reflect.Constructor<T>[] getCompatibleConstructors​(java.lang.Class<T> objectClass, boolean requirePublic, java.lang.Class<?>... parameterTypes)
      Returns all constructors of a class that are compatible with the given parameter types.
      static <T> java.lang.reflect.Constructor<T> getCompatibleDeclaredConstructor​(java.lang.Class<T> objectClass, java.lang.Class<?>... parameterTypes)
      Returns a constructor of a class that is compatible with the given parameter types, regardless of its visibility.
      static <T> java.lang.reflect.Constructor<T>[] getCompatibleDeclaredConstructors​(java.lang.Class<T> objectClass, java.lang.Class<?>... parameterTypes)
      Returns all constructors, even protected and private constructors, of a class that are compatible with the given parameter types.
      static <T> java.lang.reflect.Constructor<T> getCompatiblePublicConstructor​(java.lang.Class<T> objectClass, java.lang.Class<?>... parameterTypes)
      Returns a public constructor of a class that is compatible with the given parameter types.
      static <T> java.lang.reflect.Constructor<T>[] getCompatiblePublicConstructors​(java.lang.Class<T> objectClass, java.lang.Class<?>... parameterTypes)
      Returns all public constructors of a class that are compatible with the given parameter types.
      static java.lang.reflect.Method getCompatibleSetterMethod​(java.lang.Class<?> objectClass, java.lang.String propertyName, java.lang.Class<?> valueClass)
      Returns a setter method compatible with a given value type, i.e.
      static <T> java.lang.reflect.Constructor<T> getConstructor​(java.lang.Class<T> objectClass, java.lang.Class<?>... parameterTypes)
      Finds a defined constructor of a class.
      static <T> java.lang.reflect.Constructor<T> getDeclaredConstructor​(java.lang.Class<T> objectClass, java.lang.Class<?>... parameterTypes)
      Finds a defined constructor of a class.
      static <T> java.lang.reflect.Constructor<T> getDeclaredDefaultConstructor​(java.lang.Class<T> objectClass)
      Convenience function to locate and return the default constructor of a particular class.
      static java.lang.String getFullName​(java.lang.Class<?> objectClass, java.lang.String localName)
      Creates a full name analogous to a class name from the package of an existing class.
      static java.lang.reflect.Method getGetPropertyMethod​(java.lang.Class<?> objectClass, java.lang.String propertyName)
      Returns the "get" getter method of a given class.
      static java.lang.String getGetPropertyMethodName​(java.lang.String propertyName)
      The name of the "get" getter method corresponding to the given property.
      static java.lang.reflect.Method getGetterMethod​(java.lang.Class<?> objectClass, java.lang.String propertyName)
      Returns the getter method of a given class.
      static java.lang.String getGetterPropertyName​(java.lang.reflect.Method method)
      Determines the property name of the given getter method.
      static java.lang.String getGetterPropertyName​(java.lang.String methodName)
      Determines the property name of the given getter method name.
      static java.lang.reflect.Method getIsPropertyMethod​(java.lang.Class<?> objectClass, java.lang.String propertyName)
      Returns the "is" getter method of a given class.
      static java.lang.String getIsPropertyMethodName​(java.lang.String propertyName)
      The name of the "is" getter method corresponding to the given property.
      static java.lang.String getLocalName​(java.lang.Class<?> objectClass)
      Returns the local name of the class, with the package name removed.
      static java.lang.reflect.Method getMethod​(java.lang.Class<?> objectClass, java.lang.String name, java.lang.Class<?>... parameterTypes)
      Returns a Method object that reflects the specified public member method of the class or interface represented by this Class object.
      static java.lang.String getMethodName​(java.lang.Class<?> objectClass, java.lang.String methodLocalName)
      Creates a full method name by appending the method local name to the full class name.
      static java.lang.String getMethodName​(java.lang.String className, java.lang.String methodLocalName)
      Creates a full method name by appending the method local name to the full class name.
      static ContentType getObjectContentType​(java.lang.Class<?> objectClass)
      Returns a content type identifying an object of the given class in the form application/x-java-object;class=package.Class.
      static java.util.List<java.lang.Class<?>> getProperAncestorClasses​(java.lang.Class<?> objectClass)
      Determines all super classes and interfaces of the given class, excluding the given class itself.
      static <R> java.util.List<java.lang.Class<? extends R>> getProperAncestorClasses​(java.lang.Class<? extends R> objectClass, java.lang.Class<R> rootClass)
      Determines all super classes and interfaces of the given class, excluding the given class itself, up to and including the given class.
      static java.lang.String getPropertyName​(java.lang.Class<?> objectClass, java.lang.String localName)
      Creates a property name by appending the property local name to the full class name.
      static java.lang.String getPropertyName​(java.lang.String className, java.lang.String localName)
      Creates a property name by appending the property local name to the full class name.
      static <T> java.lang.reflect.Constructor<T> getPublicDefaultConstructor​(java.lang.Class<T> objectClass)
      Convenience function to locate and return the public default constructor of a particular class.
      static java.io.File getResource​(java.lang.Class<?> objectClass, java.lang.String name)
      Provides access to a resource in the classpath via a file object.
      static java.lang.String getResourceBasePath​(java.lang.Class<?> contextClass)
      Determines the base path necessary to access a named resource using the class loader of the given context class.
      static java.util.Optional<java.lang.String> getResourceName​(java.lang.String resourcePath)
      Retrieves the filename for a resource given its path.
      static java.lang.String getSetPropertyMethodName​(java.lang.String propertyName)
      The name of the "set" setter method corresponding to the given property.
      static java.lang.reflect.Method getSetterMethod​(java.lang.Class<?> objectClass, java.lang.String propertyName, java.lang.Class<?> valueClass)
      Returns the setter method of a given class.
      static java.lang.String getSetterPropertyName​(java.lang.reflect.Method method)
      Determines the property name of the given getter method.
      static java.lang.String getSetterPropertyName​(java.lang.String methodName)
      Determines the property name of the given getter method name.
      static java.lang.String getSimpleName​(java.lang.Class<?> objectClass)
      Returns the local name of the class, with the package name removed.
      static java.lang.String getVariableName​(java.lang.Class<?> objectClass)
      Constructs a variable name from the class by by decapitalizing all of the beginning uppercase letters of the simple name of the class.
      static boolean isCompatible​(java.lang.Class<?> objectClass, java.lang.Class<?> compatibleClass)
      Deprecated.
      Moved to io.ploop.reflect.Reflector.isReflectionAssignableFrom()
      static boolean isGetterMethod​(java.lang.reflect.Method method)
      Determines if the given method is a getter method.
      static boolean isGetterMethodName​(java.lang.String methodName)
      Determines if the given method name is that of a getter method.
      static boolean isPrimitiveWrapper​(java.lang.Class<?> objectClass)
      Determines whether the given class is a wrapper of one of the primitive types.
      static boolean isSetterMethod​(java.lang.reflect.Method method)
      Determines if the given method is a setter method.
      static boolean isSetterMethodName​(java.lang.String methodName)
      Determines if the given method name is that of a setter method.
      static <T> T readResource​(java.lang.Class<?> objectClass, java.lang.String name, IO<T> io)
      Reads a class resource using the given class' class loader and the given I/O support.
      static java.lang.String resolveResourcePath​(java.lang.Class<?> contextClass, java.lang.String resourceName)
      Determines the path necessary to access a named resource using the class loader of the given context class.
      • Methods inherited from class java.lang.Object

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

      • PRIMITIVE_WRAPPER_CLASSES

        public static final java.util.Set<java.lang.Class<?>> PRIMITIVE_WRAPPER_CLASSES
        The set of classes that wrap primitive types.
      • CLASS_NAME_EXTENSION

        public static final java.lang.String CLASS_NAME_EXTENSION
        The name extension for Java class files.
        See Also:
        Constant Field Values
      • GET_GETTER_PREFIX

        public static final java.lang.String GET_GETTER_PREFIX
        The getter prefix "get".
        See Also:
        Constant Field Values
      • IS_GETTER_PREFIX

        public static final java.lang.String IS_GETTER_PREFIX
        The getter prefix "is".
        See Also:
        Constant Field Values
      • SET_SETTER_PREFIX

        public static final java.lang.String SET_SETTER_PREFIX
        The getter prefix "set".
        See Also:
        Constant Field Values
      • GETTER_METHOD_NAME_PATTERN

        public static final java.util.regex.Pattern GETTER_METHOD_NAME_PATTERN
        The pattern recognizing a getter method name: "get" or "is" followed by any other characters (assuming they are Java characters), with the prefix in matching group 1 and the property name in matching group 2.
      • SETTER_METHOD_NAME_PATTERN

        public static final java.util.regex.Pattern SETTER_METHOD_NAME_PATTERN
        The pattern recognizing a setter method name: "set" followed by any other characters (assuming they are Java characters), with the prefix in matching group 1 and the property name in matching group 2.
      • RESOURCE_PATH_SEPARATOR

        public static final char RESOURCE_PATH_SEPARATOR
        The slash character ('/') that separates components in a resource path.
        See Also:
        Constant Field Values
      • CONCRETE_CLASS_HEIGHT_COMPARATOR

        public static final java.util.Comparator<NameValuePair<java.lang.Class<?>,​java.lang.Integer>> CONCRETE_CLASS_HEIGHT_COMPARATOR
        A comparator that sorts ancestor classes primarily in terms of height (distance from a descendant class), secondarily in terms of concreteness (concrete class, abstract class, and then interface), and tertiarily by class name.
    • Method Detail

      • asClass

        public static java.lang.Class<?> asClass​(java.net.URI resourceURI)
                                          throws java.lang.ClassNotFoundException
        Determines the Java class represented by the given URI. A URI represents a Java class if it has a "java" scheme in the form java:/com/example/package/Class.
        Parameters:
        resourceURI - The URI which is expected to represent a Java class, or null.
        Returns:
        The Java class represented by the given URI, or null if the URI is not a java: URI.
        Throws:
        java.lang.IllegalArgumentException - if the given URI represents a Java class that does not have the correct syntax, e.g. it does not have an absolute non-collection path.
        java.lang.ClassNotFoundException - if the class represented by the given URI could not be found.
        See Also:
        Java.JAVA_URI_SCHEME
      • createJavaURI

        public static java.net.URI createJavaURI​(java.lang.Class<?> objectClass)
        Creates a Java URI for a Java class using the "java" scheme in the form java:/com/example/package/Class.
        Parameters:
        objectClass - The class to use in creating the java: URI.
        Returns:
        A java: URI based upon the given class.
        Throws:
        java.lang.NullPointerException - if the given class is null.
      • createJavaURI

        public static java.net.URI createJavaURI​(java.lang.String objectClassName)
        Creates a Java URI for a named Java class using the "java" scheme in the form java:/com/example/package/Class.
        Parameters:
        objectClassName - The name of the class class to use in creating the java: URI.
        Returns:
        A java: URI based upon the given class.
        Throws:
        java.lang.NullPointerException - if the given class name is null.
      • getObjectContentType

        public static ContentType getObjectContentType​(java.lang.Class<?> objectClass)
        Returns a content type identifying an object of the given class in the form application/x-java-object;class=package.Class.
        Parameters:
        objectClass - The class for which a content type should be returned.
        Returns:
        A content type identifying an object of the given class in the form application/x-java-object;class=package.Class.
        Throws:
        java.lang.IllegalArgumentException - if the given object class is null.
      • getCompatiblePublicConstructor

        public static <T> java.lang.reflect.Constructor<T> getCompatiblePublicConstructor​(java.lang.Class<T> objectClass,
                                                                                          java.lang.Class<?>... parameterTypes)
                                                                                   throws java.lang.SecurityException
        Returns a public constructor of a class that is compatible with the given parameter types. A constructor is considered compatible if each of the given parameter types can be assigned to the formal parameter type in the constructor. A constructor is first located the formal parameter types of which match the given parameters. If that fails, a compatible constructor is located.
        Type Parameters:
        T - The type of class.
        Parameters:
        objectClass - The class for which compatible constructors should be returned.
        parameterTypes - The types of parameters to be used.
        Returns:
        A compatible constructors, or null if no compatible constructor could be found.
        Throws:
        java.lang.SecurityException - If a security manager is present that denies access to the constructor or the caller's class loader is different and denies access to the package of this class.
      • getCompatiblePublicConstructors

        public static <T> java.lang.reflect.Constructor<T>[] getCompatiblePublicConstructors​(java.lang.Class<T> objectClass,
                                                                                             java.lang.Class<?>... parameterTypes)
                                                                                      throws java.lang.SecurityException
        Returns all public constructors of a class that are compatible with the given parameter types. A constructor is considered compatible if each of the given parameter types can be assigned to the formal parameter type in the constructor.
        Type Parameters:
        T - The type of class.
        Parameters:
        objectClass - The class for which compatible constructors should be returned.
        parameterTypes - The types of parameters to be used.
        Returns:
        An array of compatible constructors.
        Throws:
        java.lang.SecurityException - If a security manager is present that denies access to the constructor or the caller's class loader is different and denies access to the package of this class.
      • getCompatibleDeclaredConstructor

        public static <T> java.lang.reflect.Constructor<T> getCompatibleDeclaredConstructor​(java.lang.Class<T> objectClass,
                                                                                            java.lang.Class<?>... parameterTypes)
                                                                                     throws java.lang.SecurityException
        Returns a constructor of a class that is compatible with the given parameter types, regardless of its visibility. A constructor is considered compatible if each of the given parameter types can be assigned to the formal parameter type in the constructor. A constructor is first located the formal parameter types of which match the given parameters. If that fails, a compatible constructor is located.
        Type Parameters:
        T - The type of class.
        Parameters:
        objectClass - The class for which compatible constructors should be returned.
        parameterTypes - The types of parameters to be used.
        Returns:
        A compatible constructors, or null if no compatible constructor could be found.
        Throws:
        java.lang.SecurityException - If a security manager is present that denies access to the constructor or the caller's class loader is different and denies access to the package of this class.
      • getCompatibleDeclaredConstructors

        public static <T> java.lang.reflect.Constructor<T>[] getCompatibleDeclaredConstructors​(java.lang.Class<T> objectClass,
                                                                                               java.lang.Class<?>... parameterTypes)
                                                                                        throws java.lang.SecurityException
        Returns all constructors, even protected and private constructors, of a class that are compatible with the given parameter types. A constructor is considered compatible if each of the given parameter types can be assigned to the formal parameter type in the constructor.
        Type Parameters:
        T - The type of class.
        Parameters:
        objectClass - The class for which compatible constructors should be returned.
        parameterTypes - The types of parameters to be used.
        Returns:
        An array of compatible constructors.
        Throws:
        java.lang.SecurityException - If a security manager is present that denies access to the constructor or the caller's class loader is different and denies access to the package of this class.
      • getCompatibleConstructors

        protected static <T> java.lang.reflect.Constructor<T>[] getCompatibleConstructors​(java.lang.Class<T> objectClass,
                                                                                          boolean requirePublic,
                                                                                          java.lang.Class<?>... parameterTypes)
                                                                                   throws java.lang.SecurityException
        Returns all constructors of a class that are compatible with the given parameter types. A constructor is considered compatible if each of the given parameter types can be assigned to the formal parameter type in the constructor.
        Type Parameters:
        T - The type of class.
        Parameters:
        objectClass - The class for which compatible constructors should be returned.
        requirePublic - Whether only public constructors should be returned.
        parameterTypes - The types of parameters to be used.
        Returns:
        An array of compatible constructors.
        Throws:
        java.lang.SecurityException - If a security manager is present that denies access to the constructor or the caller's class loader is different and denies access to the package of this class.
      • isCompatible

        @Deprecated
        public static boolean isCompatible​(java.lang.Class<?> objectClass,
                                           java.lang.Class<?> compatibleClass)
        Deprecated.
        Moved to io.ploop.reflect.Reflector.isReflectionAssignableFrom()
        Determines if a class is compatible with a given class. This method functions identically to Class.isAssignableFrom(Class), except that it allows the compatible class to be a non-primitive representation (e.g. Integer) of a primitive type (e.g. int). This method is useful for determining if some type is compatible with a method signature.
        Parameters:
        objectClass - The class with which compatibility is being determined.
        compatibleClass - The class the compatibility of which is questioned.
        Returns:
        true if the given class is compatible with the object class.
      • isPrimitiveWrapper

        public static boolean isPrimitiveWrapper​(java.lang.Class<?> objectClass)
        Determines whether the given class is a wrapper of one of the primitive types. Specifically, those classes are Boolean, Byte, Character, Short, Integer, Long, Float, Double.
        Parameters:
        objectClass - The class to check.
        Returns:
        true if the class is one of the primitive wrapper classes.
        Throws:
        java.lang.NullPointerException - if the given class is null.
      • getDeclaredConstructor

        public static <T> java.lang.reflect.Constructor<T> getDeclaredConstructor​(java.lang.Class<T> objectClass,
                                                                                  java.lang.Class<?>... parameterTypes)
                                                                           throws java.lang.SecurityException
        Finds a defined constructor of a class. This method differs from Class.getDeclaredConstructor(Class...) in that if no matching constructor is found, null is returned rather than a NoSuchMethodException being thrown.
        Type Parameters:
        T - The type of the class for which the constructor will be found.
        Parameters:
        objectClass - The class for which the constructor should be found.
        parameterTypes - The constructor parameters.
        Returns:
        The Method object of the public constructor that matches the specified parameterTypes, or null if no such constructor exists.
        Throws:
        java.lang.SecurityException - If a security manager is present that denies access to the constructor or the caller's class loader is different and denies access to the package of this class.
      • getConstructor

        public static <T> java.lang.reflect.Constructor<T> getConstructor​(java.lang.Class<T> objectClass,
                                                                          java.lang.Class<?>... parameterTypes)
                                                                   throws java.lang.SecurityException
        Finds a defined constructor of a class. This method differs from Class.getConstructor(Class...) in that if no matching constructor is found, null is returned rather than a NoSuchMethodException being thrown.
        Type Parameters:
        T - The type of the class for which the constructor will be found.
        Parameters:
        objectClass - The class for which the constructor should be found.
        parameterTypes - The constructor parameters.
        Returns:
        The Method object of the public constructor that matches the specified parameterTypes, or null if no such constructor exists.
        Throws:
        java.lang.SecurityException - If a security manager is present that denies access to the constructor or the caller's class loader is different and denies access to the package of this class.
      • getDeclaredDefaultConstructor

        public static <T> java.lang.reflect.Constructor<T> getDeclaredDefaultConstructor​(java.lang.Class<T> objectClass)
                                                                                  throws java.lang.SecurityException
        Convenience function to locate and return the default constructor of a particular class. This differs from Class.getConstructor() in that this method returns null instead of throwing a NoSuchMethodException if the given constructor is not found.

        The returned constructor may not be public.

        Type Parameters:
        T - The type of the class for which the constructor will be found.
        Parameters:
        objectClass - The class for which the default constructor should be found.
        Returns:
        The default constructor of the given class, or null if a default constructor does not exist.
        Throws:
        java.lang.SecurityException - Thrown if access to the information is denied.
        See Also:
        Class.getConstructors()
      • getPublicDefaultConstructor

        public static <T> java.lang.reflect.Constructor<T> getPublicDefaultConstructor​(java.lang.Class<T> objectClass)
                                                                                throws java.lang.SecurityException
        Convenience function to locate and return the public default constructor of a particular class. This differs from Class.getConstructor() in that this method returns null instead of throwing a NoSuchMethodException if the given constructor is not found.

        An equivalent call with more exception-handling overhead would be to enclose objectClass.getConstructor(new Class()[]) in a try...catch() block.

        Type Parameters:
        T - The type of the class for which the constructor will be found.
        Parameters:
        objectClass - The class for which the default constructor should be found.
        Returns:
        The default constructor of the given class, or null if a default constructor does not exist.
        Throws:
        java.lang.SecurityException - Thrown if access to the information is denied.
        See Also:
        Class.getConstructors()
      • getMethod

        public static java.lang.reflect.Method getMethod​(java.lang.Class<?> objectClass,
                                                         java.lang.String name,
                                                         java.lang.Class<?>... parameterTypes)
                                                  throws java.lang.SecurityException
        Returns a Method object that reflects the specified public member method of the class or interface represented by this Class object. This method differs from Class.getMethod(String, Class...) in that if no matching method is found, null is returned rather than a NoSuchMethodException being thrown.
        Parameters:
        objectClass - The class for which the method should be found.
        name - The name of the method.
        parameterTypes - The list of parameters.
        Returns:
        The Method object that matches the specified name and parameterTypes, or null if a matching method is not found or if the name is is "<init>"or "<clinit>".
        Throws:
        java.lang.NullPointerException - if name is null
        java.lang.SecurityException - If a security manager is present that denies access to the constructor or the caller's class loader is different and denies access to the package of this class.
        Since:
        JDK1.1
      • gatherAccessibleMethods

        public static java.util.Collection<java.lang.reflect.Method> gatherAccessibleMethods​(java.lang.Class<?> objectClass)
        Gathers all declared methods in this class and its ancestors that could potentially be accessible to the class, including all public and protected methods from the class and all its parents. If a method is declared with the same signature in several places, only the most shallow declaration (that is, the one declared in or closest to the given class) will be included. No private methods above the given class will be retrieved. Only concrete methods will be returned. Synthetic methods and super covariant methods are ignored. The actual accessibility of methods set via Method.setAccessible(boolean) will not be changed.
        Parameters:
        objectClass - The class from which accessible methods will be gathered, along with its ancestors.
        Returns:
        A set of methods potentially accessible to the class.
      • gatherAccessibleMethods

        protected static void gatherAccessibleMethods​(java.lang.Class<?> objectClass,
                                                      java.util.Map<MethodSignature,​java.lang.reflect.Method> methods,
                                                      boolean includePrivateMethods)
        Gathers all declared methods in this class and its ancestors that could potentially be accessible to the class, including all public and protected methods from the class and all its parents. If a method is declared with the same signature in several places, only the most shallow declaration (that is, the one declared in or closest to the given class) will be included. No private methods above the given class will be retrieved; the option to gather private methods only applies to the original class on which this method was invoked. Only concrete methods will be returned. Synthetic methods and super covariant methods are ignored. The actual accessibility of methods set via Method.setAccessible(boolean) will not be changed.
        Parameters:
        objectClass - The class from which accessible methods will be gathered, along with its ancestors.
        methods - The map into which the methods will be gathered.
        includePrivateMethods - Whether private methods should be included; from this class only.
      • getGetPropertyMethod

        public static java.lang.reflect.Method getGetPropertyMethod​(java.lang.Class<?> objectClass,
                                                                    java.lang.String propertyName)
        Returns the "get" getter method of a given class. This method differs from Class.getMethod(String, Class...) in that if no matching method is found, null is returned rather than a NoSuchMethodException being thrown.
        Parameters:
        objectClass - The class for which a getter method should be returned.
        propertyName - The property name, such as "propertyName".
        Returns:
        The method with the name "getPropertyName", or null if such a method was not found.
      • getIsPropertyMethod

        public static java.lang.reflect.Method getIsPropertyMethod​(java.lang.Class<?> objectClass,
                                                                   java.lang.String propertyName)
        Returns the "is" getter method of a given class. This method differs from Class.getMethod(String, Class...) in that if no matching method is found, null is returned rather than a NoSuchMethodException being thrown.
        Parameters:
        objectClass - The class for which a getter method should be returned.
        propertyName - The property name, such as "propertyName".
        Returns:
        The method with the name "isPropertyName" having a Boolean.TYPE return type, or null if such a method was not found.
      • getGetterMethod

        public static java.lang.reflect.Method getGetterMethod​(java.lang.Class<?> objectClass,
                                                               java.lang.String propertyName)
        Returns the getter method of a given class. This method first looks for a method with the name "getPropertyName", and then with the name "isPropertyName" having a Boolean.TYPE return type. This method differs from Class.getMethod(String, Class...) in that if no matching method is found, null is returned rather than a NoSuchMethodException being thrown.
        Parameters:
        objectClass - The class for which a getter method should be returned.
        propertyName - The property name, such as "propertyName".
        Returns:
        The method with the name "getPropertyName", or the name "isPropertyName" having a Boolean.TYPE; or null if such a method was not found.
      • getSetterMethod

        public static java.lang.reflect.Method getSetterMethod​(java.lang.Class<?> objectClass,
                                                               java.lang.String propertyName,
                                                               java.lang.Class<?> valueClass)
        Returns the setter method of a given class. This method differs from Class.getMethod(String, Class...) in that if no matching method is found, null is returned rather than a NoSuchMethodException being thrown.
        Parameters:
        objectClass - The class for which a setter method should be returned.
        propertyName - The property name, such as "propertyName".
        valueClass - The type of property value to be set.
        Returns:
        The method with the name "setPropertyName" and the given value class as a parameter type, or null if such a method was not found.
      • getCompatibleSetterMethod

        public static java.lang.reflect.Method getCompatibleSetterMethod​(java.lang.Class<?> objectClass,
                                                                         java.lang.String propertyName,
                                                                         java.lang.Class<?> valueClass)
        Returns a setter method compatible with a given value type, i.e. that could be used if the value is cast to the setter's parameter type.
        Parameters:
        objectClass - The class for which a setter method should be returned.
        propertyName - The property name, such as "propertyName".
        valueClass - The type of property value to be set.
        Returns:
        The method with the name "setPropertyName" and a single parameter assignment-compatible with the given value class, or null if such a method was not found.
      • isGetterMethod

        public static boolean isGetterMethod​(java.lang.reflect.Method method)
        Determines if the given method is a getter method.
        Parameters:
        method - The method to check
        Returns:
        true if the method has a return type but no parameters, and the name of the method is in the form "getPropertyName" or "isPropertyName".
      • isGetterMethodName

        public static boolean isGetterMethodName​(java.lang.String methodName)
        Determines if the given method name is that of a getter method.
        Parameters:
        methodName - The method name, such as "getPropertyName" or "isPropertyName".
        Returns:
        true if the name of the method is in the form "getPropertyName" or "isPropertyName".
      • isSetterMethod

        public static boolean isSetterMethod​(java.lang.reflect.Method method)
        Determines if the given method is a setter method.
        Parameters:
        method - The method name to check
        Returns:
        true if the method has no return type and a single parameter, and the name of the method is in the form "setPropertyName".
      • isSetterMethodName

        public static boolean isSetterMethodName​(java.lang.String methodName)
        Determines if the given method name is that of a setter method.
        Parameters:
        methodName - The method name, such as "setPropertyName".
        Returns:
        true if the name of the method is in the form "setPropertyName".
      • getGetterPropertyName

        public static java.lang.String getGetterPropertyName​(java.lang.reflect.Method method)
        Determines the property name of the given getter method. If the given method is not in fact a getter method, this method returns null.
        Parameters:
        method - The method the name of which to check.
        Returns:
        The property name in the form propertyName, or null if the name of the method is not in the form "getPropertyName" or "isPropertyName" or the method is not a getter method.
        See Also:
        Method.getName(), isGetterMethod(Method)
      • getGetterPropertyName

        public static java.lang.String getGetterPropertyName​(java.lang.String methodName)
        Determines the property name of the given getter method name.
        Parameters:
        methodName - The method name, such as "getPropertyName" or "isPropertyName".
        Returns:
        The property name in the form propertyName, or null if the name of the method is not in the form "getPropertyName" or "isPropertyName".
      • getSetterPropertyName

        public static java.lang.String getSetterPropertyName​(java.lang.reflect.Method method)
        Determines the property name of the given getter method.
        Parameters:
        method - The method the name of which to check.
        Returns:
        The property name in the form propertyName, or null if the name of the method is not in the form "setPropertyName".
        See Also:
        Method.getName()
      • getSetterPropertyName

        public static java.lang.String getSetterPropertyName​(java.lang.String methodName)
        Determines the property name of the given getter method name.
        Parameters:
        methodName - The method name, such as "setPropertyName".
        Returns:
        The property name in the form propertyName, or null if the name of the method is not in the form "setPropertyName".
      • getGetPropertyMethodName

        public static java.lang.String getGetPropertyMethodName​(java.lang.String propertyName)
        The name of the "get" getter method corresponding to the given property.
        Parameters:
        propertyName - The property name, such as "propertyName".
        Returns:
        The name of the getter method in the form "getPropertyName".
      • getIsPropertyMethodName

        public static java.lang.String getIsPropertyMethodName​(java.lang.String propertyName)
        The name of the "is" getter method corresponding to the given property.
        Parameters:
        propertyName - The property name, such as "propertyName".
        Returns:
        The name of the getter method in the form "isPropertyName".
      • getSetPropertyMethodName

        public static java.lang.String getSetPropertyMethodName​(java.lang.String propertyName)
        The name of the "set" setter method corresponding to the given property.
        Parameters:
        propertyName - The property name, such as "propertyName".
        Returns:
        The name of the setter method in the form "setPropertyName".
      • getPropertyName

        public static java.lang.String getPropertyName​(java.lang.String className,
                                                       java.lang.String localName)
        Creates a property name by appending the property local name to the full class name.
        Parameters:
        className - The full class name.
        localName - The local name of the property.
        Returns:
        A full class name plus property name.
        See Also:
        Packages.getFullName(Package, String)
      • getPropertyName

        public static java.lang.String getPropertyName​(java.lang.Class<?> objectClass,
                                                       java.lang.String localName)
        Creates a property name by appending the property local name to the full class name.
        Parameters:
        objectClass - The class to supply the class name.
        localName - The local name of the property.
        Returns:
        A full class name plus property name.
        See Also:
        Packages.getFullName(Package, String)
      • getMethodName

        public static java.lang.String getMethodName​(java.lang.String className,
                                                     java.lang.String methodLocalName)
        Creates a full method name by appending the method local name to the full class name.
        Parameters:
        className - The full class name.
        methodLocalName - The local name of the method.
        Returns:
        A full class name plus method name.
        See Also:
        getPropertyName(Class, String)
      • getMethodName

        public static java.lang.String getMethodName​(java.lang.Class<?> objectClass,
                                                     java.lang.String methodLocalName)
        Creates a full method name by appending the method local name to the full class name.
        Parameters:
        objectClass - The class to supply the class name.
        methodLocalName - The local name of the method.
        Returns:
        A full class name plus method name.
        See Also:
        getPropertyName(Class, String)
      • getFullName

        public static java.lang.String getFullName​(java.lang.Class<?> objectClass,
                                                   java.lang.String localName)
        Creates a full name analogous to a class name from the package of an existing class. For example, a class of com.example.Foo and a local name of Bar will result in a full name of com.example.Bar.
        Parameters:
        objectClass - The class to supply the package name.
        localName - The local name for constructing the full name within the package.
        Returns:
        A full class name in the package of the given class with the given local name.
        See Also:
        Packages.getFullName(Package, String)
      • getLocalName

        public static java.lang.String getLocalName​(java.lang.Class<?> objectClass)
        Returns the local name of the class, with the package name removed. Therefore com.example.Foo$Bar becomes Foo$Bar.
        Parameters:
        objectClass - The class for which a local name should be returned.
        Returns:
        The local name of the class within its package.
        See Also:
        getSimpleName(java.lang.Class<?>)
      • getSimpleName

        public static java.lang.String getSimpleName​(java.lang.Class<?> objectClass)
        Returns the local name of the class, with the package name removed. If the class represents an internal class, the external class name is removed as well. Therefore com.example.Foo$Bar becomes Bar.
        Parameters:
        objectClass - The class for which a simple name should be returned.
        Returns:
        The simple name of the class within its package, if any, and within its enclosing class, if any.
        See Also:
        getLocalName(java.lang.Class<?>)
      • getVariableName

        public static java.lang.String getVariableName​(java.lang.Class<?> objectClass)
        Constructs a variable name from the class by by decapitalizing all of the beginning uppercase letters of the simple name of the class.
        Parameters:
        objectClass - The class for which a variable name should be returned.
        Returns:
        A variable name appropriate for the class.
        See Also:
        getSimpleName(Class), Java.getVariableName(String)
      • getAncestorClasses

        public static java.util.List<java.lang.Class<?>> getAncestorClasses​(java.lang.Class<?> objectClass)
        Determines all super classes and interfaces of the given class, including the given class itself. Classes will be sorted primarily in terms of maximum height (distance from a descendant class), secondarily in terms of concreteness (concrete class, abstract class, and then interface), and tertiarialy by class name, in increasing order of height and abstractness.
        Parameters:
        objectClass - The class for which super classes and interfaces should be found.
        Returns:
        The set of all super classes and implemented interfaces.
        Throws:
        java.lang.NullPointerException - if the given object class is null.
        See Also:
        CONCRETE_CLASS_HEIGHT_COMPARATOR
      • getProperAncestorClasses

        public static java.util.List<java.lang.Class<?>> getProperAncestorClasses​(java.lang.Class<?> objectClass)
        Determines all super classes and interfaces of the given class, excluding the given class itself. Classes will be sorted primarily in terms of maximum height (distance from a descendant class), secondarily in terms of concreteness (concrete class, abstract class, and then interface), and tertiarialy by class name, in increasing order of height and abstractness.
        Parameters:
        objectClass - The class for which super classes and interfaces should be found.
        Returns:
        The set of all super classes and implemented interfaces.
        Throws:
        java.lang.NullPointerException - if the given object class is null.
        See Also:
        CONCRETE_CLASS_HEIGHT_COMPARATOR
      • getAncestorClasses

        public static <R> java.util.List<java.lang.Class<? extends R>> getAncestorClasses​(java.lang.Class<? extends R> objectClass,
                                                                                          java.lang.Class<R> rootClass)
        Determines all super classes and interfaces of the given class, including the given class itself, up to and including the given class. Classes will be sorted primarily in terms of maximum height (distance from a descendant class), secondarily in terms of concreteness (concrete class, abstract class, and then interface), and tertiarialy by class name, in increasing order of height and abstractness.
        Type Parameters:
        R - The type of root class.
        Parameters:
        objectClass - The class for which super classes and interfaces should be found.
        rootClass - The root class or interface to retrieve, or null if all classes should be retrieved.
        Returns:
        The set of all super classes and implemented interfaces.
        Throws:
        java.lang.NullPointerException - if the given object class and/or root class is null.
        See Also:
        CONCRETE_CLASS_HEIGHT_COMPARATOR
      • getProperAncestorClasses

        public static <R> java.util.List<java.lang.Class<? extends R>> getProperAncestorClasses​(java.lang.Class<? extends R> objectClass,
                                                                                                java.lang.Class<R> rootClass)
        Determines all super classes and interfaces of the given class, excluding the given class itself, up to and including the given class. Classes will be sorted primarily in terms of maximum height (distance from a descendant class), secondarily in terms of concreteness (concrete class, abstract class, and then interface), and tertiarialy by class name, in increasing order of height and abstractness.
        Type Parameters:
        R - The type of root class.
        Parameters:
        objectClass - The class for which super classes and interfaces should be found.
        rootClass - The root class or interface to retrieve, or null if all classes should be retrieved.
        Returns:
        The set of all super classes and implemented interfaces.
        Throws:
        java.lang.NullPointerException - if the given object class and/or root class is null.
        See Also:
        CONCRETE_CLASS_HEIGHT_COMPARATOR
      • getAncestorClasses

        public static <R> java.util.List<java.lang.Class<? extends R>> getAncestorClasses​(java.lang.Class<? extends R> objectClass,
                                                                                          java.lang.Class<R> rootClass,
                                                                                          boolean includeThisClass,
                                                                                          boolean includeSuperClasses,
                                                                                          boolean includeAbstract,
                                                                                          boolean includeInterfaces,
                                                                                          java.util.Comparator<NameValuePair<java.lang.Class<? extends R>,​java.lang.Integer>> comparator)
        Determines all super classes and interfaces of the given class. Classes will be sorted primarily in terms of maximum height (distance from a descendant class), secondarily in terms of concreteness (concrete class, abstract class, and then interface), and tertiarialy by class name, in increasing order of height and abstractness.
        Type Parameters:
        R - The type of root class.
        Parameters:
        objectClass - The class for which super classes and interfaces should be found.
        rootClass - The root class or interface to retrieve.
        includeThisClass - Whether the object class itself should be returned.
        includeSuperClasses - Whether super classes should be returned.
        includeAbstract - Whether abstract classes should be returned.
        includeInterfaces - Whether implemented interfaces should be returned.
        comparator - The strategy for sorting the returned classes, or null if the order of classes is not important.
        Returns:
        The set of all super classes and implemented interfaces.
        Throws:
        java.lang.NullPointerException - if the given object class and/or root class is null.
      • getAncestorClasses

        protected static <R> void getAncestorClasses​(java.lang.Class<? extends R> objectClass,
                                                     int height,
                                                     java.lang.Class<R> rootClass,
                                                     boolean includeSuperClasses,
                                                     boolean includeAbstract,
                                                     boolean includeInterfaces,
                                                     java.util.Map<java.lang.Class<? extends R>,​NameValuePair<java.lang.Class<? extends R>,​java.lang.Integer>> classHeightMap)
        Determines super classes and interfaces of the given class. The returned set will not include the given object class.
        Type Parameters:
        R - The type of root class.
        Parameters:
        objectClass - The class for which super classes and interfaces should be found.
        height - The zero-based distance towards the root away from the original class.
        rootClass - The root class or interface to retrieve.
        includeSuperClasses - Whether super classes should be returned.
        includeAbstract - Whether abstract classes should be returned.
        includeInterfaces - Whether implemented interfaces should be returned.
        classHeightMap - The map of class/height pairs keyed to the class.
      • getResourceBasePath

        public static java.lang.String getResourceBasePath​(@Nonnull
                                                           java.lang.Class<?> contextClass)
        Determines the base path necessary to access a named resource using the class loader of the given context class.
        Parameters:
        contextClass - The class in relation to which the resource name should be resolved.
        Returns:
        The full base path, ending with a path separator, necessary to access resources using the resource loader of the given class.
        See Also:
        resolveResourcePath(Class, String)
      • resolveResourcePath

        public static java.lang.String resolveResourcePath​(@Nonnull
                                                           java.lang.Class<?> contextClass,
                                                           @Nonnull
                                                           java.lang.String resourceName)
        Determines the path necessary to access a named resource using the class loader of the given context class.

        Accessing a resource via e.g. Class.getResource(String) for the class com.example.Foo may be accomplished using a resource name such as "bar", relative to the class package directory structure; but loading the same resource via ClassLoader.getResource(String) using the class loader for the same class requires the full path to the resource, such as com/example/bar. This method determines the full path that would need to be used to access a resource using a class loader for a class. Thus given class com.example.Foo and resource name bar, this method will return "com/example/bar".

        This method performs functionality equivalent to that performed internally to methods such as Class.getResource(String) before they delegate to the class loader.

        Parameters:
        contextClass - The class in relation to which the resource name should be resolved
        resourceName - The name of the resource to access.
        Returns:
        The full path of the resource necessary to access it using the resource loader of the given class.
      • getResourceName

        public static java.util.Optional<java.lang.String> getResourceName​(@Nonnull
                                                                           java.lang.String resourcePath)
        Retrieves the filename for a resource given its path. The filename is guaranteed never to be the empty string.
        Parameters:
        resourcePath - The path to the resource.
        Returns:
        The filename of the resource, or Optional.empty() if the path ends with a separator.
        Throws:
        java.lang.IllegalArgumentException - if the given resource path is empty.
      • getResource

        public static java.io.File getResource​(java.lang.Class<?> objectClass,
                                               java.lang.String name)
                                        throws java.io.IOException
        Provides access to a resource in the classpath via a file object. The rules for searching resources associated with a given class are implemented by the defining class loader of the class. The first time a particular resource is accessed a temporary file is created with the contents of the resource. The temporary file will be deleted when the JVM exits. This method does not guarantee that any two requests for the same resource will result in the same file object or filename. The calling method must not delete the file or modify the file in any way, as the file may be cached and used for subsequent calls to this method.
        Parameters:
        objectClass - The class the class loader of which will be used to provide access to the resource.
        name - The name of the desired resource.
        Returns:
        A file object or null if no resource with the given name is found.
        Throws:
        java.io.IOException - if there is an I/O error accessing the resource.
      • readResource

        public static <T> T readResource​(java.lang.Class<?> objectClass,
                                         java.lang.String name,
                                         IO<T> io)
                                  throws java.io.IOException
        Reads a class resource using the given class' class loader and the given I/O support.
        Type Parameters:
        T - The type of the resource.
        Parameters:
        objectClass - The class relative to which the given resource will be located.
        name - The name of the resource to read.
        io - The I/O support for reading the object.
        Returns:
        The object read from the resource.
        Throws:
        java.lang.NullPointerException - if the given class, name, and/or I/O support is null.
        java.io.IOException - if there is an error reading the data.
        java.io.FileNotFoundException - if the indicated resource does not exist.