Example usage for org.objectweb.asm Opcodes GETFIELD

List of usage examples for org.objectweb.asm Opcodes GETFIELD

Introduction

In this page you can find the example usage for org.objectweb.asm Opcodes GETFIELD.

Prototype

int GETFIELD

To view the source code for org.objectweb.asm Opcodes GETFIELD.

Click Source Link

Usage

From source file:org.pitest.mutationtest.engine.gregor.mutators.experimental.extended.UOIMutator2.java

License:Apache License

@Override
public void visitFieldInsn(int opcode, String owner, String name, String desc) {
    // GETFIELD I,F,L,D + B,S
    if ((opcode == Opcodes.GETFIELD)) {
        if (desc.equals("I")) {
            if (this.shouldMutate("Decremented (a--) integer field " + name)) {
                // stack  = [this]

                mv.visitInsn(Opcodes.DUP);
                // stack = [this] [this]

                mv.visitFieldInsn(opcode, owner, name, desc);
                // stack = [this] [this.field]

                mv.visitInsn(Opcodes.DUP_X1);
                // stack = [this.field] [this] [this.field]

                mv.visitInsn(Opcodes.ICONST_1);
                mv.visitInsn(Opcodes.ISUB);

                mv.visitFieldInsn(Opcodes.PUTFIELD, owner, name, desc);
                return;
            }//  ww w  .  j  a  v  a 2s.  c  om
        }
        if (desc.equals("F")) {
            if (this.shouldMutate("Decremented (a--) float field " + name)) {
                mv.visitInsn(Opcodes.DUP);
                mv.visitFieldInsn(opcode, owner, name, desc);
                mv.visitInsn(Opcodes.DUP_X1);
                mv.visitInsn(Opcodes.FCONST_1);
                mv.visitInsn(Opcodes.FSUB);
                mv.visitFieldInsn(Opcodes.PUTFIELD, owner, name, desc);
                return;
            }
        }
        if (desc.equals("J")) {
            if (this.shouldMutate("Decremented (a--) long field " + name)) {
                mv.visitInsn(Opcodes.DUP);
                mv.visitFieldInsn(opcode, owner, name, desc);
                mv.visitInsn(Opcodes.DUP2_X1);
                mv.visitInsn(Opcodes.LCONST_1);
                mv.visitInsn(Opcodes.LSUB);
                mv.visitFieldInsn(Opcodes.PUTFIELD, owner, name, desc);
                return;
            }
        }
        if (desc.equals("D")) {
            if (this.shouldMutate("Decremented (a--) double field " + name)) {
                mv.visitInsn(Opcodes.DUP);
                mv.visitFieldInsn(opcode, owner, name, desc);
                mv.visitInsn(Opcodes.DUP2_X1);
                mv.visitInsn(Opcodes.DCONST_1);
                mv.visitInsn(Opcodes.DSUB);
                mv.visitFieldInsn(Opcodes.PUTFIELD, owner, name, desc);
                return;
            }
        }
        if (desc.equals("B")) {
            if (this.shouldMutate("Decremented (a--) byte field " + name)) {
                mv.visitInsn(Opcodes.DUP);
                mv.visitFieldInsn(opcode, owner, name, desc);
                mv.visitInsn(Opcodes.DUP_X1);
                mv.visitInsn(Opcodes.ICONST_1);
                mv.visitInsn(Opcodes.ISUB);
                mv.visitInsn(Opcodes.I2B);
                mv.visitFieldInsn(Opcodes.PUTFIELD, owner, name, desc);
                return;
            }
        }
        if (desc.equals("S")) {
            if (this.shouldMutate("Decremented (a--) short field " + name)) {
                mv.visitInsn(Opcodes.DUP);
                mv.visitFieldInsn(opcode, owner, name, desc);
                mv.visitInsn(Opcodes.DUP_X1);
                mv.visitInsn(Opcodes.ICONST_1);
                mv.visitInsn(Opcodes.ISUB);
                mv.visitInsn(Opcodes.I2S);
                mv.visitFieldInsn(Opcodes.PUTFIELD, owner, name, desc);
                return;
            }
        }
    }

    // GETSTATIC I,F,L,D + B,S
    if (opcode == Opcodes.GETSTATIC) {
        if (desc.equals("I")) {
            if (this.shouldMutate("Decremented (a--) static integer field " + name)) {
                mv.visitFieldInsn(opcode, owner, name, desc);
                // stack = [this.sfield]

                mv.visitInsn(Opcodes.DUP);
                // stack = [this.sfield] [this.sfield]

                mv.visitInsn(Opcodes.ICONST_1);
                mv.visitInsn(Opcodes.ISUB);
                // stack = [this.sfield] [this.sfield -1]

                mv.visitFieldInsn(Opcodes.PUTSTATIC, owner, name, desc);
                return;
            }
        }
        if (desc.equals("F")) {
            if (this.shouldMutate("Decremented (a--) static float field " + name)) {
                mv.visitFieldInsn(opcode, owner, name, desc);
                mv.visitInsn(Opcodes.DUP);
                mv.visitInsn(Opcodes.FCONST_1);
                mv.visitInsn(Opcodes.FSUB);
                mv.visitFieldInsn(Opcodes.PUTSTATIC, owner, name, desc);
                return;
            }
        }
        if (desc.equals("J")) {
            if (this.shouldMutate("Decremented (a--) static long field " + name)) {
                mv.visitFieldInsn(opcode, owner, name, desc);
                mv.visitInsn(Opcodes.DUP2);
                mv.visitInsn(Opcodes.LCONST_1);
                mv.visitInsn(Opcodes.LSUB);
                mv.visitFieldInsn(Opcodes.PUTSTATIC, owner, name, desc);
                return;
            }
        }
        if (desc.equals("D")) {
            if (this.shouldMutate("Decremented (a--) static double field " + name)) {
                mv.visitFieldInsn(opcode, owner, name, desc);
                mv.visitInsn(Opcodes.DUP2);
                mv.visitInsn(Opcodes.DCONST_1);
                mv.visitInsn(Opcodes.DSUB);
                mv.visitFieldInsn(Opcodes.PUTSTATIC, owner, name, desc);
                return;
            }
        }
        if (desc.equals("B")) {
            if (this.shouldMutate("Decremented (a--) static byte field " + name)) {
                mv.visitFieldInsn(opcode, owner, name, desc);
                mv.visitInsn(Opcodes.DUP);
                mv.visitInsn(Opcodes.ICONST_1);
                mv.visitInsn(Opcodes.ISUB);
                mv.visitInsn(Opcodes.I2B);
                mv.visitFieldInsn(Opcodes.PUTSTATIC, owner, name, desc);
                return;
            }
        }
        if (desc.equals("S")) {
            if (this.shouldMutate("Decremented (a--) static short field " + name)) {
                mv.visitFieldInsn(opcode, owner, name, desc);
                mv.visitInsn(Opcodes.DUP);
                mv.visitInsn(Opcodes.ICONST_1);
                mv.visitInsn(Opcodes.ISUB);
                mv.visitInsn(Opcodes.I2S);
                mv.visitFieldInsn(Opcodes.PUTSTATIC, owner, name, desc);
                return;
            }
        }
    }
    mv.visitFieldInsn(opcode, owner, name, desc);
}

From source file:org.pitest.mutationtest.engine.gregor.mutators.experimental.extended.UOIMutator3.java

License:Apache License

@Override
public void visitFieldInsn(int opcode, String owner, String name, String desc) {
    // GETFIELD I,F,L,D + B,S
    if ((opcode == Opcodes.GETFIELD)) {
        if (desc.equals("I")) {
            if (this.shouldMutate("Incremented (++a) integer field " + name)) {
                // stack = [this]

                mv.visitInsn(Opcodes.DUP);
                // stack = [this] [this]

                mv.visitFieldInsn(opcode, owner, name, desc);
                // stack = [this] [this.field]

                mv.visitInsn(Opcodes.ICONST_1);
                mv.visitInsn(Opcodes.IADD);
                // stack = [this] [this.field +1]

                mv.visitInsn(Opcodes.DUP_X1);
                // stack = [this.field +1] [this] [this.field +1]

                mv.visitFieldInsn(Opcodes.PUTFIELD, owner, name, desc);
                return;
            }//from  w  ww . j  av  a 2 s .  c o m
        }
        if (desc.equals("F")) {
            if (this.shouldMutate("Incremented (++a) float field " + name)) {
                mv.visitInsn(Opcodes.DUP);
                mv.visitFieldInsn(opcode, owner, name, desc);
                mv.visitInsn(Opcodes.FCONST_1);
                mv.visitInsn(Opcodes.FADD);
                mv.visitInsn(Opcodes.DUP_X1);
                mv.visitFieldInsn(Opcodes.PUTFIELD, owner, name, desc);
                return;
            }
        }
        if (desc.equals("J")) {
            if (this.shouldMutate("Incremented (++a) long field " + name)) {
                mv.visitInsn(Opcodes.DUP);
                mv.visitFieldInsn(opcode, owner, name, desc);
                mv.visitInsn(Opcodes.LCONST_1);
                mv.visitInsn(Opcodes.LADD);
                mv.visitInsn(Opcodes.DUP2_X1);
                mv.visitFieldInsn(Opcodes.PUTFIELD, owner, name, desc);
                return;
            }
        }
        if (desc.equals("D")) {
            if (this.shouldMutate("Incremented (++a) double field " + name)) {
                mv.visitInsn(Opcodes.DUP);
                mv.visitFieldInsn(opcode, owner, name, desc);
                mv.visitInsn(Opcodes.DCONST_1);
                mv.visitInsn(Opcodes.DADD);
                mv.visitInsn(Opcodes.DUP2_X1);
                mv.visitFieldInsn(Opcodes.PUTFIELD, owner, name, desc);
                return;
            }
        }
        if (desc.equals("B")) {
            if (this.shouldMutate("Incremented (++a) byte field " + name)) {
                mv.visitInsn(Opcodes.DUP);
                mv.visitFieldInsn(opcode, owner, name, desc);
                mv.visitInsn(Opcodes.ICONST_1);
                mv.visitInsn(Opcodes.IADD);
                mv.visitInsn(Opcodes.I2B);
                mv.visitInsn(Opcodes.DUP_X1);
                mv.visitFieldInsn(Opcodes.PUTFIELD, owner, name, desc);
                return;
            }
        }
        if (desc.equals("S")) {
            if (this.shouldMutate("Incremented (++a) short field " + name)) {
                mv.visitInsn(Opcodes.DUP);
                mv.visitFieldInsn(opcode, owner, name, desc);
                mv.visitInsn(Opcodes.ICONST_1);
                mv.visitInsn(Opcodes.IADD);
                mv.visitInsn(Opcodes.I2S);
                mv.visitInsn(Opcodes.DUP_X1);
                mv.visitFieldInsn(Opcodes.PUTFIELD, owner, name, desc);
                return;
            }
        }
    }

    // GETSTATIC I,F,L,D + B,S
    if (opcode == Opcodes.GETSTATIC) {
        if (desc.equals("I")) {
            if (this.shouldMutate("Incremented (++a) static integer field " + name)) {
                mv.visitFieldInsn(opcode, owner, name, desc);
                mv.visitInsn(Opcodes.ICONST_1);
                mv.visitInsn(Opcodes.IADD);
                mv.visitInsn(Opcodes.DUP);
                mv.visitFieldInsn(Opcodes.PUTSTATIC, owner, name, desc);
                return;
            }
        }
        if (desc.equals("F")) {
            if (this.shouldMutate("Incremented (++a) static float field " + name)) {
                mv.visitFieldInsn(opcode, owner, name, desc);
                mv.visitInsn(Opcodes.FCONST_1);
                mv.visitInsn(Opcodes.FADD);
                mv.visitInsn(Opcodes.DUP);
                mv.visitFieldInsn(Opcodes.PUTSTATIC, owner, name, desc);
                return;
            }
        }
        if (desc.equals("J")) {
            if (this.shouldMutate("Incremented (++a) static long field " + name)) {
                mv.visitFieldInsn(opcode, owner, name, desc);
                mv.visitInsn(Opcodes.LCONST_1);
                mv.visitInsn(Opcodes.LADD);
                mv.visitInsn(Opcodes.DUP2);
                mv.visitFieldInsn(Opcodes.PUTSTATIC, owner, name, desc);
                return;
            }
        }
        if (desc.equals("D")) {
            if (this.shouldMutate("Incremented (++a) static double field " + name)) {
                mv.visitFieldInsn(opcode, owner, name, desc);
                mv.visitInsn(Opcodes.DCONST_1);
                mv.visitInsn(Opcodes.DADD);
                mv.visitInsn(Opcodes.DUP2);
                mv.visitFieldInsn(Opcodes.PUTSTATIC, owner, name, desc);
                return;
            }
        }
        if (desc.equals("B")) {
            if (this.shouldMutate("Incremented (++a) static byte field " + name)) {
                mv.visitFieldInsn(opcode, owner, name, desc);
                mv.visitInsn(Opcodes.ICONST_1);
                mv.visitInsn(Opcodes.IADD);
                mv.visitInsn(Opcodes.I2B);
                mv.visitInsn(Opcodes.DUP);
                mv.visitFieldInsn(Opcodes.PUTSTATIC, owner, name, desc);
                return;
            }
        }
        if (desc.equals("S")) {
            if (this.shouldMutate("Incremented (++a) static short field " + name)) {
                mv.visitFieldInsn(opcode, owner, name, desc);
                mv.visitInsn(Opcodes.ICONST_1);
                mv.visitInsn(Opcodes.IADD);
                mv.visitInsn(Opcodes.I2S);
                mv.visitInsn(Opcodes.DUP);
                mv.visitFieldInsn(Opcodes.PUTSTATIC, owner, name, desc);
                return;
            }
        }
    }
    mv.visitFieldInsn(opcode, owner, name, desc);
}

From source file:org.pitest.mutationtest.engine.gregor.mutators.experimental.extended.UOIMutator4.java

License:Apache License

@Override
public void visitFieldInsn(int opcode, String owner, String name, String desc) {

    // GETFIELD I,F,L,D + B,S
    if ((opcode == Opcodes.GETFIELD)) {
        if (desc.equals("I")) {
            if (this.shouldMutate("Decremented (--a) integer field")) {
                // stack = [this]

                mv.visitInsn(Opcodes.DUP);
                // stack = [this] [this]

                mv.visitFieldInsn(opcode, owner, name, desc);
                // stack = [this] [this.field]

                mv.visitInsn(Opcodes.ICONST_1);
                mv.visitInsn(Opcodes.ISUB);
                // stack = [this] [this.field -1]

                mv.visitInsn(Opcodes.DUP_X1);
                // stack = [this.field -1] [this] [this.field -1]

                mv.visitFieldInsn(Opcodes.PUTFIELD, owner, name, desc);
                return;
            }/*www .j  a  v a 2 s  .  c o m*/
        }
        if (desc.equals("F")) {
            if (this.shouldMutate("Decremented (--a) float field")) {
                mv.visitInsn(Opcodes.DUP);
                mv.visitFieldInsn(opcode, owner, name, desc);
                mv.visitInsn(Opcodes.FCONST_1);
                mv.visitInsn(Opcodes.FSUB);
                mv.visitInsn(Opcodes.DUP_X1);
                mv.visitFieldInsn(Opcodes.PUTFIELD, owner, name, desc);
                return;
            }
        }
        if (desc.equals("J")) {
            if (this.shouldMutate("Decremented (--a) long field")) {
                mv.visitInsn(Opcodes.DUP);
                mv.visitFieldInsn(opcode, owner, name, desc);
                mv.visitInsn(Opcodes.LCONST_1);
                mv.visitInsn(Opcodes.LSUB);
                mv.visitInsn(Opcodes.DUP2_X1);
                mv.visitFieldInsn(Opcodes.PUTFIELD, owner, name, desc);
                return;
            }
        }
        if (desc.equals("D")) {
            if (this.shouldMutate("Decremented (--a) double field")) {
                mv.visitInsn(Opcodes.DUP);
                mv.visitFieldInsn(opcode, owner, name, desc);
                mv.visitInsn(Opcodes.DCONST_1);
                mv.visitInsn(Opcodes.DSUB);
                mv.visitInsn(Opcodes.DUP2_X1);
                mv.visitFieldInsn(Opcodes.PUTFIELD, owner, name, desc);
                return;
            }
        }
        if (desc.equals("B")) {
            if (this.shouldMutate("Decremented (--a) double field")) {
                mv.visitInsn(Opcodes.DUP);
                mv.visitFieldInsn(opcode, owner, name, desc);
                mv.visitInsn(Opcodes.ICONST_1);
                mv.visitInsn(Opcodes.ISUB);
                mv.visitInsn(Opcodes.I2B);
                mv.visitInsn(Opcodes.DUP_X1);
                mv.visitFieldInsn(Opcodes.PUTFIELD, owner, name, desc);
                return;
            }
        }
        if (desc.equals("S")) {
            if (this.shouldMutate("Decremented (--a) short field")) {
                mv.visitInsn(Opcodes.DUP);
                mv.visitFieldInsn(opcode, owner, name, desc);
                mv.visitInsn(Opcodes.ICONST_1);
                mv.visitInsn(Opcodes.ISUB);
                mv.visitInsn(Opcodes.I2S);
                mv.visitInsn(Opcodes.DUP_X1);
                mv.visitFieldInsn(Opcodes.PUTFIELD, owner, name, desc);
                return;
            }
        }
    }

    // GETSTATIC I,F,L,D + B,S
    if (opcode == Opcodes.GETSTATIC) {
        if (desc.equals("I")) {
            if (this.shouldMutate("Decremented (--a) static integer field")) {
                mv.visitFieldInsn(opcode, owner, name, desc);
                mv.visitInsn(Opcodes.ICONST_1);
                mv.visitInsn(Opcodes.ISUB);
                mv.visitInsn(Opcodes.DUP);
                mv.visitFieldInsn(Opcodes.PUTSTATIC, owner, name, desc);
                return;
            }
        }
        if (desc.equals("F")) {
            if (this.shouldMutate("Decremented (--a) static float field")) {
                mv.visitFieldInsn(opcode, owner, name, desc);
                mv.visitInsn(Opcodes.FCONST_1);
                mv.visitInsn(Opcodes.FSUB);
                mv.visitInsn(Opcodes.DUP);
                mv.visitFieldInsn(Opcodes.PUTSTATIC, owner, name, desc);
                return;
            }
        }
        if (desc.equals("J")) {
            if (this.shouldMutate("Decremented (--a) static long field")) {
                mv.visitFieldInsn(opcode, owner, name, desc);
                mv.visitInsn(Opcodes.LCONST_1);
                mv.visitInsn(Opcodes.LSUB);
                mv.visitInsn(Opcodes.DUP2);
                mv.visitFieldInsn(Opcodes.PUTSTATIC, owner, name, desc);
                return;
            }
        }
        if (desc.equals("D")) {
            if (this.shouldMutate("Decremented (--a) static double field")) {
                mv.visitFieldInsn(opcode, owner, name, desc);
                mv.visitInsn(Opcodes.DCONST_1);
                mv.visitInsn(Opcodes.DSUB);
                mv.visitInsn(Opcodes.DUP2);
                mv.visitFieldInsn(Opcodes.PUTSTATIC, owner, name, desc);
                return;
            }
        }
        if (desc.equals("B")) {
            if (this.shouldMutate("Decremented (--a) static byte field")) {
                mv.visitFieldInsn(opcode, owner, name, desc);
                mv.visitInsn(Opcodes.ICONST_1);
                mv.visitInsn(Opcodes.ISUB);
                mv.visitInsn(Opcodes.I2B);
                mv.visitInsn(Opcodes.DUP);
                mv.visitFieldInsn(Opcodes.PUTSTATIC, owner, name, desc);
                return;
            }
        }
        if (desc.equals("S")) {
            if (this.shouldMutate("Decremented (--a) static short field")) {
                mv.visitFieldInsn(opcode, owner, name, desc);
                mv.visitInsn(Opcodes.ICONST_1);
                mv.visitInsn(Opcodes.ISUB);
                mv.visitInsn(Opcodes.I2S);
                mv.visitInsn(Opcodes.DUP);
                mv.visitFieldInsn(Opcodes.PUTSTATIC, owner, name, desc);
                return;
            }
        }
    }
    mv.visitFieldInsn(opcode, owner, name, desc);
}

From source file:org.sonar.java.bytecode.se.BytecodeEGWalkerExecuteTest.java

License:Open Source License

@Test
public void test_getfield() throws Exception {
    SymbolicValue objectRef = new SymbolicValue();
    ProgramState programState = execute(
            new Instruction(Opcodes.GETFIELD, new Instruction.FieldOrMethod("", "", "D", false)),
            ProgramState.EMPTY_STATE.stackValue(objectRef));
    SymbolicValue fieldValue = programState.peekValue();
    assertThat(fieldValue).isNotNull();/*from ww w.  ja va 2 s .  c  om*/
    assertThat(isDoubleOrLong(programState, fieldValue)).isTrue();
    assertThat(fieldValue).isNotEqualTo(objectRef);

    programState = execute(new Instruction(Opcodes.GETFIELD, new Instruction.FieldOrMethod("", "", "I", false)),
            ProgramState.EMPTY_STATE.stackValue(objectRef));
    fieldValue = programState.peekValue();
    assertThat(fieldValue).isNotNull();
    assertThat(isDoubleOrLong(programState, fieldValue)).isFalse();

    assertThatThrownBy(() -> execute(new Instruction(Opcodes.GETFIELD)))
            .hasMessage("GETFIELD needs 1 values on stack");
}

From source file:org.spongepowered.asm.mixin.injection.points.BeforeFieldAccess.java

License:MIT License

public BeforeFieldAccess(InjectionPointData data) {
    super(data);/*w  w  w  .j a v a 2s  . com*/
    this.opcode = data.getOpcode(-1, Opcodes.GETFIELD, Opcodes.PUTFIELD, Opcodes.GETSTATIC, Opcodes.PUTSTATIC,
            -1);
}

From source file:org.spongepowered.asm.mixin.transformer.MixinTargetContext.java

License:MIT License

/**
 * Handle "imaginary super" invokations, these are invokations in
 * non-derived mixins for accessing methods known to exist in a supermixin
 * which is not directly inherited by this mixix. The method can only call
 * its <b>own</b> super-implmentation and the methd must also be tagged with
 * {@link SoftOverride} to indicate that the method must exist in a super
 * class./* w  w w .  j a v  a  2s.c  o m*/
 * 
 * @param method Method being processed
 * @param fieldInsn the GETFIELD insn which access the pseudo-field which is
 *      used as a handle to the superclass
 */
private void processImaginarySuper(MethodNode method, FieldInsnNode fieldInsn) {
    if (fieldInsn.getOpcode() != Opcodes.GETFIELD) {
        if (MixinTargetContext.INIT.equals(method.name)) {
            throw new InvalidMixinException(this, "Illegal imaginary super declaration: field " + fieldInsn.name
                    + " must not specify an initialiser");
        }

        throw new InvalidMixinException(this, "Illegal imaginary super access: found "
                + ASMHelper.getOpcodeName(fieldInsn.getOpcode()) + " opcode in " + method.name + method.desc);
    }

    if ((method.access & Opcodes.ACC_PRIVATE) != 0 || (method.access & Opcodes.ACC_STATIC) != 0) {
        throw new InvalidMixinException(this, "Illegal imaginary super access: method " + method.name
                + method.desc + " is private or static");
    }

    if (ASMHelper.getInvisibleAnnotation(method, SoftOverride.class) == null) {
        throw new InvalidMixinException(this, "Illegal imaginary super access: method " + method.name
                + method.desc + " is not decorated with @SoftOverride");
    }

    for (Iterator<AbstractInsnNode> methodIter = method.instructions
            .iterator(method.instructions.indexOf(fieldInsn)); methodIter.hasNext();) {
        AbstractInsnNode insn = methodIter.next();
        if (insn instanceof MethodInsnNode) {
            MethodInsnNode methodNode = (MethodInsnNode) insn;
            if (methodNode.owner.equals(this.getClassRef()) && methodNode.name.equals(method.name)
                    && methodNode.desc.equals(method.desc)) {
                methodNode.setOpcode(Opcodes.INVOKESPECIAL);
                this.updateStaticBinding(method, methodNode);
                return;
            }
        }
    }

    throw new InvalidMixinException(this,
            "Illegal imaginary super access: could not find INVOKE for " + method.name + method.desc);
}

From source file:org.spongepowered.mod.asm.util.ASMEventListenerFactory.java

License:MIT License

@SuppressWarnings("unchecked")
private static <T> Class<T> createClass(Class<T> interf, Method input, Method output) {

    String className = getClassName(interf, input, output);

    ClassWriter cwBase = new ClassWriter(0);
    CheckClassAdapter cw = new CheckClassAdapter(cwBase);

    MethodVisitor mv;/*  w ww.jav a 2 s .com*/

    String classNameDesc = className.replace('.', '/');

    String interfaceInternalName = Type.getInternalName(interf);

    String inputName = input.getName();
    String inputMethodDescriptor = Type.getMethodDescriptor(input);

    String outputParameterTypeIntName = Type.getInternalName(output.getParameterTypes()[0]);
    String outputTargetTypeIntName = Type.getInternalName(output.getDeclaringClass());
    String outputMethodDescriptor = Type.getMethodDescriptor(output);
    String outputName = output.getName();

    boolean isOutputInterface = output.getDeclaringClass().isInterface();

    // A new class of the following form is created, with a unique name
    //
    // package org.spongepowered.mod.asm;
    // public class <className> extends java.lang.Object implements <interf>
    //
    //     private final Object target
    //
    //     public <className> (java.lang.Object target) {
    //         super();
    //         this.target = target;
    //         return;
    //     }
    //
    //     public void <inputMethod> (<inputMethodType event) {
    //         ((outputTargetType) this.target).outputMethod((outputParameteType) event);
    //         return
    //     }
    // }

    // package org.spongepowered.mod.asm;
    // public class <className> extends java.lang.Object implements <interf>
    cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER, classNameDesc, null, "java/lang/Object",
            new String[] { interfaceInternalName });

    // private final Object target
    cw.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL, "target", "Ljava/lang/Object;", null, null);

    // Constructor

    // public UniqueClass (java.lang.Object target) {
    mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "(Ljava/lang/Object;)V", null, null);
    mv.visitCode();

    // super();
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);

    // this.target = target;
    mv.visitVarInsn(Opcodes.ALOAD, 0); // Loads this
    mv.visitVarInsn(Opcodes.ALOAD, 1); // Loads target (from input)
    mv.visitFieldInsn(Opcodes.PUTFIELD, classNameDesc, "target", "Ljava/lang/Object;");

    // return;
    mv.visitInsn(Opcodes.RETURN);

    // }
    // 2 localvars due to inputs: this, target
    // 2 items on stack after double ALOAD
    mv.visitMaxs(2, 2);
    mv.visitEnd();

    // Callback method

    // public void <inputMethod> (<inputMethodType event) {
    mv = cw.visitMethod(Opcodes.ACC_PUBLIC, inputName, inputMethodDescriptor, null, null);
    mv.visitCode();

    // push((casted) this.target)
    mv.visitVarInsn(Opcodes.ALOAD, 0); // Loads this
    mv.visitFieldInsn(Opcodes.GETFIELD, classNameDesc, "target", "Ljava/lang/Object;");
    mv.visitTypeInsn(Opcodes.CHECKCAST, outputTargetTypeIntName);

    // push((casted) event)
    mv.visitVarInsn(Opcodes.ALOAD, 1); // Loads method parameter 0
    mv.visitTypeInsn(Opcodes.CHECKCAST, outputParameterTypeIntName);

    // ((outputTargetType) this.target).outputMethod((outputParameteType) event);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, outputTargetTypeIntName, outputName, outputMethodDescriptor,
            isOutputInterface);

    // return
    mv.visitInsn(Opcodes.RETURN);

    // }
    mv.visitMaxs(2, 2);
    mv.visitEnd();

    cw.visitEnd();

    byte[] bytes = cwBase.toByteArray();

    return (Class<T>) loader.defineClass(className, bytes);
}

From source file:org.springframework.xd.fluent.internal.CustomizedModuleGenerator.java

License:Apache License

/**
 * @param inDescriptor input descriptor of the form Lfoo/Bar;
 * @param outDescriptor output descriptor of the form Lfoo/Boo;
 *///w  w  w .j  a va 2s .  c o m
private static void writeCustomCode(ZipOutputStream zos, String inDescriptor, String outDescriptor) {
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
    cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER, "org/springframework/xd/code/Code", null,
            "org/springframework/xd/code/CodeDrivenProcessor", null);

    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESPECIAL, "org/springframework/xd/code/CodeDrivenProcessor", "<init>", "()V",
            false);
    mv.visitInsn(RETURN);
    mv.visitMaxs(1, 1);
    mv.visitEnd();

    // An example transform method that picks even number chars from the input payload and makes that into the output
    mv = cw.visitMethod(ACC_PUBLIC, "transform", "(" + inDescriptor + ")" + outDescriptor, null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, "org/springframework/xd/code/Code", "fn",
            "Ljava/util/function/Function;");
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEINTERFACE, "java/util/function/Function", "apply",
            "(Ljava/lang/Object;)Ljava/lang/Object;", true);
    mv.visitTypeInsn(CHECKCAST, outDescriptor.substring(1, outDescriptor.length() - 1));//"java/lang/Integer");
    mv.visitInsn(ARETURN);
    mv.visitMaxs(2, 2);
    mv.visitEnd();
    cw.visitEnd();
    try {
        zos.write(cw.toByteArray());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:org.springsource.loaded.test.infra.MethodPrinter.java

License:Apache License

public void visitFieldInsn(int opcode, String owner, String name, String desc) {
    if (opcode == Opcodes.GETSTATIC) {
        to.println("    GETSTATIC " + owner + "." + name + " " + desc);
    } else if (opcode == Opcodes.PUTSTATIC) {
        to.println("    PUTSTATIC " + owner + "." + name + " " + desc);
    } else if (opcode == Opcodes.GETFIELD) {
        to.println("    GETFIELD " + owner + "." + name + " " + desc);
    } else if (opcode == Opcodes.PUTFIELD) {
        to.println("    PUTFIELD " + owner + "." + name + " " + desc);
    } else {// w  w w  . jav a2  s.  co m
        throw new IllegalStateException(":" + opcode);
    }
}

From source file:org.teavm.flavour.regex.bytecode.MatcherClassBuilder.java

License:Apache License

private void buildValidMethod(ClassVisitor cv, String className) {
    MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC, "isValid", "()Z", null, null);
    mv.visitCode();//from  ww w.j  a  va2  s.c  o m

    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, className, "state", "I");
    Label trueLabel = new Label();
    mv.visitJumpInsn(Opcodes.IFGE, trueLabel);
    mv.visitInsn(Opcodes.ICONST_0);
    mv.visitInsn(Opcodes.IRETURN);
    mv.visitLabel(trueLabel);
    mv.visitInsn(Opcodes.ICONST_1);
    mv.visitInsn(Opcodes.IRETURN);

    mv.visitMaxs(2, 1);
    mv.visitEnd();
}