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.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
@ -26,11 +26,8 @@
|
||||
package com.sun.tools.javac.processing;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import com.sun.tools.javac.tree.JCTree.*;
|
||||
import javax.annotation.processing.*;
|
||||
import javax.lang.model.element.*;
|
||||
import javax.lang.model.type.DeclaredType;
|
||||
import javax.lang.model.type.TypeMirror;
|
||||
import javax.lang.model.util.*;
|
||||
import java.util.*;
|
||||
|
||||
@ -114,58 +111,48 @@ public class JavacRoundEnvironment implements RoundEnvironment {
|
||||
*/
|
||||
public Set<? extends Element> getElementsAnnotatedWith(TypeElement a) {
|
||||
Set<Element> result = Collections.emptySet();
|
||||
Types typeUtil = processingEnv.getTypeUtils();
|
||||
if (a.getKind() != ElementKind.ANNOTATION_TYPE)
|
||||
throw new IllegalArgumentException(NOT_AN_ANNOTATION_TYPE + a);
|
||||
|
||||
DeclaredType annotationTypeElement;
|
||||
TypeMirror tm = a.asType();
|
||||
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);
|
||||
ElementScanner8<Set<Element>, TypeElement> scanner =
|
||||
new AnnotationSetScanner(result);
|
||||
|
||||
for (Element element : rootElements)
|
||||
result = scanner.scan(element, annotationTypeElement);
|
||||
result = scanner.scan(element, a);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// Could be written as a local class inside getElementsAnnotatedWith
|
||||
private class AnnotationSetScanner extends
|
||||
ElementScanner8<Set<Element>, DeclaredType> {
|
||||
ElementScanner8<Set<Element>, TypeElement> {
|
||||
// Insertion-order preserving set
|
||||
Set<Element> annotatedElements = new LinkedHashSet<>();
|
||||
Types typeUtil;
|
||||
|
||||
AnnotationSetScanner(Set<Element> defaultSet, Types typeUtil) {
|
||||
AnnotationSetScanner(Set<Element> defaultSet) {
|
||||
super(defaultSet);
|
||||
this.typeUtil = typeUtil;
|
||||
}
|
||||
|
||||
@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
|
||||
scan(e.getTypeParameters(), p);
|
||||
return scan(e.getEnclosedElements(), p);
|
||||
}
|
||||
|
||||
@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
|
||||
scan(e.getTypeParameters(), p);
|
||||
return scan(e.getEnclosedElements(), p);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Element> scan(Element e, DeclaredType p) {
|
||||
public Set<Element> scan(Element e, TypeElement p) {
|
||||
java.util.List<? extends AnnotationMirror> annotationMirrors =
|
||||
processingEnv.getElementUtils().getAllAnnotationMirrors(e);
|
||||
for (AnnotationMirror annotationMirror : annotationMirrors) {
|
||||
if (typeUtil.isSameType(annotationMirror.getAnnotationType(), p))
|
||||
if (p.equals(annotationMirror.getAnnotationType().asElement()))
|
||||
annotatedElements.add(e);
|
||||
}
|
||||
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.
|
||||
*
|
||||
* 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",
|
||||
|
@ -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.
|
||||
*
|
||||
* 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",
|
||||
|
@ -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.
|
||||
*
|
||||
* 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("")
|
||||
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.
|
||||
*
|
||||
* 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",
|
||||
|
@ -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.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
@ -23,7 +23,7 @@
|
||||
|
||||
/*
|
||||
* @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.
|
||||
* @author Joseph D. Darcy
|
||||
* @library /tools/javac/lib
|
||||
@ -37,23 +37,18 @@
|
||||
* @compile -processor TestElementsAnnotatedWith -proc:only C2.java
|
||||
* @compile -processor TestElementsAnnotatedWith -proc:only Foo.java
|
||||
* @compile -processor TestElementsAnnotatedWith -proc:only TypeParameterAnnotations.java
|
||||
* @compile/fail/ref=ErroneousAnnotations.out -processor TestElementsAnnotatedWith -proc:only -XDrawDiagnostics ErroneousAnnotations.java
|
||||
* @compile Foo.java
|
||||
* @compile/process -processor TestElementsAnnotatedWith -proc:only Foo
|
||||
*/
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.io.*;
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import javax.annotation.processing.*;
|
||||
import javax.tools.*;
|
||||
import javax.lang.model.SourceVersion;
|
||||
import javax.lang.model.element.*;
|
||||
import javax.lang.model.util.*;
|
||||
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.
|
||||
*
|
||||
* 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",
|
||||
|
Loading…
x
Reference in New Issue
Block a user