Thu, 13 Feb 2014 20:34:27 +0400
8032814: LineNumberTable/LocalVariableTable tables duplication for the "-v -l" combination of options
Reviewed-by: ksrini
src/share/classes/com/sun/tools/javap/ClassWriter.java | file | annotate | diff | comparison | revisions | |
test/tools/javap/T8032814.java | file | annotate | diff | comparison | revisions |
1.1 --- a/src/share/classes/com/sun/tools/javap/ClassWriter.java Wed Feb 12 23:52:58 2014 +0400 1.2 +++ b/src/share/classes/com/sun/tools/javap/ClassWriter.java Thu Feb 13 20:34:27 2014 +0400 1.3 @@ -1,5 +1,5 @@ 1.4 /* 1.5 - * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved. 1.7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.8 * 1.9 * This code is free software; you can redistribute it and/or modify it 1.10 @@ -498,27 +498,23 @@ 1.11 report("Unexpected or invalid value for Code attribute"); 1.12 } 1.13 1.14 - if (options.showDisassembled && !options.showAllAttrs) { 1.15 - if (code != null) { 1.16 + if (options.showAllAttrs) { 1.17 + Attribute[] attrs = m.attributes.attrs; 1.18 + for (Attribute attr: attrs) 1.19 + attrWriter.write(m, attr, constant_pool); 1.20 + } else if (code != null) { 1.21 + if (options.showDisassembled) { 1.22 println("Code:"); 1.23 codeWriter.writeInstrs(code); 1.24 codeWriter.writeExceptionTable(code); 1.25 } 1.26 - } 1.27 1.28 - if (options.showLineAndLocalVariableTables) { 1.29 - if (code != null) { 1.30 + if (options.showLineAndLocalVariableTables) { 1.31 attrWriter.write(code, code.attributes.get(Attribute.LineNumberTable), constant_pool); 1.32 attrWriter.write(code, code.attributes.get(Attribute.LocalVariableTable), constant_pool); 1.33 } 1.34 } 1.35 1.36 - if (options.showAllAttrs) { 1.37 - Attribute[] attrs = m.attributes.attrs; 1.38 - for (Attribute attr: attrs) 1.39 - attrWriter.write(m, attr, constant_pool); 1.40 - } 1.41 - 1.42 indent(-1); 1.43 1.44 // set pendingNewline to write a newline before the next method (if any)
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/test/tools/javap/T8032814.java Thu Feb 13 20:34:27 2014 +0400 2.3 @@ -0,0 +1,93 @@ 2.4 +/* 2.5 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 2.7 + * 2.8 + * This code is free software; you can redistribute it and/or modify it 2.9 + * under the terms of the GNU General Public License version 2 only, as 2.10 + * published by the Free Software Foundation. 2.11 + * 2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 2.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 2.15 + * version 2 for more details (a copy is included in the LICENSE file that 2.16 + * accompanied this code). 2.17 + * 2.18 + * You should have received a copy of the GNU General Public License version 2.19 + * 2 along with this work; if not, write to the Free Software Foundation, 2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2.21 + * 2.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2.23 + * or visit www.oracle.com if you need additional information or have any 2.24 + * questions. 2.25 + */ 2.26 + 2.27 +/* 2.28 + * @test 2.29 + * @bug 8032814 2.30 + * @summary LineNumberTable/LocalVariableTable tables duplication for the 2.31 + * "-v -l" combination of options 2.32 + * @compile -g T8032814.java 2.33 + * @run main T8032814 2.34 + */ 2.35 + 2.36 +import java.io.*; 2.37 +import java.util.*; 2.38 + 2.39 +public class T8032814 { 2.40 + public static void main(String... args) throws Exception { 2.41 + new T8032814().run(); 2.42 + } 2.43 + 2.44 + void run() throws Exception { 2.45 + Class<?> clazz = T8032814.class; 2.46 + int count = clazz.getDeclaredConstructors().length 2.47 + + clazz.getDeclaredMethods().length; 2.48 + test(clazz, 0); 2.49 + test(clazz, count, "-v"); 2.50 + test(clazz, count, "-l"); 2.51 + test(clazz, count, "-v", "-l"); 2.52 + 2.53 + if (errors > 0) 2.54 + throw new Exception(errors + " errors occurred"); 2.55 + } 2.56 + 2.57 + void test(Class<?> clazz, int expectedCount, String... opts) throws Exception { 2.58 + System.err.println("test class " + clazz.getName() + " " + Arrays.asList(opts) + ": expect: " + expectedCount); 2.59 + List<String> args = new ArrayList<String>(); 2.60 + args.addAll(Arrays.asList(opts)); 2.61 + args.addAll(Arrays.asList("-classpath", System.getProperty("test.classes"))); 2.62 + args.add(clazz.getName()); 2.63 + StringWriter sw = new StringWriter(); 2.64 + PrintWriter pw = new PrintWriter(sw); 2.65 + int rc = com.sun.tools.javap.Main.run(args.toArray(new String[args.size()]), pw); 2.66 + pw.close(); 2.67 + String out = sw.toString(); 2.68 + if (rc != 0) 2.69 + throw new Exception("javap failed unexpectedly: rc=" + rc); 2.70 + 2.71 + int lntCount = 0, lvtCount = 0; 2.72 + for (String line: out.split("[\r\n]+")) { 2.73 + if (line.matches("^ *LineNumberTable:$")) 2.74 + lntCount++; 2.75 + if (line.matches("^ *LocalVariableTable:$")) 2.76 + lvtCount++; 2.77 + } 2.78 + checkEqual("LineNumberTable", lntCount, expectedCount); 2.79 + checkEqual("LocalVariableTable", lvtCount, expectedCount); 2.80 + } 2.81 + 2.82 + void checkEqual(String attr, int found, int expect) { 2.83 + if (found != expect) { 2.84 + error("Unexpected number of occurrences of " + attr + "\n" + 2.85 + "found: " + found + ", expected: " + expect); 2.86 + } 2.87 + } 2.88 + 2.89 + void error(String msg) { 2.90 + System.err.println("Error: " + msg); 2.91 + errors++; 2.92 + } 2.93 + 2.94 + int errors = 0; 2.95 +} 2.96 +