8030049: RoundEnvironment.getElementsAnnotatedWith receives wrong elements
Match the required and actual annotations using Element equivalence rather than TypeMirror equivalence, to avoid trouble with erroneous types. Reviewed-by: darcy
This commit is contained in:
parent
a76d1ab5a2
commit
8a2542c71f
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -26,11 +26,8 @@
|
|||||||
package com.sun.tools.javac.processing;
|
package com.sun.tools.javac.processing;
|
||||||
|
|
||||||
import java.lang.annotation.Annotation;
|
import java.lang.annotation.Annotation;
|
||||||
import com.sun.tools.javac.tree.JCTree.*;
|
|
||||||
import javax.annotation.processing.*;
|
import javax.annotation.processing.*;
|
||||||
import javax.lang.model.element.*;
|
import javax.lang.model.element.*;
|
||||||
import javax.lang.model.type.DeclaredType;
|
|
||||||
import javax.lang.model.type.TypeMirror;
|
|
||||||
import javax.lang.model.util.*;
|
import javax.lang.model.util.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
@ -114,58 +111,48 @@ public class JavacRoundEnvironment implements RoundEnvironment {
|
|||||||
*/
|
*/
|
||||||
public Set<? extends Element> getElementsAnnotatedWith(TypeElement a) {
|
public Set<? extends Element> getElementsAnnotatedWith(TypeElement a) {
|
||||||
Set<Element> result = Collections.emptySet();
|
Set<Element> result = Collections.emptySet();
|
||||||
Types typeUtil = processingEnv.getTypeUtils();
|
|
||||||
if (a.getKind() != ElementKind.ANNOTATION_TYPE)
|
if (a.getKind() != ElementKind.ANNOTATION_TYPE)
|
||||||
throw new IllegalArgumentException(NOT_AN_ANNOTATION_TYPE + a);
|
throw new IllegalArgumentException(NOT_AN_ANNOTATION_TYPE + a);
|
||||||
|
|
||||||
DeclaredType annotationTypeElement;
|
ElementScanner8<Set<Element>, TypeElement> scanner =
|
||||||
TypeMirror tm = a.asType();
|
new AnnotationSetScanner(result);
|
||||||
if ( tm instanceof DeclaredType )
|
|
||||||
annotationTypeElement = (DeclaredType) a.asType();
|
|
||||||
else
|
|
||||||
throw new AssertionError("Bad implementation type for " + tm);
|
|
||||||
|
|
||||||
ElementScanner8<Set<Element>, DeclaredType> scanner =
|
|
||||||
new AnnotationSetScanner(result, typeUtil);
|
|
||||||
|
|
||||||
for (Element element : rootElements)
|
for (Element element : rootElements)
|
||||||
result = scanner.scan(element, annotationTypeElement);
|
result = scanner.scan(element, a);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Could be written as a local class inside getElementsAnnotatedWith
|
// Could be written as a local class inside getElementsAnnotatedWith
|
||||||
private class AnnotationSetScanner extends
|
private class AnnotationSetScanner extends
|
||||||
ElementScanner8<Set<Element>, DeclaredType> {
|
ElementScanner8<Set<Element>, TypeElement> {
|
||||||
// Insertion-order preserving set
|
// Insertion-order preserving set
|
||||||
Set<Element> annotatedElements = new LinkedHashSet<>();
|
Set<Element> annotatedElements = new LinkedHashSet<>();
|
||||||
Types typeUtil;
|
|
||||||
|
|
||||||
AnnotationSetScanner(Set<Element> defaultSet, Types typeUtil) {
|
AnnotationSetScanner(Set<Element> defaultSet) {
|
||||||
super(defaultSet);
|
super(defaultSet);
|
||||||
this.typeUtil = typeUtil;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<Element> visitType(TypeElement e, DeclaredType p) {
|
public Set<Element> visitType(TypeElement e, TypeElement p) {
|
||||||
// Type parameters are not considered to be enclosed by a type
|
// Type parameters are not considered to be enclosed by a type
|
||||||
scan(e.getTypeParameters(), p);
|
scan(e.getTypeParameters(), p);
|
||||||
return scan(e.getEnclosedElements(), p);
|
return scan(e.getEnclosedElements(), p);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<Element> visitExecutable(ExecutableElement e, DeclaredType p) {
|
public Set<Element> visitExecutable(ExecutableElement e, TypeElement p) {
|
||||||
// Type parameters are not considered to be enclosed by an executable
|
// Type parameters are not considered to be enclosed by an executable
|
||||||
scan(e.getTypeParameters(), p);
|
scan(e.getTypeParameters(), p);
|
||||||
return scan(e.getEnclosedElements(), p);
|
return scan(e.getEnclosedElements(), p);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<Element> scan(Element e, DeclaredType p) {
|
public Set<Element> scan(Element e, TypeElement p) {
|
||||||
java.util.List<? extends AnnotationMirror> annotationMirrors =
|
java.util.List<? extends AnnotationMirror> annotationMirrors =
|
||||||
processingEnv.getElementUtils().getAllAnnotationMirrors(e);
|
processingEnv.getElementUtils().getAllAnnotationMirrors(e);
|
||||||
for (AnnotationMirror annotationMirror : annotationMirrors) {
|
for (AnnotationMirror annotationMirror : annotationMirrors) {
|
||||||
if (typeUtil.isSameType(annotationMirror.getAnnotationType(), p))
|
if (p.equals(annotationMirror.getAnnotationType().asElement()))
|
||||||
annotatedElements.add(e);
|
annotatedElements.add(e);
|
||||||
}
|
}
|
||||||
e.accept(this, p);
|
e.accept(this, p);
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2006, 2007, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -22,7 +22,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class to hold annotations for ElementsAnnotatedWithTest.
|
* Class to hold annotations for TestElementsAnnotatedWith.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@AnnotatedElementInfo(annotationName="java.lang.SuppressWarnings",
|
@AnnotatedElementInfo(annotationName="java.lang.SuppressWarnings",
|
||||||
|
@ -0,0 +1,12 @@
|
|||||||
|
/** /nodynamiccopyright/
|
||||||
|
* Class to hold annotations for TestElementsAnnotatedWith.
|
||||||
|
*/
|
||||||
|
|
||||||
|
@AnnotatedElementInfo(annotationName="java.lang.SuppressWarnings",
|
||||||
|
expectedSize=0,
|
||||||
|
names={})
|
||||||
|
@Undefined
|
||||||
|
public class ErroneousAnnotations {
|
||||||
|
@Undefined
|
||||||
|
private void foo() {return;}
|
||||||
|
}
|
@ -0,0 +1,4 @@
|
|||||||
|
ErroneousAnnotations.java:8:2: compiler.err.cant.resolve: kindname.class, Undefined, ,
|
||||||
|
ErroneousAnnotations.java:10:6: compiler.err.cant.resolve.location: kindname.class, Undefined, , , (compiler.misc.location: kindname.class, ErroneousAnnotations, null)
|
||||||
|
2 errors
|
||||||
|
Results: []
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -22,7 +22,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class to hold annotations for ElementsAnnotatedWithTest.
|
* Class to hold annotations for TestElementsAnnotatedWith.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@AnnotatedElementInfo(annotationName="java.lang.SuppressWarnings",
|
@AnnotatedElementInfo(annotationName="java.lang.SuppressWarnings",
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -22,7 +22,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class to hold annotations for ElementsAnnotatedWithTest.
|
* Class to hold annotations for TestElementsAnnotatedWith.
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("")
|
@SuppressWarnings("")
|
||||||
public class Part2 {
|
public class Part2 {
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -22,7 +22,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class to hold annotations for ElementsAnnotatedWithTest.
|
* Class to hold annotations for TestElementsAnnotatedWith.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@AnnotatedElementInfo(annotationName="java.lang.SuppressWarnings",
|
@AnnotatedElementInfo(annotationName="java.lang.SuppressWarnings",
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -23,7 +23,7 @@
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
* @test
|
* @test
|
||||||
* @bug 6397298 6400986 6425592 6449798 6453386 6508401 6498938 6911854
|
* @bug 6397298 6400986 6425592 6449798 6453386 6508401 6498938 6911854 8030049
|
||||||
* @summary Tests that getElementsAnnotatedWith works properly.
|
* @summary Tests that getElementsAnnotatedWith works properly.
|
||||||
* @author Joseph D. Darcy
|
* @author Joseph D. Darcy
|
||||||
* @library /tools/javac/lib
|
* @library /tools/javac/lib
|
||||||
@ -37,23 +37,18 @@
|
|||||||
* @compile -processor TestElementsAnnotatedWith -proc:only C2.java
|
* @compile -processor TestElementsAnnotatedWith -proc:only C2.java
|
||||||
* @compile -processor TestElementsAnnotatedWith -proc:only Foo.java
|
* @compile -processor TestElementsAnnotatedWith -proc:only Foo.java
|
||||||
* @compile -processor TestElementsAnnotatedWith -proc:only TypeParameterAnnotations.java
|
* @compile -processor TestElementsAnnotatedWith -proc:only TypeParameterAnnotations.java
|
||||||
|
* @compile/fail/ref=ErroneousAnnotations.out -processor TestElementsAnnotatedWith -proc:only -XDrawDiagnostics ErroneousAnnotations.java
|
||||||
* @compile Foo.java
|
* @compile Foo.java
|
||||||
* @compile/process -processor TestElementsAnnotatedWith -proc:only Foo
|
* @compile/process -processor TestElementsAnnotatedWith -proc:only Foo
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import java.lang.annotation.Annotation;
|
import java.lang.annotation.Annotation;
|
||||||
import java.io.*;
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import javax.annotation.processing.*;
|
import javax.annotation.processing.*;
|
||||||
import javax.tools.*;
|
|
||||||
import javax.lang.model.SourceVersion;
|
|
||||||
import javax.lang.model.element.*;
|
import javax.lang.model.element.*;
|
||||||
import javax.lang.model.util.*;
|
|
||||||
import static javax.lang.model.util.ElementFilter.*;
|
import static javax.lang.model.util.ElementFilter.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -22,7 +22,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class to hold annotations for ElementsAnnotatedWithTest.
|
* Class to hold annotations for TestElementsAnnotatedWith.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@AnnotatedElementInfo(annotationName="TpAnno",
|
@AnnotatedElementInfo(annotationName="TpAnno",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user