ix.util
Class ClassFinder

java.lang.Object
  |
  +--ix.util.ClassFinder

public class ClassFinder
extends java.lang.Object

Converts between class names (Strings) and classes. Note that a single ClassFinder maps in both directions to make it easier to create mappings that are consistent.

Explicit mappings and "imports" may be specified; otherwise fully qualified names must be used.

Primitive types such as byte, boolean, and int are matched with the corresponding TYPE classes Byte.TYPE, Boolean.TYPE, etc.

This class does not yet handle array classes.


Inner Class Summary
protected static class ClassFinder.Import
          Internal class that performs the name-to-class mapping that corresponds to a single import specification.
 
Field Summary
protected  java.util.Map classToName
           
protected  java.util.Map classToNameCache
           
protected  java.util.List imports
           
protected  java.util.Map nameToClass
           
protected  java.util.Map nameToClassCache
           
 
Constructor Summary
ClassFinder()
           
 
Method Summary
 void addImport(java.lang.String name)
          Records an import that allows a class name or names to be used without package-qualification.
protected  void addInitialImports()
          Adds the initial set of imports.
protected  void addInitialNames()
          Adds the initial set of name mappings.
 void addName(java.lang.String name, java.lang.Class c)
          Adds an explicit mapping in both directions between a name and a class.
 java.lang.Class classForName(java.lang.String name)
          Returns the class that corresponds to the name, as determined by any explicit mappings and imports.
protected  java.lang.Class findImportClass(java.lang.String name)
          Tries to find a class by asking each import in turn.
static void main(java.lang.String[] argv)
          A test loop that asks the user for a name and prints the corresponding class (or else null) as determined by a plain ClassFinder.
 java.lang.String nameForClass(java.lang.Class c)
          Returns the best name that this ClassFinder would map to the specified class, where a plain class name counts as better than a package-qualified one.
static java.lang.Class tryClassForName(java.lang.String name)
          Like Class.forName(String) but returns null if the class cannot be found.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

imports

protected java.util.List imports

nameToClass

protected java.util.Map nameToClass

classToName

protected java.util.Map classToName

nameToClassCache

protected java.util.Map nameToClassCache

classToNameCache

protected java.util.Map classToNameCache
Constructor Detail

ClassFinder

public ClassFinder()
Method Detail

addInitialNames

protected void addInitialNames()
Adds the initial set of name mappings.

addInitialImports

protected void addInitialImports()
Adds the initial set of imports. These are:
   java.lang.*
   java.util.*
 

addImport

public void addImport(java.lang.String name)
Records an import that allows a class name or names to be used without package-qualification. The import is specified as either a fully qualified class name or a package name suffixed by ".*", as in the Java import statement.

addName

public void addName(java.lang.String name,
                    java.lang.Class c)
Adds an explicit mapping in both directions between a name and a class. This takes precedence over all other ways of determining those relationships.

classForName

public java.lang.Class classForName(java.lang.String name)
Returns the class that corresponds to the name, as determined by any explicit mappings and imports. Fully qualified names may also be used.
Returns:
a Class or null.

findImportClass

protected java.lang.Class findImportClass(java.lang.String name)
Tries to find a class by asking each import in turn.
Throws:
java.lang.RuntimeException - if more than one import can find a class.

tryClassForName

public static java.lang.Class tryClassForName(java.lang.String name)
Like Class.forName(String) but returns null if the class cannot be found.

nameForClass

public java.lang.String nameForClass(java.lang.Class c)
Returns the best name that this ClassFinder would map to the specified class, where a plain class name counts as better than a package-qualified one.

main

public static void main(java.lang.String[] argv)
A test loop that asks the user for a name and prints the corresponding class (or else null) as determined by a plain ClassFinder. When the result is not null, it also prints the ClassFinder's "best" name for that class.