|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectbb.util.ClassUtil
public final class ClassUtil
Provides static utility methods that deal with the Class
of an object.
This class is multithread safe: it is stateless.
Nested Class Summary | |
---|---|
static class |
ClassUtil.UnitTest
See the Overview page of the project's javadocs for a general description of this unit test class. |
Constructor Summary | |
---|---|
private |
ClassUtil()
This sole private constructor suppresses the default (public) constructor, ensuring non-instantiability outside of this class. |
Method Summary | |
---|---|
private static String |
classFilePath(Class clazz)
|
static File |
findClassFile(Class clazz)
Returns a local file that clazz could have been loaded from. |
private static URL |
findFileOrJarUrl(Enumeration<URL> e)
|
static byte[] |
getBytes(Class clazz)
Returns the bytes of clazz. |
static String |
getNameSimple(Class clazz)
Returns . |
static String |
getNameSimple(Object obj)
Returns . |
static String |
getNameSimple(String nameFull)
Returns the "simple" name of the fully quallified class name that is stored in nameFull, that is, any leading package info is removed. |
static String |
getPackage(Class clazz)
Returns . |
static String |
getPackage(Object obj)
Returns . |
static String |
getPackage(String nameFull)
Returns the package name of the fully quallified class name that is stored in nameFull. |
private static File |
toFile(URL url,
String pathClass)
|
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
private ClassUtil()
Method Detail |
---|
public static File findClassFile(Class clazz) throws IllegalArgumentException, RuntimeException
Warning: because there can be multiple URLs that clazz could be loaded from, including not only local files but network URLs too, there is no guarantee that the result is the actual source of clazz's bytes.
IllegalArgumentException
- if clazz is null
RuntimeException
- if unable to find a local file that clazz could have been loaded fromprivate static URL findFileOrJarUrl(Enumeration<URL> e)
private static File toFile(URL url, String pathClass)
private static String classFilePath(Class clazz)
public static byte[] getBytes(Class clazz) throws IllegalArgumentException, IOException
getting clazz's classloader
and draining the bytes
for clazz.
If clazz was originally loaded from some .class file
then the result should be exactly equal to the bytes in that file,
regardless of whether that file is local or remote.
IllegalArgumentException
- if clazz is null
IOException
- if any I/O problem occurspublic static String getNameSimple(Object obj) throws IllegalArgumentException
getNameSimple
( obj.getClass() )
.
IllegalArgumentException
- if obj is nullpublic static String getNameSimple(Class clazz) throws IllegalArgumentException
getNameSimple
( clazz.getName() )
.
The result is usually identical to what clazz.getSimpleName()
returns.
The sole exception that it does not return an empty String for anonymous classes,
but instead returns whatever name the JVM uses for such classes.
IllegalArgumentException
- if clazz is nullpublic static String getNameSimple(String nameFull) throws IllegalArgumentException
Algorithm: if nameFull contains at least one period (i.e. '.') char, then every up thru the last period is stripped and everything that comes after is returned. Otherwise nameFull in its entirety is returned. For example, if nameFull equals "java.lang.Object" then "Object" is returned.
IllegalArgumentException
- if nameFull is blank
public static String getPackage(Object obj) throws IllegalArgumentException
getPackage
( obj.getClass() )
.
IllegalArgumentException
- if obj is nullpublic static String getPackage(Class clazz) throws IllegalArgumentException
getPackage
( clazz.getName() )
.
The result is usually identical to what clazz.getSimpleName()
returns.
The sole exception that it does not return an empty String for anonymous classes,
but instead returns whatever name the JVM uses for such classes.
IllegalArgumentException
- if clazz is nullpublic static String getPackage(String nameFull) throws IllegalArgumentException
IllegalArgumentException
- if nameFull is blank
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |