# HG changeset patch # User jlahoda # Date 1389376974 -3600 # Node ID 5ad8f004239f1c77ea645fd2a15813b5eccc177a # Parent afb6642d0603dde32074363e48e21d1bac76e23a 8030049: RoundEnvironment.getElementsAnnotatedWith receives wrong elements Summary: Match the required and actual annotations using Element equivalence rather than TypeMirror equivalence, to avoid trouble with erroneous types. Reviewed-by: darcy diff -r afb6642d0603 -r 5ad8f004239f src/share/classes/com/sun/tools/javac/processing/JavacRoundEnvironment.java --- a/src/share/classes/com/sun/tools/javac/processing/JavacRoundEnvironment.java Mon Jan 27 21:15:39 2014 +0000 +++ b/src/share/classes/com/sun/tools/javac/processing/JavacRoundEnvironment.java Fri Jan 10 19:02:54 2014 +0100 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2013, 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 Set getElementsAnnotatedWith(TypeElement a) { Set 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, DeclaredType> scanner = - new AnnotationSetScanner(result, typeUtil); + ElementScanner8, 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, DeclaredType> { + ElementScanner8, TypeElement> { // Insertion-order preserving set Set annotatedElements = new LinkedHashSet(); - Types typeUtil; - AnnotationSetScanner(Set defaultSet, Types typeUtil) { + AnnotationSetScanner(Set defaultSet) { super(defaultSet); - this.typeUtil = typeUtil; } @Override - public Set visitType(TypeElement e, DeclaredType p) { + public Set 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 visitExecutable(ExecutableElement e, DeclaredType p) { + public Set 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 scan(Element e, DeclaredType p) { + public Set scan(Element e, TypeElement p) { java.util.List 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); diff -r afb6642d0603 -r 5ad8f004239f test/tools/javac/processing/environment/round/BuriedAnnotations.java --- a/test/tools/javac/processing/environment/round/BuriedAnnotations.java Mon Jan 27 21:15:39 2014 +0000 +++ b/test/tools/javac/processing/environment/round/BuriedAnnotations.java Fri Jan 10 19:02:54 2014 +0100 @@ -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", diff -r afb6642d0603 -r 5ad8f004239f test/tools/javac/processing/environment/round/ErroneousAnnotations.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/tools/javac/processing/environment/round/ErroneousAnnotations.java Fri Jan 10 19:02:54 2014 +0100 @@ -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;} +} diff -r afb6642d0603 -r 5ad8f004239f test/tools/javac/processing/environment/round/ErroneousAnnotations.out --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/tools/javac/processing/environment/round/ErroneousAnnotations.out Fri Jan 10 19:02:54 2014 +0100 @@ -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: [] diff -r afb6642d0603 -r 5ad8f004239f test/tools/javac/processing/environment/round/Part1.java --- a/test/tools/javac/processing/environment/round/Part1.java Mon Jan 27 21:15:39 2014 +0000 +++ b/test/tools/javac/processing/environment/round/Part1.java Fri Jan 10 19:02:54 2014 +0100 @@ -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", diff -r afb6642d0603 -r 5ad8f004239f test/tools/javac/processing/environment/round/Part2.java --- a/test/tools/javac/processing/environment/round/Part2.java Mon Jan 27 21:15:39 2014 +0000 +++ b/test/tools/javac/processing/environment/round/Part2.java Fri Jan 10 19:02:54 2014 +0100 @@ -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 { diff -r afb6642d0603 -r 5ad8f004239f test/tools/javac/processing/environment/round/SurfaceAnnotations.java --- a/test/tools/javac/processing/environment/round/SurfaceAnnotations.java Mon Jan 27 21:15:39 2014 +0000 +++ b/test/tools/javac/processing/environment/round/SurfaceAnnotations.java Fri Jan 10 19:02:54 2014 +0100 @@ -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", diff -r afb6642d0603 -r 5ad8f004239f test/tools/javac/processing/environment/round/TestElementsAnnotatedWith.java --- a/test/tools/javac/processing/environment/round/TestElementsAnnotatedWith.java Mon Jan 27 21:15:39 2014 +0000 +++ b/test/tools/javac/processing/environment/round/TestElementsAnnotatedWith.java Fri Jan 10 19:02:54 2014 +0100 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006, 2013, 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.*; /** diff -r afb6642d0603 -r 5ad8f004239f test/tools/javac/processing/environment/round/TypeParameterAnnotations.java --- a/test/tools/javac/processing/environment/round/TypeParameterAnnotations.java Mon Jan 27 21:15:39 2014 +0000 +++ b/test/tools/javac/processing/environment/round/TypeParameterAnnotations.java Fri Jan 10 19:02:54 2014 +0100 @@ -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",