test/tools/javap/T7190862.java

Thu, 04 Apr 2013 19:05:42 -0700

author
katleman
date
Thu, 04 Apr 2013 19:05:42 -0700
changeset 1662
4a48f3173534
parent 1428
d9fe1f80515d
child 1819
7fe655cad9b1
permissions
-rw-r--r--

Added tag jdk8-b84 for changeset cfb65ca92082

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

mercurial