1 package org.ibex.classgen;
3 import java.util.StringTokenizer;
4 import java.util.Hashtable;
8 private static Hashtable instances = new Hashtable(); // this has to appear at the top of the file
10 // Public API //////////////////////////////////////////////////////////////////////////////
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");
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");
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];
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);
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; }
45 public Type.Array makeArray() { return (Type.Array)instance("["+descriptor); }
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; }
55 // Protected/Private //////////////////////////////////////////////////////////////////////////////
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);
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; }
72 public static class Array extends Type.Ref {
73 protected Array(Type t) { super("[" + t.getDescriptor(), t.toString() + "[]"); }
74 public Type.Array asArray() { return this; }
75 public boolean isArray() { return true; }
76 public int dimension() { return getDescriptor().lastIndexOf('['); }
79 public static class Class extends Type.Ref {
80 protected Class(String s) { super(_initHelper(s), _initHelper2(s)); }
81 public Type.Class asClass() { return this; }
82 public boolean isClass() { return true; }
83 public String getShortName() { return toString.substring(toString.lastIndexOf('.')+1); }
84 String internalForm() { return descriptor.substring(1, descriptor.length()-1); }
85 private static String _initHelper(String s) {
86 if (!s.startsWith("L") || !s.endsWith(";")) s = "L" + s.replace('.', '/') + ";";
89 private static String _initHelper2(String s) {
90 if (s.startsWith("L") && s.endsWith(";")) s = s.substring(1, s.length()-1);
91 return s.replace('/', '.');
93 String[] components() {
94 StringTokenizer st = new StringTokenizer(descriptor.substring(1, descriptor.length()-1), "/");
95 String[] a = new String[st.countTokens()];
96 for(int i=0;st.hasMoreTokens();i++) a[i] = st.nextToken();
100 public Method method(String name, Type returnType, Type[] argTypes) { return new Method(name, returnType, argTypes); }
101 public Field field(String name, Type type) { return new Field(name, type); }
103 public abstract class Member {
104 public final String name;
105 private Member(String name) { this.name = name; }
106 public Type.Class getDeclaringClass() { return Type.Class.this; }
107 public abstract String getDescriptor();
108 public boolean equals(Object o_) {
109 if(!(o_ instanceof Member)) return false;
110 Member o = (Member) o_;
111 return o.getDeclaringClass().equals(getDeclaringClass()) &&
112 o.name.equals(name) &&
113 o.getDescriptor().equals(getDescriptor());
115 public int hashCode() { return getDeclaringClass().hashCode() ^ name.hashCode() ^ getDescriptor().hashCode(); }
118 public class Field extends Member {
119 public final Type type;
120 private Field(String name, Type t) { super(name); this.type = t; }
121 public String getDescriptor() { return name; }
124 public class Method extends Member {
125 final Type[] argTypes;
126 final Type returnType;
127 private Method(String name, Type returnType, Type[] argTypes) {
129 this.argTypes = argTypes;
130 this.returnType = returnType;
132 public String getDescriptor() {
133 StringBuffer sb = new StringBuffer(argTypes.length*4);
135 for(int i=0;i<argTypes.length;i++) sb.append(argTypes[i].getDescriptor());
137 sb.append(returnType.getDescriptor());
138 return sb.toString();