1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/annotations/typeAnnotations/newlocations/AfterMethodTypeParams.java Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,138 @@ 1.4 +/* 1.5 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +/** 1.28 + * @test 1.29 + * @bug 8038788 1.30 + * @summary Verify proper handling of annotations after method's type parameters. 1.31 + * @build AfterMethodTypeParams 1.32 + * @run main AfterMethodTypeParams 1.33 + */ 1.34 + 1.35 +import java.io.IOException; 1.36 +import java.io.StringWriter; 1.37 +import java.net.URI; 1.38 +import java.util.*; 1.39 + 1.40 +import javax.lang.model.element.Name; 1.41 +import javax.tools.*; 1.42 + 1.43 +import com.sun.source.tree.*; 1.44 +import com.sun.source.util.*; 1.45 + 1.46 +public class AfterMethodTypeParams { 1.47 + 1.48 + public static void main(String... args) throws IOException { 1.49 + new AfterMethodTypeParams().run(); 1.50 + } 1.51 + 1.52 + void run() throws IOException { 1.53 + JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); 1.54 + 1.55 + for (TestCase tc : testCases) { 1.56 + String test = TEMPLATE.replace("CONTENT", tc.snippet); 1.57 + List<JavaFileObject> files = Arrays.asList(new MyFileObject(test)); 1.58 + StringWriter out = new StringWriter(); 1.59 + List<String> options = Arrays.asList("-XDrawDiagnostics", "-XDshouldStopPolicy=FLOW"); 1.60 + JavacTask task = (JavacTask) compiler.getTask(out, null, null, options, null, files); 1.61 + 1.62 + new TreePathScanner<Void, Void>() { 1.63 + boolean seenAnnotation; 1.64 + @Override 1.65 + public Void visitAnnotation(AnnotationTree node, Void p) { 1.66 + Name name = ((IdentifierTree) node.getAnnotationType()).getName(); 1.67 + seenAnnotation |= name.contentEquals("TA") || name.contentEquals("DA"); 1.68 + return null; 1.69 + } 1.70 + @Override 1.71 + public Void visitCompilationUnit(CompilationUnitTree node, Void p) { 1.72 + super.visitCompilationUnit(node, p); 1.73 + if (!seenAnnotation) 1.74 + error(test, "Annotation was missing"); 1.75 + return null; 1.76 + } 1.77 + }.scan(task.parse(), null); 1.78 + 1.79 + task.analyze(); 1.80 + 1.81 + if (!tc.error.equals(out.toString().trim())) { 1.82 + error(test, "Incorrect errors: " + out.toString()); 1.83 + } 1.84 + } 1.85 + 1.86 + if (errors > 0) { 1.87 + throw new IllegalStateException("Errors found"); 1.88 + } 1.89 + } 1.90 + 1.91 + int errors; 1.92 + 1.93 + void error(String code, String error) { 1.94 + System.out.println("Error detected: " + error); 1.95 + System.out.println("Code:"); 1.96 + System.out.println(code); 1.97 + errors++; 1.98 + } 1.99 + 1.100 + static String TEMPLATE = 1.101 + "import java.lang.annotation.*;\n" + 1.102 + "public class Test {\n" + 1.103 + " CONTENT\n" + 1.104 + "}\n" + 1.105 + "@Target({ElementType.METHOD, ElementType.CONSTRUCTOR})\n" + 1.106 + "@interface DA { }\n" + 1.107 + "@Target(ElementType.TYPE_USE)\n" + 1.108 + "@interface TA { }\n"; 1.109 + 1.110 + static class MyFileObject extends SimpleJavaFileObject { 1.111 + final String text; 1.112 + public MyFileObject(String text) { 1.113 + super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); 1.114 + this.text = text; 1.115 + } 1.116 + @Override 1.117 + public CharSequence getCharContent(boolean ignoreEncodingErrors) { 1.118 + return text; 1.119 + } 1.120 + } 1.121 + 1.122 + static TestCase[] testCases = new TestCase[] { 1.123 + new TestCase("<T> @DA int foo1() { return 0;}", ""), 1.124 + new TestCase("<T> @DA void foo2() { }", ""), 1.125 + new TestCase("<T> @TA int foo3() { return 0;}", ""), 1.126 + new TestCase("<T> @TA void foo4() { }", 1.127 + "Test.java:3:9: compiler.err.annotation.type.not.applicable"), 1.128 + new TestCase("<T> @DA Test() { }", "Test.java:3:9: compiler.err.illegal.start.of.type"), 1.129 + new TestCase("<T> @TA Test() { }", "Test.java:3:9: compiler.err.illegal.start.of.type"), 1.130 + }; 1.131 + 1.132 + static class TestCase { 1.133 + final String snippet; 1.134 + final String error; 1.135 + public TestCase(String snippet, String error) { 1.136 + this.snippet = snippet; 1.137 + this.error = error; 1.138 + } 1.139 + } 1.140 +} 1.141 +