Java Code Examples for org.objectweb.asm.MethodVisitor#visitIntInsn()

The following examples show how to use org.objectweb.asm.MethodVisitor#visitIntInsn() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: Despector   File: TernaryTests.java    License: MIT License 6 votes vote down vote up
@Test
public void testReturned() {
    TestMethodBuilder builder = new TestMethodBuilder("test_mth", "(ZI)I");
    MethodVisitor mv = builder.getGenerator();
    Label start = new Label();
    mv.visitLabel(start);
    Label end = new Label();
    Label l1 = new Label();
    mv.visitIntInsn(ILOAD, 0);
    mv.visitJumpInsn(IFEQ, l1);
    mv.visitIntInsn(BIPUSH, 6);
    mv.visitJumpInsn(GOTO, end);
    mv.visitLabel(l1);
    mv.visitInsn(ICONST_3);
    mv.visitLabel(end);
    mv.visitInsn(IRETURN);
    mv.visitLocalVariable("a", "Z", null, start, end, 0);
    mv.visitLocalVariable("i", "I", null, start, end, 1);

    String insn = TestHelper.getAsString(builder.finish(), "test_mth");
    String good = "return a ? 6 : 3;";
    Assert.assertEquals(good, insn);
}
 
Example 2
Source Project: Despector   File: MethodTests.java    License: MIT License 6 votes vote down vote up
@Test
public void testMultiNewArray() {
    TestMethodBuilder builder = new TestMethodBuilder("test_mth", "()V");
    MethodVisitor mv = builder.getGenerator();
    Label start = new Label();
    Label l1 = new Label();
    Label end = new Label();
    mv.visitLabel(start);
    mv.visitInsn(ICONST_5);
    mv.visitIntInsn(BIPUSH, 6);
    mv.visitMultiANewArrayInsn("[[I", 2);
    mv.visitVarInsn(ASTORE, 0);
    mv.visitLabel(l1);
    mv.visitInsn(RETURN);
    mv.visitLabel(end);
    mv.visitLocalVariable("a", "[[I", null, l1, end, 0);

    String insn = TestHelper.getAsString(builder.finish(), "test_mth");
    String good = "int[][] a = new int[5][6];";
    Assert.assertEquals(good, insn);
}
 
Example 3
Source Project: Despector   File: OperatorTests.java    License: MIT License 6 votes vote down vote up
@Test
public void testMultiply() {
    TestMethodBuilder builder = new TestMethodBuilder("test_mth", "(III)V");
    MethodVisitor mv = builder.getGenerator();
    Label start = new Label();
    mv.visitLabel(start);
    Label end = new Label();
    mv.visitIntInsn(ILOAD, 1);
    mv.visitIntInsn(ILOAD, 2);
    mv.visitInsn(IMUL);
    mv.visitIntInsn(ISTORE, 0);
    mv.visitLabel(end);
    mv.visitInsn(RETURN);
    mv.visitLocalVariable("i", "I", null, start, end, 0);
    mv.visitLocalVariable("a", "I", null, start, end, 1);
    mv.visitLocalVariable("b", "I", null, start, end, 2);

    String insn = KotlinTestHelper.getMethodAsString(builder.finish(), "test_mth");
    String good = "fun test_mth(i: Int, a: Int, b: Int) {\n"
            + "    i = a * b\n"
            + "}";
    Assert.assertEquals(good, insn);
}
 
Example 4
Source Project: Despector   File: OperatorTests.java    License: MIT License 6 votes vote down vote up
@Test
public void testShr() {
    TestMethodBuilder builder = new TestMethodBuilder("test_mth", "(III)V");
    MethodVisitor mv = builder.getGenerator();
    Label start = new Label();
    mv.visitLabel(start);
    Label end = new Label();
    mv.visitIntInsn(ILOAD, 1);
    mv.visitIntInsn(ILOAD, 2);
    mv.visitInsn(ISHR);
    mv.visitIntInsn(ISTORE, 0);
    mv.visitLabel(end);
    mv.visitInsn(RETURN);
    mv.visitLocalVariable("i", "I", null, start, end, 0);
    mv.visitLocalVariable("a", "I", null, start, end, 1);
    mv.visitLocalVariable("b", "I", null, start, end, 2);

    String insn = TestHelper.getAsString(builder.finish(), "test_mth");
    String good = "i = a >> b;";
    Assert.assertEquals(good, insn);
}
 
Example 5
Source Project: Despector   File: OperatorTests.java    License: MIT License 6 votes vote down vote up
@Test
public void testUshr() {
    TestMethodBuilder builder = new TestMethodBuilder("test_mth", "(III)V");
    MethodVisitor mv = builder.getGenerator();
    Label start = new Label();
    mv.visitLabel(start);
    Label end = new Label();
    mv.visitIntInsn(ILOAD, 1);
    mv.visitIntInsn(ILOAD, 2);
    mv.visitInsn(IUSHR);
    mv.visitIntInsn(ISTORE, 0);
    mv.visitLabel(end);
    mv.visitInsn(RETURN);
    mv.visitLocalVariable("i", "I", null, start, end, 0);
    mv.visitLocalVariable("a", "I", null, start, end, 1);
    mv.visitLocalVariable("b", "I", null, start, end, 2);

    String insn = TestHelper.getAsString(builder.finish(), "test_mth");
    String good = "i = a >>> b;";
    Assert.assertEquals(good, insn);
}
 
Example 6
private static void generateArrayInitialization(MethodVisitor mv, String className, String fieldName, List<Integer> values) {
    if (values.isEmpty()) {
        return;
    }
    pushIntValue(mv, values.size());
    mv.visitIntInsn(NEWARRAY, T_INT);
    int idx = 0;
    for (Integer value : values) {
        mv.visitInsn(DUP);
        pushIntValue(mv, idx);
        mv.visitLdcInsn(value);
        mv.visitInsn(IASTORE);
        idx++;
    }
    mv.visitFieldInsn(PUTSTATIC, className, fieldName, "[I");
}
 
Example 7
Source Project: Despector   File: OperatorTests.java    License: MIT License 6 votes vote down vote up
@Test
public void testRem() {
    TestMethodBuilder builder = new TestMethodBuilder("test_mth", "(III)V");
    MethodVisitor mv = builder.getGenerator();
    Label start = new Label();
    mv.visitLabel(start);
    Label end = new Label();
    mv.visitIntInsn(ILOAD, 1);
    mv.visitIntInsn(ILOAD, 2);
    mv.visitInsn(IREM);
    mv.visitIntInsn(ISTORE, 0);
    mv.visitLabel(end);
    mv.visitInsn(RETURN);
    mv.visitLocalVariable("i", "I", null, start, end, 0);
    mv.visitLocalVariable("a", "I", null, start, end, 1);
    mv.visitLocalVariable("b", "I", null, start, end, 2);

    String insn = TestHelper.getAsString(builder.finish(), "test_mth");
    String good = "i = a % b;";
    Assert.assertEquals(good, insn);
}
 
Example 8
Source Project: Despector   File: OperatorTests.java    License: MIT License 6 votes vote down vote up
@Test
public void testTypeConstant() {
    TestMethodBuilder builder = new TestMethodBuilder("test_mth", "(I)V");
    MethodVisitor mv = builder.getGenerator();
    Label start = new Label();
    mv.visitLabel(start);
    Label end = new Label();
    mv.visitLdcInsn(Type.getType("Ljava/lang/String;"));
    mv.visitIntInsn(ASTORE, 0);
    mv.visitLabel(end);
    mv.visitInsn(RETURN);
    mv.visitLocalVariable("i", "Ljava/lang/Class;", null, start, end, 0);

    String insn = TestHelper.getAsString(builder.finish(), "test_mth");
    String good = "i = String.class;";
    Assert.assertEquals(good, insn);
}
 
Example 9
Source Project: Despector   File: TernaryTests.java    License: MIT License 6 votes vote down vote up
@Test
public void testToField() {
    TestMethodBuilder builder = new TestMethodBuilder("test_mth", "(ZI)V");
    MethodVisitor mv = builder.getGenerator();
    Label start = new Label();
    mv.visitLabel(start);
    Label end = new Label();
    Label l1 = new Label();
    Label l2 = new Label();
    mv.visitIntInsn(ILOAD, 0);
    mv.visitJumpInsn(IFEQ, l1);
    mv.visitIntInsn(BIPUSH, 6);
    mv.visitJumpInsn(GOTO, l2);
    mv.visitLabel(l1);
    mv.visitInsn(ICONST_3);
    mv.visitLabel(l2);
    mv.visitFieldInsn(PUTSTATIC, THIS_TYPE.getInternalName(), "afield", "I");
    mv.visitLabel(end);
    mv.visitInsn(RETURN);
    mv.visitLocalVariable("a", "Z", null, start, end, 0);
    mv.visitLocalVariable("i", "I", null, start, end, 1);

    String insn = TestHelper.getAsString(builder.finish(), "test_mth");
    String good = "org.spongepowered.test.decompile.TernaryTests.afield = a ? 6 : 3;";
    Assert.assertEquals(good, insn);
}
 
Example 10
Source Project: Despector   File: OperatorTests.java    License: MIT License 6 votes vote down vote up
@Test
public void testShr() {
    TestMethodBuilder builder = new TestMethodBuilder("test_mth", "(III)V");
    MethodVisitor mv = builder.getGenerator();
    Label start = new Label();
    mv.visitLabel(start);
    Label end = new Label();
    mv.visitIntInsn(ILOAD, 1);
    mv.visitIntInsn(ILOAD, 2);
    mv.visitInsn(ISHR);
    mv.visitIntInsn(ISTORE, 0);
    mv.visitLabel(end);
    mv.visitInsn(RETURN);
    mv.visitLocalVariable("i", "I", null, start, end, 0);
    mv.visitLocalVariable("a", "I", null, start, end, 1);
    mv.visitLocalVariable("b", "I", null, start, end, 2);

    String insn = KotlinTestHelper.getMethodAsString(builder.finish(), "test_mth");
    String good = "fun test_mth(i: Int, a: Int, b: Int) {\n"
            + "    i = a shr b\n"
            + "}";
    Assert.assertEquals(good, insn);
}
 
Example 11
@Override
public ClassVisitor transform(ClassWriter writer) {
	return new AbstractSkinWhitelistDomainTransformer(writer) {

		@Override
		void checked(MethodVisitor mv) {
			mv.visitIntInsn(Opcodes.SIPUSH, skinWhitelist.length + 2);
			mv.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/String");
			mv.visitInsn(Opcodes.DUP);
			mv.visitInsn(Opcodes.ICONST_0);
			mv.visitLdcInsn(".minecraft.net");
			mv.visitInsn(Opcodes.AASTORE);
			mv.visitInsn(Opcodes.DUP);
			mv.visitInsn(Opcodes.ICONST_1);
			mv.visitLdcInsn(".mojang.com");
			mv.visitInsn(Opcodes.AASTORE);
			for (int i = 0; i < skinWhitelist.length; i++) {
				mv.visitInsn(Opcodes.DUP);
				mv.visitIntInsn(Opcodes.SIPUSH, i + 2);
				mv.visitLdcInsn(skinWhitelist[i]);
				mv.visitInsn(Opcodes.AASTORE);
			}
		}
	};
}
 
Example 12
Source Project: Despector   File: OperatorTests.java    License: MIT License 6 votes vote down vote up
@Test
public void testShl() {
    TestMethodBuilder builder = new TestMethodBuilder("test_mth", "(III)V");
    MethodVisitor mv = builder.getGenerator();
    Label start = new Label();
    mv.visitLabel(start);
    Label end = new Label();
    mv.visitIntInsn(ILOAD, 1);
    mv.visitIntInsn(ILOAD, 2);
    mv.visitInsn(ISHL);
    mv.visitIntInsn(ISTORE, 0);
    mv.visitLabel(end);
    mv.visitInsn(RETURN);
    mv.visitLocalVariable("i", "I", null, start, end, 0);
    mv.visitLocalVariable("a", "I", null, start, end, 1);
    mv.visitLocalVariable("b", "I", null, start, end, 2);

    String insn = TestHelper.getAsString(builder.finish(), "test_mth");
    String good = "i = a << b;";
    Assert.assertEquals(good, insn);
}
 
Example 13
Source Project: Despector   File: BytecodeIntConstantEmitter.java    License: MIT License 5 votes vote down vote up
@Override
public void emit(BytecodeEmitterContext ctx, IntConstant arg, TypeSignature type) {
    MethodVisitor mv = ctx.getMethodVisitor();
    int val = arg.getConstant();
    if (val == -1) {
        mv.visitInsn(Opcodes.ICONST_M1);
    } else if (val == 0) {
        mv.visitInsn(Opcodes.ICONST_0);
    } else if (val == 1) {
        mv.visitInsn(Opcodes.ICONST_1);
    } else if (val == 2) {
        mv.visitInsn(Opcodes.ICONST_2);
    } else if (val == 3) {
        mv.visitInsn(Opcodes.ICONST_3);
    } else if (val == 4) {
        mv.visitInsn(Opcodes.ICONST_4);
    } else if (val == 5) {
        mv.visitInsn(Opcodes.ICONST_5);
    } else if (val <= Byte.MAX_VALUE && val >= Byte.MIN_VALUE) {
        mv.visitIntInsn(Opcodes.BIPUSH, val);
    } else if (val <= Short.MAX_VALUE && val >= Short.MIN_VALUE) {
        mv.visitIntInsn(Opcodes.SIPUSH, val);
    } else {
        mv.visitLdcInsn(val);
    }
    ctx.updateStack(1);
}
 
Example 14
Source Project: Concurnas   File: InstructionAdapter.java    License: MIT License 5 votes vote down vote up
/**
 * Generates the instruction to create and push on the stack an array of the given type.
 *
 * @param methodVisitor the method visitor to use to generate the instruction.
 * @param type an array Type.
 */
static void newarray(final MethodVisitor methodVisitor, final Type type) {
  int arrayType;
  switch (type.getSort()) {
    case Type.BOOLEAN:
      arrayType = Opcodes.T_BOOLEAN;
      break;
    case Type.CHAR:
      arrayType = Opcodes.T_CHAR;
      break;
    case Type.BYTE:
      arrayType = Opcodes.T_BYTE;
      break;
    case Type.SHORT:
      arrayType = Opcodes.T_SHORT;
      break;
    case Type.INT:
      arrayType = Opcodes.T_INT;
      break;
    case Type.FLOAT:
      arrayType = Opcodes.T_FLOAT;
      break;
    case Type.LONG:
      arrayType = Opcodes.T_LONG;
      break;
    case Type.DOUBLE:
      arrayType = Opcodes.T_DOUBLE;
      break;
    default:
      methodVisitor.visitTypeInsn(Opcodes.ANEWARRAY, type.getInternalName());
      return;
  }
  methodVisitor.visitIntInsn(Opcodes.NEWARRAY, arrayType);
}
 
Example 15
private void packInt(MethodVisitor visitor, int value) {
	if (value >= -1 && value <= 5) {
		visitor.visitInsn(Opcodes.ICONST_M1 + (value + 1));
	} else if (value >= Byte.MIN_VALUE && value <= Byte.MAX_VALUE) {
		visitor.visitIntInsn(Opcodes.BIPUSH, value);
	} else if (value >= Short.MIN_VALUE && value <= Short.MAX_VALUE) {
		visitor.visitIntInsn(Opcodes.SIPUSH, value);
	} else {
		visitor.visitLdcInsn(value);
	}
}
 
Example 16
@Override
public void accept(final MethodVisitor methodVisitor) {
	methodVisitor.visitIntInsn(opcode, operand);
	acceptAnnotations(methodVisitor);
}
 
Example 17
Source Project: AnoleFix   File: InstantRunTransform.java    License: MIT License 4 votes vote down vote up
/**
 * Use asm to generate a concrete subclass of the AppPathLoaderImpl class.
 * It only implements one method :
 * String[] getPatchedClasses();
 * <p>
 * The method is supposed to return the list of classes that were patched in this iteration.
 * This will be used by the InstantRun runtime to load all patched classes and register them
 * as overrides on the original classes.2 class files.
 *
 * @param patchFileContents list of patched class names.
 * @param outputDir         output directory where to generate the .class file in.
 * @return the generated .class files
 */
public static File writePatchFileContents(
        List<String> patchFileContents, File outputDir) {

    ClassWriter cw = new ClassWriter(0);
    MethodVisitor mv;

    cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER,
            IncrementalVisitor.APP_PATCHES_LOADER_IMPL, null,
            IncrementalVisitor.ABSTRACT_PATCHES_LOADER_IMPL, null);

    {
        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL,
                IncrementalVisitor.ABSTRACT_PATCHES_LOADER_IMPL,
                "<init>", "()V", false);
        mv.visitInsn(Opcodes.RETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(Opcodes.ACC_PUBLIC,
                "getPatchedClasses", "()[Ljava/lang/String;", null, null);
        mv.visitCode();
        mv.visitIntInsn(Opcodes.BIPUSH, patchFileContents.size());
        mv.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/String");
        for (int index = 0; index < patchFileContents.size(); index++) {
            mv.visitInsn(Opcodes.DUP);
            mv.visitIntInsn(Opcodes.BIPUSH, index);
            mv.visitLdcInsn(patchFileContents.get(index));
            mv.visitInsn(Opcodes.AASTORE);
        }
        mv.visitInsn(Opcodes.ARETURN);
        mv.visitMaxs(4, 1);
        mv.visitEnd();
    }
    cw.visitEnd();

    byte[] classBytes = cw.toByteArray();
    File outputFile = new File(outputDir, IncrementalVisitor.APP_PATCHES_LOADER_IMPL + ".class");
    try {
        Files.createParentDirs(outputFile);
        Files.write(classBytes, outputFile);
        // add the files to the list of files to be processed by subsequent tasks.
        return outputFile;
    } catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
}
 
Example 18
Source Project: groovy   File: ProxyGeneratorAdapter.java    License: Apache License 2.0 4 votes vote down vote up
protected MethodVisitor makeDelegateToClosureCall(final String name, final String desc, final String signature, final String[] exceptions, final int accessFlags) {
        MethodVisitor mv = super.visitMethod(accessFlags, name, desc, signature, exceptions);
//        TraceMethodVisitor tmv = new TraceMethodVisitor(mv);
//        mv = tmv;
        mv.visitCode();
        int stackSize = 0;
        // method body should be:
        //  this.$delegate$closure$methodName.call(new Object[] { method arguments })
        Type[] args = Type.getArgumentTypes(desc);
        int arrayStore = args.length + 1;
        BytecodeHelper.pushConstant(mv, args.length);
        mv.visitTypeInsn(ANEWARRAY, "java/lang/Object"); // stack size = 1
        stackSize = 1;
        int idx = 1;
        for (int i = 0; i < args.length; i++) {
            Type arg = args[i];
            mv.visitInsn(DUP); // stack size = 2
            BytecodeHelper.pushConstant(mv, i); // array index, stack size = 3
            // primitive types must be boxed
            boxPrimitiveType(mv, idx, arg);
            idx += registerLen(arg);
            stackSize = Math.max(4, 3 + registerLen(arg));
            mv.visitInsn(AASTORE); // store value into array
        }
        mv.visitVarInsn(ASTORE, arrayStore); // store array
        int arrayIndex = arrayStore;
        mv.visitVarInsn(ALOAD, 0); // load this
        mv.visitFieldInsn(GETFIELD, proxyName, CLOSURES_MAP_FIELD, "Ljava/util/Map;"); // load closure map
        mv.visitLdcInsn(name); // load method name
        mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Map", "get", "(Ljava/lang/Object;)Ljava/lang/Object;", true);
        arrayStore++;
        mv.visitVarInsn(ASTORE, arrayStore);
        // if null, test if wildcard exists
        Label notNull = new Label();
        mv.visitIntInsn(ALOAD, arrayStore);
        mv.visitJumpInsn(IFNONNULL, notNull);
        mv.visitVarInsn(ALOAD, 0); // load this
        mv.visitFieldInsn(GETFIELD, proxyName, CLOSURES_MAP_FIELD, "Ljava/util/Map;"); // load closure map
        mv.visitLdcInsn("*"); // load wildcard
        mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Map", "get", "(Ljava/lang/Object;)Ljava/lang/Object;", true);
        mv.visitVarInsn(ASTORE, arrayStore);
        mv.visitLabel(notNull);
        mv.visitVarInsn(ALOAD, arrayStore);
        mv.visitMethodInsn(INVOKESTATIC, BytecodeHelper.getClassInternalName(this.getClass()), "ensureClosure", "(Ljava/lang/Object;)Lgroovy/lang/Closure;", false);
        mv.visitVarInsn(ALOAD, arrayIndex); // load argument array
        stackSize++;
        mv.visitMethodInsn(INVOKEVIRTUAL, "groovy/lang/Closure", "call", "([Ljava/lang/Object;)Ljava/lang/Object;", false); // call closure
        unwrapResult(mv, desc);
        mv.visitMaxs(stackSize, arrayStore + 1);
        mv.visitEnd();
//        System.out.println("tmv.getText() = " + tmv.getText());
        return null;
    }
 
Example 19
@Override
public void accept(MethodVisitor visitor)
{
	visitor.visitIntInsn(this.getType().getCode(), b);
}
 
Example 20
Source Project: byte-buddy   File: IntegerConstant.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Size apply(MethodVisitor methodVisitor, Implementation.Context implementationContext) {
    methodVisitor.visitIntInsn(Opcodes.SIPUSH, value);
    return SIZE;
}