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

The following examples show how to use org.objectweb.asm.MethodVisitor#visitInvokeDynamicInsn() . 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
@Override
void compile(final MethodVisitor mv) {
    // compiles e1
    e1.compile(mv);
    // tests if e1 is false
    mv.visitInsn(DUP);
    // convert to a boolean
    mv.visitInvokeDynamicInsn("asBoolean", "(Ljava/lang/Object;)Z", UNARY);

    Label end = new Label();
    mv.visitJumpInsn(IFEQ, end);
    // case where e1 is true : e1 && e2 is equal to e2
    mv.visitInsn(POP);
    e2.compile(mv);

    // if e1 is false, e1 && e2 is equal to e1:
    // we jump directly to this label, without evaluating e2
    mv.visitLabel(end);
}
 
Example 2
@Override
void compile(final MethodVisitor mv) {
    // compiles e1
    e1.compile(mv);
    // tests if e1 is true
    mv.visitInsn(DUP);
    // convert to a boolean
    mv.visitInvokeDynamicInsn("asBoolean", "(Ljava/lang/Object;)Z", UNARY);
    Label end = new Label();
    mv.visitJumpInsn(IFNE, end);
    // case where e1 is false : e1 || e2 is equal to e2
    mv.visitInsn(POP);
    e2.compile(mv);
    // if e1 is true, e1 || e2 is equal to e1:
    // we jump directly to this label, without evaluating e2
    mv.visitLabel(end);
}
 
Example 3
@Override
public void generate(CodeEmitter code) {
    MethodVisitor mv = code.getMethodVisitor();
    Label isNull = new Label();
    TypeWidget mathType = getType().unboxed();

    if (!mathType.isPrimitive()) {
        mv.visitFieldInsn(Opcodes.GETSTATIC, Type.getInternalName(Maths.class), "INSTANCE", Type.getDescriptor(Maths.class));
    }

    boolean maybeNull = code.cast(mathType, leftExpr.getType(), isNull);
    // compute the result
    if (mathType.isPrimitive()) {
        mv.visitInsn(mathType.getJVMType().getOpcode(Opcodes.INEG));
    } else if (mathType.getValueCoreType() == YQLCoreType.ANY) {
        String desc = Type.getMethodDescriptor(getType().getJVMType(),
                Type.getType(Maths.class),
                leftExpr.getType().getJVMType());
        mv.visitInvokeDynamicInsn("dyn:callMethod:negate", desc, Dynamic.H_DYNALIB_BOOTSTRAP);
    } else {
        throw new ProgramCompileException(loc, "Math operation NEGATE is not defined for type %s", mathType.getJVMType());
    }

    if (!getType().isPrimitive() && mathType.isPrimitive()) {
        code.box(mathType);
    }

    if (maybeNull) {
        Label done = new Label();
        mv.visitJumpInsn(Opcodes.GOTO, done);
        mv.visitLabel(isNull);
        mv.visitInsn(Opcodes.ACONST_NULL);
        mv.visitLabel(done);
    }
}
 
Example 4
@Override
void compile(final MethodVisitor mv) {
    if (value instanceof String) {
        mv.visitLdcInsn(value);
        return;
    }

    // instead of pushing the constant and then box it, we use
    // invokedynamic with the constant as bootstrap argument. The
    // boxing will be performed by the VM when calling the bootstrap
    // method
    mv.visitInvokeDynamicInsn("cst", "()Ljava/lang/Object;", CST, value);
}
 
Example 5
@Override
void compile(final MethodVisitor mv) {
    // compiles e1, e2, and adds an instruction to add the two values
    e1.compile(mv);
    e2.compile(mv);
    mv.visitInvokeDynamicInsn(
            "add", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", BINARY);
}
 
Example 6
@Override
void compile(final MethodVisitor mv) {
    // compiles e1, e2, and adds an instruction to multiply the two
    // values
    e1.compile(mv);
    e2.compile(mv);
    mv.visitInvokeDynamicInsn(
            "mul", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", BINARY);
}
 
Example 7
Source Project: Despector   File: LambdaTest.java    License: MIT License 5 votes vote down vote up
@Test
public void testLambda() {
    TestMethodBuilder builder = new TestMethodBuilder("test_mth", "()V");
    MethodVisitor mv = builder.getGenerator();
    Label start = new Label();
    mv.visitLabel(start);
    if (TestHelper.IS_ECLIPSE) {
        mv.visitInvokeDynamicInsn("run", "()Ljava/lang/Runnable;",
                new Handle(H_INVOKESTATIC, "java/lang/invoke/LambdaMetafactory", "metafactory",
                        "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;"),
                new Object[] {Type.getType("()V"), new Handle(H_INVOKESTATIC, "org/spongepowered/test/ast/LambdaTest", "lambda$0", "()V"),
                        Type.getType("()V")});
    } else {
        // javac adds an extra decoration to the lambda method name with the
        // name of the calling method
        mv.visitInvokeDynamicInsn("run", "()Ljava/lang/Runnable;",
                new Handle(H_INVOKESTATIC, "java/lang/invoke/LambdaMetafactory", "metafactory",
                        "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;"),
                new Object[] {Type.getType("()V"),
                        new Handle(H_INVOKESTATIC, "org/spongepowered/test/ast/LambdaTest", "lambda$test_lambda$0", "()V"),
                        Type.getType("()V")});
    }
    mv.visitVarInsn(ASTORE, 1);
    Label l1 = new Label();
    mv.visitLabel(l1);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEINTERFACE, "java/lang/Runnable", "run", "()V", true);
    mv.visitInsn(RETURN);
    Label end = new Label();
    mv.visitLabel(end);
    mv.visitLocalVariable("this", "Lorg/spongepowered/test/ast/LambdaTest;", null, start, end, 0);
    mv.visitLocalVariable("r", "Ljava/lang/Runnable;", null, l1, end, 1);

    String insn = TestHelper.getAsString(builder.finish(), "test_mth");
    String good = "Runnable r = () -> System.out.println(\"Hello World\");\n"
            + "r.run();";
    Assert.assertEquals(good, insn);
}
 
Example 8
Source Project: Despector   File: LambdaTest.java    License: MIT License 5 votes vote down vote up
@Test
public void testConsumer() {
    TestMethodBuilder builder = new TestMethodBuilder("test_mth", "()V");
    MethodVisitor mv = builder.getGenerator();
    Label start = new Label();
    mv.visitLabel(start);
    if (TestHelper.IS_ECLIPSE) {
        mv.visitInvokeDynamicInsn("accept", "()Ljava/util/function/Consumer;",
                new Handle(H_INVOKESTATIC, "java/lang/invoke/LambdaMetafactory", "metafactory",
                        "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;"),
                new Object[] {Type.getType("(Ljava/lang/Object;)V"),
                        new Handle(H_INVOKESTATIC, "org/spongepowered/test/ast/LambdaTest", "lambda$1", "(Ljava/lang/Object;)V"),
                        Type.getType("(Ljava/lang/Object;)V")});
    } else {
        // javac adds an extra decoration to the lambda method name with the
        // name of the calling method
        mv.visitInvokeDynamicInsn("accept", "()Ljava/util/function/Consumer;",
                new Handle(H_INVOKESTATIC, "java/lang/invoke/LambdaMetafactory", "metafactory",
                        "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;"),
                new Object[] {Type.getType("(Ljava/lang/Object;)V"),
                        new Handle(H_INVOKESTATIC, "org/spongepowered/test/ast/LambdaTest", "lambda$test_consumer$1", "(Ljava/lang/Object;)V"),
                        Type.getType("(Ljava/lang/Object;)V")});
    }
    mv.visitVarInsn(ASTORE, 1);
    Label l1 = new Label();
    mv.visitLabel(l1);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitInsn(ACONST_NULL);
    mv.visitMethodInsn(INVOKEINTERFACE, "java/util/function/Consumer", "accept", "(Ljava/lang/Object;)V", true);
    mv.visitInsn(RETURN);
    Label end = new Label();
    mv.visitLabel(end);
    mv.visitLocalVariable("this", "Lorg/spongepowered/test/ast/LambdaTest;", null, start, end, 0);
    mv.visitLocalVariable("r", "Ljava/util/function/Consumer;", "Ljava/util/function/Consumer<Ljava/lang/Object;>;", l1, end, 1);

    String insn = TestHelper.getAsString(builder.finish(), "test_mth");
    String good = "java.util.function.Consumer<Object> r = (obj) -> System.out.println(\"Hello World\");\n"
            + "r.accept(null);";
    Assert.assertEquals(good, insn);
}
 
Example 9
Source Project: uima-uimaj   File: JCasClassConversion.java    License: Apache License 2.0 5 votes vote down vote up
void genv3_setter(String name, String type) {
  MethodVisitor mv = cv.visitMethod(ACC_PUBLIC, "set" + up1st(name), "(" + type + ")V", null, null);
  mv.visitCode();
  mv.visitVarInsn(ALOAD, 0);
  mv.visitFieldInsn(GETFIELD, classname, "_casView", "Lorg/apache/uima/cas/impl/CASImpl;");
  mv.visitVarInsn(ALOAD, 0);
  mv.visitFieldInsn(GETSTATIC, classname, "_FI_" + name, "I");
  mv.visitVarInsn(ALOAD, 0);
  mv.visitVarInsn(ILOAD, 1);
  mv.visitInvokeDynamicInsn("run",
      "(Lorg/apache/uima/jcas/tcas/Annotation;I)Ljava/lang/Runnable;",
      new Handle(Opcodes.H_INVOKESTATIC, "java/lang/invoke/LambdaMetafactory", "metafactory",
          "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;"
              + "Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;"),
      new Object[] {
          Type.getType("()V"), 
          new Handle(Opcodes.H_INVOKESPECIAL,
                     "org/apache/uima/jcas/tcas/Annotation", 
                     "lambda$0", 
                     "(I)V"),
          Type.getType("()V") });
  mv.visitMethodInsn(INVOKEVIRTUAL, "org/apache/uima/cas/impl/CASImpl",
      "setWithCheckAndJournalJFRI", "(Lorg/apache/uima/jcas/cas/TOP;ILjava/lang/Runnable;)V",
      false);
  mv.visitInsn(RETURN);
  mv.visitMaxs(5, 2);
  mv.visitEnd(); 
}
 
Example 10
Source Project: Despector   File: LambdaTest.java    License: MIT License 5 votes vote down vote up
@Test
public void testMethodRef() {
    TestMethodBuilder builder = new TestMethodBuilder("test_mth", "(Ljava/lang/Runnable;)V");
    MethodVisitor mv = builder.getGenerator();
    Label start = new Label();
    mv.visitLabel(start);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitInvokeDynamicInsn("run", "(Ljava/lang/Runnable;)Ljava/lang/Runnable;",
            new Handle(H_INVOKESTATIC, "java/lang/invoke/LambdaMetafactory", "metafactory",
                    "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;"),
            new Object[] {Type.getType("()V"), new Handle(H_INVOKEINTERFACE, "java/lang/Runnable", "run", "()V"), Type.getType("()V")});
    mv.visitVarInsn(ASTORE, 2);
    Label l1 = new Label();
    mv.visitLabel(l1);
    mv.visitVarInsn(ALOAD, 2);
    mv.visitMethodInsn(INVOKEINTERFACE, "java/lang/Runnable", "run", "()V", true);
    mv.visitInsn(RETURN);
    Label end = new Label();
    mv.visitLabel(end);
    mv.visitLocalVariable("this", "Lorg/spongepowered/test/ast/LambdaTest;", null, start, end, 0);
    mv.visitLocalVariable("r", "Ljava/lang/Runnable;", null, start, end, 1);
    mv.visitLocalVariable("a", "Ljava/lang/Runnable;", null, l1, end, 2);

    String insn = TestHelper.getAsString(builder.finish(), "test_mth");
    String good = "Runnable a = r::run;\n"
            + "a.run();";
    Assert.assertEquals(good, insn);
}
 
Example 11
Source Project: Despector   File: LambdaTest.java    License: MIT License 5 votes vote down vote up
@Test
public void testMethodRefJavac() {
    // apparently javac chooses it add a call to r.getClass() before the
    // method ref
    TestMethodBuilder builder = new TestMethodBuilder("test_mth", "(Ljava/lang/Runnable;)V");
    MethodVisitor mv = builder.getGenerator();
    Label start = new Label();
    mv.visitLabel(start);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitInsn(DUP);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;", false);
    mv.visitInsn(POP);
    mv.visitInvokeDynamicInsn("run", "(Ljava/lang/Runnable;)Ljava/lang/Runnable;",
            new Handle(H_INVOKESTATIC, "java/lang/invoke/LambdaMetafactory", "metafactory",
                    "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;"),
            new Object[] {Type.getType("()V"), new Handle(H_INVOKEINTERFACE, "java/lang/Runnable", "run", "()V"), Type.getType("()V")});
    mv.visitVarInsn(ASTORE, 2);
    Label l1 = new Label();
    mv.visitLabel(l1);
    mv.visitVarInsn(ALOAD, 2);
    mv.visitMethodInsn(INVOKEINTERFACE, "java/lang/Runnable", "run", "()V", true);
    mv.visitInsn(RETURN);
    Label end = new Label();
    mv.visitLabel(end);
    mv.visitLocalVariable("this", "Lorg/spongepowered/test/ast/LambdaTest;", null, start, end, 0);
    mv.visitLocalVariable("r", "Ljava/lang/Runnable;", null, start, end, 1);
    mv.visitLocalVariable("a", "Ljava/lang/Runnable;", null, l1, end, 2);

    String insn = TestHelper.getAsString(builder.finish(), "test_mth");
    String good = "r.getClass();\n"
            + "Runnable a = r::run;\n"
            + "a.run();";
    Assert.assertEquals(good, insn);
}
 
Example 12
@Override
public void generate(CodeEmitter code) {
    for (BytecodeExpression arg : arguments) {
        arg.generate(code);
    }
    Type[] argTypes = new Type[arguments.size()];
    for (int i = 0; i < arguments.size(); ++i) {
        argTypes[i] = arguments.get(i).getType().getJVMType();
    }
    String desc = Type.getMethodDescriptor(returnType.getJVMType(), argTypes);
    MethodVisitor mv = code.getMethodVisitor();
    mv.visitInvokeDynamicInsn(operationName, desc, bootstrap, bootstrapArgs);
}
 
Example 13
Source Project: JReFrameworker   File: InvokeDynamicInsnNode.java    License: MIT License 4 votes vote down vote up
@Override
public void accept(final MethodVisitor methodVisitor) {
  methodVisitor.visitInvokeDynamicInsn(name, desc, bsm, bsmArgs);
  acceptAnnotations(methodVisitor);
}
 
Example 14
Source Project: Concurnas   File: InvokeDynamicInsnNode.java    License: MIT License 4 votes vote down vote up
@Override
public void accept(final MethodVisitor methodVisitor) {
  methodVisitor.visitInvokeDynamicInsn(name, desc, bsm, bsmArgs);
  acceptAnnotations(methodVisitor);
}
 
Example 15
@Override
protected void generateCallCode(MethodVisitor visitor) {
	visitor.visitInvokeDynamicInsn(boundName, getBootstrapDesc(), bootstrapMethod, bootstrapArgs);
}
 
Example 16
public static byte[] makeExample() throws Throwable {
	      ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
	      cw.visit(V1_7, ACC_PUBLIC | ACC_SUPER, "ComplexIndy", null, "java/lang/Object", null);
	      		      
	      MethodVisitor mv;
	      {
	         mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "gwtTest", "(Ljava/lang/Object;)Ljava/lang/String;", null, null);
	         mv.visitCode();
		 mv.visitVarInsn(ALOAD, 0);
	         mv.visitInvokeDynamicInsn("gwtBootstrap", "(Ljava/lang/Object;)Ljava/lang/String;",
	               new Handle(
	            		   H_INVOKESTATIC, 
	            		   "BootstrapMethods", 
	            		   "fibBootstrap",
	            		   Type.getType(
	            				 "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;"
	            		   ).getDescriptor())
	         );
	         mv.visitInsn(ARETURN);
	         mv.visitMaxs(0, 0);
	         mv.visitEnd();
	      }

	{
		mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "fibIndy", "(I)I", null, null);
		mv.visitCode();
		mv.visitVarInsn(ILOAD, 0);
		mv.visitInvokeDynamicInsn("fibBootstrap", "(I)I",
			new Handle(
				H_INVOKESTATIC, 
				"BootstrapMethods", 
				"fibBootstrap",
				Type.getType(
					"(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;"
				).getDescriptor())
			);
		mv.visitInsn(IRETURN);
		mv.visitMaxs(0, 0);
		mv.visitEnd();
	}
cw.visitEnd();
return cw.toByteArray();
}
 
Example 17
@Override public void call(MethodVisitor mv) {
    mv.visitInvokeDynamicInsn(name, signature, BSM);
}
 
Example 18
@Override
void compile(final MethodVisitor mv) {
    // compiles e, and applies 'not'
    e.compile(mv);
    mv.visitInvokeDynamicInsn("not", "(Ljava/lang/Object;)Ljava/lang/Object;", UNARY);
}
 
Example 19
Source Project: JReFrameworker   File: InvokeDynamicInsnNode.java    License: MIT License 4 votes vote down vote up
@Override
public void accept(final MethodVisitor methodVisitor) {
  methodVisitor.visitInvokeDynamicInsn(name, desc, bsm, bsmArgs);
  acceptAnnotations(methodVisitor);
}
 
Example 20
@Override
public void generate(CodeEmitter code) {
    MethodVisitor mv = code.getMethodVisitor();
    Label isNull = new Label();
    TypeWidget mathType = getType().unboxed();

    if (!mathType.isPrimitive() || op == ArithmeticOperation.POW) {
        mv.visitFieldInsn(Opcodes.GETSTATIC, Type.getInternalName(Maths.class), "INSTANCE", Type.getDescriptor(Maths.class));
        mv.visitFieldInsn(Opcodes.GETSTATIC, Type.getInternalName(ArithmeticOperation.class), op.name(), Type.getDescriptor(ArithmeticOperation.class));
    }

    CodeEmitter.Unification out = code.unifyAs(mathType, leftExpr, rightExpr, isNull, isNull, isNull);
    // now we have both sides unified as (if possible) a primitive type

    // compute the result
    if (mathType.isPrimitive()) {
        switch (op) {
            case ADD:
                mv.visitInsn(mathType.getJVMType().getOpcode(Opcodes.IADD));
                break;
            case SUB:
                mv.visitInsn(mathType.getJVMType().getOpcode(Opcodes.ISUB));
                break;
            case MULT:
                mv.visitInsn(mathType.getJVMType().getOpcode(Opcodes.IMUL));
                break;
            case DIV:
                mv.visitInsn(mathType.getJVMType().getOpcode(Opcodes.IDIV));
                break;
            case MOD:
                mv.visitInsn(mathType.getJVMType().getOpcode(Opcodes.IREM));
                break;
            case POW:
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(Maths.class), "binaryMath",
                        Type.getMethodDescriptor(mathType.getJVMType(),
                                Type.getType(ArithmeticOperation.class),
                                mathType.getJVMType(),
                                mathType.getJVMType()), false);
                break;
            default:
                throw new ProgramCompileException(loc, "Unknown BinaryMath operation: " + op);

        }
    } else if (mathType.getValueCoreType() == YQLCoreType.ANY) {
        String desc = Type.getMethodDescriptor(getType().getJVMType(),
                Type.getType(Maths.class),
                Type.getType(ArithmeticOperation.class),
                leftExpr.getType().boxed().getJVMType(),
                rightExpr.getType().boxed().getJVMType());
        mv.visitInvokeDynamicInsn("dyn:callMethod:binaryMath", desc, Dynamic.H_DYNALIB_BOOTSTRAP);
    } else {
        throw new ProgramCompileException(loc, "Math operation %s is not defined for type %s", op, mathType.getJVMType());
    }

    if (!getType().isPrimitive() && mathType.isPrimitive()) {
        code.box(mathType);
    }

    if (out.nullPossible) {
        Label done = new Label();
        mv.visitJumpInsn(Opcodes.GOTO, done);
        mv.visitLabel(isNull);
        if (!mathType.isPrimitive() || op == ArithmeticOperation.POW) {
            mv.visitInsn(Opcodes.POP2);
        }
        mv.visitInsn(Opcodes.ACONST_NULL);
        mv.visitLabel(done);
    }
}