Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/test/tools/javap/T7190862.java
32285 views
1/*2* @test /nodynamiccopyright/3* @bug 7190862 71097474* @summary javap shows an incorrect type for operands if the 'wide' prefix is used5*/67import com.sun.source.util.JavacTask;8import com.sun.tools.javap.JavapFileManager;9import com.sun.tools.javap.JavapTask;10import java.io.PrintWriter;11import java.io.StringWriter;12import java.net.URI;13import java.util.Arrays;14import java.util.List;15import java.util.Locale;16import javax.tools.Diagnostic;17import javax.tools.DiagnosticCollector;18import javax.tools.JavaCompiler;19import javax.tools.JavaFileManager;20import javax.tools.JavaFileObject;21import javax.tools.SimpleJavaFileObject;22import javax.tools.ToolProvider;2324public class T7190862 {2526enum TypeWideInstructionMap {27INT("int", new String[]{"istore_w", "iload_w"}),28LONG("long", new String[]{"lstore_w", "lload_w"}),29FLOAT("float", new String[]{"fstore_w", "fload_w"}),30DOUBLE("double", new String[]{"dstore_w", "dload_w"}),31OBJECT("Object", new String[]{"astore_w", "aload_w"});3233String type;34String[] instructions;3536TypeWideInstructionMap(String type, String[] instructions) {37this.type = type;38this.instructions = instructions;39}40}4142JavaSource source;4344public static void main(String[] args) {45JavaCompiler comp = ToolProvider.getSystemJavaCompiler();46new T7190862().run(comp);47}4849private void run(JavaCompiler comp) {50String code;51for (TypeWideInstructionMap typeInstructionMap: TypeWideInstructionMap.values()) {52if (typeInstructionMap != TypeWideInstructionMap.OBJECT) {53code = createWideLocalSource(typeInstructionMap.type, 300);54} else {55code = createWideLocalSourceForObject(300);56}57source = new JavaSource(code);58compile(comp);59check(typeInstructionMap.instructions);60}6162//an extra test for the iinc instruction63code = createIincSource();64source = new JavaSource(code);65compile(comp);66check(new String[]{"iinc_w"});67}6869private void compile(JavaCompiler comp) {70JavacTask ct = (JavacTask)comp.getTask(null, null, null, null, null, Arrays.asList(source));71try {72if (!ct.call()) {73throw new AssertionError("Error thrown when compiling the following source:\n" + source.getCharContent(true));74}75} catch (Throwable ex) {76throw new AssertionError("Error thrown when compiling the following source:\n" + source.getCharContent(true));77}78}7980private void check(String[] instructions) {81String out = javap(Arrays.asList("-c"), Arrays.asList("Test.class"));82for (String line: out.split(System.getProperty("line.separator"))) {83line = line.trim();84for (String instruction: instructions) {85if (line.contains(instruction) && line.contains("#")) {86throw new Error("incorrect type for operands for instruction " + instruction);87}88}89}90}9192private String javap(List<String> args, List<String> classes) {93DiagnosticCollector<JavaFileObject> dc = new DiagnosticCollector<JavaFileObject>();94StringWriter sw = new StringWriter();95PrintWriter pw = new PrintWriter(sw);96JavaFileManager fm = JavapFileManager.create(dc, pw);97JavapTask t = new JavapTask(pw, fm, dc, args, classes);98if (t.run() != 0)99throw new Error("javap failed unexpectedly");100101List<Diagnostic<? extends JavaFileObject>> diags = dc.getDiagnostics();102for (Diagnostic<? extends JavaFileObject> d: diags) {103if (d.getKind() == Diagnostic.Kind.ERROR)104throw new Error(d.getMessage(Locale.ENGLISH));105}106return sw.toString();107108}109110private String createWideLocalSource(String type, int numberOfVars) {111String result = " " + type + " x0 = 0;\n";112for (int i = 1; i < numberOfVars; i++) {113result += " " + type + " x" + i + " = x" + (i - 1) + " + 1;\n";114}115return result;116}117118private String createWideLocalSourceForObject(int numberOfVars) {119String result = " Object x0 = new Object();\n";120for (int i = 1; i < numberOfVars; i++) {121result += " Object x" + i + " = x0;\n";122}123return result;124}125126private String createIincSource() {127return " int i = 0;\n"128+ " i += 1;\n"129+ " i += 51;\n"130+ " i += 101;\n"131+ " i += 151;\n";132}133134class JavaSource extends SimpleJavaFileObject {135136String template = "class Test {\n" +137" public static void main(String[] args)\n" +138" {\n" +139" #C" +140" }\n" +141"}";142143String source;144145public JavaSource(String code) {146super(URI.create("Test.java"), JavaFileObject.Kind.SOURCE);147source = template.replaceAll("#C", code);148}149150@Override151public CharSequence getCharContent(boolean ignoreEncodingErrors) {152return source;153}154}155}156157158