package org.ibex.classgen;

import java.util.StringTokenizer;

public class Type {
    public static final Type VOID = new Type("V");
    public static final Type INT = new Type("I");
    public static final Type LONG = new Type("J");
    public static final Type BOOLEAN = new Type("Z");
    public static final Type DOUBLE = new Type("D");
    public static final Type FLOAT = new Type("F");
    
    public static final Type.Object OBJECT = new Type.Object("java.lang.Object");
    public static final Type.Object STRING = new Type.Object("java.lang.String");
    public static final Type.Object STRINGBUFFER = new Type.Object("java.lang.StringBuffer");
    public static final Type.Object INTEGER_OBJECT = new Type.Object("java.lang.Integer");
    public static final Type.Object DOUBLE_OBJECT = new Type.Object("java.lang.Double");
    public static final Type.Object FLOAT_OBJECT = new Type.Object("java.lang.Float");
    
    public static final Type[] NO_ARGS = new Type[0];
    
    String descriptor;
    
    Type() { }
    Type(String descriptor) { this.descriptor = descriptor; }
    
    public final String getDescriptor() { return descriptor; }
    public int hashCode() { return descriptor.hashCode(); }
    public boolean equals(Object o) { return o instanceof Type && ((Type)o).descriptor.equals(descriptor); }
    
    public static Type arrayType(Type base) { return arrayType(base,1); }
    public static Type arrayType(Type base, int dim) {
        StringBuffer sb = new StringBuffer(base.descriptor.length() + dim);
        for(int i=0;i<dim;i++) sb.append("[");
        sb.append(base.descriptor);
        return new Type(sb.toString());
    }
    
    public static class Object extends Type {
        public Object(String s) {
            if(!s.startsWith("L") || !s.endsWith(";")) s = "L" + s.replace('.','/') + ";";
            if(!validDescriptorString(s)) throw new IllegalArgumentException("invalid descriptor string");
            descriptor = s;
        }
        
        public String[] components() {
            StringTokenizer st = new StringTokenizer(descriptor.substring(1,descriptor.length()-1),"/");
            String[] a = new String[st.countTokens()];
            for(int i=0;st.hasMoreTokens();i++) a[i] = st.nextToken();
            return a;
        }
        
        public String internalForm() { return descriptor.substring(1,descriptor.length()-1); }
        
        // FEATURE: Do a proper check here (invalid chars, etc)
        public boolean validDescriptorString(String s) {
            return s.startsWith("L") && s.endsWith(";");
        }
    }    
}

@@ -981,24 +981,30 @@
 %5D;%0A    %0A    
+final 
 String descr
@@ -1019,23 +1019,8 @@
    %0A
-    Type() %7B %7D%0A
     
@@ -1231,16 +1231,26 @@
  equals(
+java.lang.
 Object o
@@ -1739,32 +1739,120 @@
 ject(String s) %7B
+ super(_initHelper(s)); %7D%0A        %0A        private static String _initHelper(String s) %7B
 %0A            if(
@@ -2046,28 +2046,22 @@
         
-descriptor =
+return
  s;%0A    
@@ -2062,32 +2062,24 @@
 ;%0A        %7D%0A
-        
 %0A        pub
@@ -2550,28 +2550,28 @@
 tc)%0A        
-publ
+stat
 ic boolean v

fix Type.equals