Class ClassUtil
java.lang.Object
jodd.util.ClassUtil
Class utilities.
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Class[]Empty class array.static final Stringstatic final Stringstatic final Stringprivate static ClassUtil.ReflectUtilSecurityManager -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static void_resolveAllInterfaces(Class type, Set<Class> bag) private static voidaddFieldIfNotExist(List<Field> allFields, Field newField) private static voidaddMethodIfNotExist(List<Method> allMethods, Method newMethod) static Class<?> childClassOf(Class<?> parentClass, 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(Method first, Method second) Compares method declarations: signature and return types.static booleancompareParameters(Class[] first, Class[] second) Compares classes, usually method or ctor parameters.static booleancompareSignatures(Constructor first, Constructor second) Compares constructor signatures: names and parameters.static booleancompareSignatures(Field first, Field second) static booleancompareSignatures(Method first, Method second) Compares method signatures: names and parameters.static StringconvertClassNameToFileName(Class clazz) Resolves class file name from class name by replacing dot's with '/' separator and adding class extension at the end.static StringconvertClassNameToFileName(String className) Resolves class file name from class name by replacing dot's with '/' separator.static <T> Class<T>[]static <T> Constructor<T> findConstructor(Class<T> clazz, Class<?>... parameterTypes) Finds constructor with given parameter types.static MethodfindDeclaredMethod(Class c, String methodName) private static MethodfindDeclaredMethod(Class c, String methodName, boolean publicOnly) static ClassReturnsenumclass ornullif class is not an enum.static MethodfindMethod(Class c, String methodName) Returns method from an object, matched by name.static voidforceAccess(AccessibleObject accObject) Suppress access check against a reflection object.static Field[]getAccessibleFields(Class clazz) static Field[]getAccessibleFields(Class clazz, Class limit) static Method[]getAccessibleMethods(Class clazz) Returns array of all methods that are accessible from given class.static Method[]getAccessibleMethods(Class clazz, Class limit) Returns array of all methods that are accessible from given class, upto limit (usuallyObject.class).static StringgetBeanPropertyGetterName(Method method) Returns property name from a getter method.private static intstatic StringgetBeanPropertySetterName(Method method) Returns beans property setter name ornullif method is not a real setter.private static intstatic ClassSmart variant ofgetCallerClass(int)that skips all relevant Jodd calls.static ClassgetCallerClass(int framesToSkip) EmulatesReflection.getCallerClassusing standard API.static Class[]getClasses(Object... objects) Returns classes from array of objects.static ClassgetComponentType(Type type, int index) Returns single component type.static ClassgetComponentType(Type type, Class implClass, int index) Returns single component type for given type and implementation.static Class[]getComponentTypes(Type type) static Class[]getComponentTypes(Type type, Class implClass) Returns all component types of the given type.static ClassgetGenericSupertype(Class type, int index) Shortcut forgetComponentType(type.getGenericSuperclass()).static Class[]getGenericSupertypes(Class type) Shortcut forgetComponentTypes(type.getGenericSuperclass()).static ClassgetRawType(Type type) Returns raw class for giventype.static Class<?> getRawType(Type type, Class implClass) Returns raw class for giventypewhen implementation class is known and it makes difference.static StringgetShortClassName(Class clazz) Returns short class name: packages are replaces with single letter.static StringgetShortClassName(Class clazz, int shortUpTo) static Class[]getSuperclasses(Class type) Returns all superclasses.static Field[]getSupportedFields(Class clazz) static Field[]getSupportedFields(Class clazz, Class limit) static Method[]getSupportedMethods(Class clazz) static Method[]getSupportedMethods(Class clazz, 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(Class<?>[] typesTarget, Class<?>[] typesFrom) Returnstrueif all types are assignable from the other array of types.static booleanisAssignableFrom(Member member1, Member member2) Returnstrueif the first member is accessible from second one.static booleanisBeanProperty(Method method) Returnstrueif method is a bean property.static booleanisBeanPropertyGetter(Method method) Returnstrueif method is bean getter.static booleanisBeanPropertySetter(Method method) Returnstrueif method is bean setter.static booleanisInstanceOf(Object object, Class target) Safe version ofisInstance, returnsfalseif any of the arguments isnull.static booleanisKotlinClass(Class type) Returnstrueif type is a Kotlin class.static booleanisObjectMethod(Method method) Returnstrueif method defined inObjectclass.static booleanReturnstrueif class is public.static booleanReturnstrueif class member is public.static booleanisPublicPublic(Member member) Returnstrueif class member is public and if its declaring class is also public.static booleanSafe version ofisAssignableFrommethod that returnsfalseif one of the arguments isnull.static booleanisUserDefinedMethod(Method method) Returnstrueif method is user defined and not defined inObjectclass.static JarFileReturns the jar file from which the given class is loaded; or null if no such jar file can be located.static <T> TnewInstance(Class<T> type) Creates new instances including for common mutable classes that do not have a default constructor.static <T> TnewInstance(Class<T> clazz, Object... params) Creates new instance of given class with given optional arguments.static ObjectreadAnnotationValue(Annotation annotation, String name) Reads annotation value.static Class[]resolveAllInterfaces(Class type) Resolves all interfaces of a type.static Class[]resolveAllSuperclasses(Class type) Resolves all super classes, from top (direct subclass) to down.static TyperesolveVariable(TypeVariable variable, Class implClass) ResolvesTypeVariablewith given implementation class.static StringtypeToString(Type type) ConvertsTypeto aString.private static voidtypeToString(StringBuilder sb, Type type, Set<Type> visited)
-
Field Details
-
EMPTY_CLASS_ARRAY
Empty class array. -
METHOD_GET_PREFIX
- See Also:
-
METHOD_IS_PREFIX
- See Also:
-
METHOD_SET_PREFIX
- See Also:
-
SECURITY_MANAGER
-
-
Constructor Details
-
ClassUtil
public ClassUtil()
-
-
Method Details
-
emptyClassArray
-
findMethod
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
-
findDeclaredMethod
-
findConstructor
Finds constructor with given parameter types. First matched ctor is returned. -
isAllAssignableFrom
-
getClasses
-
isTypeOf
-
isInstanceOf
-
resolveAllInterfaces
-
_resolveAllInterfaces
-
resolveAllSuperclasses
-
getAccessibleMethods
-
getAccessibleMethods
-
addMethodIfNotExist
-
getAccessibleFields
-
getAccessibleFields
-
addFieldIfNotExist
-
getSupportedMethods
-
getSupportedMethods
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
-
getSupportedFields
-
compareDeclarations
-
compareSignatures
-
compareSignatures
Compares constructor signatures: names and parameters. -
compareSignatures
-
compareParameters
-
forceAccess
Suppress access check against a reflection object. SecurityException is silently ignored. Checks first if the object is already accessible. -
isPublic
Returnstrueif class member is public. -
isPublicPublic
Returnstrueif class member is public and if its declaring class is also public. -
isPublic
Returnstrueif class is public. -
newInstance
public static <T> T newInstance(Class<T> clazz, Object... params) throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException Creates new instance of given class with given optional arguments. -
newInstance
public static <T> T newInstance(Class<T> type) throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException Creates 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. -
isAssignableFrom
-
getSuperclasses
-
isUserDefinedMethod
Returnstrueif method is user defined and not defined inObjectclass. -
isObjectMethod
Returnstrueif method defined inObjectclass. -
isBeanProperty
Returnstrueif method is a bean property. -
isBeanPropertyGetter
Returnstrueif method is bean getter. -
getBeanPropertyGetterPrefixLength
-
getBeanPropertyGetterName
-
isBeanPropertySetter
Returnstrueif method is bean setter. -
getBeanPropertySetterPrefixLength
-
getBeanPropertySetterName
-
getComponentType
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.- See Also:
-
getComponentType
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.- See Also:
-
getComponentTypes
-
getComponentTypes
-
getGenericSupertypes
-
getGenericSupertype
-
getRawType
-
getRawType
-
resolveVariable
ResolvesTypeVariablewith given implementation class. -
typeToString
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)
-
typeToString
-
readAnnotationValue
Reads annotation value. Returnsnullon error (e.g. when value name not found). -
getCallerClass
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
Smart variant ofgetCallerClass(int)that skips all relevant Jodd calls. However, this one does not use the security manager. -
findEnum
-
childClassOf
-
jarFileOf
-
convertClassNameToFileName
-
convertClassNameToFileName
-
getShortClassName
-
getShortClassName
-
isKotlinClass
Returnstrueif type is a Kotlin class.
-