classpath
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Problem in Class.java


From: John Keiser
Subject: Re: Problem in Class.java
Date: Wed, 25 Apr 2001 17:35:45 -0600

From: "John Leuner" <address@hidden>


> The method in vm/reference/java/lang/Class.java:
>
>    public Class getComponentType() {
> if(isArray()) {
>     try {
> return Class.forName(getName().substring(1));
>     } catch(ClassNotFoundException e) {
> return null;
>     }
> } else {
>     return null;
> }
>     }
>
> is wrong.
>

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)

public Class getComponentType() {
    if(isArray()) {
        try {
            String name = getName();
            switch(name.charAt(1)) {
                case "[":
                    return Class.forName(name.substring(1));
                case "L":
                    return Class.forName(name.substring(2,
name.length()-1));
                case "Z":
                    return Boolean.class;
                case "B":
                    return Byte.class;
                case "C":
                    return Character.class;
                case "S":
                    return Short.class;
                case "I":
                    return Integer.class;
                case "J":
                    return Long.class;
                case "F":
                    return Float.class;
                case "D":
                    return Double.class;
            }
        } catch(ClassNotFoundException e) {
            return null;
        }
    } else {
        return null;
    }
}

This is not even compiled but It Should Work (TM).

--John





reply via email to

[Prev in Thread] Current Thread [Next in Thread]