Package io.microsphere.util
Class ClassLoaderUtils
- java.lang.Object
-
- io.microsphere.util.BaseUtils
-
- io.microsphere.util.ClassLoaderUtils
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ClassLoaderUtils.ResourceType
Resource Type
-
Field Summary
Fields Modifier and Type Field Description protected static java.lang.management.ClassLoadingMXBean
classLoadingMXBean
-
Constructor Summary
Constructors Constructor Description ClassLoaderUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.util.Set<java.net.URL>
findAllClassPathURLs(java.lang.ClassLoader classLoader)
static java.lang.Class<?>
findLoadedClass(java.lang.ClassLoader classLoader, java.lang.String className)
Find LoadedClass
under specified inheritableClassLoader
static java.util.Set<java.lang.Class<?>>
findLoadedClasses(java.lang.ClassLoader classLoader, java.util.Set<java.lang.String> classNames)
Find LoadedClass
under specified inheritableClassLoader
and class namesstatic java.util.Set<java.lang.Class<?>>
findLoadedClassesInClassPath(java.lang.ClassLoader classLoader)
Find loaded classesSet
in class pathstatic java.util.Set<java.lang.Class<?>>
findLoadedClassesInClassPath(java.lang.ClassLoader classLoader, java.lang.String classPath)
Find loaded classesSet
in class pathstatic java.util.Set<java.lang.Class<?>>
findLoadedClassesInClassPaths(java.lang.ClassLoader classLoader, java.util.Set<java.lang.String> classPaths)
Find loaded classesSet
in class pathsSet
static java.net.URLClassLoader
findURLClassLoader(java.lang.ClassLoader classLoader)
static java.util.Set<java.lang.Class<?>>
getAllLoadedClasses(java.lang.ClassLoader classLoader)
Get all loaded classesSet
under specified inheritableClassLoader
static java.util.Map<java.lang.ClassLoader,java.util.Set<java.lang.Class<?>>>
getAllLoadedClassesMap(java.lang.ClassLoader classLoader)
Get all loaded classesMap
under specified inheritableClassLoader
,ClassLoader
as key , its loaded classesSet
as value.static java.lang.ClassLoader
getCallerClassLoader()
Return the ClassLoader from the caller classstatic java.lang.ClassLoader
getClassLoader(java.lang.Class<?> loadedClass)
Get the ClassLoader from the loaded class if present.static java.net.URL
getClassResource(java.lang.ClassLoader classLoader, java.lang.Class<?> type)
Get theClass
resource URL under specifiedClass
static java.net.URL
getClassResource(java.lang.ClassLoader classLoader, java.lang.String className)
Get theClass
resource URL under specifiedClass name
static java.lang.ClassLoader
getDefaultClassLoader()
Get the default the ClassLoader to use.static java.util.Set<java.lang.ClassLoader>
getInheritableClassLoaders(java.lang.ClassLoader classLoader)
Get all InheritableClassLoaders
Set
(includingClassLoader
argument)static int
getLoadedClassCount()
Returns the number of classes that are currently loaded in the Java virtual machine.static java.util.Set<java.lang.Class<?>>
getLoadedClasses(java.lang.ClassLoader classLoader)
Get loaded classesSet
under specifiedClassLoader
( not all inheritableClassLoaders
)static java.net.URL
getResource(java.lang.ClassLoader classLoader, ClassLoaderUtils.ResourceType resourceType, java.lang.String resourceName)
Get the resource URL under specified resource name and typestatic java.net.URL
getResource(java.lang.ClassLoader classLoader, java.lang.String resourceName)
Get the resource URL under specified resource namestatic java.util.Set<java.net.URL>
getResources(java.lang.ClassLoader classLoader, ClassLoaderUtils.ResourceType resourceType, java.lang.String resourceName)
Get the resource URLs Set under specified resource name and typestatic java.util.Set<java.net.URL>
getResources(java.lang.ClassLoader classLoader, java.lang.String resourceName)
Get the resource URLs list under specified resource namestatic long
getTotalLoadedClassCount()
Returns the total number of classes that have been loaded since the Java virtual machine has started execution.static long
getUnloadedClassCount()
Returns the total number of classes unloaded since the Java virtual machine has started execution.static boolean
isLoadedClass(java.lang.ClassLoader classLoader, java.lang.Class<?> type)
Check specifiedClass
is loaded on specified inheritableClassLoader
static boolean
isLoadedClass(java.lang.ClassLoader classLoader, java.lang.String className)
Check specifiedclass name
is loaded on specified inheritableClassLoader
static boolean
isPresent(java.lang.String className)
Test the specified class name is present in thedefault ClassLoader
static boolean
isPresent(java.lang.String className, java.lang.ClassLoader classLoader)
Test the specified class name is present in theClassLoader
static boolean
isVerbose()
Tests if the verbose output for the class loading system is enabled.static java.lang.Class<?>
loadClass(java.lang.String className, java.lang.ClassLoader classLoader)
Loaded specified class name underClassLoader
static java.lang.Class<?>
loadClass(java.lang.String className, java.lang.ClassLoader classLoader, boolean cached)
Loaded specified class name underClassLoader
static boolean
removeClassPathURL(java.lang.ClassLoader classLoader, java.net.URL url)
static java.lang.Class<?>
resolveClass(java.lang.String className)
Resolve theClass
by the specified name in thedefault ClassLoader
static java.lang.Class<?>
resolveClass(java.lang.String className, java.lang.ClassLoader classLoader)
Resolve theClass
by the specified name andClassLoader
static java.lang.Class<?>
resolveClass(java.lang.String className, java.lang.ClassLoader classLoader, boolean cached)
Resolve theClass
by the specified name andClassLoader
static void
setVerbose(boolean value)
Enables or disables the verbose output for the class loading system.
-
-
-
Method Detail
-
getLoadedClassCount
public static int getLoadedClassCount()
Returns the number of classes that are currently loaded in the Java virtual machine.- Returns:
- the number of currently loaded classes.
-
getUnloadedClassCount
public static long getUnloadedClassCount()
Returns the total number of classes unloaded since the Java virtual machine has started execution.- Returns:
- the total number of unloaded classes.
-
isVerbose
public static boolean isVerbose()
Tests if the verbose output for the class loading system is enabled.- Returns:
- true if the verbose output for the class loading system is enabled; false otherwise.
-
setVerbose
public static void setVerbose(boolean value)
Enables or disables the verbose output for the class loading system. The verbose output information and the output stream to which the verbose information is emitted are implementation dependent. Typically, a Java virtual machine implementation prints a message each time a class file is loaded.This method can be called by multiple threads concurrently. Each invocation of this method enables or disables the verbose output globally.
- Parameters:
value
- true to enable the verbose output; false to disable.- Throws:
java.lang.SecurityException
- if a security manager exists and the caller does not have ManagementPermission("control").
-
getTotalLoadedClassCount
public static long getTotalLoadedClassCount()
Returns the total number of classes that have been loaded since the Java virtual machine has started execution.- Returns:
- the total number of classes loaded.
-
getDefaultClassLoader
@Nullable public static java.lang.ClassLoader getDefaultClassLoader()
Get the default the ClassLoader to use.- Returns:
- the ClassLoader (only
null
if even the system ClassLoader isn't accessible) - See Also:
Thread.getContextClassLoader()
,Class.getClassLoader()
,ClassLoader.getSystemClassLoader()
,ReflectionUtils.getCallerClass()
-
getClassLoader
@Nullable public static java.lang.ClassLoader getClassLoader(@Nullable java.lang.Class<?> loadedClass)
Get the ClassLoader from the loaded class if present.- Parameters:
loadedClass
- the optional class was loaded by someClassLoader
- Returns:
- the ClassLoader (only
null
if even the system ClassLoader isn't accessible) - See Also:
getDefaultClassLoader()
-
getCallerClassLoader
public static java.lang.ClassLoader getCallerClassLoader()
Return the ClassLoader from the caller class- Returns:
- the ClassLoader (only
null
if the caller class was absent - See Also:
ReflectionUtils.getCallerClass()
-
findLoadedClasses
public static java.util.Set<java.lang.Class<?>> findLoadedClasses(java.lang.ClassLoader classLoader, java.util.Set<java.lang.String> classNames)
Find LoadedClass
under specified inheritableClassLoader
and class names- Parameters:
classLoader
-ClassLoader
classNames
- class names set- Returns:
Class
if loaded , ornull
-
isLoadedClass
public static boolean isLoadedClass(java.lang.ClassLoader classLoader, java.lang.Class<?> type)
Check specifiedClass
is loaded on specified inheritableClassLoader
- Parameters:
classLoader
-ClassLoader
type
-Class
- Returns:
- If Loaded , return
true
, orfalse
-
isLoadedClass
public static boolean isLoadedClass(java.lang.ClassLoader classLoader, java.lang.String className)
Check specifiedclass name
is loaded on specified inheritableClassLoader
- Parameters:
classLoader
-ClassLoader
className
-class name
- Returns:
- If Loaded , return
true
, orfalse
-
findLoadedClass
public static java.lang.Class<?> findLoadedClass(java.lang.ClassLoader classLoader, java.lang.String className)
Find LoadedClass
under specified inheritableClassLoader
- Parameters:
classLoader
-ClassLoader
className
- class name- Returns:
Class
if loaded , ornull
-
loadClass
@Nullable public static java.lang.Class<?> loadClass(@Nonnull java.lang.String className, @Nonnull java.lang.ClassLoader classLoader)
Loaded specified class name underClassLoader
- Parameters:
className
- the name ofClass
classLoader
-ClassLoader
- Returns:
Class
if can be loaded
-
loadClass
public static java.lang.Class<?> loadClass(java.lang.String className, java.lang.ClassLoader classLoader, boolean cached)
Loaded specified class name underClassLoader
- Parameters:
className
- the name ofClass
classLoader
-ClassLoader
cached
- the resolved class is required to be cached or not- Returns:
Class
if can be loaded
-
getResources
public static java.util.Set<java.net.URL> getResources(java.lang.ClassLoader classLoader, ClassLoaderUtils.ResourceType resourceType, java.lang.String resourceName) throws java.lang.NullPointerException, java.io.IOException
Get the resource URLs Set under specified resource name and type- Parameters:
classLoader
- ClassLoaderresourceType
-ClassLoaderUtils.ResourceType
EnumresourceName
- resource name ,e.g :
- Resource Name :
"/com/abc/def.log"
- Class Name :
"java.lang.String"
- Resource Name :
- Returns:
- the resource URL under specified resource name and type
- Throws:
java.lang.NullPointerException
- If any argument isnull
java.io.IOException
-
getResources
public static java.util.Set<java.net.URL> getResources(java.lang.ClassLoader classLoader, java.lang.String resourceName) throws java.lang.NullPointerException, java.io.IOException
Get the resource URLs list under specified resource name- Parameters:
classLoader
- ClassLoaderresourceName
- resource name ,e.g :
- Resource Name :
"/com/abc/def.log"
- Class Name :
"java.lang.String"
- Resource Name :
- Returns:
- the resource URL under specified resource name and type
- Throws:
java.lang.NullPointerException
- If any argument isnull
java.io.IOException
-
getResource
public static java.net.URL getResource(java.lang.ClassLoader classLoader, java.lang.String resourceName) throws java.lang.NullPointerException
Get the resource URL under specified resource name- Parameters:
classLoader
- ClassLoaderresourceName
- resource name ,e.g :
- Resource Name :
"/com/abc/def.log"
- Class Name :
"java.lang.String"
- Resource Name :
- Returns:
- the resource URL under specified resource name and type
- Throws:
java.lang.NullPointerException
- If any argument isnull
-
getResource
public static java.net.URL getResource(java.lang.ClassLoader classLoader, ClassLoaderUtils.ResourceType resourceType, java.lang.String resourceName) throws java.lang.NullPointerException
Get the resource URL under specified resource name and type- Parameters:
classLoader
- ClassLoaderresourceType
-ClassLoaderUtils.ResourceType
EnumresourceName
- resource name ,e.g :
- Resource Name :
"/com/abc/def.log"
- Class Name :
"java.lang.String"
- Resource Name :
- Returns:
- the resource URL under specified resource name and type
- Throws:
java.lang.NullPointerException
- If any argument isnull
-
getClassResource
public static java.net.URL getClassResource(java.lang.ClassLoader classLoader, java.lang.String className)
Get theClass
resource URL under specifiedClass name
- Parameters:
classLoader
- ClassLoaderclassName
- class name- Returns:
- the resource URL under specified resource name and type
- Throws:
java.lang.NullPointerException
- If any argument isnull
-
getClassResource
public static java.net.URL getClassResource(java.lang.ClassLoader classLoader, java.lang.Class<?> type)
Get theClass
resource URL under specifiedClass
- Parameters:
classLoader
- ClassLoadertype
-type
- Returns:
- the resource URL under specified resource name and type
- Throws:
java.lang.NullPointerException
- If any argument isnull
- Since:
- 1.0.0
-
getInheritableClassLoaders
@Nonnull public static java.util.Set<java.lang.ClassLoader> getInheritableClassLoaders(java.lang.ClassLoader classLoader) throws java.lang.NullPointerException
Get all InheritableClassLoaders
Set
(includingClassLoader
argument)- Parameters:
classLoader
-ClassLoader
- Returns:
- Read-only
Set
- Throws:
java.lang.NullPointerException
- IfclassLoader
argument isnull
-
getAllLoadedClassesMap
@Nonnull public static java.util.Map<java.lang.ClassLoader,java.util.Set<java.lang.Class<?>>> getAllLoadedClassesMap(java.lang.ClassLoader classLoader) throws java.lang.UnsupportedOperationException
Get all loaded classesMap
under specified inheritableClassLoader
,ClassLoader
as key , its loaded classesSet
as value.- Parameters:
classLoader
-ClassLoader
- Returns:
- Read-only Map
- Throws:
java.lang.UnsupportedOperationException
java.lang.NullPointerException
- IfclassLoader
argument isnull
-
getAllLoadedClasses
@Nonnull public static java.util.Set<java.lang.Class<?>> getAllLoadedClasses(java.lang.ClassLoader classLoader) throws java.lang.UnsupportedOperationException
Get all loaded classesSet
under specified inheritableClassLoader
- Parameters:
classLoader
-ClassLoader
- Returns:
- Read-only
Set
- Throws:
java.lang.UnsupportedOperationException
- If JVM does not supportjava.lang.NullPointerException
- IfclassLoader
argument isnull
-
getLoadedClasses
@Nonnull public static java.util.Set<java.lang.Class<?>> getLoadedClasses(java.lang.ClassLoader classLoader) throws java.lang.UnsupportedOperationException
Get loaded classesSet
under specifiedClassLoader
( not all inheritableClassLoaders
)- Parameters:
classLoader
-ClassLoader
- Returns:
- Read-only
Set
- Throws:
java.lang.UnsupportedOperationException
- If JVM does not supportjava.lang.NullPointerException
- IfclassLoader
argument isnull
- See Also:
getAllLoadedClasses(ClassLoader)
-
findLoadedClassesInClassPath
public static java.util.Set<java.lang.Class<?>> findLoadedClassesInClassPath(java.lang.ClassLoader classLoader) throws java.lang.UnsupportedOperationException
Find loaded classesSet
in class path- Parameters:
classLoader
-ClassLoader
- Returns:
- Read-only
Set
- Throws:
java.lang.UnsupportedOperationException
- If JVM does not support
-
findLoadedClassesInClassPaths
public static java.util.Set<java.lang.Class<?>> findLoadedClassesInClassPaths(java.lang.ClassLoader classLoader, java.util.Set<java.lang.String> classPaths) throws java.lang.UnsupportedOperationException
Find loaded classesSet
in class pathsSet
- Parameters:
classLoader
-ClassLoader
classPaths
- the class paths for theSet
ofJarFile
or classes directory- Returns:
- Read-only
Set
- Throws:
java.lang.UnsupportedOperationException
- If JVM does not support- See Also:
findLoadedClass(ClassLoader, String)
-
findLoadedClassesInClassPath
public static java.util.Set<java.lang.Class<?>> findLoadedClassesInClassPath(java.lang.ClassLoader classLoader, java.lang.String classPath) throws java.lang.UnsupportedOperationException
Find loaded classesSet
in class path- Parameters:
classLoader
-ClassLoader
classPath
- the class path for oneJarFile
or classes directory- Returns:
- Read-only
Set
- Throws:
java.lang.UnsupportedOperationException
- If JVM does not support- See Also:
findLoadedClass(ClassLoader, String)
-
isPresent
public static boolean isPresent(@Nullable java.lang.String className)
Test the specified class name is present in thedefault ClassLoader
- Parameters:
className
- the name ofClass
- Returns:
- If found, return
true
-
isPresent
public static boolean isPresent(@Nullable java.lang.String className, @Nullable java.lang.ClassLoader classLoader)
Test the specified class name is present in theClassLoader
- Parameters:
className
- the name ofClass
classLoader
-ClassLoader
- Returns:
- If found, return
true
-
resolveClass
public static java.lang.Class<?> resolveClass(@Nullable java.lang.String className)
Resolve theClass
by the specified name in thedefault ClassLoader
- Parameters:
className
- the name ofClass
- Returns:
- If can't be resolved , return
null
-
resolveClass
public static java.lang.Class<?> resolveClass(@Nullable java.lang.String className, @Nullable java.lang.ClassLoader classLoader)
Resolve theClass
by the specified name andClassLoader
- Parameters:
className
- the name ofClass
classLoader
-ClassLoader
- Returns:
- If can't be resolved , return
null
-
resolveClass
public static java.lang.Class<?> resolveClass(@Nullable java.lang.String className, @Nullable java.lang.ClassLoader classLoader, boolean cached)
Resolve theClass
by the specified name andClassLoader
- Parameters:
className
- the name ofClass
classLoader
-ClassLoader
cached
- the resolved class is required to be cached or not- Returns:
- If can't be resolved , return
null
-
findAllClassPathURLs
public static java.util.Set<java.net.URL> findAllClassPathURLs(java.lang.ClassLoader classLoader)
-
removeClassPathURL
public static boolean removeClassPathURL(java.lang.ClassLoader classLoader, java.net.URL url)
-
findURLClassLoader
public static java.net.URLClassLoader findURLClassLoader(java.lang.ClassLoader classLoader)
-
-