Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Java Generics broken beyond repair?



You could try asm http://asm.objectweb.org.

But its not going to be as easy as simply removing the checkcast instructions. The return type of the method itself is Object[] and arrays of primitive types are not subclasses of Object[]. 

Question. Why don't you simply use different methods for Byte[] and byte[] creation.


On 13-Dec-06, at 9:42 PM, Aeon B. C. Lee wrote:

Yes, you have nailed  it.

So if I remove the checkcast instructions, the code will be just fine. Do you know any handy tool to edit the compiled class file?

Thanks, Aeon

On Dec 13, 2006, at 10:49 AM, Jerry wrote:

Hi,

Yes, I agree that it's a bug. I think the problem is that the function definition and its call are both fine in isolation, but not when put together (and I'm glad that I don't have to try and write the code which would spot this at compile time). The annoying thing is that if the compiler didn't issue the checkcast instruction at the end of your function, I think the code would work fine. The compiler is making the faulty assumption that T[] is an Object[] when in your case it isn't.

class Array extends java.lang.Object{
Array();
  Code:
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
   4:   return

static java.lang.Object[] create(java.lang.Class, int);
  Code:
   0:   getstatic       #2; //Field java/lang/System.out:Ljava/io/PrintStream;
   3:   aload_0
   4:   invokevirtual   #3; //Method java/io/PrintStream.println:(Ljava/lang/Object;)V
   7:   aload_0
   8:   iload_1
   9:   invokestatic    #4; //Method java/lang/reflect/Array.newInstance:(Ljava/lang/Class;I)Ljava/lang/Object;
   12:  checkcast       #5; //class "[Ljava/lang/Object;"
   15:  checkcast       #5; //class "[Ljava/lang/Object;"
   18:  areturn

}

Jerry

On 13 Dec 2006, at 15:20, Aeon B. C. Lee wrote:

Thanks.  I know that Java Generics doesn't support primitives, but that is a entirely different bug.

Please note that the following statement

byte[] b = (byte[])java.lang.reflect.Array.newInstance(byte.class, 10);

is perfectly legal, because "The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects"

My code compiles without any problem, but gets a " java.lang.ClassCastException: [B cannot be cast to [Ljava.lang.Object" at runtime, which is just nonsense because the cast is from array of byte to array of T (where T is byte).

Aeon

On Dec 13, 2006, at 9:10 AM, Jerry wrote:


On 13 Dec 2006, at 13:59, Aeon B. C. Lee wrote:

Can anyone tell me what's wrong with the last statement of my code?

class Array
{
    static <T> T[] create(Class<T> type, int length)
    {
        return (T[]) java.lang.reflect.Array.newInstance(type, length);
    }
}

public class NewMain
{
    public static void main(String[] args)
    {
      byte[] b = (byte[])java.lang.reflect.Array.newInstance(byte.class, 10); // This is OK
       System.out.println(Array.create(Byte.class, 10)); // So is this
       System.out.println(Array.create(byte.class, 10)); // WTF is wrong with this?
    }
}


Jerry

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden



 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/java-dev/email@hidden

This email sent to email@hidden

References: 
 >Java Generics broken beyond repair? (From: "Aeon B. C. Lee" <email@hidden>)
 >Re: Java Generics broken beyond repair? (From: Jerry <email@hidden>)
 >Re: Java Generics broken beyond repair? (From: "Aeon B. C. Lee" <email@hidden>)
 >Re: Java Generics broken beyond repair? (From: "Aeon B. C. Lee" <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.