8190452: javac should not add MethodParameters attributes to v51 and earlier class files
authorcushon
Mon, 05 Feb 2018 10:43:31 -0500
changeset 48746 c9ab849cd2f5
parent 48745 74be5b4ed152
child 48747 f7aed48bbbdc
child 48868 f0981646b6c6
8190452: javac should not add MethodParameters attributes to v51 and earlier class files Reviewed-by: vromero, jjg
src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassWriter.java
src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Target.java
src/jdk.compiler/share/classes/com/sun/tools/javac/main/Arguments.java
src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties
test/langtools/tools/javac/MethodParameters/LegacyOutputTest/LegacyOutputTest.java
test/langtools/tools/javac/diags/examples/ParametersUnsupported.java
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassWriter.java	Mon Feb 05 06:43:23 2018 -0500
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassWriter.java	Mon Feb 05 10:43:31 2018 -0500
@@ -1175,7 +1175,7 @@
             endAttr(alenIdx);
             acount++;
         }
-        if (options.isSet(PARAMETERS)) {
+        if (options.isSet(PARAMETERS) && target.hasMethodParameters()) {
             if (!m.isLambdaMethod()) // Per JDK-8138729, do not emit parameters table for lambda bodies.
                 acount += writeMethodParametersAttr(m);
         }
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Target.java	Mon Feb 05 06:43:23 2018 -0500
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Target.java	Mon Feb 05 10:43:31 2018 -0500
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2002, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2002, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -135,6 +135,12 @@
         return compareTo(JDK1_7) >= 0;
     }
 
+    /** Does the target VM expect MethodParameters attributes?
+     */
+    public boolean hasMethodParameters() {
+        return compareTo(JDK1_8) >= 0;
+    }
+
     /** Does the VM support polymorphic method handle invocation?
      *  Affects the linkage information output to the classfile.
      *  An alias for {@code hasInvokedynamic}, since all the JSR 292 features appear together.
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/main/Arguments.java	Mon Feb 05 06:43:23 2018 -0500
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/main/Arguments.java	Mon Feb 05 10:43:31 2018 -0500
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -595,6 +595,10 @@
                 Option.LIMIT_MODULES,
                 Option.PATCH_MODULE);
 
+        if (lintOptions && options.isSet(Option.PARAMETERS) && !target.hasMethodParameters()) {
+            log.warning(Warnings.OptionParametersUnsupported(target.name, Target.JDK1_8.name));
+        }
+
         if (fm.hasLocation(StandardLocation.MODULE_SOURCE_PATH)) {
             if (!options.isSet(Option.PROC, "only")
                     && !fm.hasLocation(StandardLocation.CLASS_OUTPUT)) {
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Mon Feb 05 06:43:23 2018 -0500
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Mon Feb 05 10:43:31 2018 -0500
@@ -1758,6 +1758,11 @@
 compiler.err.option.removed.target=\
     Target option {0} is no longer supported. Use {1} or later.
 
+
+# 0: string, 1: string
+compiler.warn.option.parameters.unsupported=\
+    -parameters is not supported for target value {0}. Use {1} or later.
+
 compiler.warn.option.obsolete.suppression=\
     To suppress warnings about obsolete options, use -Xlint:-options.
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/MethodParameters/LegacyOutputTest/LegacyOutputTest.java	Mon Feb 05 10:43:31 2018 -0500
@@ -0,0 +1,124 @@
+/*
+ * Copyright (c) 2018, Google Inc. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 8190452
+ * @summary javac should not add MethodParameters attributes to v51 and earlier class files
+ * @modules jdk.jdeps/com.sun.tools.classfile
+ * @build LegacyOutputTest
+ * @run main LegacyOutputTest
+ */
+
+import com.sun.tools.classfile.ClassFile;
+import com.sun.tools.classfile.Method;
+import com.sun.tools.classfile.MethodParameters_attribute;
+import com.sun.tools.classfile.MethodParameters_attribute.Entry;
+import java.io.IOException;
+import java.net.URI;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import javax.tools.JavaCompiler;
+import javax.tools.JavaCompiler.CompilationTask;
+import javax.tools.JavaFileObject;
+import javax.tools.JavaFileObject.Kind;
+import javax.tools.SimpleJavaFileObject;
+import javax.tools.ToolProvider;
+
+/**
+ * Post https://bugs.openjdk.java.net/browse/JDK-8190452, the test verifies that MethodParameters
+ * attributes are not emitted when targeting --release < 8.
+ */
+public class LegacyOutputTest {
+    public static void main(String[] args) throws Exception {
+        new LegacyOutputTest().test();
+    }
+
+    void test() throws Exception {
+        release7();
+        release8();
+    }
+
+    void release7() throws Exception {
+        List<String> names = getParameterNames("7");
+        if (names != null) {
+            throw new AssertionError(
+                    "expected no MethodParameters for --release 7, actual: " + names);
+        }
+    }
+
+    void release8() throws Exception {
+        List<String> names = getParameterNames("8");
+        List<String> expected = Arrays.asList("x", "y");
+        if (!names.equals(expected)) {
+            throw new AssertionError(
+                    "incorrect parameter names, actual: " + names + ", expected: " + expected);
+        }
+    }
+
+    List<String> getParameterNames(String release) throws Exception {
+        JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
+        JavaFileObject fileObject =
+                new SimpleJavaFileObject(URI.create("Test.java"), Kind.SOURCE) {
+                    @Override
+                    public CharSequence getCharContent(boolean ignoreEncodingErrors)
+                            throws IOException {
+                        return "class Test { void f(int x, int y) {} }";
+                    }
+                };
+        CompilationTask task =
+                tool.getTask(
+                        null,
+                        null,
+                        null,
+                        Arrays.asList("--release", release, "-parameters"),
+                        null,
+                        Arrays.asList(fileObject));
+        if (!task.call()) {
+            throw new AssertionError("compilation failed");
+        }
+        ClassFile classFile = ClassFile.read(Paths.get("Test.class"));
+        Method method = getMethod(classFile, "f");
+        MethodParameters_attribute attribute =
+                (MethodParameters_attribute) method.attributes.get("MethodParameters");
+        if (attribute == null) {
+            return null;
+        }
+        List<String> parameterNames = new ArrayList<>();
+        for (Entry e : attribute.method_parameter_table) {
+            parameterNames.add(classFile.constant_pool.getUTF8Value(e.name_index));
+        }
+        return parameterNames;
+    }
+
+    private static Method getMethod(ClassFile classFile, String name) throws Exception {
+        for (Method method : classFile.methods) {
+            if (classFile.constant_pool.getUTF8Value(method.name_index).equals(name)) {
+                return method;
+            }
+        }
+        throw new AssertionError("could not find method: " + name);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/diags/examples/ParametersUnsupported.java	Mon Feb 05 10:43:31 2018 -0500
@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2018, Google Inc. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+// key: compiler.warn.option.parameters.unsupported
+// options: -parameters --release 7
+
+class ParametersUnsupported {
+}