提交 0c56bf05 编写于 作者: V vromero

8007907: javap, method com.sun.tools.javap.Main.run returns 0 even in case of class not found error

Reviewed-by: jjg
上级 03b756cc
...@@ -452,8 +452,7 @@ public class JavapTask implements DisassemblerTool.DisassemblerTask, Messages { ...@@ -452,8 +452,7 @@ public class JavapTask implements DisassemblerTool.DisassemblerTask, Messages {
} }
try { try {
boolean ok = run(); return run();
return ok ? EXIT_OK : EXIT_ERROR;
} finally { } finally {
if (defaultFileManager != null) { if (defaultFileManager != null) {
try { try {
...@@ -569,12 +568,13 @@ public class JavapTask implements DisassemblerTool.DisassemblerTask, Messages { ...@@ -569,12 +568,13 @@ public class JavapTask implements DisassemblerTool.DisassemblerTask, Messages {
} }
public Boolean call() { public Boolean call() {
return run(); return run() == 0;
} }
public boolean run() { public int run() {
if (classes == null || classes.size() == 0) if (classes == null || classes.isEmpty()) {
return false; return EXIT_ERROR;
}
context.put(PrintWriter.class, log); context.put(PrintWriter.class, log);
ClassWriter classWriter = ClassWriter.instance(context); ClassWriter classWriter = ClassWriter.instance(context);
...@@ -583,54 +583,55 @@ public class JavapTask implements DisassemblerTool.DisassemblerTask, Messages { ...@@ -583,54 +583,55 @@ public class JavapTask implements DisassemblerTool.DisassemblerTask, Messages {
attributeFactory.setCompat(options.compat); attributeFactory.setCompat(options.compat);
boolean ok = true; int result = EXIT_OK;
for (String className: classes) { for (String className: classes) {
JavaFileObject fo;
try { try {
writeClass(classWriter, className); result = writeClass(classWriter, className);
} catch (ConstantPoolException e) { } catch (ConstantPoolException e) {
reportError("err.bad.constant.pool", className, e.getLocalizedMessage()); reportError("err.bad.constant.pool", className, e.getLocalizedMessage());
ok = false; result = EXIT_ERROR;
} catch (EOFException e) { } catch (EOFException e) {
reportError("err.end.of.file", className); reportError("err.end.of.file", className);
ok = false; result = EXIT_ERROR;
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
reportError("err.file.not.found", e.getLocalizedMessage()); reportError("err.file.not.found", e.getLocalizedMessage());
ok = false; result = EXIT_ERROR;
} catch (IOException e) { } catch (IOException e) {
//e.printStackTrace(); //e.printStackTrace();
Object msg = e.getLocalizedMessage(); Object msg = e.getLocalizedMessage();
if (msg == null) if (msg == null) {
msg = e; msg = e;
}
reportError("err.ioerror", className, msg); reportError("err.ioerror", className, msg);
ok = false; result = EXIT_ERROR;
} catch (Throwable t) { } catch (Throwable t) {
StringWriter sw = new StringWriter(); StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw); PrintWriter pw = new PrintWriter(sw);
t.printStackTrace(pw); t.printStackTrace(pw);
pw.close(); pw.close();
reportError("err.crash", t.toString(), sw.toString()); reportError("err.crash", t.toString(), sw.toString());
ok = false; result = EXIT_ABNORMAL;
} }
} }
return ok; return result;
} }
protected boolean writeClass(ClassWriter classWriter, String className) protected int writeClass(ClassWriter classWriter, String className)
throws IOException, ConstantPoolException { throws IOException, ConstantPoolException {
JavaFileObject fo = open(className); JavaFileObject fo = open(className);
if (fo == null) { if (fo == null) {
reportError("err.class.not.found", className); reportError("err.class.not.found", className);
return false; return EXIT_ERROR;
} }
ClassFileInfo cfInfo = read(fo); ClassFileInfo cfInfo = read(fo);
if (!className.endsWith(".class")) { if (!className.endsWith(".class")) {
String cfName = cfInfo.cf.getName(); String cfName = cfInfo.cf.getName();
if (!cfName.replaceAll("[/$]", ".").equals(className.replaceAll("[/$]", "."))) if (!cfName.replaceAll("[/$]", ".").equals(className.replaceAll("[/$]", "."))) {
reportWarning("warn.unexpected.class", className, cfName.replace('/', '.')); reportWarning("warn.unexpected.class", className, cfName.replace('/', '.'));
}
} }
write(cfInfo); write(cfInfo);
...@@ -640,7 +641,7 @@ public class JavapTask implements DisassemblerTool.DisassemblerTask, Messages { ...@@ -640,7 +641,7 @@ public class JavapTask implements DisassemblerTool.DisassemblerTask, Messages {
if (a instanceof InnerClasses_attribute) { if (a instanceof InnerClasses_attribute) {
InnerClasses_attribute inners = (InnerClasses_attribute) a; InnerClasses_attribute inners = (InnerClasses_attribute) a;
try { try {
boolean ok = true; int result = EXIT_OK;
for (int i = 0; i < inners.classes.length; i++) { for (int i = 0; i < inners.classes.length; i++) {
int outerIndex = inners.classes[i].outer_class_info_index; int outerIndex = inners.classes[i].outer_class_info_index;
ConstantPool.CONSTANT_Class_info outerClassInfo = cf.constant_pool.getClassInfo(outerIndex); ConstantPool.CONSTANT_Class_info outerClassInfo = cf.constant_pool.getClassInfo(outerIndex);
...@@ -651,21 +652,22 @@ public class JavapTask implements DisassemblerTool.DisassemblerTask, Messages { ...@@ -651,21 +652,22 @@ public class JavapTask implements DisassemblerTool.DisassemblerTask, Messages {
String innerClassName = innerClassInfo.getName(); String innerClassName = innerClassInfo.getName();
classWriter.println("// inner class " + innerClassName.replaceAll("[/$]", ".")); classWriter.println("// inner class " + innerClassName.replaceAll("[/$]", "."));
classWriter.println(); classWriter.println();
ok = ok & writeClass(classWriter, innerClassName); result = writeClass(classWriter, innerClassName);
if (result != EXIT_OK) return result;
} }
} }
return ok; return result;
} catch (ConstantPoolException e) { } catch (ConstantPoolException e) {
reportError("err.bad.innerclasses.attribute", className); reportError("err.bad.innerclasses.attribute", className);
return false; return EXIT_ERROR;
} }
} else if (a != null) { } else if (a != null) {
reportError("err.bad.innerclasses.attribute", className); reportError("err.bad.innerclasses.attribute", className);
return false; return EXIT_ERROR;
} }
} }
return true; return EXIT_OK;
} }
protected JavaFileObject open(String className) throws IOException { protected JavaFileObject open(String className) throws IOException {
......
...@@ -25,26 +25,25 @@ ...@@ -25,26 +25,25 @@
* @test * @test
* @bug 4645152 4785453 * @bug 4645152 4785453
* @summary javac compiler incorrectly inserts <clinit> when -g is specified * @summary javac compiler incorrectly inserts <clinit> when -g is specified
* @library /tools/javac/lib
* @build ToolBox
* @run compile -g ConstDebugTest.java * @run compile -g ConstDebugTest.java
* @run main ConstDebugTest * @run main ConstDebugTest
*/ */
import java.nio.file.Paths;
import com.sun.tools.classfile.ClassFile;
import com.sun.tools.classfile.Method;
//original test: test/tools/javac/constDebug/ConstDebug.sh
public class ConstDebugTest { public class ConstDebugTest {
public static final long l = 12; public static final long l = 12;
public static void main(String args[]) throws Exception { public static void main(String args[]) throws Exception {
// "${TESTJAVA}${FS}bin${FS}javac" ${TESTTOOLVMOPTS} -g -d . -classpath .${PS}${TESTSRC} $1.java 2> ${TMP1} ClassFile classFile = ClassFile.read(Paths.get(System.getProperty("test.classes"),
// if "${TESTJAVA}${FS}bin${FS}javap" $1.class | grep clinit; then fail ConstDebugTest.class.getSimpleName() + ".class"));
ToolBox.JavaToolArgs javapArgs = for (Method method: classFile.methods) {
new ToolBox.JavaToolArgs().setAllArgs("-v", if (method.getName(classFile.constant_pool).equals("<clinit>")) {
"-classpath", System.getProperty("test.classes"), "ConstDebugTest.class"); throw new AssertionError(
if (ToolBox.javap(javapArgs).contains("clinit")) { "javac should not create a <clinit> method for ConstDebugTest class");
throw new AssertionError( }
"javac should not create a <clinit> method for ConstDebugTest class");
} }
} }
......
...@@ -66,8 +66,7 @@ public class JavapTaskCtorFailWithNPE { ...@@ -66,8 +66,7 @@ public class JavapTaskCtorFailWithNPE {
JavaFileManager fm = JavapFileManager.create(dc, pw); JavaFileManager fm = JavapFileManager.create(dc, pw);
JavapTask t = new JavapTask(pw, fm, dc, null, JavapTask t = new JavapTask(pw, fm, dc, null,
Arrays.asList(classToCheck.getPath())); Arrays.asList(classToCheck.getPath()));
boolean ok = t.run(); if (t.run() != 0)
if (!ok)
throw new Error("javap failed unexpectedly"); throw new Error("javap failed unexpectedly");
List<Diagnostic<? extends JavaFileObject>> diags = dc.getDiagnostics(); List<Diagnostic<? extends JavaFileObject>> diags = dc.getDiagnostics();
......
/*
* Copyright (c) 2013, 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 8007907
* @summary javap, method com.sun.tools.javap.Main.run returns 0 even in case
* of class not found error
*/
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
public class JavapReturns0AfterClassNotFoundTest {
static final String fileNotFoundErrorMsg =
"Error: class not found: Unexisting.class";
static final String exitCodeClassNotFoundAssertionMsg =
"Javap's exit code for class not found should be 1";
static final String classNotFoundMsgAssertionMsg =
"Javap's error message for class not found is incorrect";
public static void main(String args[]) throws Exception {
new JavapReturns0AfterClassNotFoundTest().run();
}
void run() throws IOException {
check(exitCodeClassNotFoundAssertionMsg, classNotFoundMsgAssertionMsg,
fileNotFoundErrorMsg, "-v", "Unexisting.class");
}
void check(String exitCodeAssertionMsg, String errMsgAssertionMsg,
String expectedErrMsg, String... params) {
int result;
StringWriter s;
String out;
try (PrintWriter pw = new PrintWriter(s = new StringWriter())) {
result = com.sun.tools.javap.Main.run(params, pw);
//no line separator, no problem
out = s.toString().trim();
}
if (result != 1) {
System.out.println("actual exit code " + result);
throw new AssertionError(exitCodeAssertionMsg);
}
if (!out.equals(expectedErrMsg)) {
System.out.println("actual " + out);
System.out.println("expected " + expectedErrMsg);
throw new AssertionError(errMsgAssertionMsg);
}
}
}
/* /*
* Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -87,11 +87,11 @@ public class T4777949 { ...@@ -87,11 +87,11 @@ public class T4777949 {
PrintWriter pw = new PrintWriter(sw); PrintWriter pw = new PrintWriter(sw);
JavaFileManager fm = JavapFileManager.create(dc, pw); JavaFileManager fm = JavapFileManager.create(dc, pw);
JavapTask t = new JavapTask(pw, fm, dc, args, classes); JavapTask t = new JavapTask(pw, fm, dc, args, classes);
boolean ok = t.run(); int ok = t.run();
List<Diagnostic<? extends JavaFileObject>> diags = dc.getDiagnostics(); List<Diagnostic<? extends JavaFileObject>> diags = dc.getDiagnostics();
if (!ok) if (ok != 0)
error("javap failed unexpectedly"); error("javap failed unexpectedly");
System.err.println("args=" + args + " classes=" + classes + "\n" System.err.println("args=" + args + " classes=" + classes + "\n"
......
...@@ -96,8 +96,7 @@ public class T7190862 { ...@@ -96,8 +96,7 @@ public class T7190862 {
PrintWriter pw = new PrintWriter(sw); PrintWriter pw = new PrintWriter(sw);
JavaFileManager fm = JavapFileManager.create(dc, pw); JavaFileManager fm = JavapFileManager.create(dc, pw);
JavapTask t = new JavapTask(pw, fm, dc, args, classes); JavapTask t = new JavapTask(pw, fm, dc, args, classes);
boolean ok = t.run(); if (t.run() != 0)
if (!ok)
throw new Error("javap failed unexpectedly"); throw new Error("javap failed unexpectedly");
List<Diagnostic<? extends JavaFileObject>> diags = dc.getDiagnostics(); List<Diagnostic<? extends JavaFileObject>> diags = dc.getDiagnostics();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册