massive cleanup of Type.java; introduced human-readable toString() and one-instance...
[org.ibex.classgen.git] / src / org / ibex / classgen / Type.java
1 package org.ibex.classgen;
2
3 import java.util.StringTokenizer;
4 import java.util.Hashtable;
5
6 public class Type {
7
8     private static Hashtable instances = new Hashtable();  // this has to appear at the top of the file
9
10     // Public API //////////////////////////////////////////////////////////////////////////////
11
12     public static final Type VOID = new Type("V", "void");
13     public static final Type INT = new Type("I", "int");
14     public static final Type LONG = new Type("J", "long");
15     public static final Type BOOLEAN = new Type("Z", "boolean");
16     public static final Type DOUBLE = new Type("D", "double");
17     public static final Type FLOAT = new Type("F", "float");
18     public static final Type BYTE = new Type("B", "byte");
19     public static final Type CHAR = new Type("C", "char");
20     public static final Type SHORT = new Type("S", "short");
21     
22     public static final Type.Object OBJECT = new Type.Object("java.lang.Object");
23     public static final Type.Object STRING = new Type.Object("java.lang.String");
24     public static final Type.Object STRINGBUFFER = new Type.Object("java.lang.StringBuffer");
25     public static final Type.Object INTEGER_OBJECT = new Type.Object("java.lang.Integer");
26     public static final Type.Object DOUBLE_OBJECT = new Type.Object("java.lang.Double");
27     public static final Type.Object FLOAT_OBJECT = new Type.Object("java.lang.Float");
28     
29     /** A zero element Type[] array (can be passed as the "args" param when a method takes no arguments */
30     public static final Type[] NO_ARGS = new Type[0];
31     
32     /** guarantee: there will only be one instance of Type for a given descriptor ==> equals() and == are interchangeable */
33     public static Type fromDescriptor(String d) {
34         Type ret = (Type)instances.get(d);
35         if (ret != null) return ret;
36         if (d.endsWith("[")) return new Type.Array(fromDescriptor(d.substring(d.length()-1)));
37         return new Type.Object(d);
38     }
39
40     public       String  toString() { return toString; }
41     public final String  getDescriptor() { return descriptor; }
42     public       int     hashCode() { return descriptor.hashCode(); }
43     public       boolean equals(java.lang.Object o) { return this==o; }
44
45     public Type.Object asObject() { throw new RuntimeException("attempted to use "+this+" as a Type.Object, which it is not"); }
46     public Type.Array  asArray() { throw new RuntimeException("attempted to use "+this+" as a Type.Array, which it is not"); }
47     public Type.Array  makeArray() { return new Type.Array(this); }
48     public boolean     isObject() { return false; }
49     public boolean     isArray() { return false; }
50
51     // Protected/Private //////////////////////////////////////////////////////////////////////////////
52
53     protected final String descriptor;
54     protected final String toString;
55     protected Type(String descriptor) { this(descriptor, descriptor); }
56     protected Type(String descriptor, String humanReadable) {
57         this.toString = humanReadable;
58         instances.put(this.descriptor = descriptor, this);
59     }
60
61     /** Class representing Object types (any non-primitive type) */
62     public static class Object extends Type {
63         protected Object(String s) { super(_initHelper(s), _initHelper2(s)); }
64         protected Object(String descriptor, String hr) { super(_initHelper(descriptor), _initHelper2(hr)); }
65         public Type.Object asObject() { return this; }
66         public boolean isObject() { return true; }
67         public String getShortName() { return toString.substring(toString.lastIndexOf('.')+1); }
68         String internalForm() { return descriptor.substring(1, descriptor.length()-1); }
69         private static String _initHelper(String s) {
70             if (!s.startsWith("L") || !s.endsWith(";")) s = "L" + s.replace('.', '/') + ";";
71             return s;
72         }
73         private static String _initHelper2(String s) {
74             if (s.startsWith("L") && s.endsWith(";")) s = s.substring(1, s.length()-1);
75             return s.replace('/', '.');
76         }
77         String[] components() {
78             StringTokenizer st = new StringTokenizer(descriptor.substring(1, descriptor.length()-1), "/");
79             String[] a = new String[st.countTokens()];
80             for(int i=0;st.hasMoreTokens();i++) a[i] = st.nextToken();
81             return a;
82         }
83     }    
84
85     public static class Array extends Type.Object {
86         protected Array(Type t) { super(t.getDescriptor() + "[", t.toString() + "[]"); }
87         public Type.Array asArray() { return this; }
88         public boolean isArray() { return true; }
89         public int dimension() { return descriptor.length() - descriptor.indexOf('['); }
90         String[] components() { throw new Error("Type.Array does not have components()"); }
91     }
92
93 }