misc cleanup and javadoc comments
[org.ibex.classgen.git] / src / org / ibex / classgen / Type.java
1 package org.ibex.classgen;
2
3 import java.util.StringTokenizer;
4
5 public class Type {
6     public static final Type VOID = new Type("V");
7     public static final Type INT = new Type("I");
8     public static final Type LONG = new Type("J");
9     public static final Type BOOLEAN = new Type("Z");
10     public static final Type DOUBLE = new Type("D");
11     public static final Type FLOAT = new Type("F");
12     public static final Type BYTE = new Type("B");
13     public static final Type CHAR = new Type("C");
14     public static final Type SHORT = new Type("S");
15     
16     public static final Type.Object OBJECT = new Type.Object("java.lang.Object");
17     public static final Type.Object STRING = new Type.Object("java.lang.String");
18     public static final Type.Object STRINGBUFFER = new Type.Object("java.lang.StringBuffer");
19     public static final Type.Object INTEGER_OBJECT = new Type.Object("java.lang.Integer");
20     public static final Type.Object DOUBLE_OBJECT = new Type.Object("java.lang.Double");
21     public static final Type.Object FLOAT_OBJECT = new Type.Object("java.lang.Float");
22     
23     /** A zero element Type[] array (can be passed as the "args" param when a method takes no arguments */
24     public static final Type[] NO_ARGS = new Type[0];
25     
26     final String descriptor;
27     
28     Type(String descriptor) { this.descriptor = descriptor; }
29     
30     /** Returns the Java descriptor string for this object ("I", or "Ljava/lang/String", "[[J", etc */
31     public final String getDescriptor() { return descriptor; }
32     public int hashCode() { return descriptor.hashCode(); }
33     public boolean equals(java.lang.Object o) { return o instanceof Type && ((Type)o).descriptor.equals(descriptor); }
34     
35     /** Returns a one dimensional array type for the base type <i>base</i>
36         @param base The base type
37         @return A one dimensional array of the base type
38     */
39     public static Type arrayType(Type base) { return arrayType(base,1); }
40     /** Returns a <i>dim</i> dimensional array type for the base type <i>base</i>
41         @param base The base type
42         @param dim Number if dimensions
43         @return A one dimensional array of the base type
44     */
45     public static Type arrayType(Type base, int dim) {
46         StringBuffer sb = new StringBuffer(base.descriptor.length() + dim);
47         for(int i=0;i<dim;i++) sb.append("[");
48         sb.append(base.descriptor);
49         return new Type(sb.toString());
50     }
51     
52     /** Class representing Objec types (any non-primitive type) */
53     public static class Object extends Type {
54         /** Create an Type.Object instance for the specified string. <i>s</i> can be a string in the form
55             "java.lang.String", "java/lang/String", or "Ljava/lang/String;".
56             @param s The type */
57         public Object(String s) { super(_initHelper(s)); }
58         
59         private static String _initHelper(String s) {
60             if(!s.startsWith("L") || !s.endsWith(";")) s = "L" + s.replace('.','/') + ";";
61             if(!validDescriptorString(s)) throw new IllegalArgumentException("invalid descriptor string");
62             return s;
63         }
64
65         String[] components() {
66             StringTokenizer st = new StringTokenizer(descriptor.substring(1,descriptor.length()-1),"/");
67             String[] a = new String[st.countTokens()];
68             for(int i=0;st.hasMoreTokens();i++) a[i] = st.nextToken();
69             return a;
70         }
71         
72         String internalForm() { return descriptor.substring(1,descriptor.length()-1); }
73         
74         // FEATURE: Do a proper check here (invalid chars, etc)
75         static boolean validDescriptorString(String s) {
76             return s.startsWith("L") && s.endsWith(";");
77         }
78     }    
79 }