1/*
2 * Copyright (c) 2013, 2015, 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 */
23
24/*
25 * @test
26 * @bug 8026857
27 * @summary Test that an empty container does not stop us from looking at
28 *          supertypes for inherited repeated annotations.
29 * @library /tools/javac/lib
30 * @modules jdk.compiler/com.sun.tools.javac.util
31 * @build   JavacTestingAbstractProcessor TestEmptyContainer
32 * @compile -XDaccessInternalAPI -processor TestEmptyContainer -proc:only TestEmptyContainer.java
33 */
34
35import com.sun.tools.javac.util.Assert;
36
37import java.lang.annotation.*;
38import java.util.Arrays;
39import java.util.Set;
40import javax.annotation.processing.*;
41import javax.lang.model.element.Element;
42import javax.lang.model.element.TypeElement;
43
44import static javax.lang.model.util.ElementFilter.*;
45
46@TestEmptyContainer.Foo(1)
47public class TestEmptyContainer extends JavacTestingAbstractProcessor {
48
49    public boolean process(Set<? extends TypeElement> annotations,
50                           RoundEnvironment roundEnv) {
51        if (!roundEnv.processingOver()) {
52            boolean hasRun = false;
53            for (Element element : roundEnv.getRootElements())
54                for (TypeElement te : typesIn(element.getEnclosedElements()))
55                    if (te.getQualifiedName().contentEquals("TestEmptyContainer.T2")) {
56                        hasRun = true;
57                        Foo[] foos = te.getAnnotationsByType(Foo.class);
58                        System.out.println("  " + te);
59                        System.out.println("  " + Arrays.asList(foos));
60                        Assert.check(foos.length == 1, "Should find one @Foo");
61                        Assert.check(foos[0].value() == 1, "Should find @Foo(1)");
62                    }
63            if (!hasRun)
64                throw new RuntimeException("Annotation processor couldn't find class T2, test broken!");
65        }
66        return true;
67    }
68
69    // This empty container should not stop us from finding @Foo(1) on TestEmptyContainer above
70    @TestEmptyContainer.FooContainer({})
71    public static class T2 extends TestEmptyContainer {
72    }
73
74    @Repeatable(FooContainer.class)
75    @Inherited
76    public static @interface Foo {
77        int value();
78    }
79
80    @Inherited
81    public static @interface FooContainer {
82        Foo[] value();
83    }
84}
85