test/tools/javac/processing/model/element/TestNonInherited.java

Tue, 22 Oct 2013 03:36:44 +0200

author
jfranck
date
Tue, 22 Oct 2013 03:36:44 +0200
changeset 2154
ac839d6f4953
parent 0
959103a6100f
permissions
-rw-r--r--

8026855: AnnoConstruct.getAnnotationsByType includes inherited indirectly present annotations even when containee type is not inheritable
Summary: In AnnoConstruct.getAnnotationByType() check that the annotation sought after is inherited before looking on supertypes.
Reviewed-by: jjg

     1 /*
     2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  */
    24 /*
    25  * @test
    26  * @bug 8026855
    27  * @summary Javac should only look on supertypes for repeatable annotations if
    28  *          both container and containee are inherited.
    29  * @library /tools/javac/lib
    30  * @build   JavacTestingAbstractProcessor TestNonInherited
    31  * @compile -processor TestNonInherited -proc:only TestNonInherited.java
    32  */
    34 import com.sun.tools.javac.util.Assert;
    36 import java.lang.annotation.*;
    37 import java.util.Arrays;
    38 import java.util.Set;
    39 import javax.annotation.processing.*;
    40 import javax.lang.model.element.*;
    41 import javax.lang.model.util.*;
    43 import static javax.lang.model.util.ElementFilter.*;
    45 @TestNonInherited.Foo(1)
    46 public class TestNonInherited extends JavacTestingAbstractProcessor {
    47     public boolean process(Set<? extends TypeElement> annotations,
    48                            RoundEnvironment roundEnv) {
    49         if (!roundEnv.processingOver()) {
    50             boolean hasRun = false;
    51             for (Element element : roundEnv.getRootElements())
    52                 for (TypeElement te : typesIn(element.getEnclosedElements()))
    53                     if (te.getQualifiedName().contentEquals("TestNonInherited.T2")) {
    54                         hasRun = true;
    55                         Foo[] foos = te.getAnnotationsByType(Foo.class);
    56                         System.out.println("  " + te);
    57                         System.out.println("  " + Arrays.asList(foos));
    58                         Assert.check(foos.length == 0, "Should not find any instance of @Foo");
    59                     }
    60             if (!hasRun)
    61                 throw new RuntimeException("The annotation processor could not find the declaration of T2, test broken!");
    62         }
    63         return true;
    64     }
    66     public static class T2 extends TestNonInherited {
    67     }
    69     @Repeatable(FooContainer.class)
    70     public static @interface Foo {
    71         int value();
    72     }
    74     @Inherited
    75     public static @interface FooContainer {
    76         Foo[] value();
    77     }
    78 }

mercurial