Class ReflectUtil


  • public abstract class ReflectUtil
    extends java.lang.Object
    Author:
    Tom Baeyens
    • Constructor Summary

      Constructors 
      Constructor Description
      ReflectUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> T createInstance​(java.lang.Class<? extends T> clazz)  
      static java.lang.ClassLoader getClassLoader()  
      static java.lang.reflect.Field getField​(java.lang.String fieldName, java.lang.Class<?> clazz)
      Returns the field of the given class or null if it doesnt exist.
      static java.lang.reflect.Field getField​(java.lang.String fieldName, java.lang.Object object)
      Returns the field of the given object or null if it doesnt exist.
      static java.lang.reflect.Method getMethod​(java.lang.Class<?> declaringType, java.lang.String methodName, java.lang.Class<?>... parameterTypes)
      Finds a method by name and parameter types.
      static java.net.URL getResource​(java.lang.String name)  
      static java.io.InputStream getResourceAsStream​(java.lang.String name)  
      static java.lang.String getResourceUrlAsString​(java.lang.String name)  
      static java.lang.reflect.Method getSetter​(java.lang.String fieldName, java.lang.Class<?> clazz, java.lang.Class<?> fieldType)
      Returns the setter-method for the given field name or null if no setter exists.
      static java.lang.reflect.Method getSingleSetter​(java.lang.String fieldName, java.lang.Class<?> clazz)
      Returns a setter method based on the fieldName and the java beans setter naming convention or null if none exists.
      static <T> T instantiate​(java.lang.Class<T> type)  
      static java.lang.Object instantiate​(java.lang.String className)  
      static java.lang.Object instantiate​(java.lang.String className, java.lang.Object[] args)  
      static java.lang.Object invoke​(java.lang.Object target, java.lang.String methodName, java.lang.Object[] args)  
      static java.lang.Class<?> loadClass​(java.lang.String className)  
      static <T> java.lang.Class<? extends T> loadClass​(java.lang.String className, java.lang.ClassLoader customClassloader, java.lang.Class<T> clazz)  
      static void setField​(java.lang.reflect.Field field, java.lang.Object object, java.lang.Object value)  
      static java.net.URI urlToURI​(java.net.URL url)
      Converts an url to an uri.
      • Methods inherited from class java.lang.Object

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

      • ReflectUtil

        public ReflectUtil()
    • Method Detail

      • getClassLoader

        public static java.lang.ClassLoader getClassLoader()
      • loadClass

        public static java.lang.Class<?> loadClass​(java.lang.String className)
      • loadClass

        public static <T> java.lang.Class<? extends T> loadClass​(java.lang.String className,
                                                                 java.lang.ClassLoader customClassloader,
                                                                 java.lang.Class<T> clazz)
                                                          throws java.lang.ClassNotFoundException,
                                                                 java.lang.ClassCastException
        Throws:
        java.lang.ClassNotFoundException
        java.lang.ClassCastException
      • getResourceAsStream

        public static java.io.InputStream getResourceAsStream​(java.lang.String name)
      • getResource

        public static java.net.URL getResource​(java.lang.String name)
      • getResourceUrlAsString

        public static java.lang.String getResourceUrlAsString​(java.lang.String name)
      • urlToURI

        public static java.net.URI urlToURI​(java.net.URL url)
        Converts an url to an uri. Escapes whitespaces if needed.
        Parameters:
        url - the url to convert
        Returns:
        the resulting uri
        Throws:
        ProcessEngineException - if the url has invalid syntax
      • instantiate

        public static java.lang.Object instantiate​(java.lang.String className)
      • instantiate

        public static <T> T instantiate​(java.lang.Class<T> type)
      • createInstance

        public static <T> T createInstance​(java.lang.Class<? extends T> clazz)
      • invoke

        public static java.lang.Object invoke​(java.lang.Object target,
                                              java.lang.String methodName,
                                              java.lang.Object[] args)
      • getField

        public static java.lang.reflect.Field getField​(java.lang.String fieldName,
                                                       java.lang.Object object)
        Returns the field of the given object or null if it doesnt exist.
      • getField

        public static java.lang.reflect.Field getField​(java.lang.String fieldName,
                                                       java.lang.Class<?> clazz)
        Returns the field of the given class or null if it doesnt exist.
      • setField

        public static void setField​(java.lang.reflect.Field field,
                                    java.lang.Object object,
                                    java.lang.Object value)
      • getSetter

        public static java.lang.reflect.Method getSetter​(java.lang.String fieldName,
                                                         java.lang.Class<?> clazz,
                                                         java.lang.Class<?> fieldType)
        Returns the setter-method for the given field name or null if no setter exists.
      • getSingleSetter

        public static java.lang.reflect.Method getSingleSetter​(java.lang.String fieldName,
                                                               java.lang.Class<?> clazz)
        Returns a setter method based on the fieldName and the java beans setter naming convention or null if none exists. If multiple setters with different parameter types are present, an exception is thrown. If they have the same parameter type, one of those methods is returned.
      • instantiate

        public static java.lang.Object instantiate​(java.lang.String className,
                                                   java.lang.Object[] args)
      • getMethod

        public static java.lang.reflect.Method getMethod​(java.lang.Class<?> declaringType,
                                                         java.lang.String methodName,
                                                         java.lang.Class<?>... parameterTypes)
        Finds a method by name and parameter types.
        Parameters:
        declaringType - the name of the class
        methodName - the name of the method to look for
        parameterTypes - the types of the parameters