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.compass.core.util.reflection.asm.AsmReflectionFieldGenerator.java

License:Apache License

private static void createGetMethod(ClassVisitor cw, Class entryClass, Field field) {
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "get", "(Ljava/lang/Object;)Ljava/lang/Object;", null,
            new String[] { "java/lang/IllegalArgumentException", "java/lang/IllegalAccessException" });
    mv.visitCode();//from ww  w.j a  v  a 2 s  .  co  m
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitTypeInsn(Opcodes.CHECKCAST, Type.getInternalName(entryClass));
    mv.visitFieldInsn(Opcodes.GETFIELD, Type.getInternalName(entryClass), field.getName(),
            Type.getDescriptor(field.getType()));
    boxIfNeeded(mv, field);
    mv.visitInsn(Opcodes.ARETURN);
    mv.visitMaxs(1, 2);
    mv.visitEnd();
}

From source file:org.decojer.cavaj.readers.asm.ReadMethodVisitor.java

License:Open Source License

@Override
public void visitFieldInsn(final int opcode, final String owner, final String name, final String desc) {
    if (owner == null || name == null || desc == null) {
        log.warn(getM() + ": Cannot read get operation with field '" + owner + "." + name + "' and descriptor '"
                + desc + "'!");
        return;/*from   w ww  . j a va 2 s  .c  o  m*/
    }
    switch (opcode) {
    /*******
     * GET *
     *******/
    case Opcodes.GETFIELD:
    case Opcodes.GETSTATIC: {
        final T ownerT = getDu().getT(owner);
        final F f = ownerT.getF(name, desc);
        f.setStatic(opcode == Opcodes.GETSTATIC);
        add(new GET(this.ops.size(), opcode, this.line, f));
        return;
    }
    /*******
     * PUT *
     *******/
    case Opcodes.PUTFIELD:
    case Opcodes.PUTSTATIC: {
        final T ownerT = getDu().getT(owner);
        final F f = ownerT.getF(name, desc);
        f.setStatic(opcode == Opcodes.PUTSTATIC);
        add(new PUT(this.ops.size(), opcode, this.line, f));
        return;
    }
    default:
        log.warn(getM() + ": Unknown field insn opcode '" + opcode + "'!");
    }
}

From source file:org.devinprogress.YAIF.Transformer.ASMTransformer.java

public static void insertWrapperStartup(MethodNode mn) {
    //Add TextField Wrapper
    //org.devinprogress.YAIF.YetAnotherInputFix.SetupTextFieldWrapper(this.displayWidth, this.displayHeight);
    //before ForgeHooksClient.createDisplay();
    //At method startGame() ()V

    //ALOAD 0/*from ww w.  j  ava2s  .  c  om*/
    //GETFIELD net/minecraft/client/Minecraft.displayWidth : I
    //ALOAD 0
    //GETFIELD net/minecraft/client/Minecraft.displayHeight : I
    //INVOKESTATIC org/devinprogress/YAIF/YetAnotherInputFix.SetupTextFieldWrapper (II)V

    //Be careful of the Exception Labels!!!
    //Insert before INVOKESTATIC net/minecraftforge/client/ForgeHooksClient.createDisplay ()V
    //10th INVOKESTATIC

    AbstractInsnNode n = ASMHelper.getNthInsnNode(mn, Opcodes.INVOKESTATIC, 10);
    String WidthName = YetAnotherInputFix.ObfuscatedEnv ? "d" : "displayWidth";
    String HeightName = YetAnotherInputFix.ObfuscatedEnv ? "e" : "displayHeight";

    if (!((MethodInsnNode) n).desc.equals("()V")) {//Here must be something wrong!
        System.out.println(new String("tryTransformMinecraft() Error, landmark desc not match."));
        return;
    }
    mn.instructions.insertBefore(n, new VarInsnNode(Opcodes.ALOAD, 0));
    mn.instructions.insertBefore(n,
            new FieldInsnNode(Opcodes.GETFIELD, obfedClassName.replace('.', '/'), WidthName, "I"));
    mn.instructions.insertBefore(n, new VarInsnNode(Opcodes.ALOAD, 0));
    mn.instructions.insertBefore(n,
            new FieldInsnNode(Opcodes.GETFIELD, obfedClassName.replace('.', '/'), HeightName, "I"));
    mn.instructions.insertBefore(n, new MethodInsnNode(Opcodes.INVOKESTATIC,
            "org/devinprogress/YAIF/YetAnotherInputFix", "SetupTextFieldWrapper", "(II)V", false));
    // Codes are braced by existed TryCatchBlock
    mn.maxStack += 1;
}

From source file:org.eclipse.objectteams.otredyn.bytecode.asm.AddImplicitActivationAdapter.java

License:Open Source License

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    if (isCandidateForImplicitActivation(name, desc, access)) {
        final MethodVisitor methodVisitor = cv.visitMethod(access, name, desc, null, null);
        final String enclTeamDesc = clazz.isRole()
                ? 'L' + clazz.getEnclosingClass().getName().replace('.', '/') + ';'
                : null;/* www  . j  a  v  a  2  s.co  m*/
        final int nesting = clazz.nestingDepth() - 1;
        return new AdviceAdapter(this.api, methodVisitor, access, name, desc) {
            @Override
            protected void onMethodEnter() {
                if (clazz.isTeam()) {
                    methodVisitor.visitIntInsn(Opcodes.ALOAD, 0);
                    methodVisitor.visitMethodInsn(INVOKEINTERFACE, TARGET_CLASS_NAME,
                            IMPLICIT_ACTIVATE_METHOD_NAME, METHOD_DESC, true);
                }
                if (clazz.isRole()) {
                    // TODO(SH): respect nesting depth (this$n)
                    methodVisitor.visitIntInsn(Opcodes.ALOAD, 0);
                    methodVisitor.visitFieldInsn(Opcodes.GETFIELD, clazz.getName().replace('.', '/'),
                            "this$" + nesting, enclTeamDesc);
                    methodVisitor.visitMethodInsn(INVOKEINTERFACE, TARGET_CLASS_NAME,
                            IMPLICIT_ACTIVATE_METHOD_NAME, METHOD_DESC, true);
                }
            }

            @Override
            protected void onMethodExit(int opcode) {
                if (clazz.isTeam()) {
                    methodVisitor.visitIntInsn(Opcodes.ALOAD, 0);
                    methodVisitor.visitMethodInsn(INVOKEINTERFACE, TARGET_CLASS_NAME,
                            IMPLICIT_DEACTIVATE_METHOD_NAME, METHOD_DESC, true);
                }
                if (clazz.isRole()) {
                    // TODO(SH): respect nesting depth (this$n)
                    methodVisitor.visitIntInsn(Opcodes.ALOAD, 0);
                    methodVisitor.visitFieldInsn(Opcodes.GETFIELD, clazz.getName().replace('.', '/'),
                            "this$" + nesting, enclTeamDesc);
                    methodVisitor.visitMethodInsn(INVOKEINTERFACE, TARGET_CLASS_NAME,
                            IMPLICIT_DEACTIVATE_METHOD_NAME, METHOD_DESC, true);
                }
            }

            @Override
            public void endMethod() {
                if (clazz.isTeam() || clazz.isRole())
                    methodVisitor.visitMaxs(0, 0);
            }
        };
    }
    return null;
}

From source file:org.eclipse.objectteams.otredyn.bytecode.asm.AddThreadNotificationAdapter.java

License:Open Source License

@Override
public MethodVisitor visitMethod(int access, String methodName, String desc, String signature,
        String[] exceptions) {/*from w  ww .  j  a  v a2  s . co  m*/
    if (INIT.equals(methodName)) {
        // into each constructor ...
        final MethodVisitor methodVisitor = cv.visitMethod(access, methodName, desc, null, null);
        return new AdviceAdapter(this.api, methodVisitor, access, methodName, desc) {
            @Override
            public void invokeConstructor(Type type, Method method) {
                super.invokeConstructor(type, method);
                // ... that contains a super(..) call (rather than this(..)):
                if (type.getInternalName().equals(clazz.getInternalSuperClassName())) {
                    // insert:
                    // this._OT$creationThread = Thread.currentThread();
                    methodVisitor.visitIntInsn(Opcodes.ALOAD, 0);
                    methodVisitor.visitMethodInsn(Opcodes.INVOKESTATIC, ClassNames.THREAD_SLASH, CURRENT_THREAD,
                            CURRENT_THREAD_DESC, false);
                    methodVisitor.visitFieldInsn(Opcodes.PUTFIELD, clazz.getInternalName(), CREATION_THREAD,
                            THREAD_DESC);
                }
            }
        };
    } else if (RUN.equals(methodName) && RUN_DESC.equals(desc)) {
        final MethodVisitor methodVisitor = cv.visitMethod(access, methodName, desc, null, null);
        return new AdviceAdapter(this.api, methodVisitor, access, methodName, desc) {

            Label start = new Label(); // start of method (scope of new local)
            Label end = new Label(); // end of method
            int isThreadStartIdx; // new local: boolean _OT$isThreadStart

            @Override
            protected void onMethodEnter() {
                methodVisitor.visitLabel(start);
                isThreadStartIdx = newLocal(Type.BOOLEAN_TYPE);
                methodVisitor.visitLocalVariable("_OT$isThreadStart", "Z", null, start, end, isThreadStartIdx);
                // TeamThreadManager.newThreadStarted(false, this._OT$creationThread)
                methodVisitor.visitInsn(Opcodes.ICONST_0);
                methodVisitor.visitIntInsn(Opcodes.ALOAD, 0);
                methodVisitor.visitFieldInsn(Opcodes.GETFIELD, clazz.getInternalName(), CREATION_THREAD,
                        THREAD_DESC);
                methodVisitor.visitMethodInsn(Opcodes.INVOKESTATIC, ClassNames.TEAM_THREAD_MANAGER_SLASH,
                        NEW_THREAD_STARTED, NEW_THREAD_STARTED_DESC, false);
                methodVisitor.visitIntInsn(Opcodes.ISTORE, isThreadStartIdx);
                // this._OT$creationThread = null; // avoid leak
                methodVisitor.visitIntInsn(Opcodes.ALOAD, 0);
                methodVisitor.visitInsn(Opcodes.ACONST_NULL);
                methodVisitor.visitFieldInsn(Opcodes.PUTFIELD, clazz.getInternalName(), CREATION_THREAD,
                        THREAD_DESC);
            }

            @Override
            protected void onMethodExit(int opcode) {
                insertThreadEndedNotification();
            }

            @Override
            public void endMethod() {
                methodVisitor.visitLabel(end);

                // insert another threadEnded notification as a handler for Throwable
                Label handler = new Label();
                methodVisitor.visitLabel(handler);
                insertThreadEndedNotification();
                methodVisitor.visitInsn(Opcodes.ATHROW); // rethrow caught exception

                methodVisitor.visitTryCatchBlock(start, end, handler, ClassNames.THROWABLE_SLASH);
                methodVisitor.visitMaxs(0, 0);
            }

            void insertThreadEndedNotification() {
                Label skip = new Label();
                // insert:
                // if (_OT$isThreadStart) TeamThreadManager.threadEnded();
                methodVisitor.visitIntInsn(Opcodes.ILOAD, isThreadStartIdx);
                methodVisitor.visitJumpInsn(Opcodes.IFEQ, skip);
                methodVisitor.visitMethodInsn(Opcodes.INVOKESTATIC, ClassNames.TEAM_THREAD_MANAGER_SLASH,
                        THREAD_ENDED, THREAD_ENDED_DESC, false);
                methodVisitor.visitLabel(skip);
            }
        };
    }
    return null;
}

From source file:org.eclipse.objectteams.otredyn.bytecode.asm.CreateAddRemoveRoleMethod.java

License:Open Source License

void genGetInitializedRoleSet(InsnList instructions, int targetLocal) {
    // x = this._OT$roleSet 
    instructions.add(new IntInsnNode(Opcodes.ALOAD, 0));
    instructions.add(new FieldInsnNode(Opcodes.GETFIELD, name, ConstantMembers.OT_ROLE_SET,
            ConstantMembers.HASH_SET_FIELD_TYPE));

    instructions.add(new IntInsnNode(Opcodes.ASTORE, targetLocal));
    instructions.add(new IntInsnNode(Opcodes.ALOAD, targetLocal));

    // if (x == null) {
    LabelNode skipInstantiation = new LabelNode();
    instructions.add(new JumpInsnNode(Opcodes.IFNONNULL, skipInstantiation));

    // this._OT$roleSet = new HashSet();
    instructions.add(new IntInsnNode(Opcodes.ALOAD, 0));
    instructions.add(new TypeInsnNode(Opcodes.NEW, ClassNames.HASH_SET_SLASH));
    instructions.add(new InsnNode(Opcodes.DUP));
    instructions/*from  w w  w .  j a va 2 s. com*/
            .add(new MethodInsnNode(Opcodes.INVOKESPECIAL, ClassNames.HASH_SET_SLASH, "<init>", "()V", false));

    instructions.add(new IntInsnNode(Opcodes.ASTORE, targetLocal));
    instructions.add(new IntInsnNode(Opcodes.ALOAD, targetLocal));
    instructions.add(new FieldInsnNode(Opcodes.PUTFIELD, name, ConstantMembers.OT_ROLE_SET,
            ConstantMembers.HASH_SET_FIELD_TYPE));

    instructions.add(skipInstantiation);
    // }
}

From source file:org.eclipse.objectteams.otredyn.bytecode.asm.CreateFieldAccessAdapter.java

License:Open Source License

@Override
public boolean transform() {

    InsnList instructions = new InsnList();
    // put accessId on the stack
    instructions.add(new IntInsnNode(Opcodes.ILOAD, firstArgIndex + 1));
    // read or write access
    LabelNode writeAccess = new LabelNode();
    instructions.add(new JumpInsnNode(Opcodes.IFNE, writeAccess));
    // read access
    if (field.isStatic()) {
        // get value of field
        instructions.add(new FieldInsnNode(Opcodes.GETSTATIC, name, field.getName(), field.getSignature()));
    } else {//from   w  w w. jav a 2  s  .com
        // put "this" on the stack
        instructions.add(new IntInsnNode(Opcodes.ALOAD, 0));
        // get value of field
        instructions.add(new FieldInsnNode(Opcodes.GETFIELD, name, field.getName(), field.getSignature()));
    }

    //box value as "Object"
    Type type = Type.getType(field.getSignature());
    instructions.add(AsmTypeHelper.getBoxingInstructionForType(type));
    instructions.add(new InsnNode(Opcodes.ARETURN));

    //write access
    instructions.add(writeAccess);
    //put "args" on the stack 
    instructions.add(new IntInsnNode(Opcodes.ALOAD, firstArgIndex + 2));
    //get the first element of "args"
    instructions.add(new InsnNode(Opcodes.ICONST_0));
    instructions.add(new InsnNode(Opcodes.AALOAD));
    //unbox it
    if (type.getSort() != Type.ARRAY && type.getSort() != Type.OBJECT) {
        String objectType = AsmTypeHelper.getObjectType(type);
        instructions.add(new TypeInsnNode(Opcodes.CHECKCAST, objectType));
        instructions.add(AsmTypeHelper.getUnboxingInstructionForType(type, objectType));
    } else {
        instructions.add(new TypeInsnNode(Opcodes.CHECKCAST, type.getInternalName()));
    }

    if (field.isStatic()) {
        //save value in field
        instructions.add(new FieldInsnNode(Opcodes.PUTSTATIC, name, field.getName(), field.getSignature()));
    } else {
        //put "this" on the stack
        instructions.add(new IntInsnNode(Opcodes.ALOAD, 0));
        instructions.add(new InsnNode(Opcodes.SWAP));
        //save value in field
        instructions.add(new FieldInsnNode(Opcodes.PUTFIELD, name, field.getName(), field.getSignature()));
    }

    //dummy return 
    instructions.add(new InsnNode(Opcodes.ACONST_NULL));
    instructions.add(new InsnNode(Opcodes.ARETURN));

    //add the instructions to a new label in the existing switch
    MethodNode method = getMethod(access);
    addNewLabelToSwitch(method.instructions, instructions, accessId);

    return true;
}

From source file:org.evosuite.graphs.cfg.ASMWrapper.java

License:Open Source License

/**
 * <p>/*from   w  w w .j a v  a 2s .  c o m*/
 * isFieldUse
 * </p>
 * 
 * @return a boolean.
 */
public boolean isFieldUse() {
    return asmNode.getOpcode() == Opcodes.GETFIELD || asmNode.getOpcode() == Opcodes.GETSTATIC
            || isFieldMethodCallUse();
}

From source file:org.evosuite.graphs.cfg.ASMWrapper.java

License:Open Source License

/**
 * <p>/*from   w  w  w . j  a  v a2 s. c  o  m*/
 * isFieldUse
 * </p>
 * 
 * @return a boolean.
 */
public boolean isFieldNodeUse() {
    return asmNode.getOpcode() == Opcodes.GETFIELD || asmNode.getOpcode() == Opcodes.GETSTATIC;
}

From source file:org.evosuite.graphs.cfg.BytecodeInstructionPool.java

License:Open Source License

/**
 * Determine how many bytes the current instruction occupies together with
 * its operands/*from  w  w  w .ja va  2s .  c  o  m*/
 * 
 * @return
 */
private int getBytecodeIncrement(AbstractInsnNode instructionNode) {
    int opcode = instructionNode.getOpcode();
    switch (opcode) {
    case Opcodes.ALOAD: // index
    case Opcodes.ASTORE: // index
    case Opcodes.DLOAD:
    case Opcodes.DSTORE:
    case Opcodes.FLOAD:
    case Opcodes.FSTORE:
    case Opcodes.ILOAD:
    case Opcodes.ISTORE:
    case Opcodes.LLOAD:
    case Opcodes.LSTORE:
        VarInsnNode varNode = (VarInsnNode) instructionNode;
        if (varNode.var > 3)
            return 1;
        else
            return 0;
    case Opcodes.BIPUSH: // byte
    case Opcodes.NEWARRAY:
    case Opcodes.RET:
        return 1;
    case Opcodes.LDC:
        LdcInsnNode ldcNode = (LdcInsnNode) instructionNode;
        if (ldcNode.cst instanceof Double || ldcNode.cst instanceof Long)
            return 2; // LDC2_W
        else
            return 1;
    case 19: //LDC_W
    case 20: //LDC2_W
        return 2;
    case Opcodes.ANEWARRAY: // indexbyte1, indexbyte2
    case Opcodes.CHECKCAST: // indexbyte1, indexbyte2
    case Opcodes.GETFIELD:
    case Opcodes.GETSTATIC:
    case Opcodes.GOTO:
    case Opcodes.IF_ACMPEQ:
    case Opcodes.IF_ACMPNE:
    case Opcodes.IF_ICMPEQ:
    case Opcodes.IF_ICMPNE:
    case Opcodes.IF_ICMPGE:
    case Opcodes.IF_ICMPGT:
    case Opcodes.IF_ICMPLE:
    case Opcodes.IF_ICMPLT:
    case Opcodes.IFLE:
    case Opcodes.IFLT:
    case Opcodes.IFGE:
    case Opcodes.IFGT:
    case Opcodes.IFNE:
    case Opcodes.IFEQ:
    case Opcodes.IFNONNULL:
    case Opcodes.IFNULL:
    case Opcodes.IINC:
    case Opcodes.INSTANCEOF:
    case Opcodes.INVOKESPECIAL:
    case Opcodes.INVOKESTATIC:
    case Opcodes.INVOKEVIRTUAL:
    case Opcodes.JSR:
    case Opcodes.NEW:
    case Opcodes.PUTFIELD:
    case Opcodes.PUTSTATIC:
    case Opcodes.SIPUSH:
        // case Opcodes.LDC_W
        // case Opcodes.LDC2_W

        return 2;
    case Opcodes.MULTIANEWARRAY:
        return 3;
    case Opcodes.INVOKEDYNAMIC:
    case Opcodes.INVOKEINTERFACE:
        return 4;

    case Opcodes.LOOKUPSWITCH:
    case Opcodes.TABLESWITCH:
        // TODO: Could be more
        return 4;
    // case Opcodes.GOTO_W 
    // case Opcodes.JSR_W
    }
    return 0;
}