Class Classes

java.lang.Object
org.sosy_lab.common.Classes

public final class Classes extends Object
Helper class for various methods related to handling Java classes and types.
  • Field Details

  • Method Details

    • getCodeLocation

      public static Path getCodeLocation(Class<?> cls)
      Return the Path to the location of the code of the given class, e.g., the JAR file. If the class is in a *.class file, the base directory of the package structure is returned.
    • createInstance

      @Deprecated public static <T> T createInstance(Class<? extends T> cls, Class<?> @Nullable [] argumentTypes, Object @Nullable [] argumentValues, Class<T> type) throws Classes.ClassInstantiationException, InvocationTargetException
      Deprecated.
      Creates an instance of class cls, passing the objects from argumentList to the constructor and casting the object to class type.
      Parameters:
      cls - The class to instantiate.
      argumentTypes - Array with the types of the parameters of the desired constructor.
      argumentValues - Array with the values that will be passed to the constructor.
      type - The return type (has to be a super type of the class, of course).
      Throws:
      Classes.ClassInstantiationException - If something goes wrong (like class cannot be found or has no constructor).
      InvocationTargetException - If the constructor throws an exception.
    • createInstance

      @Deprecated public static <T> T createInstance(Class<T> type, Class<? extends T> cls, Class<?> @Nullable [] argumentTypes, Object[] argumentValues) throws InvalidConfigurationException
      Deprecated.
      Creates an instance of class cls, passing the objects from argumentList to the constructor and casting the object to class type.

      If there is no matching constructor or the the class cannot be instantiated, an InvalidConfigurationException is thrown.

      Parameters:
      type - The return type (has to be a super type of the class, of course).
      cls - The class to instantiate.
      argumentTypes - Array with the types of the parameters of the desired constructor (optional).
      argumentValues - Array with the values that will be passed to the constructor.
      Throws:
      InvalidConfigurationException
    • createInstance

      @Deprecated public static <T, X extends Exception> T createInstance(Class<T> type, Class<? extends T> cls, Class<?> @Nullable [] argumentTypes, Object[] argumentValues, Class<X> exceptionType) throws X, InvalidConfigurationException
      Deprecated.
      Creates an instance of class cls, passing the objects from argumentList to the constructor and casting the object to class type.

      If there is no matching constructor or the the class cannot be instantiated, an InvalidConfigurationException is thrown.

      Parameters:
      type - The return type (has to be a super type of the class, of course).
      cls - The class to instantiate.
      argumentTypes - Array with the types of the parameters of the desired constructor (optional).
      argumentValues - Array with the values that will be passed to the constructor.
      exceptionType - An exception type the constructor is allowed to throw.
      Throws:
      X extends Exception
      InvalidConfigurationException
    • forName

      public static Class<?> forName(String name, @Nullable String prefix) throws ClassNotFoundException
      Similar to Class.forName(String), but if the class is not found this method re-tries with a package name prefixed.
      Parameters:
      name - The class name.
      prefix - An optional package name as prefix.
      Returns:
      The class object for name or prefix + "." + name
      Throws:
      ClassNotFoundException - If none of the two classes can be found.
      SecurityException - If a security manager denies access to the class loader
    • verifyDeclaredExceptions

      public static @Nullable String verifyDeclaredExceptions(Executable executable, Class<?>... allowedExceptionTypes)
      Verify that a constructor or method declares no other checked exceptions except a given type.

      Returns the name of any violating exception, or null if there is none.

      Parameters:
      executable - The executable to check.
      allowedExceptionTypes - The type of exception that is allowed.
      Returns:
      Null or the name of a declared exception.
    • verifyDeclaredExceptions

      public static @Nullable String verifyDeclaredExceptions(Invokable<?,?> invokable, Class<?>... allowedExceptionTypes)
      Verify that a constructor or method declares no other checked exceptions except a given type.

      Returns the name of any violating exception, or null if there is none.

      Parameters:
      invokable - The invokable to check.
      allowedExceptionTypes - The type of exception that is allowed.
      Returns:
      Null or the name of a declared exception.
    • getSingleTypeArgument

      public static TypeToken<?> getSingleTypeArgument(TypeToken<?> type)
    • getSingleTypeArgument

      public static Type getSingleTypeArgument(Type type)
      From a type X<Foo>, extract the Foo. This is the value of ParameterizedType.getActualTypeArguments(). This method also supports X<? extends Foo>, X<Foo<?>> etc.

      Example results:

      
       X<Foo>          : Foo
       X<? extends Foo>: Foo
       X<Foo<Bar>>     : Foo<Bar>
       
      Parameters:
      type - The type (needs to be parameterized with exactly one parameter)
      Returns:
      A Type object.
    • extractUpperBoundFromType

      public static Type extractUpperBoundFromType(Type type)
      Simplify a Type instance: if it is a wildcard generic type, replace it with its upper bound.

      It does not support wildcards with several upper bounds.

      Parameters:
      type - A possibly generic type.
      Returns:
      The type or its simplification.
    • produceClassLoadingWarning

      public static void produceClassLoadingWarning(LogManager logger, Class<?> cls, @Nullable Class<?> type)
    • makeExtendedURLClassLoader

      public static Classes.ClassLoaderBuilder<?> makeExtendedURLClassLoader()
      Create a class loader that is based on an URLClassLoader but implements some additional features. This method returns a builder that can be used to configure the new class loader.
    • createFactory

      public static <I> I createFactory(Class<I> factoryType, Class<?> cls) throws Classes.UnsuitedClassException
      Create a factory at runtime that implements the interface factoryType and delegates to either a constructor or a static factory method of cls.

      The factory interface needs to have exactly one method. The target class needs to have either a single public static method name create, or a single public constructor. The declared exceptions of the static method/constructor need to be a subset of those of the method of the factory interface, and the same holds for the parameters. Parameters that are annotated with an annotation named Nullable or NullableDecl may be missing in the factory interface.

      Parameters:
      factoryType - The factory interface
      cls - The class which should be instantiated by the returned factory
      Returns:
      An implementation of factoryType that instantiates cls
      Throws:
      Classes.UnsuitedClassException - If the static method/constructor of cls does not fulfill the restrictions of the factory interface
    • createFactory

      public static <I> I createFactory(TypeToken<I> factoryType, Class<?> cls) throws Classes.UnsuitedClassException
      Create a factory at runtime that implements the interface factoryType and delegates to either a constructor or a static factory method of cls.

      The factory interface needs to have exactly one method. The target class needs to have either a single public static method name create, or a single public constructor. The declared exceptions of the static method/constructor need to be a subset of those of the method of the factory interface, and the same holds for the parameters. Parameters that are annotated with an annotation named Nullable or NullableDecl may be missing in the factory interface.

      Parameters:
      factoryType - A type token that represents the factory interface
      cls - The class which should be instantiated by the returned factory
      Returns:
      An implementation of factoryType that instantiates cls
      Throws:
      Classes.UnsuitedClassException - If the static method/constructor of cls does not fulfill the restrictions of the factory interface