Fri, 24 Apr 2020 03:58:51 +0100
Merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. |
aoqi@0 | 8 | * |
aoqi@0 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 13 | * accompanied this code). |
aoqi@0 | 14 | * |
aoqi@0 | 15 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 18 | * |
aoqi@0 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 20 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 21 | * questions. |
aoqi@0 | 22 | */ |
aoqi@0 | 23 | |
aoqi@0 | 24 | /* |
aoqi@0 | 25 | * @test |
aoqi@0 | 26 | * @bug 8017216 8019422 8019421 |
aoqi@0 | 27 | * @summary verify start and end positions |
aoqi@0 | 28 | * @run main TreeEndPosTest |
aoqi@0 | 29 | */ |
aoqi@0 | 30 | |
aoqi@0 | 31 | import java.io.ByteArrayOutputStream; |
aoqi@0 | 32 | import java.io.File; |
aoqi@0 | 33 | import java.io.IOException; |
aoqi@0 | 34 | import java.io.PrintWriter; |
aoqi@0 | 35 | import java.net.URI; |
aoqi@0 | 36 | import java.util.ArrayList; |
aoqi@0 | 37 | import java.util.List; |
aoqi@0 | 38 | import javax.tools.Diagnostic; |
aoqi@0 | 39 | import javax.tools.DiagnosticCollector; |
aoqi@0 | 40 | import javax.tools.JavaCompiler; |
aoqi@0 | 41 | import javax.tools.JavaFileManager; |
aoqi@0 | 42 | import javax.tools.JavaFileObject; |
aoqi@0 | 43 | import javax.tools.SimpleJavaFileObject; |
aoqi@0 | 44 | import javax.tools.ToolProvider; |
aoqi@0 | 45 | |
aoqi@0 | 46 | public class TreeEndPosTest { |
aoqi@0 | 47 | private static JavaFileManager getJavaFileManager(JavaCompiler compiler, |
aoqi@0 | 48 | DiagnosticCollector dc) { |
aoqi@0 | 49 | return compiler.getStandardFileManager(dc, null, null); |
aoqi@0 | 50 | } |
aoqi@0 | 51 | |
aoqi@0 | 52 | static class JavaSource extends SimpleJavaFileObject { |
aoqi@0 | 53 | |
aoqi@0 | 54 | final String source; |
aoqi@0 | 55 | int startPos; |
aoqi@0 | 56 | int endPos; |
aoqi@0 | 57 | |
aoqi@0 | 58 | private JavaSource(String filename, String source) { |
aoqi@0 | 59 | super(URI.create("myfo:/" + filename), JavaFileObject.Kind.SOURCE); |
aoqi@0 | 60 | this.source = source; |
aoqi@0 | 61 | } |
aoqi@0 | 62 | |
aoqi@0 | 63 | @Override |
aoqi@0 | 64 | public CharSequence getCharContent(boolean ignoreEncodingErrors) { |
aoqi@0 | 65 | return source; |
aoqi@0 | 66 | } |
aoqi@0 | 67 | |
aoqi@0 | 68 | static JavaSource createJavaSource(String preamble, String body, |
aoqi@0 | 69 | String postamble, String expected) { |
aoqi@0 | 70 | JavaSource js = createJavaSource(preamble, body, postamble, -1, -1); |
aoqi@0 | 71 | js.startPos = js.source.indexOf(expected); |
aoqi@0 | 72 | js.endPos = js.startPos + expected.length(); |
aoqi@0 | 73 | return js; |
aoqi@0 | 74 | } |
aoqi@0 | 75 | |
aoqi@0 | 76 | static JavaSource createJavaSource(String body, String expected) { |
aoqi@0 | 77 | return createJavaSource(null, body, null, expected); |
aoqi@0 | 78 | } |
aoqi@0 | 79 | |
aoqi@0 | 80 | private static JavaSource createJavaSource(String preamble, String body, |
aoqi@0 | 81 | String postamble, int start, int end) { |
aoqi@0 | 82 | final String name = "Bug"; |
aoqi@0 | 83 | StringBuilder code = new StringBuilder(); |
aoqi@0 | 84 | if (preamble != null) { |
aoqi@0 | 85 | code.append(preamble); |
aoqi@0 | 86 | } |
aoqi@0 | 87 | code.append("public class " + name + "{"); |
aoqi@0 | 88 | if (body != null) { |
aoqi@0 | 89 | code.append(body); |
aoqi@0 | 90 | } |
aoqi@0 | 91 | code.append("}"); |
aoqi@0 | 92 | if (postamble != null) { |
aoqi@0 | 93 | code.append(postamble); |
aoqi@0 | 94 | } |
aoqi@0 | 95 | JavaSource js = new JavaSource(name + ".java", code.toString()); |
aoqi@0 | 96 | js.startPos = start; |
aoqi@0 | 97 | js.endPos = end; |
aoqi@0 | 98 | return js; |
aoqi@0 | 99 | } |
aoqi@0 | 100 | } |
aoqi@0 | 101 | |
aoqi@0 | 102 | public static void main(String... args) throws IOException { |
aoqi@0 | 103 | testUninitializedVariable(); |
aoqi@0 | 104 | testMissingAnnotationValue(); |
aoqi@0 | 105 | testFinalVariableWithDefaultConstructor(); |
aoqi@0 | 106 | testFinalVariableWithConstructor(); |
aoqi@0 | 107 | } |
aoqi@0 | 108 | |
aoqi@0 | 109 | static void testUninitializedVariable() throws IOException { |
aoqi@0 | 110 | compile(JavaSource.createJavaSource("Object o = new A().new B(); class A { }", |
aoqi@0 | 111 | "B()")); |
aoqi@0 | 112 | } |
aoqi@0 | 113 | static void testMissingAnnotationValue() throws IOException { |
aoqi@0 | 114 | compile(JavaSource.createJavaSource("@Foo(\"vvvv\")", |
aoqi@0 | 115 | null, "@interface Foo { }", "\"vvvv\"")); |
aoqi@0 | 116 | } |
aoqi@0 | 117 | |
aoqi@0 | 118 | static void testFinalVariableWithDefaultConstructor() throws IOException { |
aoqi@0 | 119 | compile(JavaSource.createJavaSource("private static final String Foo; public void bar() { }", |
aoqi@0 | 120 | "private static final String Foo;")); |
aoqi@0 | 121 | } |
aoqi@0 | 122 | |
aoqi@0 | 123 | static void testFinalVariableWithConstructor() throws IOException { |
aoqi@0 | 124 | compile(JavaSource.createJavaSource("public Bug (){} private static final String Foo; public void bar() { }", |
aoqi@0 | 125 | "{}")); |
aoqi@0 | 126 | } |
aoqi@0 | 127 | |
aoqi@0 | 128 | static void compile(JavaSource src) throws IOException { |
aoqi@0 | 129 | ByteArrayOutputStream ba = new ByteArrayOutputStream(); |
aoqi@0 | 130 | PrintWriter writer = new PrintWriter(ba); |
aoqi@0 | 131 | File tempDir = new File("."); |
aoqi@0 | 132 | JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); |
aoqi@0 | 133 | DiagnosticCollector dc = new DiagnosticCollector(); |
aoqi@0 | 134 | JavaFileManager javaFileManager = getJavaFileManager(compiler, dc); |
aoqi@0 | 135 | List<String> options = new ArrayList<>(); |
aoqi@0 | 136 | options.add("-cp"); |
aoqi@0 | 137 | options.add(tempDir.getPath()); |
aoqi@0 | 138 | options.add("-d"); |
aoqi@0 | 139 | options.add(tempDir.getPath()); |
aoqi@0 | 140 | options.add("-XDshouldStopPolicy=GENERATE"); |
aoqi@0 | 141 | |
aoqi@0 | 142 | List<JavaFileObject> sources = new ArrayList<>(); |
aoqi@0 | 143 | sources.add(src); |
aoqi@0 | 144 | JavaCompiler.CompilationTask task = |
aoqi@0 | 145 | compiler.getTask(writer, javaFileManager, |
aoqi@0 | 146 | dc, options, null, |
aoqi@0 | 147 | sources); |
aoqi@0 | 148 | task.call(); |
aoqi@0 | 149 | for (Diagnostic diagnostic : (List<Diagnostic>) dc.getDiagnostics()) { |
aoqi@0 | 150 | long actualStart = diagnostic.getStartPosition(); |
aoqi@0 | 151 | long actualEnd = diagnostic.getEndPosition(); |
aoqi@0 | 152 | System.out.println("Source: " + src.source); |
aoqi@0 | 153 | System.out.println("Diagnostic: " + diagnostic); |
aoqi@0 | 154 | System.out.print("Start position: Expected: " + src.startPos); |
aoqi@0 | 155 | System.out.println(", Actual: " + actualStart); |
aoqi@0 | 156 | System.out.print("End position: Expected: " + src.endPos); |
aoqi@0 | 157 | System.out.println(", Actual: " + actualEnd); |
aoqi@0 | 158 | if (src.startPos != actualStart || src.endPos != actualEnd) { |
aoqi@0 | 159 | throw new RuntimeException("error: trees don't match"); |
aoqi@0 | 160 | } |
aoqi@0 | 161 | } |
aoqi@0 | 162 | } |
aoqi@0 | 163 | } |