|
1 /* |
|
2 * Copyright (c) 2018, Google Inc. 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 8190452 |
|
27 * @summary javac should not add MethodParameters attributes to v51 and earlier class files |
|
28 * @modules jdk.jdeps/com.sun.tools.classfile |
|
29 * @build LegacyOutputTest |
|
30 * @run main LegacyOutputTest |
|
31 */ |
|
32 |
|
33 import com.sun.tools.classfile.ClassFile; |
|
34 import com.sun.tools.classfile.Method; |
|
35 import com.sun.tools.classfile.MethodParameters_attribute; |
|
36 import com.sun.tools.classfile.MethodParameters_attribute.Entry; |
|
37 import java.io.IOException; |
|
38 import java.net.URI; |
|
39 import java.nio.file.Paths; |
|
40 import java.util.ArrayList; |
|
41 import java.util.Arrays; |
|
42 import java.util.List; |
|
43 import javax.tools.JavaCompiler; |
|
44 import javax.tools.JavaCompiler.CompilationTask; |
|
45 import javax.tools.JavaFileObject; |
|
46 import javax.tools.JavaFileObject.Kind; |
|
47 import javax.tools.SimpleJavaFileObject; |
|
48 import javax.tools.ToolProvider; |
|
49 |
|
50 /** |
|
51 * Post https://bugs.openjdk.java.net/browse/JDK-8190452, the test verifies that MethodParameters |
|
52 * attributes are not emitted when targeting --release < 8. |
|
53 */ |
|
54 public class LegacyOutputTest { |
|
55 public static void main(String[] args) throws Exception { |
|
56 new LegacyOutputTest().test(); |
|
57 } |
|
58 |
|
59 void test() throws Exception { |
|
60 release7(); |
|
61 release8(); |
|
62 } |
|
63 |
|
64 void release7() throws Exception { |
|
65 List<String> names = getParameterNames("7"); |
|
66 if (names != null) { |
|
67 throw new AssertionError( |
|
68 "expected no MethodParameters for --release 7, actual: " + names); |
|
69 } |
|
70 } |
|
71 |
|
72 void release8() throws Exception { |
|
73 List<String> names = getParameterNames("8"); |
|
74 List<String> expected = Arrays.asList("x", "y"); |
|
75 if (!names.equals(expected)) { |
|
76 throw new AssertionError( |
|
77 "incorrect parameter names, actual: " + names + ", expected: " + expected); |
|
78 } |
|
79 } |
|
80 |
|
81 List<String> getParameterNames(String release) throws Exception { |
|
82 JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); |
|
83 JavaFileObject fileObject = |
|
84 new SimpleJavaFileObject(URI.create("Test.java"), Kind.SOURCE) { |
|
85 @Override |
|
86 public CharSequence getCharContent(boolean ignoreEncodingErrors) |
|
87 throws IOException { |
|
88 return "class Test { void f(int x, int y) {} }"; |
|
89 } |
|
90 }; |
|
91 CompilationTask task = |
|
92 tool.getTask( |
|
93 null, |
|
94 null, |
|
95 null, |
|
96 Arrays.asList("--release", release, "-parameters"), |
|
97 null, |
|
98 Arrays.asList(fileObject)); |
|
99 if (!task.call()) { |
|
100 throw new AssertionError("compilation failed"); |
|
101 } |
|
102 ClassFile classFile = ClassFile.read(Paths.get("Test.class")); |
|
103 Method method = getMethod(classFile, "f"); |
|
104 MethodParameters_attribute attribute = |
|
105 (MethodParameters_attribute) method.attributes.get("MethodParameters"); |
|
106 if (attribute == null) { |
|
107 return null; |
|
108 } |
|
109 List<String> parameterNames = new ArrayList<>(); |
|
110 for (Entry e : attribute.method_parameter_table) { |
|
111 parameterNames.add(classFile.constant_pool.getUTF8Value(e.name_index)); |
|
112 } |
|
113 return parameterNames; |
|
114 } |
|
115 |
|
116 private static Method getMethod(ClassFile classFile, String name) throws Exception { |
|
117 for (Method method : classFile.methods) { |
|
118 if (classFile.constant_pool.getUTF8Value(method.name_index).equals(name)) { |
|
119 return method; |
|
120 } |
|
121 } |
|
122 throw new AssertionError("could not find method: " + name); |
|
123 } |
|
124 } |