Mon, 30 Mar 2015 14:17:35 -0700
Added tag jdk8u52-b06 for changeset 779397f90251
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 if (t.run() != 0)
100 throw new Error("javap failed unexpectedly");
102 List<Diagnostic<? extends JavaFileObject>> diags = dc.getDiagnostics();
103 for (Diagnostic<? extends JavaFileObject> d: diags) {
104 if (d.getKind() == Diagnostic.Kind.ERROR)
105 throw new Error(d.getMessage(Locale.ENGLISH));
106 }
107 return sw.toString();
109 }
111 private String createWideLocalSource(String type, int numberOfVars) {
112 String result = " " + type + " x0 = 0;\n";
113 for (int i = 1; i < numberOfVars; i++) {
114 result += " " + type + " x" + i + " = x" + (i - 1) + " + 1;\n";
115 }
116 return result;
117 }
119 private String createWideLocalSourceForObject(int numberOfVars) {
120 String result = " Object x0 = new Object();\n";
121 for (int i = 1; i < numberOfVars; i++) {
122 result += " Object x" + i + " = x0;\n";
123 }
124 return result;
125 }
127 private String createIincSource() {
128 return " int i = 0;\n"
129 + " i += 1;\n"
130 + " i += 51;\n"
131 + " i += 101;\n"
132 + " i += 151;\n";
133 }
135 class JavaSource extends SimpleJavaFileObject {
137 String template = "class Test {\n" +
138 " public static void main(String[] args)\n" +
139 " {\n" +
140 " #C" +
141 " }\n" +
142 "}";
144 String source;
146 public JavaSource(String code) {
147 super(URI.create("Test.java"), JavaFileObject.Kind.SOURCE);
148 source = template.replaceAll("#C", code);
149 }
151 @Override
152 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
153 return source;
154 }
155 }
156 }