aoqi@0: /* aoqi@0: * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /** aoqi@0: * @test aoqi@0: * @bug 8038788 aoqi@0: * @summary Verify proper handling of annotations after method's type parameters. aoqi@0: * @build AfterMethodTypeParams aoqi@0: * @run main AfterMethodTypeParams aoqi@0: */ aoqi@0: aoqi@0: import java.io.IOException; aoqi@0: import java.io.StringWriter; aoqi@0: import java.net.URI; aoqi@0: import java.util.*; aoqi@0: aoqi@0: import javax.lang.model.element.Name; aoqi@0: import javax.tools.*; aoqi@0: aoqi@0: import com.sun.source.tree.*; aoqi@0: import com.sun.source.util.*; aoqi@0: aoqi@0: public class AfterMethodTypeParams { aoqi@0: aoqi@0: public static void main(String... args) throws IOException { aoqi@0: new AfterMethodTypeParams().run(); aoqi@0: } aoqi@0: aoqi@0: void run() throws IOException { aoqi@0: JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); aoqi@0: aoqi@0: for (TestCase tc : testCases) { aoqi@0: String test = TEMPLATE.replace("CONTENT", tc.snippet); aoqi@0: List files = Arrays.asList(new MyFileObject(test)); aoqi@0: StringWriter out = new StringWriter(); aoqi@0: List options = Arrays.asList("-XDrawDiagnostics", "-XDshouldStopPolicy=FLOW"); aoqi@0: JavacTask task = (JavacTask) compiler.getTask(out, null, null, options, null, files); aoqi@0: aoqi@0: new TreePathScanner() { aoqi@0: boolean seenAnnotation; aoqi@0: @Override aoqi@0: public Void visitAnnotation(AnnotationTree node, Void p) { aoqi@0: Name name = ((IdentifierTree) node.getAnnotationType()).getName(); aoqi@0: seenAnnotation |= name.contentEquals("TA") || name.contentEquals("DA"); aoqi@0: return null; aoqi@0: } aoqi@0: @Override aoqi@0: public Void visitCompilationUnit(CompilationUnitTree node, Void p) { aoqi@0: super.visitCompilationUnit(node, p); aoqi@0: if (!seenAnnotation) aoqi@0: error(test, "Annotation was missing"); aoqi@0: return null; aoqi@0: } aoqi@0: }.scan(task.parse(), null); aoqi@0: aoqi@0: task.analyze(); aoqi@0: aoqi@0: if (!tc.error.equals(out.toString().trim())) { aoqi@0: error(test, "Incorrect errors: " + out.toString()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: if (errors > 0) { aoqi@0: throw new IllegalStateException("Errors found"); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: int errors; aoqi@0: aoqi@0: void error(String code, String error) { aoqi@0: System.out.println("Error detected: " + error); aoqi@0: System.out.println("Code:"); aoqi@0: System.out.println(code); aoqi@0: errors++; aoqi@0: } aoqi@0: aoqi@0: static String TEMPLATE = aoqi@0: "import java.lang.annotation.*;\n" + aoqi@0: "public class Test {\n" + aoqi@0: " CONTENT\n" + aoqi@0: "}\n" + aoqi@0: "@Target({ElementType.METHOD, ElementType.CONSTRUCTOR})\n" + aoqi@0: "@interface DA { }\n" + aoqi@0: "@Target(ElementType.TYPE_USE)\n" + aoqi@0: "@interface TA { }\n"; aoqi@0: aoqi@0: static class MyFileObject extends SimpleJavaFileObject { aoqi@0: final String text; aoqi@0: public MyFileObject(String text) { aoqi@0: super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); aoqi@0: this.text = text; aoqi@0: } aoqi@0: @Override aoqi@0: public CharSequence getCharContent(boolean ignoreEncodingErrors) { aoqi@0: return text; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: static TestCase[] testCases = new TestCase[] { aoqi@0: new TestCase(" @DA int foo1() { return 0;}", ""), aoqi@0: new TestCase(" @DA void foo2() { }", ""), aoqi@0: new TestCase(" @TA int foo3() { return 0;}", ""), aoqi@0: new TestCase(" @TA void foo4() { }", aoqi@0: "Test.java:3:9: compiler.err.annotation.type.not.applicable"), aoqi@0: new TestCase(" @DA Test() { }", "Test.java:3:9: compiler.err.illegal.start.of.type"), aoqi@0: new TestCase(" @TA Test() { }", "Test.java:3:9: compiler.err.illegal.start.of.type"), aoqi@0: }; aoqi@0: aoqi@0: static class TestCase { aoqi@0: final String snippet; aoqi@0: final String error; aoqi@0: public TestCase(String snippet, String error) { aoqi@0: this.snippet = snippet; aoqi@0: this.error = error; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: