Class ClassUtil

java.lang.Object
org.djutils.reflection.ClassUtil

public final class ClassUtil
extends Object
ClassUtil is a utility class providing assistance for Java Classes.

Copyright (c) 2002-2020 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See for project information https://djutils.org. The DJUTILS project is distributed under a three-clause BSD-style license, which can be found at https://djutils.org/docs/license.html.

Author:
Peter Jacobs, Niels Lang, Alexander Verbraeck
  • Method Details

    • getAllConstructors

      public static <T> Constructor<T>[] getAllConstructors​(Class<T> clazz)
      Returns all the constructors of a class. Public, package, protected and private constructors are returned. This method returns an array of length 0 if the class object represents an interface, a primitive type, an array class, or void. Note that the constructors of the superclass are not returned as these can never be invoked.
      Type Parameters:
      T - the class of the constructors
      Parameters:
      clazz - Class<?>; the class to resolve the constructors for.
      Returns:
      an array with all constructors
    • resolveConstructor

      public static <T> Constructor<T> resolveConstructor​(Class<T> clazz, Class<?> callerClass, Class<?>[] parameterTypes) throws NoSuchMethodException, IllegalAccessException
      Returns the constructor of a class with a particular signature if and only if the caller class can invoke that constructor. So a private constructor will not be invoked, unless the caller class is the class that defines the constructor. For a protected class the superclass and classes in the same package can invoke the constructor, but other classes should not be able to invoke the constructor.
      Type Parameters:
      T - the class of the constructor
      Parameters:
      clazz - Class<?>; the class for which the constructor needs to be
      callerClass - Class<?>; the calling class for which the test is carried out whether the constructor is visible
      parameterTypes - Class<?>[]; the parameter types for the constructor's signature
      Returns:
      the retrieved constructor
      Throws:
      NoSuchMethodException - if the constructor with the given signature does not exist
      IllegalAccessException - if the constructor exists but is not callable from the callerClass
    • resolveConstructor

      public static <T> Constructor<T> resolveConstructor​(Class<T> clazz, Class<?>[] parameterTypes) throws NoSuchMethodException
      returns the interface method.
      Type Parameters:
      T - the class of the constructor
      Parameters:
      clazz - Class<?>; the class to start with
      parameterTypes - Class<?>[]; the parameterTypes
      Returns:
      Constructor
      Throws:
      NoSuchMethodException - if the method cannot be resolved
    • resolveConstructor

      public static <T> Constructor<T> resolveConstructor​(Class<T> clazz, Object[] arguments) throws NoSuchMethodException
      returns the constructor.
      Type Parameters:
      T - the class of the constructor
      Parameters:
      clazz - Class<?>; the clazz to start with
      arguments - Object[]; the arguments
      Returns:
      Constructor
      Throws:
      NoSuchMethodException - on lookup failure
    • matchSignature

      public static boolean matchSignature​(Constructor<?> constructor, Class<?>[] argTypes)
      Filters an array methods for signatures that are compatible with a given signature.
      Parameters:
      constructor - Constructor<?>; which are constructors to be filtered.
      argTypes - Class<?>[]; are the constructor's argument types
      Returns:
      boolean if methodParameters assignable from argTypes
    • matchSignature

      public static <T> Constructor<T>[] matchSignature​(Constructor<T>[] constructors, Class<?>[] argTypes)
      Filters an array methods for signatures that are compatible with a given signature.
      Type Parameters:
      T - the class of the constructor
      Parameters:
      constructors - Constructor<?>[]; which are constructors to be filtered.
      argTypes - Class<?>[]; are the constructor's argument types
      Returns:
      Constructor<?>[] An unordered Constructor-array consisting of the elements of 'constructors' that match with the given signature. An array with 0 elements is returned when no matching Method objects are found.
    • getAllFields

      public static Set<Field> getAllFields​(Class<?> clazz, Set<Field> result)
      gets all the fields of a class (public, protected, package, and private) and adds the result to the return value.
      Parameters:
      clazz - Class<?>; the class
      result - Set<Field>; the resulting set
      Returns:
      the set of fields including all fields of the field clazz
    • getAllFields

      public static Set<Field> getAllFields​(Class<?> clazz)
      gets all the fields of a class (public, protected, package, and private).
      Parameters:
      clazz - Class<?>; the class
      Returns:
      all fields of the class
    • resolveField

      public static Field resolveField​(Class<?> clazz, String fieldName) throws NoSuchFieldException
      resolves the field for a class, taking into account inner classes.
      Parameters:
      clazz - the class to resolve the field for, including inner classes
      fieldName - name of the field
      Returns:
      Field the field
      Throws:
      NoSuchFieldException - on no such field
    • resolveField

      public static Field resolveField​(Class<?> clazz, Class<?> callerClass, String name) throws NoSuchFieldException
      returns the field.
      Parameters:
      clazz - Class<?>; the class to start with
      callerClass - Class<?>; the calling class
      name - String; the fieldName
      Returns:
      Constructor
      Throws:
      NoSuchFieldException - if the method cannot be resolved
    • resolveField

      public static Field resolveField​(Object object, String fieldName) throws NoSuchFieldException
      resolves the field for a given object instance.
      Parameters:
      object - Object; the object to resolve the field for
      fieldName - String; name of the field to resolve
      Returns:
      the field (if found)
      Throws:
      NoSuchFieldException - if the field cannot be resolved
    • getAllMethods

      public static List<Method> getAllMethods​(Class<?> clazz, List<Method> result)
      gets all the methods of a class (public, protected, package, and private) and adds the result to the return value.
      Parameters:
      clazz - Class<?>; the class
      result - Set<Field>; the resulting set
      Returns:
      the set of methods including all methods of the field clazz
    • getAllMethods

      public static List<Method> getAllMethods​(Class<?> clazz)
      gets all the methods of a class (public, protected, package, and private).
      Parameters:
      clazz - Class<?>; the class
      Returns:
      all methods of the class
    • getAllMethods

      public static List<Method> getAllMethods​(Class<?> clazz, String name, List<Method> result)
      gets all the methods of a class (public, protected, package, and private) with a certain name and adds the result to the return value.
      Parameters:
      clazz - Class<?>; the class
      name - String; the name of the method to look up
      result - Set<Field>; the resulting set
      Returns:
      the set of methods including all methods with the given name of the field clazz
    • getAllMethods

      public static List<Method> getAllMethods​(Class<?> clazz, String name)
      gets all the methods of a class (public, protected, package, and private) with a certain name.
      Parameters:
      clazz - Class<?>; the class
      name - String; the name of the method to look up
      Returns:
      all methods of the class with a certain name
    • resolveMethod

      public static Method resolveMethod​(Class<?> clazz, Class<?> callerClass, String name, Class<?>[] parameterTypes) throws NoSuchMethodException
      returns the interface method.
      Parameters:
      clazz - Class<?>; the class to start with
      callerClass - Class<?>; the caller class
      name - String; the name of the method
      parameterTypes - Class<?>[]; the parameterTypes
      Returns:
      Method
      Throws:
      NoSuchMethodException - on lookup failure
    • resolveMethod

      public static Method resolveMethod​(Class<?> clazz, String name, Class<?>[] parameterTypes) throws NoSuchMethodException
      returns the interface method.
      Parameters:
      clazz - Class<?>; the class to start with
      name - String; the name of the method
      parameterTypes - Class<?>[]; the parameterTypes
      Returns:
      Method
      Throws:
      NoSuchMethodException - on lookup failure
    • resolveMethod

      public static Method resolveMethod​(Object object, String name, Class<?>[] parameterTypes) throws NoSuchMethodException
      resolves a method the method.
      Parameters:
      object - Object; the object to start with
      name - String; the name of the method
      parameterTypes - Class<?>[]; the parameterTypes
      Returns:
      Method
      Throws:
      NoSuchMethodException - on lookup failure
    • resolveMethod

      public static Method resolveMethod​(Object object, String name, Object[] arguments) throws NoSuchMethodException
      returns the method.
      Parameters:
      object - Object; the object to start with
      name - String; the name of the method
      arguments - Object[]; the arguments
      Returns:
      Method
      Throws:
      NoSuchMethodException - on lookup failure
    • getAllAnnotations

      public static Set<Annotation> getAllAnnotations​(Class<?> clazz, Set<Annotation> result)
      gets all the annotations of a class (public, protected, package, and private) and adds the result to the return value.
      Parameters:
      clazz - Class<?>; the class
      result - Set<Annotation>; the resulting set
      Returns:
      the set of annotations including all annotations of the annotation clazz
    • getAllAnnotations

      public static Set<Annotation> getAllAnnotations​(Class<?> clazz)
      gets all the annotations of a class (public, protected, package, and private).
      Parameters:
      clazz - Class<?>; the class
      Returns:
      all annotations of the class
    • resolveAnnotation

      public static Annotation resolveAnnotation​(Class<?> clazz, Class<? extends Annotation> annotationClass) throws NoSuchElementException
      resolves the annotation for a class, taking into account inner classes.
      Parameters:
      clazz - the class to resolve the annotation for, including inner classes
      annotationClass - class of the annotation
      Returns:
      Annotation the annotation
      Throws:
      NoSuchElementException - on no such annotation
    • isVisible

      public static boolean isVisible​(int modifiers, Class<?> declaringClass, Class<?> caller)
      Returns whether a declaringClass is accessible according to the modifiers.
      Parameters:
      modifiers - int; the modifiers
      declaringClass - Class<?>; the declaringClass
      caller - Class<?>; the caller
      Returns:
      boolean isVisible
    • isMoreSpecific

      public static boolean isMoreSpecific​(Class<?>[] a, Class<?>[] b)
      Determines & returns whether constructor 'a' is more specific than constructor 'b', as defined in the Java Language Specification par 15.12.
      Parameters:
      a - Class<?>[]; reflects the first constructor
      b - Class<?>[]; reflects the second constructor
      Returns:
      true if 'a' is more specific than b, false otherwise. 'false' is also returned when constructors are incompatible, e.g. have different names or a different number of parameters.
    • isMoreSpecific

      public static boolean isMoreSpecific​(Constructor<?> a, Constructor<?> b)
      Determines & returns whether constructor 'a' is more specific than constructor 'b', as defined in the Java Language Specification par 15.12.
      Parameters:
      a - Constructor<?>; the first constructor
      b - Constructor<?>; the second constructor
      Returns:
      true if 'a' is more specific than b, false otherwise. 'false' is also returned when constructors are incompatible, e.g. have different names or a different number of parameters.
    • isMoreSpecific

      public static boolean isMoreSpecific​(Method a, Method b)
      Determines & returns whether constructor 'a' is more specific than constructor 'b', as defined in the Java Language Specification par 15.12.
      Parameters:
      a - Method; reflects the first method
      b - Method; reflects the second method
      Returns:
      true if 'a' is more specific than b, false otherwise. 'false' is also returned when constructors are incompatible, e.g. have different names or a different number of parameters.
    • isVisible

      public static boolean isVisible​(Field field, Class<?> caller)
      Returns whether a field is visible for a caller.
      Parameters:
      field - Field; The field
      caller - Class<?>; The class of the caller for whom invocation visibility is checked.
      Returns:
      boolean yes or no
    • isVisible

      public static boolean isVisible​(Constructor<?> constructor, Class<?> caller)
      Returns whether a constructor is visible for a caller.
      Parameters:
      constructor - Constructor<?>; The constructor
      caller - Class<?>; The class of the caller for whom invocation visibility is checked.
      Returns:
      boolean yes or no
    • isVisible

      public static boolean isVisible​(Method method, Class<?> caller)
      Returns whether a method is visible for a caller.
      Parameters:
      method - Method; The method
      caller - Class<?>; The class of the caller for whom invocation visibility is checked.
      Returns:
      boolean yes or no
    • matchSignature

      public static List<Method> matchSignature​(List<Method> methods, String name, Class<?>[] argTypes)
      Filters an array methods for signatures that are compatible with a given signature.
      Parameters:
      methods - Method[]; which are methods to be filtered.
      name - String; reflects the method's name, part of the signature
      argTypes - Class<?>[]; are the method's argument types
      Returns:
      Method[] An unordered Method-array consisting of the elements of 'methods' that match with the given signature. An array with 0 elements is returned when no matching Method objects are found.
    • matchSignature

      public static boolean matchSignature​(Method method, String name, Class<?>[] argTypes)
      Filters an array methods for signatures that are compatible with a given signature.
      Parameters:
      method - Method; The method to be filtered.
      name - String; reflects the method's name, part of the signature
      argTypes - Class<?>[]; are the method's argument types
      Returns:
      boolean if methodParameters assignable from argTypes
    • getClass

      public static Class<?>[] getClass​(Object[] array)
      Converts an array of objects to their corresponding classes. Note that primitive types are always autoboxed to the corresponding object types. So an int in the array will have an Integer.class at the position in the resulting Class array.
      Parameters:
      array - Object[]; the array to invoke
      Returns:
      Class<?>[] the result;
    • classFileDescriptor

      public static ClassUtil.ClassFileDescriptor classFileDescriptor​(Object object)
      Retrieve a file pointer of a class, e.g. to request the last compilation date.
      Parameters:
      object - Object; the object for which the class information should be retrieved
      Returns:
      a ClassFileDescriptor with some information of the .class file
    • classFileDescriptor

      public static ClassUtil.ClassFileDescriptor classFileDescriptor​(Class<?> clazz)
      Retrieve a file pointer of a class, e.g. to request the last compilation date.
      Parameters:
      clazz - Class<?>; the class for which a file descriptor should be retrieved
      Returns:
      a ClassFileDescriptor with some information of the .class file
    • classFileDescriptor

      public static ClassUtil.ClassFileDescriptor classFileDescriptor​(URL clazzUrl)
      Retrieve a file pointer of a class, e.g. to request the last compilation date.
      Parameters:
      clazzUrl - URL; the URL to a class for which a file descriptor should be retrieved
      Returns:
      a ClassFileDescriptor with some information of the .class file