Class ClassUtil

java.lang.Object
jodd.util.ClassUtil

public class ClassUtil extends Object
Class utilities.
  • Field Details

  • Constructor Details

    • ClassUtil

      public ClassUtil()
  • Method Details

    • emptyClassArray

      public static <T> Class<T>[] emptyClassArray()
    • findMethod

      public static Method findMethod(Class c, 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 examine
      methodName - Full name of the method.
      Returns:
      null if method not found
    • findDeclaredMethod

      public static Method findDeclaredMethod(Class c, String methodName)
      See Also:
    • findDeclaredMethod

      private static Method findDeclaredMethod(Class c, String methodName, boolean publicOnly)
    • findConstructor

      public static <T> Constructor<T> findConstructor(Class<T> clazz, Class<?>... parameterTypes)
      Finds constructor with given parameter types. First matched ctor is returned.
    • isAllAssignableFrom

      public static boolean isAllAssignableFrom(Class<?>[] typesTarget, Class<?>[] typesFrom)
      Returns true if all types are assignable from the other array of types.
    • getClasses

      public static Class[] getClasses(Object... objects)
      Returns classes from array of objects. It accepts null values.
    • isTypeOf

      public static boolean isTypeOf(Class<?> lookupClass, Class<?> targetClass)
      Safe version of isAssignableFrom method that returns false if one of the arguments is null.
    • isInstanceOf

      public static boolean isInstanceOf(Object object, Class target)
      Safe version of isInstance, returns false if any of the arguments is null.
    • resolveAllInterfaces

      public static Class[] resolveAllInterfaces(Class type)
      Resolves all interfaces of a type. No duplicates are returned. Direct interfaces are prior the interfaces of subclasses in the returned array.
    • _resolveAllInterfaces

      private static void _resolveAllInterfaces(Class type, Set<Class> bag)
    • resolveAllSuperclasses

      public static Class[] resolveAllSuperclasses(Class type)
      Resolves all super classes, from top (direct subclass) to down. Object class is not included in the list.
    • getAccessibleMethods

      public static Method[] getAccessibleMethods(Class clazz)
      Returns array of all methods that are accessible from given class.
      See Also:
    • getAccessibleMethods

      public static Method[] getAccessibleMethods(Class clazz, Class limit)
      Returns array of all methods that are accessible from given class, upto limit (usually Object.class). Abstract methods are ignored.
    • addMethodIfNotExist

      private static void addMethodIfNotExist(List<Method> allMethods, Method newMethod)
    • getAccessibleFields

      public static Field[] getAccessibleFields(Class clazz)
    • getAccessibleFields

      public static Field[] getAccessibleFields(Class clazz, Class limit)
    • addFieldIfNotExist

      private static void addFieldIfNotExist(List<Field> allFields, Field newField)
    • getSupportedMethods

      public static Method[] getSupportedMethods(Class clazz)
    • getSupportedMethods

      public static Method[] getSupportedMethods(Class clazz, Class limit)
      Returns a Method array 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 by java.lang.Object. If limit is null then all methods are returned.
    • getSupportedFields

      public static Field[] getSupportedFields(Class clazz)
    • getSupportedFields

      public static Field[] getSupportedFields(Class clazz, Class limit)
    • compareDeclarations

      public static boolean compareDeclarations(Method first, Method second)
      Compares method declarations: signature and return types.
    • compareSignatures

      public static boolean compareSignatures(Method first, Method second)
      Compares method signatures: names and parameters.
    • compareSignatures

      public static boolean compareSignatures(Constructor first, Constructor second)
      Compares constructor signatures: names and parameters.
    • compareSignatures

      public static boolean compareSignatures(Field first, Field second)
    • compareParameters

      public static boolean compareParameters(Class[] first, Class[] second)
      Compares classes, usually method or ctor parameters.
    • forceAccess

      public static void forceAccess(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(Member member)
      Returns true if class member is public.
    • isPublicPublic

      public static boolean isPublicPublic(Member member)
      Returns true if class member is public and if its declaring class is also public.
    • isPublic

      public static boolean isPublic(Class c)
      Returns true if 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.
      Throws:
      InstantiationException
      IllegalAccessException
      InvocationTargetException
      NoSuchMethodException
    • newInstance

      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 of if blocks is faster then using a HashMap.
      Throws:
      IllegalAccessException
      InstantiationException
      NoSuchMethodException
      InvocationTargetException
    • isAssignableFrom

      public static boolean isAssignableFrom(Member member1, Member member2)
      Returns true if the first member is accessible from second one.
    • getSuperclasses

      public static Class[] getSuperclasses(Class type)
      Returns all superclasses.
    • isUserDefinedMethod

      public static boolean isUserDefinedMethod(Method method)
      Returns true if method is user defined and not defined in Object class.
    • isObjectMethod

      public static boolean isObjectMethod(Method method)
      Returns true if method defined in Object class.
    • isBeanProperty

      public static boolean isBeanProperty(Method method)
      Returns true if method is a bean property.
    • isBeanPropertyGetter

      public static boolean isBeanPropertyGetter(Method method)
      Returns true if method is bean getter.
    • getBeanPropertyGetterPrefixLength

      private static int getBeanPropertyGetterPrefixLength(Method method)
    • getBeanPropertyGetterName

      public static String getBeanPropertyGetterName(Method method)
      Returns property name from a getter method. Returns null if method is not a real getter.
    • isBeanPropertySetter

      public static boolean isBeanPropertySetter(Method method)
      Returns true if method is bean setter.
    • getBeanPropertySetterPrefixLength

      private static int getBeanPropertySetterPrefixLength(Method method)
    • getBeanPropertySetterName

      public static String getBeanPropertySetterName(Method method)
      Returns beans property setter name or null if method is not a real setter.
    • getComponentType

      public static Class getComponentType(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. Returns null if component type does not exist or if index is out of bounds.
      See Also:
    • getComponentType

      public static Class getComponentType(Type type, 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. Returns null if component type does not exist or if index is out of bounds.

      See Also:
    • getComponentTypes

      public static Class[] getComponentTypes(Type type)
      See Also:
    • getComponentTypes

      public static Class[] getComponentTypes(Type type, 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 Class[] getGenericSupertypes(Class type)
      Shortcut for getComponentTypes(type.getGenericSuperclass()).
      See Also:
    • getGenericSupertype

      public static Class getGenericSupertype(Class type, int index)
      Shortcut for getComponentType(type.getGenericSuperclass()).
      See Also:
    • getRawType

      public static Class getRawType(Type type)
      Returns raw class for given type. 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

      public static Class<?> getRawType(Type type, Class implClass)
      Returns raw class for given type when implementation class is known and it makes difference.
      See Also:
    • resolveVariable

      public static Type resolveVariable(TypeVariable variable, Class implClass)
      Resolves TypeVariable with given implementation class.
    • typeToString

      public static String typeToString(Type type)
      Converts Type to a String. Supports successor interfaces:
      • java.lang.Class - represents usual class
      • java.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

      private static void typeToString(StringBuilder sb, Type type, Set<Type> visited)
    • readAnnotationValue

      public static Object readAnnotationValue(Annotation annotation, String name)
      Reads annotation value. Returns null on error (e.g. when value name not found).
    • getCallerClass

      public static Class getCallerClass(int framesToSkip)
      Emulates Reflection.getCallerClass using standard API. This implementation uses custom SecurityManager and it is the fastest. Other implementations are:
      • new Throwable().getStackTrace()[callStackDepth]
      • Thread.currentThread().getStackTrace()[callStackDepth] (the slowest)

      In case when usage of SecurityManager is not allowed, this method fails back to the second implementation.

      Note that original Reflection.getCallerClass is way faster then any emulation.

    • getCallerClass

      public static Class getCallerClass()
      Smart variant of getCallerClass(int) that skips all relevant Jodd calls. However, this one does not use the security manager.
    • findEnum

      public static Class findEnum(Class target)
      Returns enum class or null if class is not an enum.
    • childClassOf

      public 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.
    • jarFileOf

      public static JarFile jarFileOf(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 String convertClassNameToFileName(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 String convertClassNameToFileName(String className)
      Resolves class file name from class name by replacing dot's with '/' separator.
    • getShortClassName

      public static String getShortClassName(Class clazz)
      Returns short class name: packages are replaces with single letter.
    • getShortClassName

      public static String getShortClassName(Class clazz, int shortUpTo)
    • isKotlinClass

      public static boolean isKotlinClass(Class type)
      Returns true if type is a Kotlin class.