Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -574,10 +574,8 @@ protected void writeMethod(MethodModel m) {

if (options.showAllAttrs) {
attrWriter.write(m.attributes());
} else if (code != null) {
if (options.showDisassembled || options.showLineAndLocalVariableTables) {
codeWriter.writeMinimal(code);
}
} else if (code != null && options.showDisassembled) {
codeWriter.writeMinimal(code);
}

indent(-1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,8 @@ private void writeInternal(CodeAttribute attr, boolean minimal) {
}

private void writeMinimalMode(CodeAttribute attr) {
if (options.showDisassembled) {
writeInstrs(attr);
writeExceptionTable(attr);
}

writeInstrs(attr);
writeExceptionTable(attr);
if (options.showLineAndLocalVariableTables) {
writeLineAndLocalVariableTables(attr);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,10 @@ else if (allowClasses) {
throw new BadArgs("err.incompatible.options", sb);
}

if (!options.showDisassembled && !options.verbose && options.showLineAndLocalVariableTables) {
reportWarning("err.incompatible.options", "-l without -c, line number and local variable tables will not be printed");
}

if ((classes == null || classes.size() == 0) &&
!(noArgs || options.help || options.version || options.fullVersion)) {
throw new BadArgs("err.no.classes.specified");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ main.opt.v=\
\ -v -verbose Print additional information

main.opt.l=\
\ -l Print line number and local variable tables
\ -l Print line number and local variable tables, works in combination with -c

main.opt.public=\
\ -public Show only public classes and members
Expand Down
101 changes: 101 additions & 0 deletions test/langtools/tools/javap/ClassWriterNoLineVariableTableTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* 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 8345145
* @summary javap should not print LineNumberTable/LocalVariableTable (-l) without disassembled code (-c).
* @compile -g ClassWriterNoLineVariableTableTest.java
* @run junit ClassWriterNoLineVariableTableTest
* @modules jdk.jdeps/com.sun.tools.javap
*/

import java.io.PrintWriter;
import java.io.StringWriter;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import static org.junit.jupiter.api.Assertions.*;

public class ClassWriterNoLineVariableTableTest {
String expectedErrorOutput = "Warning: bad combination of options: -l without -c, line number and local variable tables will not be printed";

@Test
public void testJavapWithoutCodeAttribute() {
String output = javap("-l");
assertContains(output, expectedErrorOutput,
"javap should throw warning, when -l used without -c or -v");
assertNotContains(output, "LineNumberTable",
"There should be no LineNumberTable output when javap is provided l without -c or -v");
assertNotContains(output, "LocalVariableTable",
"There should be no LineNumberTable output when javap is provided l without -c or -v");
}

@ParameterizedTest(name = "Test javap with fixed option -l and varying option: {0}")
@ValueSource(strings = {"-v", "-c"})
public void testJavapWithCodeAttribute(String addedOption) {
String output = javap("-l", addedOption);
assertNotContains(output, expectedErrorOutput,
"There should be no warning when javap is provided -l and " + addedOption);
assertContains(output, "LineNumberTable",
"There should be LineNumberTable output when javap is provided -l and " + addedOption);
assertContains(output, "LocalVariableTable",
"There should be LocalVariableTable output when javap is provided -l and " + addedOption);
}

private static void assertContains(String actual, String expectedSubstring, String message) {
assertTrue(actual.contains(expectedSubstring),
message + " - Expected '" + actual + "' to contain '" + expectedSubstring + "'");
}

private static void assertNotContains(String actual, String expectedSubstring, String message) {
assertFalse(actual.contains(expectedSubstring),
message + " - Expected '" + actual + "' not to contain '" + expectedSubstring + "'");
}

private String javap(String... args) {
StringWriter sw = new StringWriter();
PrintWriter out = new PrintWriter(sw);

String[] fullArgs = new String[args.length + 1];
System.arraycopy(args, 0, fullArgs, 0, args.length);
fullArgs[args.length] = System.getProperty("test.classes") + "/RandomLoop8345145.class";

int rc = com.sun.tools.javap.Main.run(fullArgs, out);
if (rc != 0)
throw new Error("javap failed. rc=" + rc);
out.close();
System.out.println(sw);
return sw.toString();
}
}

class RandomLoop8345145 {
public void randomLoop() {
int x = 5;
for (int i = 0; i < 10; i++) {
x*=2;
}
}
}
2 changes: 1 addition & 1 deletion test/langtools/tools/javap/ClassWriterTableIndentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void run() {
* line 145: 14
* ...
*/
List<String[]> runArgsList = List.of(new String[]{"-c", "-l"}, new String[]{"-v"}, new String[]{"-l"});
List<String[]> runArgsList = List.of(new String[]{"-c", "-l"}, new String[]{"-v"});
for (String[] runArgs : runArgsList) {
String output = javap(runArgs);
int methodIntent = findNthMatchPrecedingSpaces(output, "public void emptyLoop();", 0);
Expand Down
2 changes: 1 addition & 1 deletion test/langtools/tools/javap/T4459541.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ File compileTestFile(File f) {
String javap(File f) {
StringWriter sw = new StringWriter();
PrintWriter out = new PrintWriter(sw);
int rc = com.sun.tools.javap.Main.run(new String[] { "-l", f.getPath() }, out);
int rc = com.sun.tools.javap.Main.run(new String[] { "-l", "-c", f.getPath() }, out);
if (rc != 0)
throw new Error("javap failed. rc=" + rc);
out.close();
Expand Down
2 changes: 1 addition & 1 deletion test/langtools/tools/javap/T8032814.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void run() throws Exception {
+ clazz.getDeclaredMethods().length;
test(clazz, 0);
test(clazz, count, "-v");
test(clazz, count, "-l");
test(clazz, count, "-c", "-l");
test(clazz, count, "-v", "-l");

if (errors > 0)
Expand Down