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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Classes.ClassInstantiationException
Deprecated.static class
Classes.ClassLoaderBuilder<B extends Classes.ClassLoaderBuilder<B>>
A builder for class loaders with more features thanURLClassLoader
.static class
Classes.UnexpectedCheckedException
An exception that should be used if a checked exception is encountered in a situation where it is not excepted (e.g., when getting the result from aCallable
of which you know it shouldn't throw such exceptions).static class
Classes.UnsuitedClassException
Exception thrown whencreateFactory(TypeToken, Class)
is called with a class that does not satisfy the requirements of the factory interface.
-
Field Summary
Fields Modifier and Type Field Description static Predicate<Class<?>>
IS_GENERATED
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static <I> I
createFactory(TypeToken<I> factoryType, Class<?> cls)
Create a factory at runtime that implements the interfacefactoryType
and delegates to either a constructor or a static factory method ofcls
.static <I> I
createFactory(Class<I> factoryType, Class<?> cls)
Create a factory at runtime that implements the interfacefactoryType
and delegates to either a constructor or a static factory method ofcls
.static <T> T
createInstance(Class<? extends T> cls, Class<?> @Nullable [] argumentTypes, Object @Nullable [] argumentValues, Class<T> type)
Deprecated.static <T> T
createInstance(Class<T> type, Class<? extends T> cls, Class<?> @Nullable [] argumentTypes, Object[] argumentValues)
Deprecated.static <T,X extends Exception>
TcreateInstance(Class<T> type, Class<? extends T> cls, Class<?> @Nullable [] argumentTypes, Object[] argumentValues, Class<X> exceptionType)
Deprecated.static Type
extractUpperBoundFromType(Type type)
Simplify aType
instance: if it is a wildcard generic type, replace it with its upper bound.static Class<?>
forName(String name, @Nullable String prefix)
Similar toClass.forName(String)
, but if the class is not found this method re-tries with a package name prefixed.static Path
getCodeLocation(Class<?> cls)
Return thePath
to the location of the code of the given class, e.g., the JAR file.static TypeToken<?>
getSingleTypeArgument(TypeToken<?> type)
static Type
getSingleTypeArgument(Type type)
From a typeX<Foo>
, extract theFoo
.static Classes.ClassLoaderBuilder<?>
makeExtendedURLClassLoader()
Create a class loader that is based on anURLClassLoader
but implements some additional features.static void
produceClassLoadingWarning(LogManager logger, Class<?> cls, @Nullable Class<?> type)
static @Nullable String
verifyDeclaredExceptions(Invokable<?,?> invokable, Class<?>... allowedExceptionTypes)
Verify that a constructor or method declares no other checked exceptions except a given type.static @Nullable String
verifyDeclaredExceptions(Executable executable, Class<?>... allowedExceptionTypes)
Verify that a constructor or method declares no other checked exceptions except a given type.
-
-
-
Method Detail
-
getCodeLocation
public static Path getCodeLocation(Class<?> cls)
Return thePath
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 extends Exception, 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 toClass.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 Type getSingleTypeArgument(Type type)
From a typeX<Foo>
, extract theFoo
. This is the value ofParameterizedType.getActualTypeArguments()
. This method also supportsX<? 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 aType
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 anURLClassLoader
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 interfacefactoryType
and delegates to either a constructor or a static factory method ofcls
.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 namedNullable
orNullableDecl
may be missing in the factory interface.- Parameters:
factoryType
- The factory interfacecls
- The class which should be instantiated by the returned factory- Returns:
- An implementation of
factoryType
that instantiatescls
- Throws:
Classes.UnsuitedClassException
- If the static method/constructor ofcls
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 interfacefactoryType
and delegates to either a constructor or a static factory method ofcls
.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 namedNullable
orNullableDecl
may be missing in the factory interface.- Parameters:
factoryType
- A type token that represents the factory interfacecls
- The class which should be instantiated by the returned factory- Returns:
- An implementation of
factoryType
that instantiatescls
- Throws:
Classes.UnsuitedClassException
- If the static method/constructor ofcls
does not fulfill the restrictions of the factory interface
-
-