> Very good catch.  Definitely needs a test, too. The only thing it'll work
> for right now is nested arrays.
> 
> I think it would be a lot easier (and more efficient) to parse in
> Class.java.  How's this?
> 
> (Source:
> http://java.sun.com/products/jdk/1.1/docs/guide/jni/spec/types.doc.html#1643
> 2)
> 
> This is not even compiled but It Should Work (TM).

I don't mean to criticise, but writing code without compiling it is extremely error 
prone.
 
The following patch works with my JVM (and I've run some tests against the IBM JVM), I 
hope I haven't missed anything.

BTW, is this the correct way to generate and submit a patch?

John Leuner
-- 
Index: vm/reference/java/lang/Class.java
===================================================================
RCS file: /cvs/classpath/vm/reference/java/lang/Class.java,v
retrieving revision 1.11
diff -u -r1.11 Class.java
--- vm/reference/java/lang/Class.java   2001/01/09 23:35:07     1.11
+++ vm/reference/java/lang/Class.java   2001/04/26 01:01:13
@@ -244,26 +244,62 @@
      */
     public native boolean isPrimitive();
     
-    /** 
-     * If this is an array, get the Class representing the
-     * type of array.  Examples: [[java.lang.String would
-     * return [java.lang.String, and calling getComponentType
-     * on that would give java.lang.String.  If this is not
-     * an array, returns null.
-     * @return the array type of this class, or null.
-     * @since JDK1.1
-     */
-    public Class getComponentType() {
-       if(isArray()) {
-           try {
-               return Class.forName(getName().substring(1));
-           } catch(ClassNotFoundException e) {
-               return null;
+  /** If this is an array, get the Class representing the 
+   ** type of array.  Examples: [[Ljava.lang.String; would 
+   ** return [Ljava.lang.String;, and calling getComponentType 
+   ** on that would give java.lang.String.  If this is not 
+   ** an array, returns null. 
+   ** @return the array type of this class, or null. 
+   ** @since JDK1.1 
+   **/ 
+       public Class getComponentType()
+       {
+         if(isArray())
+           {
+             String name = getName();
+             try {
+               if(name.charAt(0) != '[')
+                 throw new InternalError("Bad array class name encountered in 
+Class.getComponentType(): \"" + name + "\", doesn't start with '['");
+               try {
+                 /* Check it is an object array */
+                 if(name.charAt(1) == 'L')
+                   return Class.forName(name.substring(2, name.length() - 1));
+                 /* Check it is a nested array */
+                 if(name.charAt(1) == '[')
+                   return Class.forName(name.substring(1)); 
+                 /* Check for primitive types */
+                 switch(name.charAt(1))
+                   {
+                   case 'Z':
+                     return Boolean.TYPE;
+                   case 'B':
+                     return Byte.TYPE;
+                   case 'C':
+                     return Character.TYPE;
+                   case 'S':
+                     return Short.TYPE;
+                   case 'I':
+                     return Integer.TYPE;
+                   case 'J':
+                     return Long.TYPE;
+                   case 'F':
+                     return Float.TYPE;
+                   case 'D':
+                     return Double.TYPE;
+                   }
+                 /* If we get here then the classname is incorrect */
+                 throw new InternalError("Bad class name encountered in 
+Class.getComponentType(): \"" + name + "\"");
+                 } catch(ArrayIndexOutOfBoundsException _)
+                   {
+                     throw new InternalError("Bad class name encountered in 
+Class.getComponentType(): \"" + name + "\", '[' not followed by anything");
+                   }
+             } catch(ClassNotFoundException __)
+               {
+                 return null;
+               }
            }
-       } else {
-           return null;
+         return null;
        }
-    }
     
     /** 
      * Get the signers of this class.

Reply via email to