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:de.sanandrew.core.manpack.transformer.TransformPlayerDismountCtrl.java

License:Creative Commons License

/**
 * Transforms the EntityPlayer.class by hooking into the updateRidden method and adding a call to _SAP_canDismountOnInput
 * in order for the ridden entity to control whether or not the rider can dismount via sneaking.
 *
 * @param bytes     the class bytes to be transformed
 * @return the transformed class bytes/*  w  ww.  j a v  a2 s. c om*/
 */
private static byte[] transformPlayer(byte[] bytes) {
    ClassNode clazz = ASMHelper.createClassNode(bytes);
    MethodNode method = ASMHelper.findMethod(clazz, ASMNames.MD_PLAYER_UPDATE_RIDDEN);

    InsnList needle = new InsnList();
    needle.add(new VarInsnNode(Opcodes.ALOAD, 0));
    needle.add(ASMHelper.getFieldInsnNode(Opcodes.GETFIELD, ASMNames.FD_PLAYER_WORLD_OBJ));
    needle.add(ASMHelper.getFieldInsnNode(Opcodes.GETFIELD, ASMNames.FD_WORLD_IS_REMOTE));
    LabelNode ln1 = new LabelNode();
    needle.add(new JumpInsnNode(Opcodes.IFNE, ln1));
    needle.add(new VarInsnNode(Opcodes.ALOAD, 0));
    needle.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_PLAYER_IS_SNEAKING, false));
    needle.add(new JumpInsnNode(Opcodes.IFEQ, ln1));

    AbstractInsnNode insertPoint = ASMHelper.findLastNodeFromNeedle(method.instructions, needle);

    InsnList injectList = new InsnList();
    injectList.add(new VarInsnNode(Opcodes.ALOAD, 0));
    injectList.add(ASMHelper.getFieldInsnNode(Opcodes.GETFIELD, ASMNames.FD_PLAYER_RIDING_ENTITY));
    injectList.add(new VarInsnNode(Opcodes.ALOAD, 0));
    injectList.add(
            ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_SAP_CAN_DISMOUNT_ON_INPUT, false));
    injectList.add(new JumpInsnNode(Opcodes.IFEQ, ((JumpInsnNode) insertPoint).label));

    method.instructions.insert(insertPoint, injectList);

    bytes = ASMHelper.createBytes(clazz, /*ClassWriter.COMPUTE_FRAMES |*/ ClassWriter.COMPUTE_MAXS);

    return bytes;
}

From source file:de.tuberlin.uebb.jbop.optimizer.ClassNodeBuilder.java

License:Open Source License

/**
 * Creates a getter for the last added field.
 * //from  w  ww.  j  a v a2  s .c  o  m
 * @return the class node builder
 */
public ClassNodeBuilder withGetter() {
    if (isInterface) {
        return this;
    }
    final String name = lastField.name;
    final String desc = lastField.desc;
    addMethod("get" + Character.toUpperCase(name.charAt(0)) + name.substring(1), "()" + desc);
    final Type type = Type.getType(desc);
    addInsn(new VarInsnNode(Opcodes.ALOAD, 0));
    addInsn(new FieldInsnNode(Opcodes.GETFIELD, classNode.name, name, desc));
    addInsn(new InsnNode(type.getOpcode(Opcodes.IRETURN)));
    return this;
}

From source file:de.tuberlin.uebb.jbop.optimizer.ClassNodeBuilder.java

License:Open Source License

/**
 * Inits the multi array with.//  ww  w . jav  a 2 s.c  o m
 * 
 * @param value
 *          the value
 * @param indexes
 *          the indexes
 * @return the class node builder
 */
public ClassNodeBuilder initMultiArrayWith(final Object value, final int... indexes) {
    if (isInterface) {
        return this;
    }
    final InsnList list = new InsnList();
    list.add(new VarInsnNode(Opcodes.ALOAD, 0));
    list.add(new FieldInsnNode(Opcodes.GETFIELD, classNode.name, lastField.name, lastField.desc));
    for (int i = 0; i < indexes.length - 1; ++i) {
        list.add(NodeHelper.getInsnNodeFor(indexes[i]));
        list.add(new InsnNode(Opcodes.AALOAD));
    }
    list.add(NodeHelper.getInsnNodeFor(indexes[indexes.length - 1]));
    list.add(NodeHelper.getInsnNodeFor(value));
    final Type elementType = Type.getType(lastField.desc).getElementType();
    if (elementType.getDescriptor().startsWith("L")) {
        list.add(new InsnNode(Opcodes.AASTORE));
    } else {
        list.add(new InsnNode(toPrimitive(Type.getType(value.getClass())).getOpcode(Opcodes.IASTORE)));

    }
    addToConstructor(list);
    return this;
}

From source file:de.tuberlin.uebb.jbop.optimizer.ClassNodeBuilder.java

License:Open Source License

/**
 * adds a FieldInsnNode for the given Field.
 * /*  w  ww  .  jav a2s  .c  om*/
 * @param builder
 *          the builder
 * @param field
 *          the field
 * @return the abstract optimizer test
 */
public ClassNodeBuilder addGetField(final String owner, final String field, final String desc) {

    final FieldInsnNode node = new FieldInsnNode(Opcodes.GETFIELD, owner, field, desc);
    return addInsn(node);
}

From source file:de.tuberlin.uebb.jbop.optimizer.methodsplitter.Block.java

License:Open Source License

/**
 * Objects that are not parameters and not written before
 * these can be://  w  w  w. jav  a  2 s.  c om
 * getField
 * getStatic
 * new
 * new array
 * new multi array
 * return type of method call
 */
private Type resolveType(final AbstractInsnNode node) {
    int arrayCount = 0;
    AbstractInsnNode currentNode = NodeHelper.getPrevious(node);
    while (currentNode != null) {
        final int opcode2 = currentNode.getOpcode();
        if (opcode2 == Opcodes.NEWARRAY) {
            final int operand = ((IntInsnNode) currentNode).operand;
            return getObjectType(operand);
        } else if (opcode2 == Opcodes.ANEWARRAY) {
            return getObjectType(((TypeInsnNode) currentNode).desc);
        } else if (opcode2 == Opcodes.MULTIANEWARRAY) {
            return getObjectType(((MultiANewArrayInsnNode) currentNode).desc);
        } else if (opcode2 == Opcodes.NEW) {
            final String desc = ((TypeInsnNode) currentNode).desc;
            return getObjectType(desc);
        } else if ((opcode2 >= Opcodes.IALOAD) && (opcode2 <= Opcodes.AALOAD)) {
            arrayCount++;
        } else if ((opcode2 == Opcodes.GETFIELD) || (opcode2 == Opcodes.GETSTATIC)) {
            final String desc = ((FieldInsnNode) currentNode).desc;
            return getObjectType(removeArrayType(desc, arrayCount));
        } else if ((opcode2 == Opcodes.ALOAD)) {
            final Type type2 = readers.getFirstVar(((VarInsnNode) currentNode).var).getParameterType();
            return getObjectType(removeArrayType(type2.getDescriptor(), arrayCount));
        } else if ((opcode2 >= Opcodes.INVOKEVIRTUAL) && (opcode2 <= Opcodes.INVOKEDYNAMIC)) {
            return Type.getReturnType(((MethodInsnNode) currentNode).desc);
        }
        currentNode = NodeHelper.getPrevious(currentNode);
    }
    return Type.VOID_TYPE;
}

From source file:de.tuberlin.uebb.jbop.optimizer.utils.NodeHelper.java

License:Open Source License

/**
 * Checks if node is getField./*from  ww w .ja v  a2s .co m*/
 * 
 * @param node
 *          the node
 * @return true if node is a getField
 */
public static boolean isGetField(final AbstractInsnNode node) {
    if (node == null) {
        return false;
    }
    return node.getOpcode() == Opcodes.GETFIELD;
}

From source file:de.unisb.cs.st.javaslicer.common.classRepresentation.instructions.FieldInstruction.java

License:Open Source License

@Override
public String toString() {
    String type;/*from  ww w  . j  a v a  2s . co  m*/
    switch (getOpcode()) {
    case Opcodes.PUTSTATIC:
        type = "PUTSTATIC";
        break;
    case Opcodes.GETSTATIC:
        type = "GETSTATIC";
        break;

    case Opcodes.GETFIELD:
        type = "GETFIELD";
        break;

    case Opcodes.PUTFIELD:
        type = "PUTFIELD";
        break;

    default:
        assert false;
        type = "--ERROR--";
        break;
    }

    StringBuilder sb = new StringBuilder(type.length() + this.ownerInternalClassName.length()
            + this.fieldName.length() + this.fieldDesc.length() + 3);
    sb.append(type).append(' ').append(this.ownerInternalClassName).append('.').append(this.fieldName)
            .append(' ').append(this.fieldDesc);
    return sb.toString();
}

From source file:de.unisb.cs.st.javaslicer.common.classRepresentation.instructions.FieldInstruction2.java

License:Open Source License

@Override
public String toString() {
    String type;/*w w w  .  j  ava  2  s  .  c  o m*/
    switch (getOpcode()) {
    case Opcodes.PUTSTATIC:
        type = "PUTSTATIC";
        break;
    case Opcodes.GETSTATIC:
        type = "GETSTATIC";
        break;

    case Opcodes.GETFIELD:
        type = "GETFIELD";
        break;

    case Opcodes.PUTFIELD:
        type = "PUTFIELD";
        break;

    default:
        assert false;
        type = "--ERROR--";
        break;
    }

    StringBuilder sb = new StringBuilder(type.length() + this.ownerInternalClassName.length()
            + this.fieldName.length() + this.fieldDesc.length() + 3);
    sb.append(type).append(' ').append(this.ownerInternalClassName).append(".&&").append(this.fieldName)
            .append(' ').append(this.fieldDesc);
    return sb.toString();
}

From source file:de.zib.sfs.instrument.AbstractSfsAdapter.java

License:BSD License

protected void wrapMethod(int access, String name, Type returnType, Type[] argumentTypes, String signature,
        String[] exceptions, String callbackName, Type additionalCallbackArgumentType,
        ResultPasser resultPasser) {//from   w ww  .jav  a2  s  . c  o  m
    argumentTypes = argumentTypes == null ? new Type[] {} : argumentTypes;
    String methodDescriptor = Type.getMethodDescriptor(returnType, argumentTypes);

    // <access> <returnType> <name>(<argumentTypes> arguments) throws
    // <exceptions> {
    MethodVisitor mv = this.cv.visitMethod(access, name, methodDescriptor, signature, exceptions);
    mv.visitCode();

    // if (isInstrumentationActive()) {
    isInstrumentationActive(mv);
    Label instrumentationActiveLabel = new Label();
    mv.visitJumpInsn(Opcodes.IFEQ, instrumentationActiveLabel);

    // return? methodPrefix<name>(arguments);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    int argumentIndex = 1;
    for (Type argument : argumentTypes) {
        mv.visitVarInsn(argument.getOpcode(Opcodes.ILOAD), argumentIndex);
        argumentIndex += argument.getSize();
    }
    mv.visitMethodInsn((access & Opcodes.ACC_STATIC) == 0 ? Opcodes.INVOKESPECIAL : Opcodes.INVOKESTATIC,
            this.instrumentedTypeInternalName, this.methodPrefix + name, methodDescriptor, false);
    if (!Type.VOID_TYPE.equals(returnType)) {
        mv.visitInsn(returnType.getOpcode(Opcodes.IRETURN));
    } else {
        mv.visitInsn(Opcodes.RETURN);
    }

    // }
    mv.visitLabel(instrumentationActiveLabel);

    // setInstrumentationActive(true);
    setInstrumentationActive(mv, true);

    // long startTime = System.nanoTime();
    int startTimeIndex = 1;
    for (Type argument : argumentTypes) {
        startTimeIndex += argument.getSize();
    }
    storeTime(mv, startTimeIndex);

    // <returnType> result =? methodPrefix<name>(arguments);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    argumentIndex = 1;
    for (Type argument : argumentTypes) {
        mv.visitVarInsn(argument.getOpcode(Opcodes.ILOAD), argumentIndex);
        argumentIndex += argument.getSize();
    }
    mv.visitMethodInsn((access & Opcodes.ACC_STATIC) == 0 ? Opcodes.INVOKESPECIAL : Opcodes.INVOKESTATIC,
            this.instrumentedTypeInternalName, this.methodPrefix + name, methodDescriptor, false);
    int endTimeIndex = startTimeIndex + 2;
    if (!Type.VOID_TYPE.equals(returnType)) {
        mv.visitVarInsn(returnType.getOpcode(Opcodes.ISTORE), startTimeIndex + 2);
        endTimeIndex += returnType.getSize();
    }

    // long endTime = System.nanoTime();
    storeTime(mv, endTimeIndex);

    // callback.<callbackMethod>(startTime, endTime, result?);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, this.instrumentedTypeInternalName, "callback",
            this.callbackTypeDescriptor);
    mv.visitVarInsn(Opcodes.LLOAD, startTimeIndex);
    mv.visitVarInsn(Opcodes.LLOAD, endTimeIndex);

    // -1 indicates no result should be passed
    int resultIndex = resultPasser.getResultIndex();
    if (resultIndex != -1) {
        // result of the actual operation requested
        if (resultIndex == 0) {
            mv.visitVarInsn(returnType.getOpcode(Opcodes.ILOAD), startTimeIndex + 2);
            resultPasser.passResult(mv);
        } else {
            // some parameter requested
            mv.visitVarInsn(argumentTypes[resultIndex - 1].getOpcode(Opcodes.ILOAD), resultIndex);
            resultPasser.passResult(mv);
        }
    }

    Type[] callbackArgumentTypes;
    if (additionalCallbackArgumentType == null) {
        callbackArgumentTypes = new Type[] { Type.LONG_TYPE, Type.LONG_TYPE };
    } else {
        callbackArgumentTypes = new Type[] { Type.LONG_TYPE, Type.LONG_TYPE, additionalCallbackArgumentType };
    }
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, this.callbackTypeInternalName, callbackName,
            Type.getMethodDescriptor(Type.VOID_TYPE, callbackArgumentTypes), false);

    // setInstrumentationActive(false);
    setInstrumentationActive(mv, false);

    // return result;?
    // }
    if (!Type.VOID_TYPE.equals(returnType)) {
        mv.visitVarInsn(returnType.getOpcode(Opcodes.ILOAD), startTimeIndex + 2);
        mv.visitInsn(returnType.getOpcode(Opcodes.IRETURN));
    } else {
        mv.visitInsn(Opcodes.RETURN);
    }
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}

From source file:de.zib.sfs.instrument.AbstractSfsAdapter.java

License:BSD License

@Override
public void visitEnd() {
    // public void setInstrumentationActive(boolean instrumentationActive) {
    MethodVisitor setInstrumentationActiveMV = this.cv.visitMethod(Opcodes.ACC_PUBLIC,
            "setInstrumentationActive", Type.getMethodDescriptor(Type.VOID_TYPE, Type.BOOLEAN_TYPE), null,
            null);/*  w w  w .jav a 2 s  . c om*/
    setInstrumentationActiveMV.visitCode();

    // this.instrumentationActive.setInstrumentationActive(instrumentationActive);
    setInstrumentationActiveMV.visitVarInsn(Opcodes.ALOAD, 0);
    setInstrumentationActiveMV.visitFieldInsn(Opcodes.GETFIELD, this.instrumentedTypeInternalName,
            "instrumentationActive", Type.getDescriptor(InstrumentationActive.class));
    setInstrumentationActiveMV.visitVarInsn(Opcodes.ILOAD, 1);
    setInstrumentationActiveMV.visitMethodInsn(Opcodes.INVOKEVIRTUAL,
            Type.getInternalName(InstrumentationActive.class), "setInstrumentationActive",
            Type.getMethodDescriptor(Type.VOID_TYPE, Type.BOOLEAN_TYPE), false);

    // }
    setInstrumentationActiveMV.visitInsn(Opcodes.RETURN);
    setInstrumentationActiveMV.visitMaxs(0, 0);
    setInstrumentationActiveMV.visitEnd();

    // public boolean isInstrumentationActive() {
    MethodVisitor isInstrumentationActiveMV = this.cv.visitMethod(Opcodes.ACC_PUBLIC, "isInstrumentationActive",
            Type.getMethodDescriptor(Type.BOOLEAN_TYPE), null, null);
    isInstrumentationActiveMV.visitCode();

    // return instrumentationActive.isInstrumentationActive();
    // }
    isInstrumentationActiveMV.visitVarInsn(Opcodes.ALOAD, 0);
    isInstrumentationActiveMV.visitFieldInsn(Opcodes.GETFIELD, this.instrumentedTypeInternalName,
            "instrumentationActive", Type.getDescriptor(InstrumentationActive.class));
    isInstrumentationActiveMV.visitMethodInsn(Opcodes.INVOKEVIRTUAL,
            Type.getInternalName(InstrumentationActive.class), "isInstrumentationActive",
            Type.getMethodDescriptor(Type.BOOLEAN_TYPE), false);
    isInstrumentationActiveMV.visitInsn(Opcodes.IRETURN);
    isInstrumentationActiveMV.visitMaxs(0, 0);
    isInstrumentationActiveMV.visitEnd();

    appendWrappedMethods(this.cv);

    this.cv.visitEnd();
}