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