Bug 156891 - [1.5] [compiler] Compilation difference between javac and jdt
Summary: [1.5] [compiler] Compilation difference between javac and jdt
Status: RESOLVED DUPLICATE of bug 149751
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.3   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Olivier Thomann CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-09-11 12:31 EDT by Julio Faerman CLA
Modified: 2006-09-12 11:35 EDT (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Julio Faerman CLA 2006-09-11 12:31:03 EDT
The following code does not compile using javac and compiles using eclipse:

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

public class Weird {
	
	public static final SomeAnnotParam[] myparams = {SomeAnnotParam.param1,SomeAnnotParam.param2};
	
	@SomeAnnot(params=myparams) 
	public void dothetrick(){} 
	
	
	public static void main(String[] args)throws Exception {		
		SomeAnnot sluck = Weird.class.getMethod("dothetrick", new Class[0]).getAnnotation(SomeAnnot.class);
		System.out.println(sluck.params());
	}
}
 
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@interface SomeAnnot {
	
	public SomeAnnotParam[] params();
}
 
enum SomeAnnotParam {
	param1,
	param2,
	param3
}
Comment 1 Olivier Thomann CLA 2006-09-11 13:00:45 EDT
If you write:
        @SomeAnnot(params= { SomeAnnotParam.param1, SomeAnnotParam.param2 } ) 
        public void dothetrick(){} 
it works with javac.
Comment 2 Julio Faerman CLA 2006-09-11 13:22:45 EDT
(In reply to comment #1)
> If you write:
>         @SomeAnnot(params= { SomeAnnotParam.param1, SomeAnnotParam.param2 } ) 
>         public void dothetrick(){} 
> it works with javac.
> 

That is not the point. If if does not compile using javac, it should not compile using eclipse too, right? But this code gives no error on eclipse.
Comment 3 Olivier Thomann CLA 2006-09-12 10:40:22 EDT
The point is that the error message returned by javac is awkwards.
X.java:11: incompatible types
found   : SomeAnnotParam[]
required: SomeAnnotParam
        @SomeAnnot(params=myparams)
                          ^
1 error

The types are not incompatibles. I believe the problem is the same than referring to a constant indirectly.
For example, with:
[import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

public class X {

      public static final SomeAnnotParam myparam = SomeAnnotParam.param1;

        @SomeAnnot(param=myparam) 
        public void dothetrick(){} 


        public static void main(String[] args)throws Exception {                
                SomeAnnot sluck = X.class.getMethod("dothetrick", new
Class[0]).getAnnotation(SomeAnnot.class);
                System.out.println(sluck.param());
        }
}

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@interface SomeAnnot {

        public SomeAnnotParam param();
}

enum SomeAnnotParam {
        param1,
        param2,
        param3
}]

you get:

X.java:11: an enum annotation value must be an enum constant
        @SomeAnnot(param=myparam)
                         ^
1 error

I'll investigate.
Comment 4 Olivier Thomann CLA 2006-09-12 11:35:58 EDT
This is a case similar to bug 149751.
This covers the array initializer case referenced through a name reference. Both should be fixed at the same time.
Closing as dup of bug 149751.

*** This bug has been marked as a duplicate of 149751 ***