1 package org.ibex.classgen;
3 import java.util.StringTokenizer;
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");
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");
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];
26 final String descriptor;
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+")");
41 protected Type(String descriptor) { this.descriptor = descriptor; }
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);
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); }
63 /** Returns a one dimensional array type for the base type <i>base</i>
64 @param base The base type
65 @return A one dimensional array of the base type
67 public static Type arrayType(Type base) { return arrayType(base, 1); }
68 /** Returns a <i>dim</i> dimensional array type for the base type <i>base</i>
69 @param base The base type
70 @param dim Number if dimensions
71 @return A one dimensional array of the base type
73 public static Type arrayType(Type base, int dim) { return new Type.Array(base, dim); }
75 public String toString() { return getDescriptor(); }
76 public Type.Object asObject() { throw new RuntimeException("attempted to use "+this+" as a Type.Object, which it is not"); }
77 public Type.Array asArray() { throw new RuntimeException("attempted to use "+this+" as a Type.Array, which it is not"); }
78 public boolean isObject() { return false; }
79 public boolean isArray() { return false; }
81 /** Class representing Object types (any non-primitive type) */
82 public static class Object extends Type {
83 /** Create an Type.Object instance for the specified string. <i>s</i> can be a string in the form
84 "java.lang.String", "java/lang/String", or "Ljava/lang/String;".
86 protected Object(String s) { super(_initHelper(s)); }
87 public Type.Object asObject() { return this; }
88 public boolean isObject() { return true; }
89 public String humanReadable() { return internalForm().replace('/', '.'); }
90 public String getShortName() {
91 String hr = humanReadable();
92 return hr.substring(hr.lastIndexOf('.')+1);
95 private static String _initHelper(String s) {
96 if(!s.startsWith("L") || !s.endsWith(";")) s = "L" + s.replace('.', '/') + ";";
97 if(!validDescriptorString(s)) throw new IllegalArgumentException("invalid descriptor string");
101 String[] components() {
102 StringTokenizer st = new StringTokenizer(descriptor.substring(1, descriptor.length()-1), "/");
103 String[] a = new String[st.countTokens()];
104 for(int i=0;st.hasMoreTokens();i++) a[i] = st.nextToken();
108 String internalForm() { return descriptor.substring(1, descriptor.length()-1); }
110 static boolean validDescriptorString(String s) {
111 return s.startsWith("L") && s.endsWith(";");
115 public static class Array extends Object {
117 protected Array(Type t, int dim) { super(_initHelper(t, dim)); this.dim = dim; }
118 public Type.Array asArray() { return this; }
119 public boolean isArray() { return true; }
120 public String humanReadable() {
121 String ret = super.internalForm().replace('/', '.');
122 for(int i=0; i<dim; i++) ret += "[]";
125 String internalForm() { throw new Error("Type.Array does not have an internalForm()"); }
126 String[] components() { throw new Error("Type.Array does not have components()"); }
127 private static String _initHelper(Type t, int dim) {
128 StringBuffer sb = new StringBuffer(t.descriptor.length() + dim);
129 for(int i=0;i<dim;i++) sb.append("[");
130 sb.append(t.descriptor);
131 return sb.toString();