Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/thermidor/util/ClassUtil.java


1   package com.thermidor.util;
2   import java.util.Hashtable;
3   import java.lang.reflect.Array;
4   /**
5      The purpose of the ClassUtil class is to provide utility operations for the
6      management of class types. The lookup operation allows the the classes of
7      primitives and array types to be retrieved in the same manners as normal 
8      java classes. The semantics of the lookup operation are similar to those
9      of Class.forName().
10     <p></p>
11  <table border="1">
12  <tr><th>class name</th><th>description</th></tr>
13  <tr><td>int</td><td>maps to Integer.TYPE</td></tr>
14  <tr><td>short</td><td>maps to Short.TYPE</td></tr>
15  <tr><td>byte</td><td>maps to Byte.TYPE</td></tr>
16  <tr><td>char</td><td>maps to Character.TYPE</td></tr>
17  <tr><td>long</td><td>maps to Long.TYPE</td></tr>
18  <tr><td>float</td><td>maps to Float.TYPE</td></tr>
19  <tr><td>double</td><td>maps to double.TYPE</td></tr>
20  <tr><td>boolean</td><td>maps to Boolean.TYPE</td></tr>
21  <tr><td>ClassName</td><td>Any fully qualified class name</td></tr>
22  <tr><td>'['+(ClassName|int|short|byte|char|long|float|double|boolean)</td>
23  <td>maps to the class of an array of any type, primitive or Class all array
24  class names should begin with the '[' character and the number of consequetive
25   '[' indicates the cardinality of the array</td></tr>
26  </table>
27  <p></p>
28  @author Edward Turnock
29   */
30  public class ClassUtil {
31      /**
32         The map attribute maintains the mapping between primitive names and
33         thier classes.
34       */
35      private static Hashtable map = new Hashtable();
36      /**
37         Initialises the primitive type map.
38       */
39      static{
40          map.put( "int", Integer.TYPE );
41          map.put( "short", Short.TYPE );
42          map.put( "byte", Byte.TYPE );
43          map.put( "char", Character.TYPE );
44          map.put( "long", Long.TYPE );
45          map.put( "float", Float.TYPE );
46          map.put( "double", Double.TYPE );
47          map.put( "boolean", Boolean.TYPE );
48      }
49      /**
50         Lookup the class of either a primitive of normal java class.
51         @param name the name of the class to load
52         (ClassName|int|short|byte|char|long|float|double|boolean)
53         @return the class named in the name parameter
54         @throws ClassNotFoundException is raised if the class could not be 
55         found.
56       */
57      private static Class internalLookup( String name )
58      throws ClassNotFoundException {
59          Class t = ( Class ) map.get( name );
60  
61          if ( t == null ) {
62              t = Class.forName( name );
63          }
64  
65          return t;
66      }
67  
68      /**
69         Lookup the class for a standard java class. primitive or array by name.
70         @param name nameof the class to load
71         (['+(ClassName|int|short|byte|char|long|float|double|boolean))|
72         (ClassName|int|short|byte|char|long|float|double|boolean)
73         @return the class named in the name parameter.
74         @throws ClassNotFoundException is raised if the class could not be 
75         found.
76       */
77      public static Class lookup( String name )
78      throws ClassNotFoundException {
79          if ( name.startsWith( "[" ) ) {
80              int len = name.length();
81              int cnt = 0;
82  
83              for ( ;cnt < len;cnt++ ) {
84                  if ( name.charAt( cnt ) != '[' ) {
85                      break;
86                  }
87              }
88  
89              String classType = name.substring( cnt + 1 );
90              Class re = Array.newInstance( internalLookup( classType
91                                                            ), cnt ).getClass();
92              return re;
93  
94          } else {
95              return internalLookup( name );
96          }
97      }
98      /**
99         Is the 'root' class really a super class or super interface of 'sub'
100        @param root the proposed super class.
101        @param sub the proposed subclass.
102        @return true if the too is the super class, false otherwise.
103      */
104     public static boolean isSuperClass( Class root, Class sub ) {
105         boolean retval = false;
106         retval = root.isAssignableFrom( sub );
107         return retval;
108     }
109 
110 }