From ec0618742ff6cfd6d83f1278e8d245673fb9ef2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20Lamp=C3=A9rth?= Date: Mon, 28 Oct 2024 09:42:03 +0000 Subject: [PATCH] 8034066: Incorrect alignment in the "Code" section for "-c -XDdetails" options Reviewed-by: jvernee, liach --- .../com/sun/tools/javap/ClassWriter.java | 4 +- .../com/sun/tools/javap/CodeWriter.java | 26 ++-- .../javap/ClassWriterCodeIndentTest.java | 113 ++++++++++++++++++ 3 files changed, 133 insertions(+), 10 deletions(-) create mode 100644 test/langtools/tools/javap/ClassWriterCodeIndentTest.java diff --git a/src/jdk.jdeps/share/classes/com/sun/tools/javap/ClassWriter.java b/src/jdk.jdeps/share/classes/com/sun/tools/javap/ClassWriter.java index 77fbce8839e..92a0c06c1c3 100644 --- a/src/jdk.jdeps/share/classes/com/sun/tools/javap/ClassWriter.java +++ b/src/jdk.jdeps/share/classes/com/sun/tools/javap/ClassWriter.java @@ -576,9 +576,7 @@ public class ClassWriter extends BasicWriter { attrWriter.write(m.attributes()); } else if (code != null) { if (options.showDisassembled) { - println("Code:"); - codeWriter.writeInstrs(code); - codeWriter.writeExceptionTable(code); + codeWriter.writeMinimal(code); } if (options.showLineAndLocalVariableTables) { diff --git a/src/jdk.jdeps/share/classes/com/sun/tools/javap/CodeWriter.java b/src/jdk.jdeps/share/classes/com/sun/tools/javap/CodeWriter.java index 8f6b9b1d2ed..cb401c9f197 100644 --- a/src/jdk.jdeps/share/classes/com/sun/tools/javap/CodeWriter.java +++ b/src/jdk.jdeps/share/classes/com/sun/tools/javap/CodeWriter.java @@ -70,13 +70,11 @@ public class CodeWriter extends BasicWriter { } void write(CodeAttribute attr) { - println("Code:"); - indent(+1); - writeVerboseHeader(attr); - writeInstrs(attr); - writeExceptionTable(attr); - attrWriter.write(attr.attributes(), attr); - indent(-1); + writeInternal(attr, false); + } + + void writeMinimal(CodeAttribute attr) { + writeInternal(attr, true); } public void writeVerboseHeader(CodeAttribute attr) { @@ -259,6 +257,20 @@ public class CodeWriter extends BasicWriter { return detailWriters; } + private void writeInternal(CodeAttribute attr, boolean minimal) { + println("Code:"); + indent(+1); + if (!minimal) { + writeVerboseHeader(attr); + } + writeInstrs(attr); + writeExceptionTable(attr); + if (!minimal) { + attrWriter.write(attr.attributes(), attr); + } + indent(-1); + } + private AttributeWriter attrWriter; private ClassWriter classWriter; private ConstantWriter constantWriter; diff --git a/test/langtools/tools/javap/ClassWriterCodeIndentTest.java b/test/langtools/tools/javap/ClassWriterCodeIndentTest.java new file mode 100644 index 00000000000..993c0a4364a --- /dev/null +++ b/test/langtools/tools/javap/ClassWriterCodeIndentTest.java @@ -0,0 +1,113 @@ +/* + * Copyright (c) 2024, 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 + * 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 8034066 + * @summary javap incorrect indentation when CodeWriter called via ClassWriter + * @run main ClassWriterCodeIndentTest + * @modules jdk.jdeps/com.sun.tools.javap + */ + +import java.io.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class ClassWriterCodeIndentTest { + public static void main(String[] args) { + new ClassWriterCodeIndentTest().run(); + } + + public void run() { + /* + * Partial expected output within a larger file. There exists another "Code: " section above, and thus we + * select the second occurrence in `findNthMatchPrecedingSpaces(output, "Code:", 1);` + * ... + * Code: + * 0: iconst_0 + * 1: istore_1 + * StackMap locals: this int + * StackMap stack: + * ... + */ + String output = javap(); + + int codeHeaderIndent = findNthMatchPrecedingSpaces(output, "Code:", 1); + int detailIndent = findNthMatchPrecedingSpaces(output, "StackMap ", 0); + int bytecodeIndent = findNthMatchPrecedingSpaces(output, "0: iconst_0", 0); + + if (detailIndent - codeHeaderIndent != 2) { + error("Details are not indented correctly with respect to code header."); + } + + if (bytecodeIndent - codeHeaderIndent != 5) { + error("Bytecode is not indented correctly with respect to code header."); + } + + if (errors > 0) { + throw new Error(errors + " found."); + } + } + + String javap() { + StringWriter sw = new StringWriter(); + PrintWriter out = new PrintWriter(sw); + int rc = com.sun.tools.javap.Main.run(new String[]{"-c", "-XDdetails:stackMaps", + System.getProperty("test.classes") + "/EmptyLoop.class"}, out); + if (rc != 0) + throw new Error("javap failed. rc=" + rc); + out.close(); + System.out.println(sw.toString()); + return sw.toString(); + } + + public static int findNthMatchPrecedingSpaces(String inputString, String searchString, int occurrence) { + String regex = "^(\\s*)" + Pattern.quote(searchString); + Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE); + Matcher matcher = pattern.matcher(inputString); + + int count = 0; + while (matcher.find()) { + if (count == occurrence) { + return matcher.group(1).length(); + } + count++; + } + + throw new Error("Could not find " + searchString + " in " + inputString); + } + + void error(String msg) { + System.err.println(msg); + errors++; + } + + int errors; +} + +class EmptyLoop { + public void emptyLoop() { + for (int i = 0; i < 10; i++) { + } + } +} \ No newline at end of file