ec5a30a83363075e43743f2a177293bfe7080108
[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     Type(String descriptor) { this.descriptor = descriptor; }
29     
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(Type.Object.validDescriptorString(descriptor)) return new Type.Object(descriptor);
41         return null;
42     }
43         
44     /** Returns the Java descriptor string for this object ("I", or "Ljava/lang/String", "[[J", etc */
45     public final String getDescriptor() { return descriptor; }
46     public int hashCode() { return descriptor.hashCode(); }
47     public boolean equals(java.lang.Object o) { return o instanceof Type && ((Type)o).descriptor.equals(descriptor); }
48     
49     /** Returns a one dimensional array type for the base type <i>base</i>
50         @param base The base type
51         @return A one dimensional array of the base type
52     */
53     public static Type arrayType(Type base) { return arrayType(base,1); }
54     /** Returns a <i>dim</i> dimensional array type for the base type <i>base</i>
55         @param base The base type
56         @param dim Number if dimensions
57         @return A one dimensional array of the base type
58     */
59     public static Type arrayType(Type base, int dim) {
60         StringBuffer sb = new StringBuffer(base.descriptor.length() + dim);
61         for(int i=0;i<dim;i++) sb.append("[");
62         sb.append(base.descriptor);
63         return new Type(sb.toString());
64     }
65     
66     /** Class representing Objec types (any non-primitive type) */
67     public static class Object extends Type {
68         /** Create an Type.Object instance for the specified string. <i>s</i> can be a string in the form
69             "java.lang.String", "java/lang/String", or "Ljava/lang/String;".
70             @param s The type */
71         public Object(String s) { super(_initHelper(s)); }
72         
73         private static String _initHelper(String s) {
74             if(!s.startsWith("L") || !s.endsWith(";")) s = "L" + s.replace('.','/') + ";";
75             if(!validDescriptorString(s)) throw new IllegalArgumentException("invalid descriptor string");
76             return s;
77         }
78
79         String[] components() {
80             StringTokenizer st = new StringTokenizer(descriptor.substring(1,descriptor.length()-1),"/");
81             String[] a = new String[st.countTokens()];
82             for(int i=0;st.hasMoreTokens();i++) a[i] = st.nextToken();
83             return a;
84         }
85         
86         String internalForm() { return descriptor.substring(1,descriptor.length()-1); }
87         
88         static boolean validDescriptorString(String s) {
89             return s.startsWith("L") && s.endsWith(";");
90         }
91     }    
92 }