org.milyn.util
Class ClassUtil

java.lang.Object
  extended by org.milyn.util.ClassUtil

public class ClassUtil
extends Object

Utility methods to aid in class/resource loading.

Author:
Kevin Conner

Constructor Summary
ClassUtil()
           
 
Method Summary
static boolean containsAssignableClass(Class<?> toFind, Class<?>... classes)
          Checks if the class in the first parameter is assignable to one of the classes in the second or any later parameter.
static List<Class> findAnnotatedWith(Class<? extends Annotation> type)
           
static List<Class> findAnnotatedWith(Class<? extends Annotation> type, String[] igrnoreList, String[] includeList)
           
static List<Class> findInstancesOf(Class type)
           
static List<Class> findInstancesOf(Class type, String[] igrnoreList, String[] includeList)
           
static Class forName(String className, Class caller)
          Load the specified class.
static List<Field> getAnnotatedFields(Class runtimeClass, Class<? extends Annotation> annotationClass)
           
static
<T> List<Class<T>>
getClasses(String fileName, Class<T> instanceOf)
          Will try to create a List of classes that are listed in the passed in file.
static
<U> Object
getField(Field field, U instance)
           
static Method getGetterMethod(String getterName, Object bean, Class<?> returnType)
           
static Method getGetterMethodByProperty(String propertyName, Class<?> beanClass, Class<?> returnType)
           
static InputStream getResourceAsStream(String resourceName, Class caller)
          Get the specified resource as a stream.
static InputStream getResourceAsStream(String resourceName, ClassLoader classLoader)
          Get the specified resource as a stream.
static List<URL> getResources(String resourcePath, Class<?> caller)
           
static List<URL> getResources(String resourcePath, ClassLoader callerClassLoader)
           
static Method getSetterMethod(String setterName, Class beanclass, Class<?> setterParamType)
           
static Method getSetterMethod(String setterName, Object bean, Class<?> setterParamType)
           
static int indexOfFirstAssignableClass(Class<?> toFind, Class<?>... classes)
          Gets the array index of the first class within an array of classes to which a specified class is assignable.
static Object newProxyInstance(Class[] classes, InvocationHandler handler)
           
static
<U> void
setField(Field field, U instance, Object value)
           
static String toFilePath(Package aPackage)
           
static String toGetterName(String property)
           
static String toIsGetterName(String property)
           
static String toSetterName(String property)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ClassUtil

public ClassUtil()
Method Detail

forName

public static Class forName(String className,
                            Class caller)
                     throws ClassNotFoundException
Load the specified class.

Parameters:
className - The name of the class to load.
caller - The class of the caller.
Returns:
The specified class.
Throws:
ClassNotFoundException - If the class cannot be found.

getResourceAsStream

public static InputStream getResourceAsStream(String resourceName,
                                              Class caller)
Get the specified resource as a stream.

Parameters:
resourceName - The name of the class to load.
caller - The class of the caller.
Returns:
The input stream for the resource or null if not found.

getResourceAsStream

public static InputStream getResourceAsStream(String resourceName,
                                              ClassLoader classLoader)
Get the specified resource as a stream.

Parameters:
resourceName - The name of the class to load.
classLoader - The ClassLoader to use, if the resource is not located via the Thread context ClassLoader.
Returns:
The input stream for the resource or null if not found.

getResources

public static List<URL> getResources(String resourcePath,
                                     Class<?> caller)
                              throws IOException
Throws:
IOException

getResources

public static List<URL> getResources(String resourcePath,
                                     ClassLoader callerClassLoader)
                              throws IOException
Throws:
IOException

findInstancesOf

public static List<Class> findInstancesOf(Class type,
                                          String[] igrnoreList,
                                          String[] includeList)

findInstancesOf

public static List<Class> findInstancesOf(Class type)

findAnnotatedWith

public static List<Class> findAnnotatedWith(Class<? extends Annotation> type,
                                            String[] igrnoreList,
                                            String[] includeList)

findAnnotatedWith

public static List<Class> findAnnotatedWith(Class<? extends Annotation> type)

newProxyInstance

public static Object newProxyInstance(Class[] classes,
                                      InvocationHandler handler)

getClasses

public static <T> List<Class<T>> getClasses(String fileName,
                                            Class<T> instanceOf)
Will try to create a List of classes that are listed in the passed in file. The fileName is expected to be found on the classpath.

Parameters:
fileName - The name of the file containing the list of classes, one class name per line.
instanceOf - The instanceof filter.
Returns:
List> list of the classes contained in the file.

toFilePath

public static String toFilePath(Package aPackage)

containsAssignableClass

public static boolean containsAssignableClass(Class<?> toFind,
                                              Class<?>... classes)
Checks if the class in the first parameter is assignable to one of the classes in the second or any later parameter.

Parameters:
toFind - The class to check for assignment compatibility.
classes - The classes against which toFind should be checked.
Returns:
true if toFind is assignable to any one of the specified classes.

setField

public static <U> void setField(Field field,
                                U instance,
                                Object value)
                     throws IllegalAccessException
Throws:
IllegalAccessException

getField

public static <U> Object getField(Field field,
                                  U instance)
                       throws IllegalAccessException
Throws:
IllegalAccessException

getAnnotatedFields

public static List<Field> getAnnotatedFields(Class runtimeClass,
                                             Class<? extends Annotation> annotationClass)

indexOfFirstAssignableClass

public static int indexOfFirstAssignableClass(Class<?> toFind,
                                              Class<?>... classes)
Gets the array index of the first class within an array of classes to which a specified class is assignable.

Parameters:
toFind - The class to check for assignment compatibility.
classes - The classes against which toFind should be checked.
Returns:
The array index of the first class within classes to which toFind is assignable, if it can be assigned to one of the specified classes, -1 otherwise.

toSetterName

public static String toSetterName(String property)

toGetterName

public static String toGetterName(String property)

toIsGetterName

public static String toIsGetterName(String property)

getSetterMethod

public static Method getSetterMethod(String setterName,
                                     Object bean,
                                     Class<?> setterParamType)

getSetterMethod

public static Method getSetterMethod(String setterName,
                                     Class beanclass,
                                     Class<?> setterParamType)

getGetterMethod

public static Method getGetterMethod(String getterName,
                                     Object bean,
                                     Class<?> returnType)

getGetterMethodByProperty

public static Method getGetterMethodByProperty(String propertyName,
                                               Class<?> beanClass,
                                               Class<?> returnType)


Copyright © 2018. All rights reserved.