Class ClassUtil
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringReturns classloader information about the specified class.static StringgetClassLoaderInfo(Object obj) Returns classloader information about the specified objectstatic StringThis method can be called from either a instance or a static method.static StringgetLoadedFromPath(Class caller) Returns either the directory or jar file from which the specified class was loadedstatic StringReturns the fully qualified name of the class that contained the main() method used to invoke the application.static StringgetParentDir(Class caller) Returns the right most parent directory from which the actual class was loaded.static URLgetParentDirAsURL(Class caller) Returns the parent directory from which the actual class was loaded.static booleanReturns true is the specified class is a system/JDK class.static void
-
Constructor Details
-
ClassUtil
public ClassUtil()
-
-
Method Details
-
getClassLoaderInfo
Returns classloader information about the specified object -
getClassLoaderInfo
Returns classloader information about the specified class. -
getClassName
This method can be called from either a instance or a static method. Returns the class name in which the calling method was declared.Important note: The returned class name is wherever the method is invoked from. For instance methods, it is the runtime virtual class. For static it is the class name where the method was declared (static methods are not virtual and bound at compile time). For example:
Class A { static void foo(...) { System.out.print(ClassUtil.getClassName()); } } public class B extends A { } % java B will print: A (NOT B) -
getMainClassName
Returns the fully qualified name of the class that contained the main() method used to invoke the application. Note: this method must be called from the same thread (the "main" thread) that started the application. Newly started threads that call this method will instead obtain the class name containing that thread's run() method. -
isSystemClass
Returns true is the specified class is a system/JDK class. This method should work, but given the vagaries of classes, classloaders, etc, there isn't any guarantee.- Parameters:
caller- the object orClassClass corresponding to that object
-
getLoadedFromPath
Returns either the directory or jar file from which the specified class was loadedThis does not work for system/bootstrap classes and will return null for system classes.
- Parameters:
caller- theClassclass to be looked up
-
getParentDir
Returns the right most parent directory from which the actual class was loaded. This is the leaf directory where the actual class is located.For classes loaded from other sources (jar, etc), the return value will be null.
This also does not work for system/bootstrap classes (they are typically loaded from jars) either (those will return null also)
- Parameters:
caller- theClassclass to be looked up
-
getParentDirAsURL
Returns the parent directory from which the actual class was loaded. This is the leaf directory where the actual class is located.For classes loaded from other sources (jar, etc), a URL will be returned. For jar URL's, the URL.getProtocol() method returns "jar", so that can be useful for further processing as needed.
- Parameters:
caller- theClassclass to be looked up
-
main
-