Class ClassUtilities


  • public class ClassUtilities
    extends Object
    • Field Detail

      • doWarn

        public static boolean doWarn
    • Constructor Detail

      • ClassUtilities

        public ClassUtilities()
    • Method Detail

      • deepFindField

        public static Field deepFindField​(Class clazz,
                                          String name)
                                   throws NoSuchFieldException
        Search for and return the named field in the current class and in its super classes. If the field is not found, this throws a NoSuchFieldException
        Parameters:
        clazz - the clazz to begin the search for the field in.
        name - the name of the field to find
        Returns:
        the found field
        Throws:
        NoSuchFieldException - is the field is not found
      • isNumericType

        public static boolean isNumericType​(Class clazz)
        Parameters:
        clazz - the type to test
        Returns:
        true if the type is numeric, otherwise false
      • findMethods

        public static Method[] findMethods​(Class<?> clazz,
                                           Class<? extends Annotation>... annotationsToLookFor)
        Inspects a class looking for methods marked with the specified annotation and returns all those methods that are marked.
        Parameters:
        clazz - the class to look into
        annotationsToLookFor - the annotation to check for on the class's methods
        Returns:
        an array of the methods with the specified annotation
      • findFields

        public static Field[] findFields​(Class<?> clazz,
                                         Class<? extends Annotation>... annotationsToLookFor)
        Inspects a class looking for fields marked with the specified annotation and returns all those fields that are marked.
        Parameters:
        clazz - the class to look into
        annotationsToLookFor - the annotation to check for on the class's fields
        Returns:
        an array of the fields with the specified annotation
      • findMethod

        public static Method findMethod​(Class<?> clazz,
                                        String methodName,
                                        Class<?>... params)
        Finds the named method with the specified parameters in the specified class. This will search in superclasses and attempt to match parameters using Class.isAssignable.

        If this fails to find any matches then it will try to convert all the Object primitives in params, if any, to their primitive representations and then redo the search.

        Parameters:
        clazz - the class to find the method in
        methodName - the name of the method
        params - the parameters types of the method, if null then it is assumed the method takes no arguments
        Returns:
        the found method, or null if the method is not found.
      • findFirstMethodWithArgs

        public static Method findFirstMethodWithArgs​(Class clazz,
                                                     String name,
                                                     int argCount)
      • getPropertyName

        public static String getPropertyName​(String name)
      • setValue

        public static boolean setValue​(Object objToSetOn,
                                       String propertyName,
                                       Object value)
      • deepAnnotationCheck

        public static <T extends Annotation> T deepAnnotationCheck​(Method method,
                                                                   Class<T> annotationClass)
                                                            throws SecurityException
        Checks a method for an annotation by checking the method itself and then any interfaces the method's class implements. This allows for finding annotations set on methods in an interface, as these annotations do not carry over to the classes that implement the interface.

        This works by grabbing the passed in method's declaring class and searching its declared interfaces for the method with the same name and parameters. It then will repeat this process on the class's superclass until the current class has no super class.

        An example of what this will find annotations (deeply) on is: interface InterfaceWithAnnotation {

        Parameters:
        method - the method to check for annotations
        annotationClass -
        Returns:
        Throws:
        SecurityException - }

        class ClassImplementingInterface implements InterfaceWithAnnotation { public void methodWithAnnotation() { } }

        Note: this checking only works on public methods.