test/tools/javap/T7190862.java

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

aoqi@0 1
aoqi@0 2 /*
aoqi@0 3 * @test /nodynamiccopyright/
aoqi@0 4 * @bug 7190862 7109747
aoqi@0 5 * @summary javap shows an incorrect type for operands if the 'wide' prefix is used
aoqi@0 6 */
aoqi@0 7
aoqi@0 8 import com.sun.source.util.JavacTask;
aoqi@0 9 import com.sun.tools.javap.JavapFileManager;
aoqi@0 10 import com.sun.tools.javap.JavapTask;
aoqi@0 11 import java.io.PrintWriter;
aoqi@0 12 import java.io.StringWriter;
aoqi@0 13 import java.net.URI;
aoqi@0 14 import java.util.Arrays;
aoqi@0 15 import java.util.List;
aoqi@0 16 import java.util.Locale;
aoqi@0 17 import javax.tools.Diagnostic;
aoqi@0 18 import javax.tools.DiagnosticCollector;
aoqi@0 19 import javax.tools.JavaCompiler;
aoqi@0 20 import javax.tools.JavaFileManager;
aoqi@0 21 import javax.tools.JavaFileObject;
aoqi@0 22 import javax.tools.SimpleJavaFileObject;
aoqi@0 23 import javax.tools.ToolProvider;
aoqi@0 24
aoqi@0 25 public class T7190862 {
aoqi@0 26
aoqi@0 27 enum TypeWideInstructionMap {
aoqi@0 28 INT("int", new String[]{"istore_w", "iload_w"}),
aoqi@0 29 LONG("long", new String[]{"lstore_w", "lload_w"}),
aoqi@0 30 FLOAT("float", new String[]{"fstore_w", "fload_w"}),
aoqi@0 31 DOUBLE("double", new String[]{"dstore_w", "dload_w"}),
aoqi@0 32 OBJECT("Object", new String[]{"astore_w", "aload_w"});
aoqi@0 33
aoqi@0 34 String type;
aoqi@0 35 String[] instructions;
aoqi@0 36
aoqi@0 37 TypeWideInstructionMap(String type, String[] instructions) {
aoqi@0 38 this.type = type;
aoqi@0 39 this.instructions = instructions;
aoqi@0 40 }
aoqi@0 41 }
aoqi@0 42
aoqi@0 43 JavaSource source;
aoqi@0 44
aoqi@0 45 public static void main(String[] args) {
aoqi@0 46 JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
aoqi@0 47 new T7190862().run(comp);
aoqi@0 48 }
aoqi@0 49
aoqi@0 50 private void run(JavaCompiler comp) {
aoqi@0 51 String code;
aoqi@0 52 for (TypeWideInstructionMap typeInstructionMap: TypeWideInstructionMap.values()) {
aoqi@0 53 if (typeInstructionMap != TypeWideInstructionMap.OBJECT) {
aoqi@0 54 code = createWideLocalSource(typeInstructionMap.type, 300);
aoqi@0 55 } else {
aoqi@0 56 code = createWideLocalSourceForObject(300);
aoqi@0 57 }
aoqi@0 58 source = new JavaSource(code);
aoqi@0 59 compile(comp);
aoqi@0 60 check(typeInstructionMap.instructions);
aoqi@0 61 }
aoqi@0 62
aoqi@0 63 //an extra test for the iinc instruction
aoqi@0 64 code = createIincSource();
aoqi@0 65 source = new JavaSource(code);
aoqi@0 66 compile(comp);
aoqi@0 67 check(new String[]{"iinc_w"});
aoqi@0 68 }
aoqi@0 69
aoqi@0 70 private void compile(JavaCompiler comp) {
aoqi@0 71 JavacTask ct = (JavacTask)comp.getTask(null, null, null, null, null, Arrays.asList(source));
aoqi@0 72 try {
aoqi@0 73 if (!ct.call()) {
aoqi@0 74 throw new AssertionError("Error thrown when compiling the following source:\n" + source.getCharContent(true));
aoqi@0 75 }
aoqi@0 76 } catch (Throwable ex) {
aoqi@0 77 throw new AssertionError("Error thrown when compiling the following source:\n" + source.getCharContent(true));
aoqi@0 78 }
aoqi@0 79 }
aoqi@0 80
aoqi@0 81 private void check(String[] instructions) {
aoqi@0 82 String out = javap(Arrays.asList("-c"), Arrays.asList("Test.class"));
aoqi@0 83 for (String line: out.split(System.getProperty("line.separator"))) {
aoqi@0 84 line = line.trim();
aoqi@0 85 for (String instruction: instructions) {
aoqi@0 86 if (line.contains(instruction) && line.contains("#")) {
aoqi@0 87 throw new Error("incorrect type for operands for instruction " + instruction);
aoqi@0 88 }
aoqi@0 89 }
aoqi@0 90 }
aoqi@0 91 }
aoqi@0 92
aoqi@0 93 private String javap(List<String> args, List<String> classes) {
aoqi@0 94 DiagnosticCollector<JavaFileObject> dc = new DiagnosticCollector<JavaFileObject>();
aoqi@0 95 StringWriter sw = new StringWriter();
aoqi@0 96 PrintWriter pw = new PrintWriter(sw);
aoqi@0 97 JavaFileManager fm = JavapFileManager.create(dc, pw);
aoqi@0 98 JavapTask t = new JavapTask(pw, fm, dc, args, classes);
aoqi@0 99 if (t.run() != 0)
aoqi@0 100 throw new Error("javap failed unexpectedly");
aoqi@0 101
aoqi@0 102 List<Diagnostic<? extends JavaFileObject>> diags = dc.getDiagnostics();
aoqi@0 103 for (Diagnostic<? extends JavaFileObject> d: diags) {
aoqi@0 104 if (d.getKind() == Diagnostic.Kind.ERROR)
aoqi@0 105 throw new Error(d.getMessage(Locale.ENGLISH));
aoqi@0 106 }
aoqi@0 107 return sw.toString();
aoqi@0 108
aoqi@0 109 }
aoqi@0 110
aoqi@0 111 private String createWideLocalSource(String type, int numberOfVars) {
aoqi@0 112 String result = " " + type + " x0 = 0;\n";
aoqi@0 113 for (int i = 1; i < numberOfVars; i++) {
aoqi@0 114 result += " " + type + " x" + i + " = x" + (i - 1) + " + 1;\n";
aoqi@0 115 }
aoqi@0 116 return result;
aoqi@0 117 }
aoqi@0 118
aoqi@0 119 private String createWideLocalSourceForObject(int numberOfVars) {
aoqi@0 120 String result = " Object x0 = new Object();\n";
aoqi@0 121 for (int i = 1; i < numberOfVars; i++) {
aoqi@0 122 result += " Object x" + i + " = x0;\n";
aoqi@0 123 }
aoqi@0 124 return result;
aoqi@0 125 }
aoqi@0 126
aoqi@0 127 private String createIincSource() {
aoqi@0 128 return " int i = 0;\n"
aoqi@0 129 + " i += 1;\n"
aoqi@0 130 + " i += 51;\n"
aoqi@0 131 + " i += 101;\n"
aoqi@0 132 + " i += 151;\n";
aoqi@0 133 }
aoqi@0 134
aoqi@0 135 class JavaSource extends SimpleJavaFileObject {
aoqi@0 136
aoqi@0 137 String template = "class Test {\n" +
aoqi@0 138 " public static void main(String[] args)\n" +
aoqi@0 139 " {\n" +
aoqi@0 140 " #C" +
aoqi@0 141 " }\n" +
aoqi@0 142 "}";
aoqi@0 143
aoqi@0 144 String source;
aoqi@0 145
aoqi@0 146 public JavaSource(String code) {
aoqi@0 147 super(URI.create("Test.java"), JavaFileObject.Kind.SOURCE);
aoqi@0 148 source = template.replaceAll("#C", code);
aoqi@0 149 }
aoqi@0 150
aoqi@0 151 @Override
aoqi@0 152 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
aoqi@0 153 return source;
aoqi@0 154 }
aoqi@0 155 }
aoqi@0 156 }

mercurial