bb.util
Class ClassUtil

java.lang.Object
  extended by bb.util.ClassUtil

public final class ClassUtil
extends Object

Provides static utility methods that deal with the Class of an object.

This class is multithread safe: it is stateless.

Author:
Brent Boyer

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 getNameSimple( clazz.getName() ).
static String getNameSimple(Object obj)
          Returns getNameSimple( obj.getClass() ).
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 getPackage( clazz.getName() ).
static String getPackage(Object obj)
          Returns getPackage( obj.getClass() ).
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

ClassUtil

private ClassUtil()
This sole private constructor suppresses the default (public) constructor, ensuring non-instantiability outside of this class.

Method Detail

findClassFile

public static File findClassFile(Class clazz)
                          throws IllegalArgumentException,
                                 RuntimeException
Returns a local file that clazz could have been loaded from. Typically this is either a .class or a .jar file.

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.

Throws:
IllegalArgumentException - if clazz is null
RuntimeException - if unable to find a local file that clazz could have been loaded from
See Also:
this article, this article

findFileOrJarUrl

private static URL findFileOrJarUrl(Enumeration<URL> e)

toFile

private static File toFile(URL url,
                           String pathClass)

classFilePath

private static String classFilePath(Class clazz)

getBytes

public static byte[] getBytes(Class clazz)
                       throws IllegalArgumentException,
                              IOException
Returns the bytes of clazz. This is accomplished by 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.

Throws:
IllegalArgumentException - if clazz is null
IOException - if any I/O problem occurs

getNameSimple

public static String getNameSimple(Object obj)
                            throws IllegalArgumentException
Returns getNameSimple( obj.getClass() ).

Throws:
IllegalArgumentException - if obj is null

getNameSimple

public static String getNameSimple(Class clazz)
                            throws IllegalArgumentException
Returns 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.

Throws:
IllegalArgumentException - if clazz is null

getNameSimple

public static String getNameSimple(String nameFull)
                            throws IllegalArgumentException
Returns the "simple" name of the fully quallified class name that is stored in nameFull, that is, any leading package info is removed.

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.

Throws:
IllegalArgumentException - if nameFull is blank

getPackage

public static String getPackage(Object obj)
                         throws IllegalArgumentException
Returns getPackage( obj.getClass() ).

Throws:
IllegalArgumentException - if obj is null

getPackage

public static String getPackage(Class clazz)
                         throws IllegalArgumentException
Returns 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.

Throws:
IllegalArgumentException - if clazz is null

getPackage

public static String getPackage(String nameFull)
                         throws IllegalArgumentException
Returns the package name of the fully quallified class name that is stored in nameFull. In other words, the "simple" name at the end of nameFull is removed. For example, if nameFull equals "java.lang.Object" then "java.lang" is returned. If nameFull represents a class in the default (unnamed) package, then null is returned.

Throws:
IllegalArgumentException - if nameFull is blank