public abstract class ClassUtils extends Object
Modifier and Type | Field and Description |
---|---|
static String |
ARRAY_SUFFIX
Suffix for array class names: "[]"
|
Constructor and Description |
---|
ClassUtils() |
Modifier and Type | Method and Description |
---|---|
static String |
classPackageAsResourcePath(Class<?> clazz)
Given an input class object, return a string which consists of the
class's package name as a pathname, i.e., all dots ('.') are replaced by
slashes ('/').
|
static Class<?> |
forName(String name,
ClassLoader classLoader)
Replacement for
Class.forName() that also returns Class instances
for primitives (e.g."int") and array class names (e.g. |
static ClassLoader |
getDefaultClassLoader()
Return the default ClassLoader to use: typically the thread context
ClassLoader, if available; the ClassLoader that loaded the ClassUtils
class will be used as fallback.
|
static Class<?> |
resolvePrimitiveClassName(String name)
Resolve the given class name as primitive class, if appropriate,
according to the JVM's naming rules for primitive classes.
|
public static final String ARRAY_SUFFIX
public static ClassLoader getDefaultClassLoader()
Call this method if you intend to use the thread context ClassLoader
in a scenario where you absolutely need a non-null ClassLoader reference:
for example, for class path resource loading (but not necessarily for
Class.forName
, which accepts a null
ClassLoader
reference as well).
null
)Thread.getContextClassLoader()
public static Class<?> forName(String name, ClassLoader classLoader) throws ClassNotFoundException, LinkageError
Class.forName()
that also returns Class instances
for primitives (e.g."int") and array class names (e.g. "String[]").
Furthermore, it is also capable of resolving inner class names in Java source
style (e.g. "java.lang.Thread.State" instead of "java.lang.Thread$State").name
- the name of the ClassclassLoader
- the class loader to use
(may be null
, which indicates the default class loader)ClassNotFoundException
- if the class was not foundLinkageError
- if the class file could not be loadedClass.forName(String, boolean, ClassLoader)
public static Class<?> resolvePrimitiveClassName(String name)
Also supports the JVM's internal class names for primitive arrays.
Does not support the "[]" suffix notation for primitive arrays;
this is only supported by forName(String, ClassLoader)
.
name
- the name of the potentially primitive classnull
if the name does not denote
a primitive class or primitive array classpublic static String classPackageAsResourcePath(Class<?> clazz)
ClassLoader.getResource()
. For it to be fed to
Class.getResource
instead, a leading slash would also have
to be prepended to the returned value.clazz
- the input class. A null
value or the default
(empty) package will result in an empty string ("") being returned.ClassLoader.getResource(java.lang.String)
,
Class.getResource(java.lang.String)
Copyright © 2019. All rights reserved.