Package io.micronaut.core.reflect
Class ClassUtils
- java.lang.Object
-
- io.micronaut.core.reflect.ClassUtils
-
public class ClassUtils extends java.lang.Object
Utility methods for loading classes.- Since:
- 1.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ClassUtils.Optimizations
Optimizations for computing missing types.
-
Field Summary
Fields Modifier and Type Field Description static java.util.Map<java.lang.String,java.lang.Class>
BASIC_TYPE_MAP
static java.lang.String
CLASS_EXTENSION
Default extension for class files.static java.util.Map<java.lang.String,java.lang.Class>
COMMON_CLASS_MAP
static int
EMPTY_OBJECT_ARRAY_HASH_CODE
static java.lang.String
PROPERTY_MICRONAUT_CLASSLOADER_LOGGING
System property to indicate whether classloader logging should be activated.static org.slf4j.Logger
REFLECTION_LOGGER
A logger that should be used for any reflection access.
-
Constructor Summary
Constructors Constructor Description ClassUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.util.Optional<java.lang.Class>
arrayTypeForPrimitive(java.lang.String primitiveType)
Returns the array type for the given primitive type name.static java.util.Optional<java.lang.Class>
forName(java.lang.String name, java.lang.ClassLoader classLoader)
Attempt to load a class for the given name from the given class loader.static org.slf4j.Logger
getLogger(java.lang.Class type)
Special casegetLogger
method that should be used by classes that are used in the annotation processor.static java.util.Optional<java.lang.Class>
getPrimitiveType(java.lang.String primitiveType)
The primitive type for the given type name.static boolean
isJavaBasicType(java.lang.Class<?> type)
Expanded version ofisJavaLangType(Class)
that includes common Java types likeURI
.static boolean
isJavaBasicType(java.lang.String name)
Expanded version ofisJavaLangType(Class)
that includes common Java types likeURI
.static boolean
isJavaLangType(java.lang.Class type)
Return whether the given class is a common type found injava.lang
such as String or a primitive type.static boolean
isJavaLangType(java.lang.String typeName)
Return whether the given class is a common type found injava.lang
such as String or a primitive type.static boolean
isPresent(java.lang.String name, java.lang.ClassLoader classLoader)
Check whether the given class is present in the given classloader.static java.lang.String
pathToClassName(java.lang.String path)
Converts a URI to a class file reference to the class namestatic java.util.List<java.lang.Class>
resolveHierarchy(java.lang.Class<?> type)
Builds a class hierarchy that includes all super classes and interfaces that the given class implements or extends from.
-
-
-
Field Detail
-
PROPERTY_MICRONAUT_CLASSLOADER_LOGGING
public static final java.lang.String PROPERTY_MICRONAUT_CLASSLOADER_LOGGING
System property to indicate whether classloader logging should be activated. This is required because this class is used both at compilation time and runtime and we don't want logging at compilation time.- See Also:
- Constant Field Values
-
EMPTY_OBJECT_ARRAY_HASH_CODE
public static final int EMPTY_OBJECT_ARRAY_HASH_CODE
-
COMMON_CLASS_MAP
public static final java.util.Map<java.lang.String,java.lang.Class> COMMON_CLASS_MAP
-
BASIC_TYPE_MAP
public static final java.util.Map<java.lang.String,java.lang.Class> BASIC_TYPE_MAP
-
CLASS_EXTENSION
public static final java.lang.String CLASS_EXTENSION
Default extension for class files.- See Also:
- Constant Field Values
-
REFLECTION_LOGGER
public static final org.slf4j.Logger REFLECTION_LOGGER
A logger that should be used for any reflection access.
-
-
Method Detail
-
getLogger
@NonNull public static org.slf4j.Logger getLogger(@NonNull java.lang.Class type)
Special casegetLogger
method that should be used by classes that are used in the annotation processor.- Parameters:
type
- The type- Returns:
- The logger
-
arrayTypeForPrimitive
@NonNull public static java.util.Optional<java.lang.Class> arrayTypeForPrimitive(java.lang.String primitiveType)
Returns the array type for the given primitive type name.- Parameters:
primitiveType
- The primitive type name- Returns:
- The array type
-
pathToClassName
public static java.lang.String pathToClassName(java.lang.String path)
Converts a URI to a class file reference to the class name
.ie. ClassUtils.pathToClassName("foo/bar/MyClass.class") == "foo.bar.MyClass"
- Parameters:
path
- The path name- Returns:
- The class name
-
isPresent
public static boolean isPresent(java.lang.String name, @Nullable java.lang.ClassLoader classLoader)
Check whether the given class is present in the given classloader.- Parameters:
name
- The name of the classclassLoader
- The classloader. If null will fallback to attempt the thread context loader, otherwise the system loader- Returns:
- True if it is
-
isJavaLangType
public static boolean isJavaLangType(java.lang.Class type)
Return whether the given class is a common type found injava.lang
such as String or a primitive type.- Parameters:
type
- The type- Returns:
- True if it is
-
isJavaLangType
public static boolean isJavaLangType(java.lang.String typeName)
Return whether the given class is a common type found injava.lang
such as String or a primitive type.- Parameters:
typeName
- The type name- Returns:
- True if it is
-
isJavaBasicType
public static boolean isJavaBasicType(@Nullable java.lang.Class<?> type)
Expanded version ofisJavaLangType(Class)
that includes common Java types likeURI
.- Parameters:
type
- The URI- Returns:
- True if is a Java basic type
-
isJavaBasicType
public static boolean isJavaBasicType(@Nullable java.lang.String name)
Expanded version ofisJavaLangType(Class)
that includes common Java types likeURI
.- Parameters:
name
- The name of the type- Returns:
- True if is a Java basic type
-
getPrimitiveType
public static java.util.Optional<java.lang.Class> getPrimitiveType(java.lang.String primitiveType)
The primitive type for the given type name. For example the value "byte" returnsByte.TYPE
.- Parameters:
primitiveType
- The type name- Returns:
- An optional type
-
forName
public static java.util.Optional<java.lang.Class> forName(java.lang.String name, @Nullable java.lang.ClassLoader classLoader)
Attempt to load a class for the given name from the given class loader. This method should be used as a last resort, and note that any usage of this method will create complications on GraalVM.- Parameters:
name
- The name of the classclassLoader
- The classloader. If null will fallback to attempt the thread context loader, otherwise the system loader- Returns:
- An optional of the class
-
resolveHierarchy
public static java.util.List<java.lang.Class> resolveHierarchy(java.lang.Class<?> type)
Builds a class hierarchy that includes all super classes and interfaces that the given class implements or extends from.- Parameters:
type
- The class to start with- Returns:
- The class hierarchy
-
-