1/*
2 * Copyright (c) 2010, 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 * @summary Checks that the interaction between annotated and unannotated
27  *         array levels
28 * @modules jdk.compiler/com.sun.tools.javac.api
29 *          jdk.compiler/com.sun.tools.javac.file
30 */
31
32import com.sun.tools.javac.api.JavacTool;
33import java.lang.annotation.*;
34import java.io.File;
35import java.io.PrintWriter;
36import java.util.Arrays;
37import java.util.List;
38import java.util.Map;
39import java.util.HashMap;
40import javax.tools.JavaFileManager;
41import javax.tools.JavaFileObject;
42import com.sun.source.tree.*;
43import com.sun.source.util.JavacTask;
44import com.sun.source.util.TreeScanner;
45import javax.tools.StandardJavaFileManager;
46
47
48public class ArrayPositionConsistency {
49    public static void main(String[] args) throws Exception {
50        PrintWriter out = new PrintWriter(System.out, true);
51        JavacTool tool = JavacTool.create();
52        try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) {
53            File testSrc = new File(System.getProperty("test.src"));
54            Iterable<? extends JavaFileObject> f =
55                fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrc, "ArrayPositionConsistency.java")));
56            JavacTask task = tool.getTask(out, fm, null, null, null, f);
57            Iterable<? extends CompilationUnitTree> trees = task.parse();
58            out.flush();
59
60            Scanner s = new Scanner();
61            for (CompilationUnitTree t: trees)
62                s.scan(t, null);
63        }
64    }
65
66    private static class Scanner extends TreeScanner<Void,Void> {
67        int foundAnnotations = 0;
68        public Void visitCompilationUnit(CompilationUnitTree node, Void ignore) {
69            super.visitCompilationUnit(node, ignore);
70            if (foundAnnotations != expectedAnnotations) {
71                throw new AssertionError("Expected " + expectedAnnotations +
72                        " annotations but found: " + foundAnnotations);
73            }
74            return null;
75        }
76
77        private void testAnnotations(List<? extends AnnotationTree> annos, int found) {
78            String annotation = annos.get(0).toString();
79            foundAnnotations++;
80
81            int expected = -1;
82            if (annotation.equals("@A()"))
83                expected = 0;
84            else if (annotation.equals("@B()"))
85                expected = 1;
86            else if (annotation.equals("@C()"))
87                expected = 2;
88            else
89                throw new AssertionError("found an unexpected annotation: " + annotation);
90            if (found != expected) {
91                throw new AssertionError("Unexpected found length" +
92                    ", found " + found + " but expected " + expected);
93            }
94        }
95
96        public Void visitAnnotatedType(AnnotatedTypeTree node, Void ignore) {
97            testAnnotations(node.getAnnotations(), arrayLength(node));
98            return super.visitAnnotatedType(node, ignore);
99        }
100
101        private int arrayLength(Tree tree) {
102            switch (tree.getKind()) {
103            case ARRAY_TYPE:
104                return 1 + arrayLength(((ArrayTypeTree)tree).getType());
105            case ANNOTATED_TYPE:
106                return arrayLength(((AnnotatedTypeTree)tree).getUnderlyingType());
107            default:
108                return 0;
109            }
110        }
111    }
112
113    static int expectedAnnotations = 23;
114
115    // visited code
116    @A String @C [] @B [] field1;
117    @A String @C []    [] field2;
118    @A String    [] @B [] field3;
119       String    [] @B [] field4;
120
121    @A List<String> @C [] @B [] genfield1;
122    @A List<String> @C []    [] genfield2;
123    @A List<String>    [] @B [] genfield3;
124       List<String>    [] @B [] genfield4;
125
126    List<@A String @C [] @B []> typearg1;
127    List<@A String @C []    []> typearg2;
128    List<@A String    [] @B []> typearg3;
129    List<   String    [] @B []> typearg4;
130
131    void vararg1(@A String @C [] @B ... arg) {}
132    void vararg2(@A String @C []    ... arg) {}
133    void vararg3(@A String    [] @B ... arg) {}
134    void vararg4(   String    [] @B ... arg) {}
135
136    @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})
137    @interface A {}
138    @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})
139    @interface B {}
140    @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})
141    @interface C {}
142
143}
144