Package spark.utils
Class ClassUtils
- java.lang.Object
-
- spark.utils.ClassUtils
-
public abstract class ClassUtils extends java.lang.Object
Miscellaneous class utility methods. Mainly for internal use within the framework.- Since:
- 1.1 Code copied from Spring source. Modifications made (mostly removal of methods) by Per Wendel.
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
ARRAY_SUFFIX
Suffix for array class names: "[]"private static java.util.Map<java.lang.String,java.lang.Class<?>>
commonClassCache
Map with common "java.lang" class name as key and corresponding Class as value.private static java.lang.String
INTERNAL_ARRAY_PREFIX
Prefix for internal array class names: "["private static java.lang.String
NON_PRIMITIVE_ARRAY_PREFIX
Prefix for internal non-primitive array class names: "[L"private static java.util.Map<java.lang.String,java.lang.Class<?>>
primitiveTypeNameMap
Map with primitive type name as key and corresponding primitive type as value, for example: "int" -> "int.class".private static java.util.Map<java.lang.Class<?>,java.lang.Class<?>>
primitiveTypeToWrapperMap
Map with primitive type as key and corresponding wrapper type as value, for example: int.class -> Integer.class.private static java.util.Map<java.lang.Class<?>,java.lang.Class<?>>
primitiveWrapperTypeMap
Map with primitive wrapper type as key and corresponding primitive type as value, for example: Integer.class -> int.class.
-
Constructor Summary
Constructors Constructor Description ClassUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.String
classPackageAsResourcePath(java.lang.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 java.lang.Class<?>
forName(java.lang.String name, java.lang.ClassLoader classLoader)
Replacement forClass.forName()
that also returns Class instances for primitives (e.g."int") and array class names (e.g.static java.lang.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.private static void
registerCommonClasses(java.lang.Class<?>... commonClasses)
Register the given common classes with the ClassUtils cache.static java.lang.Class<?>
resolvePrimitiveClassName(java.lang.String name)
Resolve the given class name as primitive class, if appropriate, according to the JVM's naming rules for primitive classes.
-
-
-
Field Detail
-
ARRAY_SUFFIX
public static final java.lang.String ARRAY_SUFFIX
Suffix for array class names: "[]"- See Also:
- Constant Field Values
-
INTERNAL_ARRAY_PREFIX
private static final java.lang.String INTERNAL_ARRAY_PREFIX
Prefix for internal array class names: "["- See Also:
- Constant Field Values
-
NON_PRIMITIVE_ARRAY_PREFIX
private static final java.lang.String NON_PRIMITIVE_ARRAY_PREFIX
Prefix for internal non-primitive array class names: "[L"- See Also:
- Constant Field Values
-
primitiveWrapperTypeMap
private static final java.util.Map<java.lang.Class<?>,java.lang.Class<?>> primitiveWrapperTypeMap
Map with primitive wrapper type as key and corresponding primitive type as value, for example: Integer.class -> int.class.
-
primitiveTypeToWrapperMap
private static final java.util.Map<java.lang.Class<?>,java.lang.Class<?>> primitiveTypeToWrapperMap
Map with primitive type as key and corresponding wrapper type as value, for example: int.class -> Integer.class.
-
primitiveTypeNameMap
private static final java.util.Map<java.lang.String,java.lang.Class<?>> primitiveTypeNameMap
Map with primitive type name as key and corresponding primitive type as value, for example: "int" -> "int.class".
-
commonClassCache
private static final java.util.Map<java.lang.String,java.lang.Class<?>> commonClassCache
Map with common "java.lang" class name as key and corresponding Class as value. Primarily for efficient deserialization of remote invocations.
-
-
Method Detail
-
registerCommonClasses
private static void registerCommonClasses(java.lang.Class<?>... commonClasses)
Register the given common classes with the ClassUtils cache.
-
getDefaultClassLoader
public static java.lang.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.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 anull
ClassLoader reference as well).- Returns:
- the default ClassLoader (never
null
) - See Also:
Thread.getContextClassLoader()
-
forName
public static java.lang.Class<?> forName(java.lang.String name, java.lang.ClassLoader classLoader) throws java.lang.ClassNotFoundException, java.lang.LinkageError
Replacement forClass.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").- Parameters:
name
- the name of the ClassclassLoader
- the class loader to use (may benull
, which indicates the default class loader)- Returns:
- Class instance for the supplied name
- Throws:
java.lang.ClassNotFoundException
- if the class was not foundjava.lang.LinkageError
- if the class file could not be loaded- See Also:
Class.forName(String, boolean, ClassLoader)
-
resolvePrimitiveClassName
public static java.lang.Class<?> resolvePrimitiveClassName(java.lang.String name)
Resolve the given class name as primitive class, if appropriate, according to the JVM's naming rules for primitive classes.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)
.- Parameters:
name
- the name of the potentially primitive class- Returns:
- the primitive class, or
null
if the name does not denote a primitive class or primitive array class
-
classPackageAsResourcePath
public static java.lang.String classPackageAsResourcePath(java.lang.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 ('/'). Neither a leading nor trailing slash is added. The result could be concatenated with a slash and the name of a resource and fed directly toClassLoader.getResource()
. For it to be fed toClass.getResource
instead, a leading slash would also have to be prepended to the returned value.- Parameters:
clazz
- the input class. Anull
value or the default (empty) package will result in an empty string ("") being returned.- Returns:
- a path which represents the package name
- See Also:
ClassLoader.getResource(java.lang.String)
,Class.getResource(java.lang.String)
-
-