Package org.datanucleus.util
Class ClassUtils
- java.lang.Object
-
- org.datanucleus.util.ClassUtils
-
public class ClassUtils extends java.lang.Object
Utilities for handling classes. These are to supplement the methods provided by the Class object.
-
-
Constructor Summary
Constructors Constructor Description ClassUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
assertClassForJarExistsInClasspath(ClassLoaderResolver clr, java.lang.String className, java.lang.String jarName)
Convenience method to throw a NucleusUserException if the specified class is not loadable from the ClassLoaderResolver.private static java.lang.String
buildJavaBeanName(java.lang.String prefix, java.lang.String fieldName)
static boolean
classesAreDescendents(ClassLoaderResolver clr, java.lang.String class_name_1, java.lang.String class_name_2)
Method to check if 2 classes are direct descendants, so whether one of them is a superclass of the other.static void
clearFlags(boolean[] flags)
Utility to clear the supplied flags.static void
clearFlags(boolean[] flags, int[] fields)
Utility to clear the supplied flags.private static void
collectSuperinterfaces(java.lang.Class<?> c, java.util.List<java.lang.Class<?>> result)
static java.lang.String
createFullClassName(java.lang.String pkg_name, java.lang.String cls_name)
Utility to create the full class name given the package and class name.static void
dumpClassInformation(java.lang.Class cls)
Utility to use Reflection to dump out the details of a class.private static java.lang.reflect.Method
findDeclaredMethodInHeirarchy(java.lang.Class cls, java.lang.String methodName, java.lang.Class... parameterTypes)
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 java.util.Collection<java.io.File>
getClassFilesForDirectory(java.io.File dir, boolean includeNormal, boolean includeInner)
Method to return the class files below the specified directory.static java.lang.Class
getClassForGenericType(java.lang.reflect.Type genericType, int pos)
Convenience method to attempt to return the class for the provided generic type.static java.lang.Class
getClassForMemberOfClass(java.lang.Class cls, java.lang.String memberName)
Method that returns the class type of a member of the specified class (if present).static java.lang.String
getClassNameForClass(java.lang.Class cls)
Utility to return the class name without the package name for a class.static java.lang.String
getClassNameForFileName(java.lang.String fileName, ClassLoaderResolver clr)
Convenience method to try to find the class name stored in the specified file.static java.lang.String
getClassNameForFileURL(java.net.URL fileURL)
Utility to find the class name of a class given the absolute file name of its class file.static java.lang.String[]
getClassNamesForDirectoryAndBelow(java.io.File dir)
Convenience method to return the names of classes specified in the directory and below.static java.lang.String[]
getClassNamesForJarFile(java.lang.String jarFileName)
Convenience accessor for the names of all class files in the jar file with the specified name.static java.lang.String[]
getClassNamesForJarFile(java.net.URI jarFileURI)
Convenience accessor for the names of all class files in the jar file with the specified URL.static java.lang.String[]
getClassNamesForJarFile(java.net.URL jarFileURL)
Convenience accessor for the names of all class files in the jar file with the specified URL.private static java.lang.String[]
getClassNamesForJarFile(java.util.jar.JarFile jar)
Convenience method to return the names of classes specified in the jar file.static java.lang.Class
getCollectionElementType(java.lang.Class type, java.lang.reflect.Type genericType)
Convenience method to extract the element type of a collection when using JDK1.5 generics given the input field.static java.lang.String
getCollectionElementType(java.lang.reflect.Field field)
Convenience method to extract the element type of a collection when using JDK1.5 generics given the input field.static java.lang.String
getCollectionElementType(java.lang.reflect.Method method)
Convenience method to extract the element type of a collection when using JDK1.5 generics, given the input method (getter).static <T> java.lang.reflect.Constructor<T>
getConstructorWithArguments(java.lang.Class<T> cls, java.lang.Class[] argTypes)
Convenience method to return the constructor of the passed class that accepts the supplied argument types.static <T> java.lang.reflect.Constructor<T>
getConstructorWithArguments(java.lang.Class<T> cls, java.lang.Class[] argTypes, boolean[] argTypeCheck)
Convenience method to return the constructor of the passed class that accepts the supplied argument types.private static java.lang.reflect.Field
getDeclaredFieldPrivileged(java.lang.Class clazz, java.lang.String fieldName)
Helper method to retrieve the java.lang.reflect.Fieldprivate static java.lang.reflect.Method
getDeclaredMethodPrivileged(java.lang.Class clazz, java.lang.String methodName, java.lang.Class... argTypes)
Helper method to retrieve the java.lang.reflect.Methodstatic java.lang.reflect.Field
getFieldForClass(java.lang.Class cls, java.lang.String fieldName)
Obtain a field from a class or superclasses using reflection.static java.lang.String
getFieldNameForJavaBeanGetter(java.lang.String methodName)
Generate a field name for JavaBeans compatible getter methodstatic java.lang.String
getFieldNameForJavaBeanSetter(java.lang.String methodName)
Generate a field name for JavaBeans compatible setter methodprivate static java.lang.String[]
getFileNamesWithSuffixForJarFile(java.util.jar.JarFile jar, java.lang.String suffix)
Convenience method to return the names of files specified in the jar file that end with the specified suffix.static java.util.Collection<java.io.File>
getFilesForDirectory(java.io.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 java.lang.reflect.Method
getGetterMethodForClass(java.lang.Class cls, java.lang.String beanName)
Obtain a (Java bean) getter method from a class or superclasses using reflection.static int[]
getIndexesOfCollectionInList(java.util.List delegate, java.util.Collection elements)
Convenience method to return the list index positions in "delegate" where the passed "elements" are located.static java.lang.String
getJavaBeanGetterName(java.lang.String fieldName, boolean isBoolean)
Generate a JavaBeans compatible getter namestatic java.lang.String
getJavaBeanSetterName(java.lang.String fieldName)
Generate a JavaBeans compatible setter namestatic java.lang.String
getJavaLangClassForType(java.lang.String type)
Convenience method to return the passed type as a java.lang type wherever possible.static java.lang.Class
getMapKeyType(java.lang.Class type, java.lang.reflect.Type genericType)
Convenience method to extract the key type of a map when using JDK1.5 generics given the input field.static java.lang.String
getMapKeyType(java.lang.reflect.Field field)
Convenience method to extract the key type of a map when using JDK1.5 generics given the input field.static java.lang.String
getMapKeyType(java.lang.reflect.Method method)
Convenience method to extract the key type of a map when using JDK1.5 generics given the input method.static java.lang.Class
getMapValueType(java.lang.Class type, java.lang.reflect.Type genericType)
Convenience method to extract the value type of a map when using JDK1.5 generics given the input fieldstatic java.lang.String
getMapValueType(java.lang.reflect.Field field)
Convenience method to extract the value type of a map when using JDK1.5 generics given the input fieldstatic java.lang.String
getMapValueType(java.lang.reflect.Method method)
Convenience method to extract the value type of a map when using JDK1.5 generics given the input method.static java.lang.reflect.Method
getMethodForClass(java.lang.Class cls, java.lang.String methodName, java.lang.Class[] argtypes)
Obtain a method from a class or superclasses using reflectionstatic java.lang.reflect.Method
getMethodWithArgument(java.lang.Class cls, java.lang.String methodName, java.lang.Class argType)
Obtain a method from a class or superclasses using reflection.static int
getModifiersForFieldOfClass(ClassLoaderResolver clr, java.lang.String className, java.lang.String fieldName)
Convenience accessor for the modifiers of a field in a class.static java.lang.String[]
getPackageJdoFilesForJarFile(java.lang.String jarFileName)
Convenience accessor for the names of all "package.jdo" files in the jar file with the specified name.static java.lang.String[]
getPackageJdoFilesForJarFile(java.net.URI jarFileURI)
Convenience accessor for the names of all "package.jdo" files in the jar file with the specified URL.static java.lang.String[]
getPackageJdoFilesForJarFile(java.net.URL jarFileURL)
Convenience accessor for the names of all "package.jdo" files in the jar file with the specified URL.static java.lang.String
getPackageNameForClass(java.lang.Class cls)
Utility to return the package name for a class.static java.lang.String
getPackageNameForClassName(java.lang.String clsName)
Utility to return the package name for a class name.static java.lang.Class
getPrimitiveTypeForType(java.lang.Class type)
Method to return the primitive equivalent of the specified type (if any).static java.lang.reflect.Method
getSetterMethodForClass(java.lang.Class cls, java.lang.String beanName, java.lang.Class type)
Obtain a (Java bean) setter method from a class or superclasses using reflection.static java.util.Collection<java.lang.Class<?>>
getSuperclasses(java.lang.Class<?> cls)
Method to return the superclasses for a class.static java.util.Collection<java.lang.Class<?>>
getSuperinterfaces(java.lang.Class<?> cls)
Method to return the superinterfaces for a class.static java.lang.Object
getValueForIdentityField(java.lang.Object id, java.lang.String fieldName)
Method to find the value of a field of the provided user-defined identity.static java.lang.Object
getValueOfFieldByReflection(java.lang.Object object, java.lang.String fieldName)
Helper method to return the value of a field of an object using reflection.static java.lang.Object
getValueOfMethodByReflection(java.lang.Object object, java.lang.String methodName, java.lang.Object... args)
Helper method to return the value returned by a method on an object using reflection.static java.lang.Class
getWrapperTypeForPrimitiveType(java.lang.Class type)
Convenience method to return the object wrapper type for a primitive type.static java.lang.String
getWrapperTypeNameForPrimitiveTypeName(java.lang.String typeName)
Convenience method to return the object wrapper type for a primitive type name.static boolean
hasDefaultConstructor(java.lang.Class cls)
Method to check for a default constructor on a class.static boolean
isClassPresent(java.lang.String className, ClassLoaderResolver clr)
Convenience method to say whether a class is present.static boolean
isInnerClass(java.lang.String class_name)
Method to check whether a classname is for an inner class.static boolean
isJavaBeanGetterMethod(java.lang.reflect.Method method)
Convenience method to return if the supplied method is a valid java bean getter method.static boolean
isPrimitiveArrayType(java.lang.String typeName)
Convenience method to return if the passed type (name) is a primitive array type.static boolean
isPrimitiveType(java.lang.String typeName)
Convenience method to return if the passed type (name) is a primitive type.static boolean
isPrimitiveWrapperType(java.lang.String typeName)
Convenience method to return if the passed type (name) is a primitive wrapper type.static boolean
isReferenceType(java.lang.Class cls)
Method to return whether the passes type is a "reference" type.static <T> T
newInstance(java.lang.Class<T> type, java.lang.Class[] parameterTypes, java.lang.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(java.lang.String[] array, java.lang.String value)
Convenience method to return if a String array contains a value.private static java.lang.String
truncateJavaBeanMethodName(java.lang.String methodName, int prefixLength)
static boolean
typesAreCompatible(java.lang.Class cls1, java.lang.Class cls2)
Convenience method to return if two types are compatible.static boolean
typesAreCompatible(java.lang.Class cls1, java.lang.String clsName2, ClassLoaderResolver clr)
Convenience method to return if two types are compatible.
-
-
-
Method Detail
-
newInstance
public static <T> T newInstance(java.lang.Class<T> type, java.lang.Class[] parameterTypes, java.lang.Object[] parameters)
Accessor for a new instance of an object. Uses reflection to generate the instance using the passed constructor parameter arguments.- Type Parameters:
T
- Type of the new instance- Parameters:
type
- Type of object (the class).parameterTypes
- Classes of params for the constructorparameters
- The parameters for the constructor- Returns:
- The object
- Throws:
NucleusException
- If an error occurs creating the instance
-
getConstructorWithArguments
public static <T> java.lang.reflect.Constructor<T> getConstructorWithArguments(java.lang.Class<T> cls, java.lang.Class[] argTypes)
Convenience method to return the constructor of the passed class that accepts the supplied argument types. Allows for primitive to primitive wrapper conversion. Typically used by the JDOQL/JPQL ResultClass mapping process.- Type Parameters:
T
- Type that the constructor is for- Parameters:
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.- Returns:
- The constructor
-
getConstructorWithArguments
public static <T> java.lang.reflect.Constructor<T> getConstructorWithArguments(java.lang.Class<T> cls, java.lang.Class[] argTypes, boolean[] argTypeCheck)
Convenience method to return the constructor of the passed class that accepts the supplied argument types. Allows for primitive to primitive wrapper conversion. Typically used by the JDOQL/JPQL ResultClass mapping process.- Type Parameters:
T
- Type that the constructor is for- Parameters:
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.argTypeCheck
- Whether to check the type of the different arguments. Useful where we don't know the result type of an argument until processing results- Returns:
- The constructor
-
getMethodWithArgument
public static java.lang.reflect.Method getMethodWithArgument(java.lang.Class cls, java.lang.String methodName, java.lang.Class argType)
Obtain a method from a class or superclasses using reflection. The method will have the specified name and will take a single argument. Allows for the argument type being primitive or its associated wrapper.- Parameters:
cls
- the class to find the declared fields and populate the mapmethodName
- the method name to findargType
- the argument type- Returns:
- The Method
-
getMethodForClass
public static java.lang.reflect.Method getMethodForClass(java.lang.Class cls, java.lang.String methodName, java.lang.Class[] argtypes)
Obtain a method from a class or superclasses using reflection- Parameters:
cls
- the class where to look for the methodmethodName
- the method name to findargtypes
- the classes argument of the method- Returns:
- The Method
-
getClassFilesForDirectory
public static java.util.Collection<java.io.File> getClassFilesForDirectory(java.io.File dir, boolean includeNormal, boolean includeInner)
Method to return the class files below the specified directory.- Parameters:
dir
- The directoryincludeNormal
- Whether to include normal classesincludeInner
- Whether to include inner classes- Returns:
- The class files (Collection of File objects).
-
getFilesForDirectory
public static java.util.Collection<java.io.File> getFilesForDirectory(java.io.File dir)
Method to return the files below the specified directory.- Parameters:
dir
- The directory- Returns:
- The files
-
getClassNamesForJarFile
public static java.lang.String[] getClassNamesForJarFile(java.lang.String jarFileName)
Convenience accessor for the names of all class files in the jar file with the specified name. The returned class names are of the form "org.datanucleus.MyClass".- Parameters:
jarFileName
- Name of the jar file- Returns:
- The class names
-
getClassNamesForJarFile
public static java.lang.String[] getClassNamesForJarFile(java.net.URL jarFileURL)
Convenience accessor for the names of all class files in the jar file with the specified URL. The returned class names are of the form "org.datanucleus.MyClass".- Parameters:
jarFileURL
- URL for the jar file- Returns:
- The class names
-
getClassNamesForJarFile
public static java.lang.String[] getClassNamesForJarFile(java.net.URI jarFileURI)
Convenience accessor for the names of all class files in the jar file with the specified URL. The returned class names are of the form "org.datanucleus.MyClass".- Parameters:
jarFileURI
- URI for the jar file- Returns:
- The class names
-
getClassNamesForJarFile
private static java.lang.String[] getClassNamesForJarFile(java.util.jar.JarFile jar)
Convenience method to return the names of classes specified in the jar file. All inner classes are ignored.- Parameters:
jar
- Jar file- Returns:
- The class names
-
getPackageJdoFilesForJarFile
public static java.lang.String[] getPackageJdoFilesForJarFile(java.lang.String jarFileName)
Convenience accessor for the names of all "package.jdo" files in the jar file with the specified name.- Parameters:
jarFileName
- Name of the jar file- Returns:
- The "package.jdo" file names
-
getPackageJdoFilesForJarFile
public static java.lang.String[] getPackageJdoFilesForJarFile(java.net.URL jarFileURL)
Convenience accessor for the names of all "package.jdo" files in the jar file with the specified URL.- Parameters:
jarFileURL
- URL for the jar file- Returns:
- The "package.jdo" file names
-
getPackageJdoFilesForJarFile
public static java.lang.String[] getPackageJdoFilesForJarFile(java.net.URI jarFileURI)
Convenience accessor for the names of all "package.jdo" files in the jar file with the specified URL.- Parameters:
jarFileURI
- URI for the jar file- Returns:
- The "package.jdo" file names
-
getFileNamesWithSuffixForJarFile
private static java.lang.String[] getFileNamesWithSuffixForJarFile(java.util.jar.JarFile jar, java.lang.String suffix)
Convenience method to return the names of files specified in the jar file that end with the specified suffix.- Parameters:
jar
- Jar filesuffix
- Suffix for the file (can be the filename without the path)- Returns:
- The fully-qualified names of the files with this suffix in the jar file
-
getClassNamesForDirectoryAndBelow
public static java.lang.String[] getClassNamesForDirectoryAndBelow(java.io.File dir)
Convenience method to return the names of classes specified in the directory and below. All inner classes are ignored.- Parameters:
dir
- Directory that we should look below (root of classpath)- Returns:
- The class names
-
isInnerClass
public static boolean isInnerClass(java.lang.String class_name)
Method to check whether a classname is for an inner class. Currently checks for the presence of $ in the name.- Parameters:
class_name
- The class name- Returns:
- Whether it is an inner class
-
hasDefaultConstructor
public static boolean hasDefaultConstructor(java.lang.Class cls)
Method to check for a default constructor on a class. Doesn't check superclasses for the default constructor.- Parameters:
cls
- The class- Returns:
- Whether it has a default constructor
-
getSuperclasses
public static java.util.Collection<java.lang.Class<?>> getSuperclasses(java.lang.Class<?> cls)
Method to return the superclasses for a class. The superclasses will be ordered.- Parameters:
cls
- The class- Returns:
- The superclass of this class.
-
getSuperinterfaces
public static java.util.Collection<java.lang.Class<?>> getSuperinterfaces(java.lang.Class<?> cls)
Method to return the superinterfaces for a class. The superinterfaces will be ordered and unique.- Parameters:
cls
- The class- Returns:
- The superinterfaces of this class.
-
collectSuperinterfaces
private static void collectSuperinterfaces(java.lang.Class<?> c, java.util.List<java.lang.Class<?>> result)
-
getFieldForClass
public static java.lang.reflect.Field getFieldForClass(java.lang.Class cls, java.lang.String fieldName)
Obtain a field from a class or superclasses using reflection.- Parameters:
cls
- the class to find the field fromfieldName
- the field name to find- Returns:
- The Field
-
getGetterMethodForClass
public static java.lang.reflect.Method getGetterMethodForClass(java.lang.Class cls, java.lang.String beanName)
Obtain a (Java bean) getter method from a class or superclasses using reflection. Any 'get...' method will take precedence over 'is...' methods.- Parameters:
cls
- the class to find the getter frombeanName
- the name of the java bean to find the getter for- Returns:
- The getter Method
-
getSetterMethodForClass
public static java.lang.reflect.Method getSetterMethodForClass(java.lang.Class cls, java.lang.String beanName, java.lang.Class type)
Obtain a (Java bean) setter method from a class or superclasses using reflection.- Parameters:
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 for- Returns:
- The setter Method
-
findDeclaredMethodInHeirarchy
private static java.lang.reflect.Method findDeclaredMethodInHeirarchy(java.lang.Class cls, java.lang.String methodName, java.lang.Class... parameterTypes)
-
getWrapperTypeNameForPrimitiveTypeName
public static java.lang.String getWrapperTypeNameForPrimitiveTypeName(java.lang.String typeName)
Convenience method to return the object wrapper type for a primitive type name. If the type is not a primitive then just returns the type name- Parameters:
typeName
- The primitive type name- Returns:
- The object wrapper type name for this primitive
-
getWrapperTypeForPrimitiveType
public static java.lang.Class getWrapperTypeForPrimitiveType(java.lang.Class type)
Convenience method to return the object wrapper type for a primitive type.- Parameters:
type
- The primitive type- Returns:
- The object wrapper type for this primitive
-
getPrimitiveTypeForType
public static java.lang.Class getPrimitiveTypeForType(java.lang.Class type)
Method to return the primitive equivalent of the specified type (if any). Returns null if there is no primitive equivalent.- Parameters:
type
- The type- Returns:
- The primitive equivalent.
-
isPrimitiveWrapperType
public static boolean isPrimitiveWrapperType(java.lang.String typeName)
Convenience method to return if the passed type (name) is a primitive wrapper type.- Parameters:
typeName
- Name of the type- Returns:
- Whether it is a primitive wrapper
-
isPrimitiveArrayType
public static boolean isPrimitiveArrayType(java.lang.String typeName)
Convenience method to return if the passed type (name) is a primitive array type.- Parameters:
typeName
- Name of the type- Returns:
- Whether it is a primitive array
-
isPrimitiveType
public static boolean isPrimitiveType(java.lang.String typeName)
Convenience method to return if the passed type (name) is a primitive type.- Parameters:
typeName
- Name of the type- Returns:
- Whether it is a primitive
-
typesAreCompatible
public static boolean typesAreCompatible(java.lang.Class cls1, java.lang.String clsName2, ClassLoaderResolver clr)
Convenience method to return if two types are compatible. Returns true if both types are primitive/wrappers and are of the same type. Returns true if clsName2 is the same or a subclass of cls1. Otherwise returns false;- Parameters:
cls1
- First classclsName2
- Name of the second classclr
- ClassLoader resolver to use- Returns:
- Whether they are compatible
-
typesAreCompatible
public static boolean typesAreCompatible(java.lang.Class cls1, java.lang.Class cls2)
Convenience method to return if two types are compatible. Returns true if both types are primitive/wrappers and are of the same type. Returns true if cls2 is the same or a subclass of cls1. Otherwise returns false;- Parameters:
cls1
- First classcls2
- Second class- Returns:
- Whether they are compatible
-
createFullClassName
public static java.lang.String createFullClassName(java.lang.String pkg_name, java.lang.String cls_name)
Utility to create the full class name given the package and class name. Some examplespackageName=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
- Parameters:
pkg_name
- package name.cls_name
- class name.- Returns:
- generated full class name.
-
getJavaLangClassForType
public static java.lang.String getJavaLangClassForType(java.lang.String type)
Convenience method to return the passed type as a java.lang type wherever possible. The passed type will be stripped of any package name and will be checked if it is a known java.lang class. This is used where the user has specified a class name for a collection or map element/key/value type and meant a java.lang class but didn't fully qualify it.- Parameters:
type
- The type name- Returns:
- The java.lang equivalent (or the input type if not possible)
-
classesAreDescendents
public static boolean classesAreDescendents(ClassLoaderResolver clr, java.lang.String class_name_1, java.lang.String class_name_2)
Method to check if 2 classes are direct descendants, so whether one of them is a superclass of the other.- Parameters:
clr
- ClassLoaderResolver for loading the classesclass_name_1
- Name of first classclass_name_2
- Name of second class- Returns:
- Whether they are direct descendants.
-
dumpClassInformation
public static void dumpClassInformation(java.lang.Class cls)
Utility to use Reflection to dump out the details of a class. Will list all superclasses, interfaces, methods and fields. Can be used, for example, in checking the methods adding by the enhancement process. The information is dumped out the GENERAL log.- Parameters:
cls
- The class to dump out to the log
-
getJavaBeanGetterName
public static java.lang.String getJavaBeanGetterName(java.lang.String fieldName, boolean isBoolean)
Generate a JavaBeans compatible getter name- Parameters:
fieldName
- the field nameisBoolean
- whether the field is primitive boolean type- Returns:
- the getter name
-
getJavaBeanSetterName
public static java.lang.String getJavaBeanSetterName(java.lang.String fieldName)
Generate a JavaBeans compatible setter name- Parameters:
fieldName
- the field name- Returns:
- the setter name
-
buildJavaBeanName
private static java.lang.String buildJavaBeanName(java.lang.String prefix, java.lang.String fieldName)
-
getFieldNameForJavaBeanGetter
public static java.lang.String getFieldNameForJavaBeanGetter(java.lang.String methodName)
Generate a field name for JavaBeans compatible getter method- Parameters:
methodName
- the method name- Returns:
- the field name
-
getFieldNameForJavaBeanSetter
public static java.lang.String getFieldNameForJavaBeanSetter(java.lang.String methodName)
Generate a field name for JavaBeans compatible setter method- Parameters:
methodName
- the method name- Returns:
- the field name
-
truncateJavaBeanMethodName
private static java.lang.String truncateJavaBeanMethodName(java.lang.String methodName, int prefixLength)
-
getClassNameForFileName
public static java.lang.String getClassNameForFileName(java.lang.String fileName, ClassLoaderResolver clr)
Convenience method to try to find the class name stored in the specified file. Moves along the file, chopping off components from the front until it finds a class in the class path. This is typically used as a fallback after trying the method after this.- Parameters:
fileName
- Name of fileclr
- ClassLoader resolver- Returns:
- The class name (if found)
-
getClassNameForFileURL
public static java.lang.String getClassNameForFileURL(java.net.URL fileURL) throws java.lang.ClassNotFoundException
Utility to find the class name of a class given the absolute file name of its class file. Creates a loader and loads the class directly to find it.- Parameters:
fileURL
- URL for the class file- Returns:
- The name of the class
- Throws:
java.lang.ClassNotFoundException
- Thrown when the file is not found
-
getPackageNameForClass
public static java.lang.String getPackageNameForClass(java.lang.Class cls)
Utility to return the package name for a class. Allows for the result of class.getPackage() being null.- Parameters:
cls
- The class- Returns:
- The name of its package (or null if no package e.g a primitive)
-
getPackageNameForClassName
public static java.lang.String getPackageNameForClassName(java.lang.String clsName)
Utility to return the package name for a class name.- Parameters:
clsName
- The class- Returns:
- The name of its package (or null if no package e.g a primitive)
-
getClassNameForClass
public static java.lang.String getClassNameForClass(java.lang.Class cls)
Utility to return the class name without the package name for a class.- Parameters:
cls
- The class- Returns:
- The name of the class without its package
-
getClassForGenericType
public static java.lang.Class getClassForGenericType(java.lang.reflect.Type genericType, int pos)
Convenience method to attempt to return the class for the provided generic type.- Parameters:
genericType
- The generic typepos
- The position of the generic arg (in case of multiple)- Returns:
- The class (if determinable), or null
-
getCollectionElementType
public static java.lang.String getCollectionElementType(java.lang.reflect.Field field)
Convenience method to extract the element type of a collection when using JDK1.5 generics given the input field.- Parameters:
field
- The field- Returns:
- The name of the element class
-
getCollectionElementType
public static java.lang.String getCollectionElementType(java.lang.reflect.Method method)
Convenience method to extract the element type of a collection when using JDK1.5 generics, given the input method (getter).- Parameters:
method
- The method- Returns:
- The name of the element class
-
getCollectionElementType
public static java.lang.Class getCollectionElementType(java.lang.Class type, java.lang.reflect.Type genericType)
Convenience method to extract the element type of a collection when using JDK1.5 generics given the input field.- Parameters:
type
- the field typegenericType
- the generic type- Returns:
- The element class
-
getMapKeyType
public static java.lang.String getMapKeyType(java.lang.reflect.Field field)
Convenience method to extract the key type of a map when using JDK1.5 generics given the input field.- Parameters:
field
- The field- Returns:
- The name of the key class
-
getMapKeyType
public static java.lang.String getMapKeyType(java.lang.reflect.Method method)
Convenience method to extract the key type of a map when using JDK1.5 generics given the input method.- Parameters:
method
- The method- Returns:
- The name of the key class
-
getMapKeyType
public static java.lang.Class getMapKeyType(java.lang.Class type, java.lang.reflect.Type genericType)
Convenience method to extract the key type of a map when using JDK1.5 generics given the input field.- Parameters:
type
- the field typegenericType
- the generic type- Returns:
- The name of the key class
-
getMapValueType
public static java.lang.String getMapValueType(java.lang.reflect.Field field)
Convenience method to extract the value type of a map when using JDK1.5 generics given the input field- Parameters:
field
- The field- Returns:
- The name of the value class
-
getMapValueType
public static java.lang.String getMapValueType(java.lang.reflect.Method method)
Convenience method to extract the value type of a map when using JDK1.5 generics given the input method.- Parameters:
method
- The method- Returns:
- The name of the value class
-
getMapValueType
public static java.lang.Class getMapValueType(java.lang.Class type, java.lang.reflect.Type genericType)
Convenience method to extract the value type of a map when using JDK1.5 generics given the input field- Parameters:
type
- the field typegenericType
- the generic type- Returns:
- The name of the value class
-
getModifiersForFieldOfClass
public static int getModifiersForFieldOfClass(ClassLoaderResolver clr, java.lang.String className, java.lang.String fieldName)
Convenience accessor for the modifiers of a field in a class.- Parameters:
clr
- ClassLoader resolverclassName
- Name of the classfieldName
- Name of the field- Returns:
- The modifiers
-
isReferenceType
public static boolean isReferenceType(java.lang.Class cls)
Method to return whether the passes type is a "reference" type. A "reference" type is either an interface or an Object, so can be used as a reference to a persistable object.- Parameters:
cls
- The type- Returns:
- Whether it is a reference type
-
isClassPresent
public static boolean isClassPresent(java.lang.String className, ClassLoaderResolver clr)
Convenience method to say whether a class is present.- Parameters:
className
- The class nameclr
- ClassLoader resolver- Returns:
- Whether it is present
-
assertClassForJarExistsInClasspath
public static void assertClassForJarExistsInClasspath(ClassLoaderResolver clr, java.lang.String className, java.lang.String jarName)
Convenience method to throw a NucleusUserException if the specified class is not loadable from the ClassLoaderResolver.- Parameters:
clr
- ClassLoader resolverclassName
- Name of the classjarName
- Name of the jar containing the class- Throws:
NucleusUserException
- if the class is not found
-
stringArrayContainsValue
public static boolean stringArrayContainsValue(java.lang.String[] array, java.lang.String value)
Convenience method to return if a String array contains a value.- Parameters:
array
- The String arrayvalue
- The value- Returns:
- Whether it is contained
-
getValueOfMethodByReflection
public static java.lang.Object getValueOfMethodByReflection(java.lang.Object object, java.lang.String methodName, java.lang.Object... args)
Helper method to return the value returned by a method on an object using reflection.- Parameters:
object
- The objectmethodName
- Name of the methodargs
- The arguments- Returns:
- The value
-
getValueOfFieldByReflection
public static java.lang.Object getValueOfFieldByReflection(java.lang.Object object, java.lang.String fieldName)
Helper method to return the value of a field of an object using reflection.- Parameters:
object
- The objectfieldName
- Name of the field- Returns:
- The value
-
getDeclaredFieldPrivileged
private static java.lang.reflect.Field getDeclaredFieldPrivileged(java.lang.Class clazz, java.lang.String fieldName)
Helper method to retrieve the java.lang.reflect.Field- Parameters:
clazz
- the Class instance of the declaring class or interfacefieldName
- the field name- Returns:
- The field
-
getDeclaredMethodPrivileged
private static java.lang.reflect.Method getDeclaredMethodPrivileged(java.lang.Class clazz, java.lang.String methodName, java.lang.Class... argTypes)
Helper method to retrieve the java.lang.reflect.Method- Parameters:
clazz
- the Class instance of the declaring class or interfacemethodName
- the method nameargTypes
- Types of the arguments to this method- Returns:
- The method
-
getValueForIdentityField
public static java.lang.Object getValueForIdentityField(java.lang.Object id, java.lang.String fieldName)
Method to find the value of a field of the provided user-defined identity. The field has to be either accessible, or have a Java-bean style getter.- Parameters:
id
- The identityfieldName
- Name of the field- Returns:
- The value of the field in this identity
- Throws:
NucleusUserException
- if not possible to get the value
-
getClassForMemberOfClass
public static java.lang.Class getClassForMemberOfClass(java.lang.Class cls, java.lang.String memberName)
Method that returns the class type of a member of the specified class (if present). The member can either be a field, or a Java bean name (for a getter).- Parameters:
cls
- The classmemberName
- The member name- Returns:
- The member class (or null if not present)
-
isJavaBeanGetterMethod
public static boolean isJavaBeanGetterMethod(java.lang.reflect.Method method)
Convenience method to return if the supplied method is a valid java bean getter method. Checks that it is not static, that its name starts "get" or "is", that it has a return type and that it takes no arguments.- Parameters:
method
- The method- Returns:
- Whether this method is a java bean getter
-
clearFlags
public static void clearFlags(boolean[] flags)
Utility to clear the supplied flags.- Parameters:
flags
- The flags array to clear
-
clearFlags
public static void clearFlags(boolean[] flags, int[] fields)
Utility to clear the supplied flags.- Parameters:
flags
- Flags to clearfields
- fields numbers where the flags will be cleared
-
getFlagsSetTo
public 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.- Parameters:
flags
- Array of flags (true or false)state
- The state to search (true or false)- Returns:
- The settings of the flags
-
getFlagsSetTo
public 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.- Parameters:
flags
- the boolean arrayindices
- The positions in the arraystate
- The state that we want to match- Returns:
- The positions of flags that are set to this state
-
getBitFromInt
public 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.- Parameters:
bits
- The int storing the bitsbitIndex
- The index of this bit- Returns:
- The value of this bit (as a boolean)
-
setBitInInt
public 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.- Parameters:
bits
- The int storing the bitsbitIndex
- The index of this bitflag
- The boolean value to store- Returns:
- The int with this bit set
-
getIndexesOfCollectionInList
public static int[] getIndexesOfCollectionInList(java.util.List delegate, java.util.Collection elements)
Convenience method to return the list index positions in "delegate" where the passed "elements" are located. The index positions are in reverse numeric order (i.e highest first).- Parameters:
delegate
- The delegate listelements
- The elements to check for- Returns:
- The index positions (highest first)
-
-