public class ClassUtils extends Object
Modifier and Type | Field and Description |
---|---|
protected static Map |
constructorsCache
caching for constructors - using caching, the perf is at least doubled
|
Constructor and Description |
---|
ClassUtils() |
Modifier and Type | Method and Description |
---|---|
static void |
assertClassForJarExistsInClasspath(ClassLoaderResolver clr,
String className,
String jarName)
Convenience method to throw a NucleusUserException if the specified class is not loadable
from the ClassLoaderResolver.
|
static boolean |
classesAreDescendents(ClassLoaderResolver clr,
String class_name_1,
String class_name_2)
Method to check if 2 classes are direct descendents.
|
static void |
clearFlags(boolean[] flags)
Utility to clear the supplied flags.
|
static void |
clearFlags(boolean[] flags,
int[] fields)
Utility to clear the supplied flags.
|
static Object |
convertValue(Object value,
Class cls)
Convenience method to convert the passed value to an object of the specified type (if possible).
|
static String |
createFullClassName(String pkg_name,
String cls_name)
Utility to create the full class name given the package and class name.
|
static void |
dumpClassInformation(Class cls)
Utility to use Reflection to dump out the details of a class.
|
static boolean |
getBitFromInt(int bits,
int bitIndex)
Convenience method to get the value of a bit from an int when we are storing (up to 32) boolean in an int
for memory utilisation purposes.
|
static Collection<File> |
getClassFilesForDirectory(File dir,
boolean normal_classes,
boolean inner_classes)
Method to return the class files below the specified directory.
|
static Class |
getClassForGenericType(Type genericType,
int pos)
Convenience method to attempt to return the class for the provided generic type.
|
static Class |
getClassForMemberOfClass(Class cls,
String memberName)
Method that returns the class type of a member of the specified class (if present).
|
static String |
getClassNameForClass(Class cls)
Utility to return the class name without the package name for a class.
|
static String |
getClassNameForFileName(String fileName,
ClassLoaderResolver clr)
Convenience method to try to find the class name stored in the specified file.
|
static String |
getClassnameForFilename(String filename,
String rootfilename)
Utility to get the Class name given the file name and the file name of the root of the package structure.
|
static String |
getClassNameForFileURL(URL fileURL)
Utility to find the class name of a class given the absolute file name of its class file.
|
static String[] |
getClassNamesForDirectoryAndBelow(File dir)
Convenience method to return the names of classes specified in the directory and below.
|
static String[] |
getClassNamesForJarFile(String jarFileName)
Convenience accessor for the names of all class files in the jar file with the specified name.
|
static String[] |
getClassNamesForJarFile(URI jarFileURI)
Convenience accessor for the names of all class files in the jar file with the specified URL.
|
static String[] |
getClassNamesForJarFile(URL jarFileURL)
Convenience accessor for the names of all class files in the jar file with the specified URL.
|
static Class |
getCollectionElementType(Class type,
Type genericType)
Convenience method to extract the element type of a collection when using JDK1.5 generics given the
input field.
|
static String |
getCollectionElementType(Field field)
Convenience method to extract the element type of a collection when using JDK1.5 generics given the
input field.
|
static String |
getCollectionElementType(Method method)
Convenience method to extract the element type of a collection when using JDK1.5 generics, given
the input method (getter).
|
static Constructor |
getConstructorWithArguments(Class cls,
Class[] argTypes)
Convenience method to return the constructor of the passed class that accepts the supplied
argument types.
|
static Field |
getFieldForClass(Class cls,
String fieldName)
Obtain a field from a class or superclasses using reflection.
|
static String |
getFieldNameForJavaBeanGetter(String methodName)
Generate a field name for JavaBeans compatible getter method
|
static String |
getFieldNameForJavaBeanSetter(String methodName)
Generate a field name for JavaBeans compatible setter method
|
static Collection<File> |
getFilesForDirectory(File dir)
Method to return the files below the specified directory.
|
static int[] |
getFlagsSetTo(boolean[] flags,
boolean state)
Returns an array of integers containing the indices of all elements in
flags that are in the state passed as argument.
|
static int[] |
getFlagsSetTo(boolean[] flags,
int[] indices,
boolean state)
Returns an array of integers containing the indices of all elements in
flags whose index occurs in indices and whose value is state.
|
static Method |
getGetterMethodForClass(Class cls,
String beanName)
Obtain a (Java bean) getter method from a class or superclasses using reflection.
|
static String |
getJavaBeanGetterName(String fieldName,
boolean isBoolean)
Generate a JavaBeans compatible getter name
|
static String |
getJavaBeanSetterName(String fieldName)
Generate a JavaBeans compatible setter name
|
static String |
getJavaLangClassForType(String type)
Convenience method to return the passed type as a java.lang type wherever possible.
|
static Class |
getMapKeyType(Class type,
Type genericType)
Convenience method to extract the key type of a map when using JDK1.5 generics given the
input field.
|
static String |
getMapKeyType(Field field)
Convenience method to extract the key type of a map when using JDK1.5 generics given the
input field.
|
static String |
getMapKeyType(Method method)
Convenience method to extract the key type of a map when using JDK1.5 generics given the
input method.
|
static Class |
getMapValueType(Class type,
Type genericType)
Convenience method to extract the value type of a map when using JDK1.5 generics given the
input field
|
static String |
getMapValueType(Field field)
Convenience method to extract the value type of a map when using JDK1.5 generics given the
input field
|
static String |
getMapValueType(Method method)
Convenience method to extract the value type of a map when using JDK1.5 generics given the
input method.
|
static Method |
getMethodForClass(Class cls,
String methodName,
Class[] argtypes)
Obtain a method from a class or superclasses using reflection
|
static Method |
getMethodWithArgument(Class cls,
String methodName,
Class argType)
Obtain a method from a class or superclasses using reflection.
|
static int |
getModifiersForFieldOfClass(ClassLoaderResolver clr,
String className,
String fieldName)
Convenience accessor for the modifiers of a field in a class.
|
static String[] |
getPackageJdoFilesForJarFile(String jarFileName)
Convenience accessor for the names of all "package.jdo" files in the jar file with the specified name.
|
static String[] |
getPackageJdoFilesForJarFile(URI jarFileURI)
Convenience accessor for the names of all "package.jdo" files in the jar file with the specified URL.
|
static String[] |
getPackageJdoFilesForJarFile(URL jarFileURL)
Convenience accessor for the names of all "package.jdo" files in the jar file with the specified URL.
|
static String |
getPackageNameForClass(Class cls)
Utility to return the package name for a class.
|
static Class |
getPrimitiveTypeForType(Class type)
Method to return the primitive equivalent of the specified type (if any).
|
static Method |
getSetterMethodForClass(Class cls,
String beanName,
Class type)
Obtain a (Java bean) setter method from a class or superclasses using reflection.
|
static Collection<Class<?>> |
getSuperclasses(Class<?> the_class)
Method to return the superclasses for a class.
|
static Collection<Class<?>> |
getSuperinterfaces(Class<?> the_class)
Method to return the superinterfaces for a class.
|
static Object |
getValueForIdentityField(Object id,
String fieldName)
Method to find the value of a field of the provided user-defined identity.
|
static Object |
getValueOfFieldByReflection(Object object,
String fieldName)
Helper method to return the value of a field of an object using reflection.
|
static Object |
getValueOfMethodByReflection(Object object,
String methodName,
Object... args)
Helper method to return the value returned by a method on an object using reflection.
|
static Class |
getWrapperTypeForPrimitiveType(Class type)
Convenience method to return the object wrapper type for a primitive type.
|
static String |
getWrapperTypeNameForPrimitiveTypeName(String typeName)
Convenience method to return the object wrapper type for a primitive type name.
|
static boolean |
hasDefaultConstructor(Class the_class)
Method to check for a default constructor on a class.
|
static boolean |
isInnerClass(String class_name)
Method to check whether a classname is for an inner class.
|
static boolean |
isJavaBeanGetterMethod(Method method)
Convenience method to return if the supplied method is a valid java bean getter method.
|
static boolean |
isPrimitiveArrayType(String typeName)
Convenience method to return if the passed type (name) is a primitive array type.
|
static boolean |
isPrimitiveType(String typeName)
Convenience method to return if the passed type (name) is a primitive type.
|
static boolean |
isPrimitiveWrapperType(String typeName)
Convenience method to return if the passed type (name) is a primitive wrapper type.
|
static boolean |
isReferenceType(Class cls)
Method to return whether the passes type is a "reference" type.
|
static Object |
newInstance(Class type,
Class[] parameterTypes,
Object[] parameters)
Accessor for a new instance of an object.
|
static int |
setBitInInt(int bits,
int bitIndex,
boolean flag)
Convenience method to set a boolean as a bit in the specified int, for memory utilisation purposes.
|
static boolean |
stringArrayContainsValue(String[] array,
String value)
Convenience method to return if a String array contains a value.
|
static boolean |
typesAreCompatible(Class cls1,
Class cls2)
Convenience method to return if two types are compatible.
|
static boolean |
typesAreCompatible(Class cls1,
String clsName2,
ClassLoaderResolver clr)
Convenience method to return if two types are compatible.
|
protected static final Map constructorsCache
public static Object newInstance(Class type, Class[] parameterTypes, Object[] parameters)
type
- Type of object (the class).parameterTypes
- Classes of params for the constructorparameters
- The parameters for the constructorNucleusException
- If an error occurs creating the instancepublic static Constructor getConstructorWithArguments(Class cls, Class[] argTypes)
cls
- The classargTypes
- The constructor argument types. If we know we need a parameter yet don't know the type then this will have a null for that argument type.public static Method getMethodWithArgument(Class cls, String methodName, Class argType)
cls
- the class to find the declared fields and populate the mapmethodName
- the method name to findargType
- the argument typepublic static Method getMethodForClass(Class cls, String methodName, Class[] argtypes)
cls
- the class where to look for the methodmethodName
- the method name to findargtypes
- the classes argument of the methodpublic static String getClassnameForFilename(String filename, String rootfilename)
filename
- The filename of the class filerootfilename
- The filename of the root of the package structurepublic static Collection<File> getClassFilesForDirectory(File dir, boolean normal_classes, boolean inner_classes)
dir
- The directorynormal_classes
- Whether to include normal classesinner_classes
- Whether to include inner classespublic static Collection<File> getFilesForDirectory(File dir)
dir
- The directorypublic static String[] getClassNamesForJarFile(String jarFileName)
jarFileName
- Name of the jar filepublic static String[] getClassNamesForJarFile(URL jarFileURL)
jarFileURL
- URL for the jar filepublic static String[] getClassNamesForJarFile(URI jarFileURI)
jarFileURI
- URI for the jar filepublic static String[] getPackageJdoFilesForJarFile(String jarFileName)
jarFileName
- Name of the jar filepublic static String[] getPackageJdoFilesForJarFile(URL jarFileURL)
jarFileURL
- URL for the jar filepublic static String[] getPackageJdoFilesForJarFile(URI jarFileURI)
jarFileURI
- URI for the jar filepublic static String[] getClassNamesForDirectoryAndBelow(File dir)
dir
- Directory that we should look below (root of classpath)public static boolean isInnerClass(String class_name)
class_name
- The class namepublic static boolean hasDefaultConstructor(Class the_class)
the_class
- The classpublic static Collection<Class<?>> getSuperclasses(Class<?> the_class)
the_class
- The classpublic static Collection<Class<?>> getSuperinterfaces(Class<?> the_class)
the_class
- The classpublic static Field getFieldForClass(Class cls, String fieldName)
cls
- the class to find the field fromfieldName
- the field name to findpublic static Method getGetterMethodForClass(Class cls, String beanName)
cls
- the class to find the getter frombeanName
- the name of the java bean to find the getter forpublic static Method getSetterMethodForClass(Class cls, String beanName, Class type)
cls
- the class to find the setter frombeanName
- the name of the java bean to find the setter fortype
- the type of the java bean to find the setter forpublic static String getWrapperTypeNameForPrimitiveTypeName(String typeName)
typeName
- The primitive type namepublic static Class getWrapperTypeForPrimitiveType(Class type)
type
- The primitive typepublic static Class getPrimitiveTypeForType(Class type)
type
- The typepublic static boolean isPrimitiveWrapperType(String typeName)
typeName
- Name of the typepublic static boolean isPrimitiveArrayType(String typeName)
typeName
- Name of the typepublic static boolean isPrimitiveType(String typeName)
typeName
- Name of the typepublic static Object convertValue(Object value, Class cls)
value
- The valuecls
- The classpublic static boolean typesAreCompatible(Class cls1, String clsName2, ClassLoaderResolver clr)
cls1
- First classclsName2
- Name of the second classclr
- ClassLoader resolver to usepublic static boolean typesAreCompatible(Class cls1, Class cls2)
cls1
- First classcls2
- Second classpublic static String createFullClassName(String pkg_name, String cls_name)
packageName=test className=Test, returns result=test.Test packageName=test className=test1.Test, returns result=test1.Test packageName=<null> className=Test, returns result=Test packageName=<null> className=test1.Test, returns result=test1.Test
pkg_name
- package name.cls_name
- class name.public static String getJavaLangClassForType(String type)
type
- The type namepublic static boolean classesAreDescendents(ClassLoaderResolver clr, String class_name_1, String class_name_2)
clr
- ClassLoaderResolver for loading the classesclass_name_1
- Name of first classclass_name_2
- Name of second classpublic static void dumpClassInformation(Class cls)
cls
- The class to dump out to the logpublic static String getJavaBeanGetterName(String fieldName, boolean isBoolean)
fieldName
- the field nameisBoolean
- whether the field is primitive boolean typepublic static String getJavaBeanSetterName(String fieldName)
fieldName
- the field namepublic static String getFieldNameForJavaBeanGetter(String methodName)
methodName
- the method namepublic static String getFieldNameForJavaBeanSetter(String methodName)
methodName
- the method namepublic static String getClassNameForFileName(String fileName, ClassLoaderResolver clr)
fileName
- Name of fileclr
- ClassLoader resolverpublic static String getClassNameForFileURL(URL fileURL) throws ClassNotFoundException
fileURL
- URL for the class fileClassNotFoundException
- Thrown when the file is not foundpublic static String getPackageNameForClass(Class cls)
cls
- The classpublic static String getClassNameForClass(Class cls)
cls
- The classpublic static Class getClassForGenericType(Type genericType, int pos)
genericType
- The generic typepos
- The position of the generic arg (in case of multiple)public static String getCollectionElementType(Field field)
field
- The fieldpublic static String getCollectionElementType(Method method)
method
- The methodpublic static Class getCollectionElementType(Class type, Type genericType)
type
- the field typegenericType
- the generic typepublic static String getMapKeyType(Field field)
field
- The fieldpublic static String getMapKeyType(Method method)
method
- The methodpublic static Class getMapKeyType(Class type, Type genericType)
type
- the field typegenericType
- the generic typepublic static String getMapValueType(Field field)
field
- The fieldpublic static String getMapValueType(Method method)
method
- The methodpublic static Class getMapValueType(Class type, Type genericType)
type
- the field typegenericType
- the generic typepublic static int getModifiersForFieldOfClass(ClassLoaderResolver clr, String className, String fieldName)
clr
- ClassLoader resolverclassName
- Name of the classfieldName
- Name of the fieldpublic static boolean isReferenceType(Class cls)
cls
- The typepublic static void assertClassForJarExistsInClasspath(ClassLoaderResolver clr, String className, String jarName)
clr
- ClassLoader resolverclassName
- Name of the classjarName
- Name of the jar containing the classNucleusUserException
- if the class is not foundpublic static boolean stringArrayContainsValue(String[] array, String value)
array
- The String arrayvalue
- The valuepublic static Object getValueOfMethodByReflection(Object object, String methodName, Object... args)
object
- The objectmethodName
- Name of the methodargs
- The argumentspublic static Object getValueOfFieldByReflection(Object object, String fieldName)
object
- The objectfieldName
- Name of the fieldpublic static Object getValueForIdentityField(Object id, String fieldName)
id
- The identityfieldName
- Name of the fieldNucleusUserException
- if not possible to get the valuepublic static Class getClassForMemberOfClass(Class cls, String memberName)
cls
- The classmemberName
- The member namepublic static boolean isJavaBeanGetterMethod(Method method)
method
- The methodpublic static void clearFlags(boolean[] flags)
flags
- The flags array to clearpublic static void clearFlags(boolean[] flags, int[] fields)
flags
- Flags to clearfields
- fields numbers where the flags will be clearedpublic static int[] getFlagsSetTo(boolean[] flags, boolean state)
flags
- Array of flags (true or false)state
- The state to search (true or false)public static int[] getFlagsSetTo(boolean[] flags, int[] indices, boolean state)
flags
- the boolean arrayindices
- The positions in the arraystate
- The state that we want to matchpublic static boolean getBitFromInt(int bits, int bitIndex)
bits
- The int storing the bitsbitIndex
- The index of this bitpublic static int setBitInInt(int bits, int bitIndex, boolean flag)
bits
- The int storing the bitsbitIndex
- The index of this bitflag
- The boolean value to storeCopyright © 2017. All rights reserved.