aoqi@0: /* aoqi@0: * Copyright (c) 2013, 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 8017216 8019422 8019421 aoqi@0: * @summary verify start and end positions aoqi@0: * @run main TreeEndPosTest aoqi@0: */ aoqi@0: aoqi@0: import java.io.ByteArrayOutputStream; aoqi@0: import java.io.File; aoqi@0: import java.io.IOException; aoqi@0: import java.io.PrintWriter; aoqi@0: import java.net.URI; aoqi@0: import java.util.ArrayList; aoqi@0: import java.util.List; aoqi@0: import javax.tools.Diagnostic; aoqi@0: import javax.tools.DiagnosticCollector; aoqi@0: import javax.tools.JavaCompiler; aoqi@0: import javax.tools.JavaFileManager; aoqi@0: import javax.tools.JavaFileObject; aoqi@0: import javax.tools.SimpleJavaFileObject; aoqi@0: import javax.tools.ToolProvider; aoqi@0: aoqi@0: public class TreeEndPosTest { aoqi@0: private static JavaFileManager getJavaFileManager(JavaCompiler compiler, aoqi@0: DiagnosticCollector dc) { aoqi@0: return compiler.getStandardFileManager(dc, null, null); aoqi@0: } aoqi@0: aoqi@0: static class JavaSource extends SimpleJavaFileObject { aoqi@0: aoqi@0: final String source; aoqi@0: int startPos; aoqi@0: int endPos; aoqi@0: aoqi@0: private JavaSource(String filename, String source) { aoqi@0: super(URI.create("myfo:/" + filename), JavaFileObject.Kind.SOURCE); aoqi@0: this.source = source; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public CharSequence getCharContent(boolean ignoreEncodingErrors) { aoqi@0: return source; aoqi@0: } aoqi@0: aoqi@0: static JavaSource createJavaSource(String preamble, String body, aoqi@0: String postamble, String expected) { aoqi@0: JavaSource js = createJavaSource(preamble, body, postamble, -1, -1); aoqi@0: js.startPos = js.source.indexOf(expected); aoqi@0: js.endPos = js.startPos + expected.length(); aoqi@0: return js; aoqi@0: } aoqi@0: aoqi@0: static JavaSource createJavaSource(String body, String expected) { aoqi@0: return createJavaSource(null, body, null, expected); aoqi@0: } aoqi@0: aoqi@0: private static JavaSource createJavaSource(String preamble, String body, aoqi@0: String postamble, int start, int end) { aoqi@0: final String name = "Bug"; aoqi@0: StringBuilder code = new StringBuilder(); aoqi@0: if (preamble != null) { aoqi@0: code.append(preamble); aoqi@0: } aoqi@0: code.append("public class " + name + "{"); aoqi@0: if (body != null) { aoqi@0: code.append(body); aoqi@0: } aoqi@0: code.append("}"); aoqi@0: if (postamble != null) { aoqi@0: code.append(postamble); aoqi@0: } aoqi@0: JavaSource js = new JavaSource(name + ".java", code.toString()); aoqi@0: js.startPos = start; aoqi@0: js.endPos = end; aoqi@0: return js; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public static void main(String... args) throws IOException { aoqi@0: testUninitializedVariable(); aoqi@0: testMissingAnnotationValue(); aoqi@0: testFinalVariableWithDefaultConstructor(); aoqi@0: testFinalVariableWithConstructor(); aoqi@0: } aoqi@0: aoqi@0: static void testUninitializedVariable() throws IOException { aoqi@0: compile(JavaSource.createJavaSource("Object o = new A().new B(); class A { }", aoqi@0: "B()")); aoqi@0: } aoqi@0: static void testMissingAnnotationValue() throws IOException { aoqi@0: compile(JavaSource.createJavaSource("@Foo(\"vvvv\")", aoqi@0: null, "@interface Foo { }", "\"vvvv\"")); aoqi@0: } aoqi@0: aoqi@0: static void testFinalVariableWithDefaultConstructor() throws IOException { aoqi@0: compile(JavaSource.createJavaSource("private static final String Foo; public void bar() { }", aoqi@0: "private static final String Foo;")); aoqi@0: } aoqi@0: aoqi@0: static void testFinalVariableWithConstructor() throws IOException { aoqi@0: compile(JavaSource.createJavaSource("public Bug (){} private static final String Foo; public void bar() { }", aoqi@0: "{}")); aoqi@0: } aoqi@0: aoqi@0: static void compile(JavaSource src) throws IOException { aoqi@0: ByteArrayOutputStream ba = new ByteArrayOutputStream(); aoqi@0: PrintWriter writer = new PrintWriter(ba); aoqi@0: File tempDir = new File("."); aoqi@0: JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); aoqi@0: DiagnosticCollector dc = new DiagnosticCollector(); aoqi@0: JavaFileManager javaFileManager = getJavaFileManager(compiler, dc); aoqi@0: List options = new ArrayList<>(); aoqi@0: options.add("-cp"); aoqi@0: options.add(tempDir.getPath()); aoqi@0: options.add("-d"); aoqi@0: options.add(tempDir.getPath()); aoqi@0: options.add("-XDshouldStopPolicy=GENERATE"); aoqi@0: aoqi@0: List sources = new ArrayList<>(); aoqi@0: sources.add(src); aoqi@0: JavaCompiler.CompilationTask task = aoqi@0: compiler.getTask(writer, javaFileManager, aoqi@0: dc, options, null, aoqi@0: sources); aoqi@0: task.call(); aoqi@0: for (Diagnostic diagnostic : (List) dc.getDiagnostics()) { aoqi@0: long actualStart = diagnostic.getStartPosition(); aoqi@0: long actualEnd = diagnostic.getEndPosition(); aoqi@0: System.out.println("Source: " + src.source); aoqi@0: System.out.println("Diagnostic: " + diagnostic); aoqi@0: System.out.print("Start position: Expected: " + src.startPos); aoqi@0: System.out.println(", Actual: " + actualStart); aoqi@0: System.out.print("End position: Expected: " + src.endPos); aoqi@0: System.out.println(", Actual: " + actualEnd); aoqi@0: if (src.startPos != actualStart || src.endPos != actualEnd) { aoqi@0: throw new RuntimeException("error: trees don't match"); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: }