提交 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 {
}
try {
boolean ok = run();
return ok ? EXIT_OK : EXIT_ERROR;
return run();
} finally {
if (defaultFileManager != null) {
try {
......@@ -569,12 +568,13 @@ public class JavapTask implements DisassemblerTool.DisassemblerTask, Messages {
}
public Boolean call() {
return run();
return run() == 0;
}
public boolean run() {
if (classes == null || classes.size() == 0)
return false;
public int run() {
if (classes == null || classes.isEmpty()) {
return EXIT_ERROR;
}
context.put(PrintWriter.class, log);
ClassWriter classWriter = ClassWriter.instance(context);
......@@ -583,54 +583,55 @@ public class JavapTask implements DisassemblerTool.DisassemblerTask, Messages {
attributeFactory.setCompat(options.compat);
boolean ok = true;
int result = EXIT_OK;
for (String className: classes) {
JavaFileObject fo;
try {
writeClass(classWriter, className);
result = writeClass(classWriter, className);
} catch (ConstantPoolException e) {
reportError("err.bad.constant.pool", className, e.getLocalizedMessage());
ok = false;
result = EXIT_ERROR;
} catch (EOFException e) {
reportError("err.end.of.file", className);
ok = false;
result = EXIT_ERROR;
} catch (FileNotFoundException e) {
reportError("err.file.not.found", e.getLocalizedMessage());
ok = false;
result = EXIT_ERROR;
} catch (IOException e) {
//e.printStackTrace();
Object msg = e.getLocalizedMessage();
if (msg == null)
if (msg == null) {
msg = e;
}
reportError("err.ioerror", className, msg);
ok = false;
result = EXIT_ERROR;
} catch (Throwable t) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
t.printStackTrace(pw);
pw.close();
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 {
JavaFileObject fo = open(className);
if (fo == null) {
reportError("err.class.not.found", className);
return false;
return EXIT_ERROR;
}
ClassFileInfo cfInfo = read(fo);
if (!className.endsWith(".class")) {
String cfName = cfInfo.cf.getName();
if (!cfName.replaceAll("[/$]", ".").equals(className.replaceAll("[/$]", ".")))
if (!cfName.replaceAll("[/$]", ".").equals(className.replaceAll("[/$]", "."))) {
reportWarning("warn.unexpected.class", className, cfName.replace('/', '.'));
}
}
write(cfInfo);
......@@ -640,7 +641,7 @@ public class JavapTask implements DisassemblerTool.DisassemblerTask, Messages {
if (a instanceof InnerClasses_attribute) {
InnerClasses_attribute inners = (InnerClasses_attribute) a;
try {
boolean ok = true;
int result = EXIT_OK;
for (int i = 0; i < inners.classes.length; i++) {
int outerIndex = inners.classes[i].outer_class_info_index;
ConstantPool.CONSTANT_Class_info outerClassInfo = cf.constant_pool.getClassInfo(outerIndex);
......@@ -651,21 +652,22 @@ public class JavapTask implements DisassemblerTool.DisassemblerTask, Messages {
String innerClassName = innerClassInfo.getName();
classWriter.println("// inner class " + innerClassName.replaceAll("[/$]", "."));
classWriter.println();
ok = ok & writeClass(classWriter, innerClassName);
result = writeClass(classWriter, innerClassName);
if (result != EXIT_OK) return result;
}
}
return ok;
return result;
} catch (ConstantPoolException e) {
reportError("err.bad.innerclasses.attribute", className);
return false;
return EXIT_ERROR;
}
} else if (a != null) {
reportError("err.bad.innerclasses.attribute", className);
return false;
return EXIT_ERROR;
}
}
return true;
return EXIT_OK;
}
protected JavaFileObject open(String className) throws IOException {
......
......@@ -25,26 +25,25 @@
* @test
* @bug 4645152 4785453
* @summary javac compiler incorrectly inserts <clinit> when -g is specified
* @library /tools/javac/lib
* @build ToolBox
* @run compile -g ConstDebugTest.java
* @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 static final long l = 12;
public static void main(String args[]) throws Exception {
// "${TESTJAVA}${FS}bin${FS}javac" ${TESTTOOLVMOPTS} -g -d . -classpath .${PS}${TESTSRC} $1.java 2> ${TMP1}
// if "${TESTJAVA}${FS}bin${FS}javap" $1.class | grep clinit; then fail
ToolBox.JavaToolArgs javapArgs =
new ToolBox.JavaToolArgs().setAllArgs("-v",
"-classpath", System.getProperty("test.classes"), "ConstDebugTest.class");
if (ToolBox.javap(javapArgs).contains("clinit")) {
throw new AssertionError(
"javac should not create a <clinit> method for ConstDebugTest class");
ClassFile classFile = ClassFile.read(Paths.get(System.getProperty("test.classes"),
ConstDebugTest.class.getSimpleName() + ".class"));
for (Method method: classFile.methods) {
if (method.getName(classFile.constant_pool).equals("<clinit>")) {
throw new AssertionError(
"javac should not create a <clinit> method for ConstDebugTest class");
}
}
}
......
......@@ -66,8 +66,7 @@ public class JavapTaskCtorFailWithNPE {
JavaFileManager fm = JavapFileManager.create(dc, pw);
JavapTask t = new JavapTask(pw, fm, dc, null,
Arrays.asList(classToCheck.getPath()));
boolean ok = t.run();
if (!ok)
if (t.run() != 0)
throw new Error("javap failed unexpectedly");
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.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -87,11 +87,11 @@ public class T4777949 {
PrintWriter pw = new PrintWriter(sw);
JavaFileManager fm = JavapFileManager.create(dc, pw);
JavapTask t = new JavapTask(pw, fm, dc, args, classes);
boolean ok = t.run();
int ok = t.run();
List<Diagnostic<? extends JavaFileObject>> diags = dc.getDiagnostics();
if (!ok)
if (ok != 0)
error("javap failed unexpectedly");
System.err.println("args=" + args + " classes=" + classes + "\n"
......
......@@ -96,8 +96,7 @@ public class T7190862 {
PrintWriter pw = new PrintWriter(sw);
JavaFileManager fm = JavapFileManager.create(dc, pw);
JavapTask t = new JavapTask(pw, fm, dc, args, classes);
boolean ok = t.run();
if (!ok)
if (t.run() != 0)
throw new Error("javap failed unexpectedly");
List<Diagnostic<? extends JavaFileObject>> diags = dc.getDiagnostics();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册