jjg@1473: /* jjg@1473: * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. jjg@1473: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@1473: * jjg@1473: * This code is free software; you can redistribute it and/or modify it jjg@1473: * under the terms of the GNU General Public License version 2 only, as jjg@1473: * published by the Free Software Foundation. jjg@1473: * jjg@1473: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@1473: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@1473: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@1473: * version 2 for more details (a copy is included in the LICENSE file that jjg@1473: * accompanied this code). jjg@1473: * jjg@1473: * You should have received a copy of the GNU General Public License version jjg@1473: * 2 along with this work; if not, write to the Free Software Foundation, jjg@1473: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@1473: * jjg@1473: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jjg@1473: * or visit www.oracle.com if you need additional information or have any jjg@1473: * questions. jjg@1473: */ jjg@1473: jjg@1473: /* jjg@1473: * @test jjg@1473: * @summary javac should generate method parameters correctly. jjg@1473: */ jjg@1473: // key: opt.arg.parameters jjg@1473: import com.sun.tools.classfile.*; jjg@1473: import com.sun.tools.javac.file.JavacFileManager; jjg@1473: import com.sun.tools.javac.main.Main; jjg@1473: import com.sun.tools.javac.util.Context; jjg@1473: import com.sun.tools.javac.util.Name; jjg@1473: import com.sun.tools.javac.util.Names; jjg@1473: import java.io.*; jjg@1473: import javax.lang.model.element.*; jjg@1473: import java.util.*; jjg@1473: jjg@1473: public class MethodParameters { jjg@1473: jjg@1473: static final String Foo_name = "Foo"; jjg@1473: static final String Foo_contents = jjg@1473: "public class Foo {\n" + jjg@1473: " Foo() {}\n" + jjg@1473: " void foo0() {}\n" + jjg@1473: " void foo2(int j, int k) {}\n" + jjg@1473: "}"; jjg@1473: static final String Bar_name = "Bar"; jjg@1473: static final String Bar_contents = jjg@1473: "public class Bar {\n" + jjg@1473: " Bar(int i) {}" + jjg@1473: " Foo foo() { return new Foo(); }\n" + jjg@1473: "}"; jjg@1473: static final String Baz_name = "Baz"; jjg@1473: static final String Baz_contents = jjg@1473: "public class Baz {\n" + jjg@1473: " int baz;" + jjg@1473: " Baz(int i) {}" + jjg@1473: "}"; jjg@1473: static final String Qux_name = "Qux"; jjg@1473: static final String Qux_contents = jjg@1473: "public class Qux extends Baz {\n" + jjg@1473: " Qux(int i) { super(i); }" + jjg@1473: "}"; jjg@1473: static final File classesdir = new File("methodparameters"); jjg@1473: jjg@1473: public static void main(String... args) throws Exception { jjg@1473: new MethodParameters().run(); jjg@1473: } jjg@1473: jjg@1473: void run() throws Exception { jjg@1473: classesdir.mkdir(); jjg@1473: final File Foo_java = jjg@1473: writeFile(classesdir, Foo_name + ".java", Foo_contents); jjg@1473: final File Bar_java = jjg@1473: writeFile(classesdir, Bar_name + ".java", Bar_contents); jjg@1473: final File Baz_java = jjg@1473: writeFile(classesdir, Baz_name + ".java", Baz_contents); jjg@1473: System.err.println("Test compile with -parameter"); jjg@1473: compile("-parameters", "-d", classesdir.getPath(), Foo_java.getPath()); jjg@1473: // First test: make sure javac doesn't choke to death on jjg@1473: // MethodParameter attributes jjg@1473: System.err.println("Test compile with classfile containing MethodParameter attributes"); jjg@1473: compile("-parameters", "-d", classesdir.getPath(), jjg@1473: "-cp", classesdir.getPath(), Bar_java.getPath()); jjg@1473: System.err.println("Examine class foo"); jjg@1473: checkFoo(); jjg@1473: checkBar(); jjg@1473: System.err.println("Test debug information conflict"); jjg@1473: compile("-g", "-parameters", "-d", classesdir.getPath(), jjg@1473: "-cp", classesdir.getPath(), Baz_java.getPath()); jjg@1473: System.err.println("Introducing debug information conflict"); jjg@1473: Baz_java.delete(); jjg@1473: modifyBaz(false); jjg@1473: System.err.println("Checking language model"); jjg@1473: inspectBaz(); jjg@1473: System.err.println("Permuting attributes"); jjg@1473: modifyBaz(true); jjg@1473: System.err.println("Checking language model"); jjg@1473: inspectBaz(); jjg@1473: jjg@1473: if(0 != errors) jjg@1473: throw new Exception("MethodParameters test failed with " + jjg@1473: errors + " errors"); jjg@1473: } jjg@1473: jjg@1473: void inspectBaz() throws Exception { jjg@1473: final File Qux_java = jjg@1473: writeFile(classesdir, Qux_name + ".java", Qux_contents); jjg@1473: final String[] args = { "-XDsave-parameter-names", "-d", jjg@1473: classesdir.getPath(), jjg@1473: "-cp", classesdir.getPath(), jjg@1473: Qux_java.getPath() }; jjg@1473: final StringWriter sw = new StringWriter(); jjg@1473: final PrintWriter pw = new PrintWriter(sw); jjg@1473: jjg@1473: // We need to be able to crack open javac and look at its data jjg@1473: // structures. We'll rig up a compiler instance, but keep its jjg@1473: // Context, thus allowing us to get at the ClassReader. jjg@1473: Context context = new Context(); jjg@1473: Main comp = new Main("javac", pw); jjg@1473: JavacFileManager.preRegister(context); jjg@1473: jjg@1473: // Compile Qux, which uses Baz. jjg@1473: comp.compile(args, context); jjg@1473: pw.close(); jjg@1473: final String out = sw.toString(); jjg@1473: if (out.length() > 0) jjg@1473: System.err.println(out); jjg@1473: jjg@1473: // Now get the class reader, construct a name for Baz, and load it. jjg@1473: com.sun.tools.javac.jvm.ClassReader cr = jjg@1473: com.sun.tools.javac.jvm.ClassReader.instance(context); jjg@1473: Name name = Names.instance(context).fromString(Baz_name); jjg@1473: jjg@1473: // Now walk down the language model and check the name of the jjg@1473: // parameter. jjg@1473: final Element baz = cr.loadClass(name); jjg@1473: for (Element e : baz.getEnclosedElements()) { jjg@1473: if (e instanceof ExecutableElement) { jjg@1473: final ExecutableElement ee = (ExecutableElement) e; jjg@1473: final List params = jjg@1473: ee.getParameters(); jjg@1473: if (1 != params.size()) jjg@1473: throw new Exception("Classfile Baz badly formed: wrong number of methods"); jjg@1473: final VariableElement param = params.get(0); jjg@1473: if (!param.getSimpleName().contentEquals("baz")) { jjg@1473: errors++; jjg@1473: System.err.println("javac did not correctly resolve the metadata conflict, parameter's name reads as " + param.getSimpleName()); jjg@1473: } else jjg@1473: System.err.println("javac did correctly resolve the metadata conflict"); jjg@1473: } jjg@1473: } jjg@1473: } jjg@1473: jjg@1473: void modifyBaz(boolean flip) throws Exception { jjg@1473: final File Baz_class = new File(classesdir, Baz_name + ".class"); jjg@1473: final ClassFile baz = ClassFile.read(Baz_class); jjg@1473: final int ind = baz.constant_pool.getUTF8Index("baz"); jjg@1473: MethodParameters_attribute mpattr = null; jjg@1473: int mpind = 0; jjg@1473: Code_attribute cattr = null; jjg@1473: int cind = 0; jjg@1473: jjg@1473: // Find the indexes of the MethodParameters and the Code attributes jjg@1473: if (baz.methods.length != 1) jjg@1473: throw new Exception("Classfile Baz badly formed: wrong number of methods"); jjg@1473: if (!baz.methods[0].getName(baz.constant_pool).equals("")) jjg@1473: throw new Exception("Classfile Baz badly formed: method has name " + jjg@1473: baz.methods[0].getName(baz.constant_pool)); jjg@1473: for (int i = 0; i < baz.methods[0].attributes.attrs.length; i++) { jjg@1473: if (baz.methods[0].attributes.attrs[i] instanceof jjg@1473: MethodParameters_attribute) { jjg@1473: mpattr = (MethodParameters_attribute) jjg@1473: baz.methods[0].attributes.attrs[i]; jjg@1473: mpind = i; jjg@1473: } else if (baz.methods[0].attributes.attrs[i] instanceof jjg@1473: Code_attribute) { jjg@1473: cattr = (Code_attribute) baz.methods[0].attributes.attrs[i]; jjg@1473: cind = i; jjg@1473: } jjg@1473: } jjg@1473: if (null == mpattr) jjg@1473: throw new Exception("Classfile Baz badly formed: no method parameters info"); jjg@1473: if (null == cattr) jjg@1473: throw new Exception("Classfile Baz badly formed: no local variable table"); jjg@1473: jjg@1473: int flags = mpattr.method_parameter_table[0].flags; jjg@1473: jjg@1473: // Alter the MethodParameters attribute, changing the name of jjg@1473: // the parameter from i to baz. This requires Black Magic... jjg@1473: // jjg@1473: // The (well-designed) classfile library (correctly) does not jjg@1473: // allow us to mess around with the attribute data structures, jjg@1473: // or arbitrarily generate new ones. jjg@1473: // jjg@1473: // Instead, we install a new subclass of Attribute that jjg@1473: // hijacks the Visitor pattern and outputs the sequence of jjg@1473: // bytes that we want. This only works in this particular jjg@1473: // instance, because we know we'll only every see one kind of jjg@1473: // visitor. jjg@1473: // jjg@1473: // If anyone ever changes the makeup of the Baz class, or jjg@1473: // tries to install some kind of visitor that gets run prior jjg@1473: // to serialization, this will break. jjg@1473: baz.methods[0].attributes.attrs[mpind] = jjg@1473: new Attribute(mpattr.attribute_name_index, jjg@1473: mpattr.attribute_length) { jjg@1473: public R accept(Visitor visitor, D data) { jjg@1473: if (data instanceof ByteArrayOutputStream) { jjg@1473: ByteArrayOutputStream out = jjg@1473: (ByteArrayOutputStream) data; jjg@1473: out.write(1); jjg@1473: out.write((ind >> 8) & 0xff); jjg@1473: out.write(ind & 0xff); jjg@1473: out.write((flags >> 24) & 0xff); jjg@1473: out.write((flags >> 16) & 0xff); jjg@1473: out.write((flags >> 8) & 0xff); jjg@1473: out.write(flags & 0xff); jjg@1473: } else jjg@1473: throw new RuntimeException("Output stream is of type " + data.getClass() + ", which is not handled by this test. Update the test and it should work."); jjg@1473: return null; jjg@1473: } jjg@1473: }; jjg@1473: jjg@1473: // Flip the code and method attributes. This is for checking jjg@1473: // that order doesn't matter. jjg@1473: if (flip) { jjg@1473: baz.methods[0].attributes.attrs[mpind] = cattr; jjg@1473: baz.methods[0].attributes.attrs[cind] = mpattr; jjg@1473: } jjg@1473: jjg@1473: new ClassWriter().write(baz, Baz_class); jjg@1473: } jjg@1473: jjg@1473: // Run a bunch of structural tests on foo to make sure it looks right. jjg@1473: void checkFoo() throws Exception { jjg@1473: final File Foo_class = new File(classesdir, Foo_name + ".class"); jjg@1473: final ClassFile foo = ClassFile.read(Foo_class); jjg@1473: for (int i = 0; i < foo.methods.length; i++) { jjg@1473: System.err.println("Examine method Foo." + foo.methods[i].getName(foo.constant_pool)); jjg@1473: if (foo.methods[i].getName(foo.constant_pool).equals("foo2")) { jjg@1473: for (int j = 0; j < foo.methods[i].attributes.attrs.length; j++) jjg@1473: if (foo.methods[i].attributes.attrs[j] instanceof jjg@1473: MethodParameters_attribute) { jjg@1473: MethodParameters_attribute mp = jjg@1473: (MethodParameters_attribute) jjg@1473: foo.methods[i].attributes.attrs[j]; jjg@1473: System.err.println("Foo.foo2 should have 2 parameters: j and k"); jjg@1473: if (2 != mp.method_parameter_table_length) jjg@1473: error("expected 2 method parameter entries in foo2, got " + jjg@1473: mp.method_parameter_table_length); jjg@1473: else if (!foo.constant_pool.getUTF8Value(mp.method_parameter_table[0].name_index).equals("j")) jjg@1473: error("expected first parameter to foo2 to be \"j\", got \"" + jjg@1473: foo.constant_pool.getUTF8Value(mp.method_parameter_table[0].name_index) + jjg@1473: "\" instead"); jjg@1473: else if (!foo.constant_pool.getUTF8Value(mp.method_parameter_table[1].name_index).equals("k")) jjg@1473: error("expected first parameter to foo2 to be \"k\", got \"" + jjg@1473: foo.constant_pool.getUTF8Value(mp.method_parameter_table[1].name_index) + jjg@1473: "\" instead"); jjg@1473: } jjg@1473: } jjg@1473: else if (foo.methods[i].getName(foo.constant_pool).equals("")) { jjg@1473: for (int j = 0; j < foo.methods[i].attributes.attrs.length; j++) { jjg@1473: if (foo.methods[i].attributes.attrs[j] instanceof jjg@1473: MethodParameters_attribute) jjg@1473: error("Zero-argument constructor shouldn't have MethodParameters"); jjg@1473: } jjg@1473: } jjg@1473: else if (foo.methods[i].getName(foo.constant_pool).equals("foo0")) { jjg@1473: for (int j = 0; j < foo.methods[i].attributes.attrs.length; j++) jjg@1473: if (foo.methods[i].attributes.attrs[j] instanceof jjg@1473: MethodParameters_attribute) jjg@1473: error("Zero-argument method shouldn't have MethodParameters"); jjg@1473: } jjg@1473: else jjg@1473: error("Unknown method " + foo.methods[i].getName(foo.constant_pool) + " showed up in class Foo"); jjg@1473: } jjg@1473: } jjg@1473: jjg@1473: // Run a bunch of structural tests on Bar to make sure it looks right. jjg@1473: void checkBar() throws Exception { jjg@1473: final File Bar_class = new File(classesdir, Bar_name + ".class"); jjg@1473: final ClassFile bar = ClassFile.read(Bar_class); jjg@1473: for (int i = 0; i < bar.methods.length; i++) { jjg@1473: System.err.println("Examine method Bar." + bar.methods[i].getName(bar.constant_pool)); jjg@1473: if (bar.methods[i].getName(bar.constant_pool).equals("")) { jjg@1473: for (int j = 0; j < bar.methods[i].attributes.attrs.length; j++) jjg@1473: if (bar.methods[i].attributes.attrs[j] instanceof jjg@1473: MethodParameters_attribute) { jjg@1473: MethodParameters_attribute mp = jjg@1473: (MethodParameters_attribute) jjg@1473: bar.methods[i].attributes.attrs[j]; jjg@1473: System.err.println("Bar constructor should have 1 parameter: i"); jjg@1473: if (1 != mp.method_parameter_table_length) jjg@1473: error("expected 1 method parameter entries in constructor, got " + jjg@1473: mp.method_parameter_table_length); jjg@1473: else if (!bar.constant_pool.getUTF8Value(mp.method_parameter_table[0].name_index).equals("i")) jjg@1473: error("expected first parameter to foo2 to be \"i\", got \"" + jjg@1473: bar.constant_pool.getUTF8Value(mp.method_parameter_table[0].name_index) + jjg@1473: "\" instead"); jjg@1473: } jjg@1473: } jjg@1473: else if (bar.methods[i].getName(bar.constant_pool).equals("foo")) { jjg@1473: for (int j = 0; j < bar.methods[i].attributes.attrs.length; j++) { jjg@1473: if (bar.methods[i].attributes.attrs[j] instanceof jjg@1473: MethodParameters_attribute) jjg@1473: error("Zero-argument constructor shouldn't have MethodParameters"); jjg@1473: } jjg@1473: } jjg@1473: } jjg@1473: } jjg@1473: jjg@1473: String compile(String... args) throws Exception { jjg@1473: System.err.println("compile: " + Arrays.asList(args)); jjg@1473: StringWriter sw = new StringWriter(); jjg@1473: PrintWriter pw = new PrintWriter(sw); jjg@1473: int rc = com.sun.tools.javac.Main.compile(args, pw); jjg@1473: pw.close(); jjg@1473: String out = sw.toString(); jjg@1473: if (out.length() > 0) jjg@1473: System.err.println(out); jjg@1473: if (rc != 0) jjg@1473: error("compilation failed, rc=" + rc); jjg@1473: return out; jjg@1473: } jjg@1473: jjg@1473: File writeFile(File dir, String path, String body) throws IOException { jjg@1473: File f = new File(dir, path); jjg@1473: f.getParentFile().mkdirs(); jjg@1473: FileWriter out = new FileWriter(f); jjg@1473: out.write(body); jjg@1473: out.close(); jjg@1473: return f; jjg@1473: } jjg@1473: jjg@1473: void error(String msg) { jjg@1473: System.err.println("Error: " + msg); jjg@1473: errors++; jjg@1473: } jjg@1473: jjg@1473: int errors; jjg@1473: }