formatting only
[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     public String humanReadable() {
29         if (descriptor.equals("V")) return "void";
30         if (descriptor.equals("I")) return "int";
31         if (descriptor.equals("J")) return "long";
32         if (descriptor.equals("Z")) return "boolean";
33         if (descriptor.equals("D")) return "double";
34         if (descriptor.equals("F")) return "float";
35         if (descriptor.equals("B")) return "byte";
36         if (descriptor.equals("C")) return "char";
37         if (descriptor.equals("S")) return "short";
38         throw new Error("confounded by Type("+descriptor+")");
39     }
40
41     protected Type(String descriptor) { this.descriptor = descriptor; }
42     
43     public static Type fromDescriptor(String d) {
44         if (d.equals("V")) return VOID;
45         if (d.equals("I")) return INT;
46         if (d.equals("J")) return LONG;
47         if (d.equals("Z")) return BOOLEAN;
48         if (d.equals("D")) return DOUBLE;
49         if (d.equals("F")) return FLOAT;
50         if (d.equals("B")) return BYTE;
51         if (d.equals("C")) return CHAR;
52         if (d.equals("S")) return SHORT;
53         if (d.endsWith("["))
54             return new Type.Array(fromDescriptor(d.substring(0, d.indexOf('['))),
55                                   d.length() - d.indexOf('['));
56         return new Type.Object(d);
57     }
58         
59     /** Returns the Java descriptor string for this object ("I", or "Ljava/lang/String", "[[J", etc */
60     public final String getDescriptor() { return descriptor; }
61     public int hashCode() { return descriptor.hashCode(); }
62     public boolean equals(java.lang.Object o) { return o instanceof Type && ((Type)o).descriptor.equals(descriptor); }
63     
64     public String toString() { return getDescriptor(); }
65     public Type.Object asObject() { throw new RuntimeException("attempted to use "+this+" as a Type.Object, which it is not"); }
66     public Type.Array asArray() { throw new RuntimeException("attempted to use "+this+" as a Type.Array, which it is not"); }
67     public Type.Array makeArray() { return new Type.Array(this); }
68     public Type.Array makeArray(int dim) { return new Type.Array(this, dim); }
69     public boolean isObject() { return false; }
70     public boolean isArray() { return false; }
71
72     /** Class representing Object types (any non-primitive type) */
73     public static class Object extends Type {
74         protected Object(String s) { super(_initHelper(s)); }
75         public Type.Object asObject() { return this; }
76         public boolean isObject() { return true; }
77         public String humanReadable() { return internalForm().replace('/', '.'); }
78         public String getShortName() {
79             String hr = humanReadable();
80             return hr.substring(hr.lastIndexOf('.')+1);
81         }
82
83         private static String _initHelper(String s) {
84             if (!s.startsWith("L") || !s.endsWith(";")) s = "L" + s.replace('.', '/') + ";";
85             return s;
86         }
87
88         String[] components() {
89             StringTokenizer st = new StringTokenizer(descriptor.substring(1, descriptor.length()-1), "/");
90             String[] a = new String[st.countTokens()];
91             for(int i=0;st.hasMoreTokens();i++) a[i] = st.nextToken();
92             return a;
93         }
94        
95         String internalForm() { return descriptor.substring(1, descriptor.length()-1); }
96     }    
97
98     public static class Array extends Object {
99         private int dim;
100         protected Array(Type t) { super(_initHelper(t, 1)); this.dim = 1; }
101         protected Array(Type t, int dim) { super(_initHelper(t, dim)); this.dim = dim; }
102         public Type.Array asArray() { return this; }
103         public boolean isArray() { return true; }
104         public String humanReadable() { 
105             String ret = super.internalForm().replace('/', '.');
106             for(int i=0; i<dim; i++) ret += "[]";
107             return ret;
108         }
109         String internalForm() { throw new Error("Type.Array does not have an internalForm()"); }
110         String[] components() { throw new Error("Type.Array does not have components()"); }
111         private static String _initHelper(Type t, int dim) {
112             StringBuffer sb = new StringBuffer(t.descriptor.length() + dim);
113             for(int i=0;i<dim;i++) sb.append("[");
114             sb.append(t.descriptor);
115             return sb.toString();
116         }
117     }
118
119 }