提交 5e476a0e 编写于 作者: K ksrini

6746111: Improve pack200 error message

Reviewed-by: jrose
上级 d6986e1a
......@@ -654,8 +654,8 @@ class Attribute implements Comparable, Constants {
String layout;
public FormatException(String message,
int ctype, String name, String layout) {
super(ATTR_CONTEXT_NAME[ctype]+"."+name
+(message == null? "": (": "+message)));
super(ATTR_CONTEXT_NAME[ctype]+ " attribute \"" + name + "\"" +
(message == null? "" : (": " + message)));
this.ctype = ctype;
this.name = name;
this.layout = layout;
......
......@@ -30,6 +30,7 @@ import java.util.*;
import com.sun.java.util.jar.pack.Package.Class;
import com.sun.java.util.jar.pack.Package.InnerClass;
import com.sun.java.util.jar.pack.ConstantPool.*;
import com.sun.tools.classfile.AttributeException;
/**
* Reader for a class file that is being incorporated into a package.
......@@ -405,7 +406,7 @@ class ClassReader implements Constants {
skip(length, "unknown "+name+" attribute in "+h);
continue;
} else {
String message = "unknown in "+h;
String message = " is unknown attribute in class " + h;
throw new Attribute.FormatException(message, ctype, name,
unknownAttrCommand);
}
......@@ -434,6 +435,10 @@ class ClassReader implements Constants {
in.readFully(bytes);
a = a.addContent(bytes);
}
if (a.size() == 0 && !a.layout().isEmpty()) {
throw new ClassFormatException(name +
": attribute length cannot be zero, in " + h);
}
h.addAttribute(a);
if (verbose > 2)
Utils.log.fine("read "+a);
......
......@@ -20,12 +20,11 @@
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/*
* @test
* @bug 6982312
......@@ -34,12 +33,11 @@ import java.util.List;
* @run main AttributeTests
* @author ksrini
*/
public class AttributeTests {
public static void main(String... args) throws Exception {
test6982312();
test6746111();
}
/*
* This is an interim test, which ensures pack200 handles JSR-292 related
......@@ -72,4 +70,62 @@ public class AttributeTests {
testJar.delete();
dynJar.delete();
}
/*
* this test checks to see if we get the expected strings for output
*/
static void test6746111() throws Exception {
String pack200Cmd = Utils.getPack200Cmd();
File badAttrJar = new File(".", "badattr.jar");
Utils.copyFile(new File(Utils.TEST_SRC_DIR, "badattr.jar"), badAttrJar);
File testJar = new File(".", "test.jar");
List<String> cmds = new ArrayList<String>();
cmds.add(pack200Cmd);
cmds.add("--repack");
cmds.add("-v");
cmds.add(testJar.getAbsolutePath());
cmds.add(badAttrJar.getAbsolutePath());
List<String> output = Utils.runExec(cmds);
/*
* compare the repacked jar bit-wise, as all the files
* should be transmitted "as-is".
*/
Utils.doCompareBitWise(badAttrJar.getAbsoluteFile(), testJar.getAbsoluteFile());
String[] expectedStrings = {
"WARNING: Passing class file uncompressed due to unrecognized" +
" attribute: Foo.class",
"INFO: com.sun.java.util.jar.pack.Attribute$FormatException: " +
"class attribute \"XourceFile\": is unknown attribute " +
"in class Foo",
"INFO: com.sun.java.util.jar.pack.ClassReader$ClassFormatException: " +
"AnnotationDefault: attribute length cannot be zero, in Test.message()",
"WARNING: Passing class file uncompressed due to unknown class format: Test.class"
};
List<String> notfoundList = new ArrayList<String>();
notfoundList.addAll(Arrays.asList(expectedStrings));
// make sure the expected messages are emitted
for (String x : output) {
findString(x, notfoundList, expectedStrings);
}
if (!notfoundList.isEmpty()) {
System.out.println("Not found:");
for (String x : notfoundList) {
System.out.println(x);
}
throw new Exception("Test fails: " + notfoundList.size() +
" expected strings not found");
}
testJar.delete();
badAttrJar.delete();
}
private static void findString(String outputStr, List<String> notfoundList,
String[] expectedStrings) {
for (String y : expectedStrings) {
if (outputStr.contains(y)) {
notfoundList.remove(y);
return;
}
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册