pulled {Method,Member,Field}Ref into Type.Class; made them inner classes; much cleaner
[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.Class OBJECT = new Type.Class("java.lang.Object");
23     public static final Type.Class STRING = new Type.Class("java.lang.String");
24     public static final Type.Class STRINGBUFFER = new Type.Class("java.lang.StringBuffer");
25     public static final Type.Class INTEGER_OBJECT = new Type.Class("java.lang.Integer");
26     public static final Type.Class DOUBLE_OBJECT = new Type.Class("java.lang.Double");
27     public static final Type.Class FLOAT_OBJECT = new Type.Class("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 instance(String d) {
34         Type ret = (Type)instances.get(d);
35         if (ret != null) return ret;
36         if (d.startsWith("[")) return new Type.Array(instance(d.substring(1)));
37         return new Type.Class(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.Array  makeArray() { return (Type.Array)instance("["+descriptor); }
46
47     public Type.Ref    asRef()       { throw new RuntimeException("attempted to use "+this+" as a Type.Ref, which it is not"); }
48     public Type.Class  asClass()     { throw new RuntimeException("attempted to use "+this+" as a Type.Class, which it is not"); }
49     public Type.Array  asArray()     { throw new RuntimeException("attempted to use "+this+" as a Type.Array, which it is not"); }
50     public boolean     isPrimitive() { return !isRef(); }
51     public boolean     isRef()       { return false; }
52     public boolean     isClass()     { return false; }
53     public boolean     isArray()     { return false; }
54
55     // Protected/Private //////////////////////////////////////////////////////////////////////////////
56
57     protected final String descriptor;
58     protected final String toString;
59     protected Type(String descriptor) { this(descriptor, descriptor); }
60     protected Type(String descriptor, String humanReadable) {
61         this.toString = humanReadable;
62         instances.put(this.descriptor = descriptor, this);
63     }
64
65     public static class Ref extends Type {
66         protected Ref(String descriptor) { super(descriptor); }
67         protected Ref(String descriptor, String humanReadable) { super(descriptor, humanReadable); }
68         public    Type.Ref asRef() { return this; }
69         public    boolean  isRef() { return true; }
70     }
71
72     public static class Class extends Type.Ref {
73         protected Class(String s) { super(_initHelper(s), _initHelper2(s)); }
74         public Type.Class asClass() { return this; }
75         public boolean isClass() { return true; }
76         public String getShortName() { return toString.substring(toString.lastIndexOf('.')+1); }
77         String internalForm() { return descriptor.substring(1, descriptor.length()-1); }
78         private static String _initHelper(String s) {
79             if (!s.startsWith("L") || !s.endsWith(";")) s = "L" + s.replace('.', '/') + ";";
80             return s;
81         }
82         private static String _initHelper2(String s) {
83             if (s.startsWith("L") && s.endsWith(";")) s = s.substring(1, s.length()-1);
84             return s.replace('/', '.');
85         }
86         String[] components() {
87             StringTokenizer st = new StringTokenizer(descriptor.substring(1, descriptor.length()-1), "/");
88             String[] a = new String[st.countTokens()];
89             for(int i=0;st.hasMoreTokens();i++) a[i] = st.nextToken();
90             return a;
91         }
92
93         public Method method(String name, Type returnType, Type[] argTypes) { return new Method(name, returnType, argTypes); }
94         public Field  field(String name, Type type) { return new Field(name, type); }
95
96         public abstract class Member {
97             public final String name;
98             private Member(String name) { this.name = name; }
99             public Type.Class getDeclaringClass() { return Type.Class.this; }
100             public abstract String getDescriptor();
101             public boolean equals(Object o_) {
102                 if(!(o_ instanceof Member)) return false;
103                 Member o = (Member) o_;
104                 return o.getDeclaringClass().equals(getDeclaringClass()) &&
105                     o.name.equals(name) &&
106                     o.getDescriptor().equals(getDescriptor());
107             }
108             public int hashCode() { return getDeclaringClass().hashCode() ^ name.hashCode() ^ getDescriptor().hashCode(); }
109         }
110     
111         public class Field extends Member {
112             /** Create a reference to field <i>name</i> of class <i>c</i> with the type <i>t</i>  */    
113             public final Type type;
114             private Field(String name, Type t) { super(name); this.type = t; }
115             public String getDescriptor() { return name; }
116         }
117
118         public class Method extends Member {
119             final Type[] argTypes;
120             final Type   returnType;
121             private Method(String name, Type returnType, Type[] argTypes) {
122                 super(name);
123                 this.argTypes = argTypes;
124                 this.returnType = returnType;
125             }
126             public String getDescriptor() {
127                 StringBuffer sb = new StringBuffer(argTypes.length*4);
128                 sb.append("(");
129                 for(int i=0;i<argTypes.length;i++) sb.append(argTypes[i].getDescriptor());
130                 sb.append(")");
131                 sb.append(returnType.getDescriptor());
132                 return sb.toString();
133             }
134         }
135
136     }    
137
138     public static class Array extends Type.Ref {
139         protected Array(Type t) { super("[" + t.getDescriptor(), t.toString() + "[]"); }
140         public Type.Array asArray() { return this; }
141         public boolean isArray() { return true; }
142         public int dimension() { return getDescriptor().lastIndexOf('['); }
143     }
144
145 }