|
1 /* |
|
2 * Copyright (c) 2014, 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 8038788 |
|
27 * @summary Verify proper handling of annotations after method's type parameters. |
|
28 * @build AfterMethodTypeParams |
|
29 * @run main AfterMethodTypeParams |
|
30 */ |
|
31 |
|
32 import java.io.IOException; |
|
33 import java.io.StringWriter; |
|
34 import java.net.URI; |
|
35 import java.util.*; |
|
36 |
|
37 import javax.lang.model.element.Name; |
|
38 import javax.tools.*; |
|
39 |
|
40 import com.sun.source.tree.*; |
|
41 import com.sun.source.util.*; |
|
42 |
|
43 public class AfterMethodTypeParams { |
|
44 |
|
45 public static void main(String... args) throws IOException { |
|
46 new AfterMethodTypeParams().run(); |
|
47 } |
|
48 |
|
49 void run() throws IOException { |
|
50 JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); |
|
51 |
|
52 for (TestCase tc : testCases) { |
|
53 String test = TEMPLATE.replace("CONTENT", tc.snippet); |
|
54 List<JavaFileObject> files = Arrays.asList(new MyFileObject(test)); |
|
55 StringWriter out = new StringWriter(); |
|
56 List<String> options = Arrays.asList("-XDrawDiagnostics", "-XDshouldStopPolicy=FLOW"); |
|
57 JavacTask task = (JavacTask) compiler.getTask(out, null, null, options, null, files); |
|
58 |
|
59 new TreePathScanner<Void, Void>() { |
|
60 boolean seenAnnotation; |
|
61 @Override |
|
62 public Void visitAnnotation(AnnotationTree node, Void p) { |
|
63 Name name = ((IdentifierTree) node.getAnnotationType()).getName(); |
|
64 seenAnnotation |= name.contentEquals("TA") || name.contentEquals("DA"); |
|
65 return null; |
|
66 } |
|
67 @Override |
|
68 public Void visitCompilationUnit(CompilationUnitTree node, Void p) { |
|
69 super.visitCompilationUnit(node, p); |
|
70 if (!seenAnnotation) |
|
71 error(test, "Annotation was missing"); |
|
72 return null; |
|
73 } |
|
74 }.scan(task.parse(), null); |
|
75 |
|
76 task.analyze(); |
|
77 |
|
78 if (!tc.error.equals(out.toString().trim())) { |
|
79 error(test, "Incorrect errors: " + out.toString()); |
|
80 } |
|
81 } |
|
82 |
|
83 if (errors > 0) { |
|
84 throw new IllegalStateException("Errors found"); |
|
85 } |
|
86 } |
|
87 |
|
88 int errors; |
|
89 |
|
90 void error(String code, String error) { |
|
91 System.out.println("Error detected: " + error); |
|
92 System.out.println("Code:"); |
|
93 System.out.println(code); |
|
94 errors++; |
|
95 } |
|
96 |
|
97 static String TEMPLATE = |
|
98 "import java.lang.annotation.*;\n" + |
|
99 "public class Test {\n" + |
|
100 " CONTENT\n" + |
|
101 "}\n" + |
|
102 "@Target({ElementType.METHOD, ElementType.CONSTRUCTOR})\n" + |
|
103 "@interface DA { }\n" + |
|
104 "@Target(ElementType.TYPE_USE)\n" + |
|
105 "@interface TA { }\n"; |
|
106 |
|
107 static class MyFileObject extends SimpleJavaFileObject { |
|
108 final String text; |
|
109 public MyFileObject(String text) { |
|
110 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); |
|
111 this.text = text; |
|
112 } |
|
113 @Override |
|
114 public CharSequence getCharContent(boolean ignoreEncodingErrors) { |
|
115 return text; |
|
116 } |
|
117 } |
|
118 |
|
119 static TestCase[] testCases = new TestCase[] { |
|
120 new TestCase("<T> @DA int foo1() { return 0;}", ""), |
|
121 new TestCase("<T> @DA void foo2() { }", ""), |
|
122 new TestCase("<T> @TA int foo3() { return 0;}", ""), |
|
123 new TestCase("<T> @TA void foo4() { }", |
|
124 "Test.java:3:9: compiler.err.annotation.type.not.applicable"), |
|
125 new TestCase("<T> @DA Test() { }", "Test.java:3:9: compiler.err.illegal.start.of.type"), |
|
126 new TestCase("<T> @TA Test() { }", "Test.java:3:9: compiler.err.illegal.start.of.type"), |
|
127 }; |
|
128 |
|
129 static class TestCase { |
|
130 final String snippet; |
|
131 final String error; |
|
132 public TestCase(String snippet, String error) { |
|
133 this.snippet = snippet; |
|
134 this.error = error; |
|
135 } |
|
136 } |
|
137 } |
|
138 |