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