Bug 163560 - [1.5][compiler] Type mismatch between Annotation and a specific annotation
Summary: [1.5][compiler] Type mismatch between Annotation and a specific annotation
Status: RESOLVED DUPLICATE of bug 106744
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: Maxime Daniel CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-11-06 11:56 EST by Olivier Thomann CLA
Modified: 2006-12-05 11:31 EST (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Olivier Thomann CLA 2006-11-06 11:56:26 EST
Using latest (3.3M3), the compiler reports an error compiling this code:
[
import java.lang.annotation.Target;
import java.lang.annotation.Retention;
import static java.lang.annotation.RetentionPolicy.*;
import static java.lang.annotation.ElementType.*;
import java.lang.reflect.*;

@Target(CONSTRUCTOR)
@Retention(RUNTIME)
@interface Annot {
    String value();
}

public class X {
	@Annot("SUCCESS")
	public X() {
	}

    static String getAnnotationValue(Constructor constructor) {
        Annot annotation = constructor.getAnnotation(Annot.class);
        return (annotation != null)
                ? annotation.value()
                : null;
    }

    static void displayConstructorAnnotationValues(Class type) {
        for (Constructor constructor : type.getConstructors()) {
            System.out.println(getAnnotationValue(constructor));
        }
    }

    public static void main(String[] args) {
    	displayConstructorAnnotationValues(X.class);
    }
}
]
----------
1. ERROR in D:\tests_sources\X.java (at line 19)
	Annot annotation = constructor.getAnnotation(Annot.class);
	                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Type mismatch: cannot convert from Annotation to Annot
----------
1 problem (1 error)

javac 1.5.0_09 and 1.6b104 compile it fine and at runtime returns:
"SUCCESS"
Comment 1 Olivier Thomann CLA 2006-11-06 12:03:26 EST
This looks like the returned type for:
constructor.getAnnotation(Annot.class)
is Annotation instead of Annot.
Comment 2 Philipe Mulet CLA 2006-12-05 07:50:27 EST
Feels like raw conversion would be more selective by not altering all type parameters (only those carried by the generic type being converted, not params from a generic method).
Comment 3 Philipe Mulet CLA 2006-12-05 07:55:59 EST
I take back my previous comment. Nothing seems to have changed since both javac 1.5 and 1.6 agree with us in rejecting the following:

public class X<T> {
	<T> T foo(Class<T> c) {
		return null;
	}
	static <T> T staticFoo(Class<T> c) {
		return null;
	}
	void bar() {
		X x = null;
		x = x.foo(X.class); // rawified
		x = staticFoo(X.class);
	}
}
Comment 4 Philipe Mulet CLA 2006-12-05 07:57:56 EST
Also see bug 106744
Comment 5 Maxime Daniel CLA 2006-12-05 11:31:15 EST

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