Example usage for org.objectweb.asm Opcodes GETSTATIC

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

Introduction

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

Prototype

int GETSTATIC

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

Click Source Link

Usage

From source file:org.coldswap.asm.field.PublicStaticFieldReplacer.java

License:Open Source License

/**
 * Replaces any GETSTATIC/PUTSTATIC call of the field in the old class with the field
 * introduced in the new class./*w ww  . j  a  va  2  s.  co m*/
 *
 * @param classNode containing the old class.
 * @param fieldNode containing the old field.
 */
private void replaceReferences(ClassNode classNode, FieldNode fieldNode) {
    List<MethodNode> methodNodes = classNode.methods;
    String contClass = classNode.name.substring(classNode.name.lastIndexOf("/") + 1);
    final String className = classPackage
            + TransformerNameGenerator.getPublicStaticFieldClassName(contClass, fieldNode.name);
    for (MethodNode method : methodNodes) {
        InsnList inst = method.instructions;
        Iterator iter = inst.iterator();
        while (iter.hasNext()) {
            AbstractInsnNode absIns = (AbstractInsnNode) iter.next();
            int opcode = absIns.getOpcode();
            // check if instruction is GETSTATIC or PUTSTATIC
            if (opcode == Opcodes.GETSTATIC) {
                // get type
                if (absIns.getType() == AbstractInsnNode.FIELD_INSN) {
                    final Boolean[] foundField = { false };
                    final ClassNode cNode = classNode;
                    final FieldNode fNode = fieldNode;
                    absIns.accept(new MethodVisitor(Opcodes.ASM5) {
                        @Override
                        public void visitFieldInsn(int i, String s, String s2, String s3) {
                            if (cNode.name.equals(s) && fNode.name.equals(s2)) {
                                foundField[0] = true;
                            }
                            super.visitFieldInsn(i, s, s2, s3);
                        }
                    });
                    if (foundField[0]) {
                        inst.set(absIns, new FieldInsnNode(Opcodes.GETSTATIC, className, fieldNode.name,
                                fieldNode.desc));
                    }
                }
            } else if (opcode == Opcodes.PUTSTATIC) {
                if (absIns.getType() == AbstractInsnNode.FIELD_INSN) {
                    final Boolean[] foundField = { false };
                    final ClassNode cNode = classNode;
                    final FieldNode fNode = fieldNode;
                    absIns.accept(new MethodVisitor(Opcodes.ASM5) {
                        @Override
                        public void visitFieldInsn(int i, String s, String s2, String s3) {
                            if (cNode.name.equals(s) && fNode.name.equals(s2)) {
                                foundField[0] = true;
                            }
                            super.visitFieldInsn(i, s, s2, s3);
                        }
                    });
                    if (foundField[0]) {
                        inst.set(absIns, new FieldInsnNode(Opcodes.PUTSTATIC, className, fieldNode.name,
                                fieldNode.desc));
                    }
                }
            }
        }
    }
}

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  w  w . j av a 2s  . 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.eclipse.objectteams.otredyn.bytecode.asm.AbstractTransformableClassNode.java

License:Open Source License

/**
 * This method could be used to generate debug outputs in the generated code in the form: <br>
 * <code>/*from ww  w  .jav  a 2  s  .  c  o m*/
 * Sytsem.out.println(message);
 * </code>
 * @param message
 * @return
 */
protected InsnList getInstructionsForDebugOutput(String message) {
    InsnList instructions = new InsnList();
    instructions.add(new FieldInsnNode(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"));
    instructions.add(new LdcInsnNode(message));
    instructions.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println",
            "(Ljava/lang/String;)V", false));
    return instructions;
}

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

License:Open Source License

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    synchronized (AddGlobalTeamActivationAdapter.class) {
        if (!done && isMainMethod(name, desc, access)) {
            done = true;/* w  w  w  .j  a va 2  s  .c  o m*/
            final MethodVisitor methodVisitor = cv.visitMethod(access, name, desc, null, null);
            return new AdviceAdapter(this.api, methodVisitor, access, name, desc) {
                @Override
                protected void onMethodEnter() {
                    List<String> teams = getTeamsFromConfigFile();
                    for (String aTeam : teams) {
                        Label start, end, typeHandler, ctorHandler, after;

                        String aTeamSlash = aTeam.replace('.', '/');

                        // new SomeTeam():
                        methodVisitor.visitLabel(start = new Label());
                        methodVisitor.visitTypeInsn(Opcodes.NEW, aTeamSlash);
                        //       .activate(Team.ALL_THREADS):
                        methodVisitor.visitInsn(Opcodes.DUP);
                        methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, aTeamSlash, "<init>", "()V",
                                false);
                        methodVisitor.visitFieldInsn(Opcodes.GETSTATIC, ClassNames.TEAM_SLASH, "ALL_THREADS",
                                "Ljava/lang/Thread;");
                        methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, aTeamSlash, "activate",
                                "(Ljava/lang/Thread;)V", false);

                        methodVisitor.visitLabel(end = new Label());
                        methodVisitor.visitJumpInsn(Opcodes.GOTO, after = new Label());

                        // catch (ClassNotFoundException, NoClassDefFoundError):
                        //   System.err.println(...)
                        methodVisitor.visitLabel(typeHandler = new Label());
                        methodVisitor.visitInsn(Opcodes.POP); // discard the exception
                        methodVisitor.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "err",
                                "Ljava/io/PrintStream;");
                        methodVisitor.visitLdcInsn("Config error: Team class '" + aTeam + "' in config file '"
                                + TEAM_CONFIG_FILE + "' can not be found!");
                        methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println",
                                "(Ljava/lang/String;)V", false);
                        methodVisitor.visitJumpInsn(Opcodes.GOTO, after);
                        methodVisitor.visitTryCatchBlock(start, end, typeHandler,
                                "java/lang/ClassNotFoundException");
                        // dup to avoid stackmap errors (ASM bug at 1.8)
                        methodVisitor.visitLabel(typeHandler = new Label());
                        methodVisitor.visitInsn(Opcodes.POP); // discard the exception
                        methodVisitor.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "err",
                                "Ljava/io/PrintStream;");
                        methodVisitor.visitLdcInsn("Config error: Team class '" + aTeam + "' in config file '"
                                + TEAM_CONFIG_FILE + "' can not be found!");
                        methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println",
                                "(Ljava/lang/String;)V", false);
                        methodVisitor.visitJumpInsn(Opcodes.GOTO, after);
                        //
                        methodVisitor.visitTryCatchBlock(start, end, typeHandler,
                                "java/lang/NoClassDefFoundError");

                        // catch (NoSuchMethodError):
                        //   System.err.println(...)
                        methodVisitor.visitLabel(ctorHandler = new Label());
                        methodVisitor.visitInsn(Opcodes.POP); // discard the exception
                        methodVisitor.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "err",
                                "Ljava/io/PrintStream;");
                        methodVisitor.visitLdcInsn(
                                "Activation failed: Team class '" + aTeam + "' has no default constuctor!");
                        methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println",
                                "(Ljava/lang/String;)V", false);
                        methodVisitor.visitTryCatchBlock(start, end, ctorHandler,
                                "java/lang/NoSuchMethodError");

                        methodVisitor.visitLabel(after);
                    }
                }

                @Override
                public void visitMaxs(int maxStack, int maxLocals) {
                    super.visitMaxs(Math.max(maxStack, 3), maxLocals);
                }
            };
        }
        return null;
    }
}

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  .ja v a2s .  co  m*/
        // 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  ww.jav  a  2  s  . 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>/* w w  w .  j  ava2s . 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.ASMWrapper.java

License:Open Source License

/**
 * <p>//from  ww  w.  j a v a2 s .  com
 * isStaticDefUse
 * </p>
 * 
 * @return a boolean.
 */
public boolean isStaticDefUse() {
    return asmNode.getOpcode() == Opcodes.PUTSTATIC || asmNode.getOpcode() == Opcodes.GETSTATIC
            || isStaticArrayUsage();
}

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 ww  .  j a v  a  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;
}

From source file:org.evosuite.instrumentation.ArrayAllocationLimitMethodAdapter.java

License:Open Source License

/** {@inheritDoc} */
@Override/*from w  w w . j  av a 2 s. co m*/
public void visitIntInsn(int opcode, int operand) {
    if (opcode == Opcodes.NEWARRAY) {
        Label origTarget = new Label();
        visitInsn(Opcodes.DUP);
        visitFieldInsn(Opcodes.GETSTATIC, PackageInfo.getNameWithSlash(org.evosuite.Properties.class),
                "ARRAY_LIMIT", "I");
        super.visitJumpInsn(Opcodes.IF_ICMPLT, origTarget);
        super.visitTypeInsn(Opcodes.NEW, PackageInfo.getNameWithSlash(TestCaseExecutor.TimeoutExceeded.class));
        super.visitInsn(Opcodes.DUP);
        super.visitMethodInsn(Opcodes.INVOKESPECIAL,
                PackageInfo.getNameWithSlash(TestCaseExecutor.TimeoutExceeded.class), "<init>", "()V", false);
        super.visitInsn(Opcodes.ATHROW);
        super.visitLabel(origTarget);

    }
    super.visitIntInsn(opcode, operand);
}