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 protected Type(String descriptor) { this.descriptor = descriptor; }
30 public static Type fromDescriptor(String descriptor) {
31 if(descriptor.equals("V")) return VOID;
32 if(descriptor.equals("I")) return INT;
33 if(descriptor.equals("J")) return LONG;
34 if(descriptor.equals("Z")) return BOOLEAN;
35 if(descriptor.equals("D")) return DOUBLE;
36 if(descriptor.equals("F")) return FLOAT;
37 if(descriptor.equals("B")) return BYTE;
38 if(descriptor.equals("C")) return CHAR;
39 if(descriptor.equals("S")) return SHORT;
40 if(descriptor.endsWith("[")) return new Type.Array(fromDescriptor(descriptor.substring(0, descriptor.indexOf('['))),
41 descriptor.length() - descriptor.indexOf('['));
42 if(Type.Object.validDescriptorString(descriptor)) return new Type.Object(descriptor);
46 /** Returns the Java descriptor string for this object ("I", or "Ljava/lang/String", "[[J", etc */
47 public final String getDescriptor() { return descriptor; }
48 public int hashCode() { return descriptor.hashCode(); }
49 public boolean equals(java.lang.Object o) { return o instanceof Type && ((Type)o).descriptor.equals(descriptor); }
51 /** Returns a one dimensional array type for the base type <i>base</i>
52 @param base The base type
53 @return A one dimensional array of the base type
55 public static Type arrayType(Type base) { return arrayType(base, 1); }
56 /** Returns a <i>dim</i> dimensional array type for the base type <i>base</i>
57 @param base The base type
58 @param dim Number if dimensions
59 @return A one dimensional array of the base type
61 public static Type arrayType(Type base, int dim) { return new Type.Array(base, dim); }
63 public Type.Object asObject() { throw new RuntimeException("attempted to use "+this+" as a Type.Object, which it is not"); }
64 public Type.Array asArray() { throw new RuntimeException("attempted to use "+this+" as a Type.Array, which it is not"); }
65 public boolean isObject() { return false; }
66 public boolean isArray() { return false; }
68 /** Class representing Object types (any non-primitive type) */
69 public static class Object extends Type {
70 /** Create an Type.Object instance for the specified string. <i>s</i> can be a string in the form
71 "java.lang.String", "java/lang/String", or "Ljava/lang/String;".
73 protected Object(String s) { super(_initHelper(s)); }
74 public Type.Object asObject() { return this; }
75 public boolean isObject() { return true; }
77 private static String _initHelper(String s) {
78 if(!s.startsWith("L") || !s.endsWith(";")) s = "L" + s.replace('.', '/') + ";";
79 if(!validDescriptorString(s)) throw new IllegalArgumentException("invalid descriptor string");
83 String[] components() {
84 StringTokenizer st = new StringTokenizer(descriptor.substring(1, descriptor.length()-1), "/");
85 String[] a = new String[st.countTokens()];
86 for(int i=0;st.hasMoreTokens();i++) a[i] = st.nextToken();
90 String internalForm() { return descriptor.substring(1, descriptor.length()-1); }
92 static boolean validDescriptorString(String s) {
93 return s.startsWith("L") && s.endsWith(";");
97 public static class Array extends Object {
98 protected Array(Type t, int dim) { super(_initHelper(t, dim)); }
99 public Type.Array asArray() { return this; }
100 public boolean isArray() { return true; }
101 String internalForm() { throw new Error("Type.Array does not have an internalForm()"); }
102 String[] components() { throw new Error("Type.Array does not have components()"); }
103 private static String _initHelper(Type t, int dim) {
104 StringBuffer sb = new StringBuffer(t.descriptor.length() + dim);
105 for(int i=0;i<dim;i++) sb.append("[");
106 sb.append(t.descriptor);
107 return sb.toString();