Class ReflectUtils


  • public class ReflectUtils
    extends java.lang.Object
    Reflection Utility methods
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.util.List<java.lang.String> DEFAULT_PACKAGES  
    • Constructor Summary

      Constructors 
      Constructor Description
      ReflectUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static int addConstrcutors​(java.lang.Class aClass, java.util.List<java.lang.reflect.Member> list)
      Adds all Constructor (from Class.getConstructorCalls) to the list
      static int addFields​(java.lang.Class aClass, java.util.List<java.lang.reflect.Member> list)
      Adds all Fields (from Class.getFields) to the list
      static int addMethods​(java.lang.Class aClass, java.util.List<java.lang.reflect.Member> list)
      Adds all methods (from Class.getMethodCalls) to the list
      static java.lang.StringBuilder addParamsString​(java.lang.StringBuilder call, java.lang.Class[] params)
      Adds the class SimpleNames, comma sepearated and surrounded by paranthesis to the call StringBuffer
      static int addStaticFields​(java.lang.Class aClass, java.util.List<java.lang.reflect.Member> list)
      Adds all static Fields (from Class.getFields) to the list
      static int addStaticMethods​(java.lang.Class aClass, java.util.List<java.lang.reflect.Member> list)
      Adds all static methods (from Class.getMethodCalls) to the list
      static boolean callSetter​(java.lang.Object obj, java.lang.String property, java.lang.Object value)
      Find a setter method for the give object's property and try to call it.
      static java.lang.Class findClass​(java.lang.String className, java.util.List<java.lang.String> packages)
      Attempt to find the given className within any of the packages.
      static java.lang.String[] getConstructorCalls​(java.lang.Class aClass)
      Gets an array of all Constructor calls for the given class
      static java.lang.String getJavaCallString​(java.lang.reflect.Constructor c)
      Convert the constructor to a Java Code String (arguments are replaced by the simple types)
      static java.lang.String getJavaCallString​(java.lang.reflect.Method method)
      Convert the Method to a Java Code String (arguments are replaced by the simple types)
      static java.lang.String[] getMethodCalls​(java.lang.Class aClass)
      Gets a String array of all method calls for the given class
      static java.lang.String getParamsString​(java.lang.Class[] params)
      Return a paranthesis enclosed, comma sepearated String of all SimpleClass names in params.
      • Methods inherited from class java.lang.Object

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

      • DEFAULT_PACKAGES

        public static final java.util.List<java.lang.String> DEFAULT_PACKAGES
    • Constructor Detail

      • ReflectUtils

        public ReflectUtils()
    • Method Detail

      • addMethods

        public static int addMethods​(java.lang.Class aClass,
                                     java.util.List<java.lang.reflect.Member> list)
        Adds all methods (from Class.getMethodCalls) to the list
        Parameters:
        aClass -
        list -
        Returns:
        number of methods added
      • addStaticMethods

        public static int addStaticMethods​(java.lang.Class aClass,
                                           java.util.List<java.lang.reflect.Member> list)
        Adds all static methods (from Class.getMethodCalls) to the list
        Parameters:
        aClass -
        list -
        Returns:
        number of methods added
      • addStaticFields

        public static int addStaticFields​(java.lang.Class aClass,
                                          java.util.List<java.lang.reflect.Member> list)
        Adds all static Fields (from Class.getFields) to the list
        Parameters:
        aClass -
        list -
        Returns:
        number of fields added
      • addFields

        public static int addFields​(java.lang.Class aClass,
                                    java.util.List<java.lang.reflect.Member> list)
        Adds all Fields (from Class.getFields) to the list
        Parameters:
        aClass -
        list -
        Returns:
        number of fields added
      • addConstrcutors

        public static int addConstrcutors​(java.lang.Class aClass,
                                          java.util.List<java.lang.reflect.Member> list)
        Adds all Constructor (from Class.getConstructorCalls) to the list
        Parameters:
        aClass -
        list -
        Returns:
        number of constructors added
      • getJavaCallString

        public static java.lang.String getJavaCallString​(java.lang.reflect.Constructor c)
        Convert the constructor to a Java Code String (arguments are replaced by the simple types)
        Parameters:
        c - Constructor
        Returns:
      • getJavaCallString

        public static java.lang.String getJavaCallString​(java.lang.reflect.Method method)
        Convert the Method to a Java Code String (arguments are replaced by the simple types)
        Parameters:
        method - Method
        Returns:
      • addParamsString

        public static java.lang.StringBuilder addParamsString​(java.lang.StringBuilder call,
                                                              java.lang.Class[] params)
        Adds the class SimpleNames, comma sepearated and surrounded by paranthesis to the call StringBuffer
        Parameters:
        call -
        params -
        Returns:
      • getMethodCalls

        public static java.lang.String[] getMethodCalls​(java.lang.Class aClass)
        Gets a String array of all method calls for the given class
        Parameters:
        aClass -
        Returns:
      • getConstructorCalls

        public static java.lang.String[] getConstructorCalls​(java.lang.Class aClass)
        Gets an array of all Constructor calls for the given class
        Parameters:
        aClass -
        Returns:
      • getParamsString

        public static java.lang.String getParamsString​(java.lang.Class[] params)
        Return a paranthesis enclosed, comma sepearated String of all SimpleClass names in params.
        Parameters:
        params -
        Returns:
      • findClass

        public static java.lang.Class findClass​(java.lang.String className,
                                                java.util.List<java.lang.String> packages)
        Attempt to find the given className within any of the packages. If the class is not found, then null is returned
        Parameters:
        className - Fully or partially qualified classname within any of the packages
        packages - List of packages for search
        Returns:
        CLass object or null if not found.
      • callSetter

        public static boolean callSetter​(java.lang.Object obj,
                                         java.lang.String property,
                                         java.lang.Object value)
        Find a setter method for the give object's property and try to call it. No exceptions are thrown. You typically call this method because either you are sure no exceptions will be thrown, or to silently ignore any that may be thrown. This will also find a setter that accepts an interface that the value implements. This is still not very effcient and should only be called if performance is not of an issue. You can check the return value to see if the call was seuccessful or not.
        Parameters:
        obj - Object to receive the call
        property - property name (without set. First letter will be capitalized)
        value - Value of the property.
        Returns: