Package org.smooks.support
Class ClassUtils
- java.lang.Object
-
- org.smooks.support.ClassUtils
-
public final class ClassUtils extends Object
Utility methods to aid in class/resource loading.- Author:
- Kevin Conner
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static booleancontainsAssignableClass(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> ObjectgetField(Field field, U instance)static MethodgetGetterMethod(String getterName, Object bean, Class<?> returnType)static MethodgetGetterMethodByProperty(String propertyName, Class<?> beanClass, Class<?> returnType)static StringgetLongMemberName(Member field)static InputStreamgetResourceAsStream(String resourceName, Class<?> caller)Get the specified resource as a stream.static InputStreamgetResourceAsStream(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 MethodgetSetterMethod(String setterName, Class beanclass, Class<?> setterParamType)static MethodgetSetterMethod(String setterName, Object bean, Class<?> setterParamType)static intindexOfFirstAssignableClass(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 ObjectnewProxyInstance(Class[] classes, InvocationHandler handler)static <U> voidsetField(Field field, U instance, Object value)static StringtoClassName(String fileName)Convert the Java-class-file-name to the equivalent Java-class-name (dot delimited package name).static StringtoFilePath(Package aPackage)static StringtoGetterName(String property)static StringtoIsGetterName(String property)static StringtoSetterName(String property)
-
-
-
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)
-
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.
-
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 whichtoFindshould be checked.- Returns:
- true if
toFindis 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 whichtoFindshould be checked.- Returns:
- The array index of the first class within
classesto whichtoFindis assignable, if it can be assigned to one of the specified classes,-1otherwise.
-
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)
-
toClassName
public static String toClassName(String fileName)
Convert the Java-class-file-name to the equivalent Java-class-name (dot delimited package name). EG:
a/b/c/X.class converts to a.b.c.X
a/b/c/X converts to a.b.c.X
a.b.c.X converts to a.b.c.X
a.b.c.X.class converts to a.b.c.X- Parameters:
fileName- The file name String to be translated.- Returns:
- Java Class runtime name representation of the supplied file name String.
-
-