Package jodd.util
Class ClassUtil
- java.lang.Object
-
- jodd.util.ClassUtil
-
public class ClassUtil extends java.lang.ObjectClass utilities.
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.Class[]EMPTY_CLASS_ARRAYEmpty class array.static java.lang.StringMETHOD_GET_PREFIXstatic java.lang.StringMETHOD_IS_PREFIXstatic java.lang.StringMETHOD_SET_PREFIX
-
Constructor Summary
Constructors Constructor Description ClassUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.Class<?>childClassOf(java.lang.Class<?> parentClass, java.lang.Object instance)Returns the class of the immediate subclass of the given parent class for the given object instance; or null if such immediate subclass cannot be uniquely identified for the given object instance.static booleancompareDeclarations(java.lang.reflect.Method first, java.lang.reflect.Method second)Compares method declarations: signature and return types.static booleancompareParameters(java.lang.Class[] first, java.lang.Class[] second)Compares classes, usually method or ctor parameters.static booleancompareSignatures(java.lang.reflect.Constructor first, java.lang.reflect.Constructor second)Compares constructor signatures: names and parameters.static booleancompareSignatures(java.lang.reflect.Field first, java.lang.reflect.Field second)static booleancompareSignatures(java.lang.reflect.Method first, java.lang.reflect.Method second)Compares method signatures: names and parameters.static java.lang.StringconvertClassNameToFileName(java.lang.Class clazz)Resolves class file name from class name by replacing dot's with '/' separator and adding class extension at the end.static java.lang.StringconvertClassNameToFileName(java.lang.String className)Resolves class file name from class name by replacing dot's with '/' separator.static <T> java.lang.Class<T>[]emptyClassArray()static <T> java.lang.reflect.Constructor<T>findConstructor(java.lang.Class<T> clazz, java.lang.Class<?>... parameterTypes)Finds constructor with given parameter types.static java.lang.reflect.MethodfindDeclaredMethod(java.lang.Class c, java.lang.String methodName)static java.lang.ClassfindEnum(java.lang.Class target)Returnsenumclass ornullif class is not an enum.static java.lang.reflect.MethodfindMethod(java.lang.Class c, java.lang.String methodName)Returns method from an object, matched by name.static voidforceAccess(java.lang.reflect.AccessibleObject accObject)Suppress access check against a reflection object.static java.lang.reflect.Field[]getAccessibleFields(java.lang.Class clazz)static java.lang.reflect.Field[]getAccessibleFields(java.lang.Class clazz, java.lang.Class limit)static java.lang.reflect.Method[]getAccessibleMethods(java.lang.Class clazz)Returns array of all methods that are accessible from given class.static java.lang.reflect.Method[]getAccessibleMethods(java.lang.Class clazz, java.lang.Class limit)Returns array of all methods that are accessible from given class, upto limit (usuallyObject.class).static java.lang.StringgetBeanPropertyGetterName(java.lang.reflect.Method method)Returns property name from a getter method.static java.lang.StringgetBeanPropertySetterName(java.lang.reflect.Method method)Returns beans property setter name ornullif method is not a real setter.static java.lang.ClassgetCallerClass()Smart variant ofgetCallerClass(int)that skips all relevant Jodd calls.static java.lang.ClassgetCallerClass(int framesToSkip)EmulatesReflection.getCallerClassusing standard API.static java.lang.Class[]getClasses(java.lang.Object... objects)Returns classes from array of objects.static java.lang.ClassgetComponentType(java.lang.reflect.Type type, int index)Returns single component type.static java.lang.ClassgetComponentType(java.lang.reflect.Type type, java.lang.Class implClass, int index)Returns single component type for given type and implementation.static java.lang.Class[]getComponentTypes(java.lang.reflect.Type type)static java.lang.Class[]getComponentTypes(java.lang.reflect.Type type, java.lang.Class implClass)Returns all component types of the given type.static java.lang.ClassgetGenericSupertype(java.lang.Class type, int index)Shortcut forgetComponentType(type.getGenericSuperclass()).static java.lang.Class[]getGenericSupertypes(java.lang.Class type)Shortcut forgetComponentTypes(type.getGenericSuperclass()).static java.lang.ClassgetRawType(java.lang.reflect.Type type)Returns raw class for giventype.static java.lang.Class<?>getRawType(java.lang.reflect.Type type, java.lang.Class implClass)Returns raw class for giventypewhen implementation class is known and it makes difference.static java.lang.StringgetShortClassName(java.lang.Class clazz)Returns short class name: packages are replaces with single letter.static java.lang.StringgetShortClassName(java.lang.Class clazz, int shortUpTo)static java.lang.Class[]getSuperclasses(java.lang.Class type)Returns all superclasses.static java.lang.reflect.Field[]getSupportedFields(java.lang.Class clazz)static java.lang.reflect.Field[]getSupportedFields(java.lang.Class clazz, java.lang.Class limit)static java.lang.reflect.Method[]getSupportedMethods(java.lang.Class clazz)static java.lang.reflect.Method[]getSupportedMethods(java.lang.Class clazz, java.lang.Class limit)Returns aMethodarray of the methods to which instances of the specified respond except for those methods defined in the class specified by limit or any of its superclasses.static booleanisAllAssignableFrom(java.lang.Class<?>[] typesTarget, java.lang.Class<?>[] typesFrom)Returnstrueif all types are assignable from the other array of types.static booleanisAssignableFrom(java.lang.reflect.Member member1, java.lang.reflect.Member member2)Returnstrueif the first member is accessible from second one.static booleanisBeanProperty(java.lang.reflect.Method method)Returnstrueif method is a bean property.static booleanisBeanPropertyGetter(java.lang.reflect.Method method)Returnstrueif method is bean getter.static booleanisBeanPropertySetter(java.lang.reflect.Method method)Returnstrueif method is bean setter.static booleanisInstanceOf(java.lang.Object object, java.lang.Class target)Safe version ofisInstance, returnsfalseif any of the arguments isnull.static booleanisKotlinClass(java.lang.Class type)Returnstrueif type is a Kotlin class.static booleanisObjectMethod(java.lang.reflect.Method method)Returnstrueif method defined inObjectclass.static booleanisPublic(java.lang.Class c)Returnstrueif class is public.static booleanisPublic(java.lang.reflect.Member member)Returnstrueif class member is public.static booleanisPublicPublic(java.lang.reflect.Member member)Returnstrueif class member is public and if its declaring class is also public.static booleanisTypeOf(java.lang.Class<?> lookupClass, java.lang.Class<?> targetClass)Safe version ofisAssignableFrommethod that returnsfalseif one of the arguments isnull.static booleanisUserDefinedMethod(java.lang.reflect.Method method)Returnstrueif method is user defined and not defined inObjectclass.static java.util.jar.JarFilejarFileOf(java.lang.Class<?> klass)Returns the jar file from which the given class is loaded; or null if no such jar file can be located.static <T> TnewInstance(java.lang.Class<T> type)Creates new instances including for common mutable classes that do not have a default constructor.static <T> TnewInstance(java.lang.Class<T> clazz, java.lang.Object... params)Creates new instance of given class with given optional arguments.static java.lang.ObjectreadAnnotationValue(java.lang.annotation.Annotation annotation, java.lang.String name)Reads annotation value.static java.lang.Class[]resolveAllInterfaces(java.lang.Class type)Resolves all interfaces of a type.static java.lang.Class[]resolveAllSuperclasses(java.lang.Class type)Resolves all super classes, from top (direct subclass) to down.static java.lang.reflect.TyperesolveVariable(java.lang.reflect.TypeVariable variable, java.lang.Class implClass)ResolvesTypeVariablewith given implementation class.static java.lang.StringtypeToString(java.lang.reflect.Type type)ConvertsTypeto aString.
-
-
-
Field Detail
-
EMPTY_CLASS_ARRAY
public static final java.lang.Class[] EMPTY_CLASS_ARRAY
Empty class array.
-
METHOD_GET_PREFIX
public static final java.lang.String METHOD_GET_PREFIX
- See Also:
- Constant Field Values
-
METHOD_IS_PREFIX
public static final java.lang.String METHOD_IS_PREFIX
- See Also:
- Constant Field Values
-
METHOD_SET_PREFIX
public static final java.lang.String METHOD_SET_PREFIX
- See Also:
- Constant Field Values
-
-
Method Detail
-
emptyClassArray
public static <T> java.lang.Class<T>[] emptyClassArray()
-
findMethod
public static java.lang.reflect.Method findMethod(java.lang.Class c, java.lang.String methodName)Returns method from an object, matched by name. This may be considered as a slow operation, since methods are matched one by one. Returns only accessible methods. Only first method is matched.- Parameters:
c- class to examinemethodName- Full name of the method.- Returns:
- null if method not found
-
findDeclaredMethod
public static java.lang.reflect.Method findDeclaredMethod(java.lang.Class c, java.lang.String methodName)- See Also:
findMethod(Class, String)
-
findConstructor
public static <T> java.lang.reflect.Constructor<T> findConstructor(java.lang.Class<T> clazz, java.lang.Class<?>... parameterTypes)Finds constructor with given parameter types. First matched ctor is returned.
-
isAllAssignableFrom
public static boolean isAllAssignableFrom(java.lang.Class<?>[] typesTarget, java.lang.Class<?>[] typesFrom)Returnstrueif all types are assignable from the other array of types.
-
getClasses
public static java.lang.Class[] getClasses(java.lang.Object... objects)
Returns classes from array of objects. It acceptsnullvalues.
-
isTypeOf
public static boolean isTypeOf(java.lang.Class<?> lookupClass, java.lang.Class<?> targetClass)Safe version ofisAssignableFrommethod that returnsfalseif one of the arguments isnull.
-
isInstanceOf
public static boolean isInstanceOf(java.lang.Object object, java.lang.Class target)Safe version ofisInstance, returnsfalseif any of the arguments isnull.
-
resolveAllInterfaces
public static java.lang.Class[] resolveAllInterfaces(java.lang.Class type)
Resolves all interfaces of a type. No duplicates are returned. Direct interfaces are prior the interfaces of subclasses in the returned array.
-
resolveAllSuperclasses
public static java.lang.Class[] resolveAllSuperclasses(java.lang.Class type)
Resolves all super classes, from top (direct subclass) to down.Objectclass is not included in the list.
-
getAccessibleMethods
public static java.lang.reflect.Method[] getAccessibleMethods(java.lang.Class clazz)
Returns array of all methods that are accessible from given class.- See Also:
getAccessibleMethods(Class, Class)
-
getAccessibleMethods
public static java.lang.reflect.Method[] getAccessibleMethods(java.lang.Class clazz, java.lang.Class limit)Returns array of all methods that are accessible from given class, upto limit (usuallyObject.class). Abstract methods are ignored.
-
getAccessibleFields
public static java.lang.reflect.Field[] getAccessibleFields(java.lang.Class clazz)
-
getAccessibleFields
public static java.lang.reflect.Field[] getAccessibleFields(java.lang.Class clazz, java.lang.Class limit)
-
getSupportedMethods
public static java.lang.reflect.Method[] getSupportedMethods(java.lang.Class clazz)
-
getSupportedMethods
public static java.lang.reflect.Method[] getSupportedMethods(java.lang.Class clazz, java.lang.Class limit)Returns aMethodarray of the methods to which instances of the specified respond except for those methods defined in the class specified by limit or any of its superclasses. Note that limit is usually used to eliminate them methods defined byjava.lang.Object. If limit isnullthen all methods are returned.
-
getSupportedFields
public static java.lang.reflect.Field[] getSupportedFields(java.lang.Class clazz)
-
getSupportedFields
public static java.lang.reflect.Field[] getSupportedFields(java.lang.Class clazz, java.lang.Class limit)
-
compareDeclarations
public static boolean compareDeclarations(java.lang.reflect.Method first, java.lang.reflect.Method second)Compares method declarations: signature and return types.
-
compareSignatures
public static boolean compareSignatures(java.lang.reflect.Method first, java.lang.reflect.Method second)Compares method signatures: names and parameters.
-
compareSignatures
public static boolean compareSignatures(java.lang.reflect.Constructor first, java.lang.reflect.Constructor second)Compares constructor signatures: names and parameters.
-
compareSignatures
public static boolean compareSignatures(java.lang.reflect.Field first, java.lang.reflect.Field second)
-
compareParameters
public static boolean compareParameters(java.lang.Class[] first, java.lang.Class[] second)Compares classes, usually method or ctor parameters.
-
forceAccess
public static void forceAccess(java.lang.reflect.AccessibleObject accObject)
Suppress access check against a reflection object. SecurityException is silently ignored. Checks first if the object is already accessible.
-
isPublic
public static boolean isPublic(java.lang.reflect.Member member)
Returnstrueif class member is public.
-
isPublicPublic
public static boolean isPublicPublic(java.lang.reflect.Member member)
Returnstrueif class member is public and if its declaring class is also public.
-
isPublic
public static boolean isPublic(java.lang.Class c)
Returnstrueif class is public.
-
newInstance
public static <T> T newInstance(java.lang.Class<T> clazz, java.lang.Object... params) throws java.lang.InstantiationException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException, java.lang.NoSuchMethodExceptionCreates new instance of given class with given optional arguments.- Throws:
java.lang.InstantiationExceptionjava.lang.IllegalAccessExceptionjava.lang.reflect.InvocationTargetExceptionjava.lang.NoSuchMethodException
-
newInstance
public static <T> T newInstance(java.lang.Class<T> type) throws java.lang.IllegalAccessException, java.lang.InstantiationException, java.lang.NoSuchMethodException, java.lang.reflect.InvocationTargetExceptionCreates new instances including for common mutable classes that do not have a default constructor. more user-friendly. It examines if class is a map, list, String, Character, Boolean or a Number. Immutable instances are cached and not created again. Arrays are also created with no elements. Note that this bunch ofifblocks is faster then using aHashMap.- Throws:
java.lang.IllegalAccessExceptionjava.lang.InstantiationExceptionjava.lang.NoSuchMethodExceptionjava.lang.reflect.InvocationTargetException
-
isAssignableFrom
public static boolean isAssignableFrom(java.lang.reflect.Member member1, java.lang.reflect.Member member2)Returnstrueif the first member is accessible from second one.
-
getSuperclasses
public static java.lang.Class[] getSuperclasses(java.lang.Class type)
Returns all superclasses.
-
isUserDefinedMethod
public static boolean isUserDefinedMethod(java.lang.reflect.Method method)
Returnstrueif method is user defined and not defined inObjectclass.
-
isObjectMethod
public static boolean isObjectMethod(java.lang.reflect.Method method)
Returnstrueif method defined inObjectclass.
-
isBeanProperty
public static boolean isBeanProperty(java.lang.reflect.Method method)
Returnstrueif method is a bean property.
-
isBeanPropertyGetter
public static boolean isBeanPropertyGetter(java.lang.reflect.Method method)
Returnstrueif method is bean getter.
-
getBeanPropertyGetterName
public static java.lang.String getBeanPropertyGetterName(java.lang.reflect.Method method)
Returns property name from a getter method. Returnsnullif method is not a real getter.
-
isBeanPropertySetter
public static boolean isBeanPropertySetter(java.lang.reflect.Method method)
Returnstrueif method is bean setter.
-
getBeanPropertySetterName
public static java.lang.String getBeanPropertySetterName(java.lang.reflect.Method method)
Returns beans property setter name ornullif method is not a real setter.
-
getComponentType
public static java.lang.Class getComponentType(java.lang.reflect.Type type, int index)Returns single component type. Index is used when type consist of many components. If negative, index will be calculated from the end of the returned array. Returnsnullif component type does not exist or if index is out of bounds.
-
getComponentType
public static java.lang.Class getComponentType(java.lang.reflect.Type type, java.lang.Class implClass, int index)Returns single component type for given type and implementation. Index is used when type consist of many components. If negative, index will be calculated from the end of the returned array. Returnsnullif component type does not exist or if index is out of bounds.
-
getComponentTypes
public static java.lang.Class[] getComponentTypes(java.lang.reflect.Type type)
-
getComponentTypes
public static java.lang.Class[] getComponentTypes(java.lang.reflect.Type type, java.lang.Class implClass)Returns all component types of the given type. For example the following types all have the component-type MyClass:- MyClass[]
- List<MyClass>
- Foo<? extends MyClass>
- Bar<? super MyClass>
- <T extends MyClass> T[]
-
getGenericSupertypes
public static java.lang.Class[] getGenericSupertypes(java.lang.Class type)
Shortcut forgetComponentTypes(type.getGenericSuperclass()).
-
getGenericSupertype
public static java.lang.Class getGenericSupertype(java.lang.Class type, int index)Shortcut forgetComponentType(type.getGenericSuperclass()).
-
getRawType
public static java.lang.Class getRawType(java.lang.reflect.Type type)
Returns raw class for giventype. Use this method with both regular and generic types.- Parameters:
type- the type to convert- Returns:
- the closest class representing the given
type - See Also:
getRawType(java.lang.reflect.Type, Class)
-
getRawType
public static java.lang.Class<?> getRawType(java.lang.reflect.Type type, java.lang.Class implClass)Returns raw class for giventypewhen implementation class is known and it makes difference.
-
resolveVariable
public static java.lang.reflect.Type resolveVariable(java.lang.reflect.TypeVariable variable, java.lang.Class implClass)ResolvesTypeVariablewith given implementation class.
-
typeToString
public static java.lang.String typeToString(java.lang.reflect.Type type)
ConvertsTypeto aString. Supports successor interfaces:java.lang.Class- represents usual classjava.lang.reflect.ParameterizedType- class with generic parameter (e.g.List)java.lang.reflect.TypeVariable- generic type literal (e.g.List,T- type variable)java.lang.reflect.WildcardType- wildcard type (List<? extends Number>,"? extends Number- wildcard type)java.lang.reflect.GenericArrayType- type for generic array (e.g.T[],T- array type)
-
readAnnotationValue
public static java.lang.Object readAnnotationValue(java.lang.annotation.Annotation annotation, java.lang.String name)Reads annotation value. Returnsnullon error (e.g. when value name not found).
-
getCallerClass
public static java.lang.Class getCallerClass(int framesToSkip)
EmulatesReflection.getCallerClassusing standard API. This implementation uses customSecurityManagerand it is the fastest. Other implementations are:new Throwable().getStackTrace()[callStackDepth]Thread.currentThread().getStackTrace()[callStackDepth](the slowest)
In case when usage of
SecurityManageris not allowed, this method fails back to the second implementation.Note that original
Reflection.getCallerClassis way faster then any emulation.
-
getCallerClass
public static java.lang.Class getCallerClass()
Smart variant ofgetCallerClass(int)that skips all relevant Jodd calls. However, this one does not use the security manager.
-
findEnum
public static java.lang.Class findEnum(java.lang.Class target)
Returnsenumclass ornullif class is not an enum.
-
childClassOf
public static java.lang.Class<?> childClassOf(java.lang.Class<?> parentClass, java.lang.Object instance)Returns the class of the immediate subclass of the given parent class for the given object instance; or null if such immediate subclass cannot be uniquely identified for the given object instance.
-
jarFileOf
public static java.util.jar.JarFile jarFileOf(java.lang.Class<?> klass)
Returns the jar file from which the given class is loaded; or null if no such jar file can be located.
-
convertClassNameToFileName
public static java.lang.String convertClassNameToFileName(java.lang.Class clazz)
Resolves class file name from class name by replacing dot's with '/' separator and adding class extension at the end. If array, component type is returned.
-
convertClassNameToFileName
public static java.lang.String convertClassNameToFileName(java.lang.String className)
Resolves class file name from class name by replacing dot's with '/' separator.
-
getShortClassName
public static java.lang.String getShortClassName(java.lang.Class clazz)
Returns short class name: packages are replaces with single letter.
-
getShortClassName
public static java.lang.String getShortClassName(java.lang.Class clazz, int shortUpTo)
-
isKotlinClass
public static boolean isKotlinClass(java.lang.Class type)
Returnstrueif type is a Kotlin class.
-
-