com.triactive.jdo.util
Class ClassFinder

java.lang.Object
  extended bycom.triactive.jdo.util.ClassFinder

public class ClassFinder
extends java.lang.Object

Used to load classes according to the rules defined in the JDO spec. See section 12.5 of the JDO 1.0.1 spec.

This class also provides a static method for loading classes that utilizes a cache. The cache contains class names that were previously not found, organized by the loader that failed to find them. This speeds up cases where attempts to load the same non-existent class are repeated many times.

Author:
Mike Martin

Method Summary
 java.lang.Class classForName(java.lang.String className, boolean initialize)
          Returns the Class object associated with the class or interface with the given string name.
 java.lang.Class classForName(java.lang.String className, boolean initialize, java.lang.Class contextClass)
          Returns the Class object associated with the class or interface with the given string name.
static java.lang.Class classForName(java.lang.String className, boolean initialize, java.lang.ClassLoader loader)
          Returns the Class object associated with the class or interface with the given string name.
 boolean equals(java.lang.Object obj)
          Indicates whether some object is "equal to" this one.
static ClassFinder getInstance()
          Returns the class finder instance associated with the context class loader of the calling thread.
static ClassFinder getInstance(java.lang.ClassLoader ctxLoader)
          Returns the class finder instance associated with the specified class loader.
 int hashCode()
          Returns a hash code value for this object.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getInstance

public static ClassFinder getInstance()
Returns the class finder instance associated with the context class loader of the calling thread.


getInstance

public static ClassFinder getInstance(java.lang.ClassLoader ctxLoader)
Returns the class finder instance associated with the specified class loader. The specified loader becomes the finder's "original context class loader" for the algorithm implemented by classForName(String,boolean,Class).

Parameters:
ctxLoader - the context class loader

classForName

public java.lang.Class classForName(java.lang.String className,
                                    boolean initialize,
                                    java.lang.Class contextClass)
                             throws java.lang.ClassNotFoundException
Returns the Class object associated with the class or interface with the given string name.

This method implements the algorithm described in section 12.5 of the JDO 1.0.1 spec. It attempts to load the class using up to three loaders:

  1. The loader that loaded the class or instance referred to in the API that caused this class to be loaded (the context class).
  2. The loader returned in the current context by Thread.getContextClassLoader().
  3. The loader returned by Thread.getContextClassLoader() at the time this ClassFinder was constructed (which should equate to the time of PersistenceManagerFactory.getPersistenceManager()).

Parameters:
className - fully qualified name of the desired class
initialize - whether the class must be initialized
contextClass - another class to serve as context for the loading of the named class, or null if there is no such other class
Returns:
class object representing the desired class
Throws:
java.lang.ClassNotFoundException - if the class cannot be located

classForName

public java.lang.Class classForName(java.lang.String className,
                                    boolean initialize)
                             throws java.lang.ClassNotFoundException
Returns the Class object associated with the class or interface with the given string name.

This method is equivalent to:

 classForName(className, initialize, null)
 

Parameters:
className - fully qualified name of the desired class
initialize - whether the class must be initialized
Returns:
class object representing the desired class
Throws:
java.lang.ClassNotFoundException - if the class cannot be located

classForName

public static java.lang.Class classForName(java.lang.String className,
                                           boolean initialize,
                                           java.lang.ClassLoader loader)
                                    throws java.lang.ClassNotFoundException
Returns the Class object associated with the class or interface with the given string name.

This method is functionally equivalent to Class.forName(String,boolean,ClassLoader) except that it maintains a cache of class names that are not found. In many cases, especially in an app server environment, searching for non-existent classes can be a slow process. It's important because some things in TJDO, notably the macro mechanism used in e.g. view definitions, will search for class names that don't exist.

Parameters:
className - fully qualified name of the desired class
initialize - whether the class must be initialized
loader - class loader from which the class must be loaded
Returns:
class object representing the desired class
Throws:
java.lang.ClassNotFoundException - if the class cannot be located

hashCode

public int hashCode()
Returns a hash code value for this object.


equals

public boolean equals(java.lang.Object obj)
Indicates whether some object is "equal to" this one. Two ClassFinder objects are considered equal if their original context class loaders are all equal.

Parameters:
obj - the reference object with which to compare
Returns:
true if this object is equal to the obj argument; false otherwise.


Copyright 2001-2007 The TJDO Project All Rights Reserved.