Example usage for org.objectweb.asm Opcodes PUTSTATIC

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

Introduction

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

Prototype

int PUTSTATIC

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

Click Source Link

Usage

From source file:org.evosuite.testcarver.instrument.Instrumenter.java

License:Open Source License

private void instrumentPUTXXXFieldAccesses(final ClassNode cn, final String internalClassName,
        final MethodNode methodNode) {
    final InsnList instructions = methodNode.instructions;

    AbstractInsnNode ins = null;/*w  w  w .  j  a v a  2 s.c  o  m*/
    FieldInsnNode fieldIns = null;

    // needed get right receiver var in case of PUTFIELD

    for (int i = 0; i < instructions.size(); i++) {
        ins = instructions.get(i);
        if (ins instanceof FieldInsnNode) {
            fieldIns = (FieldInsnNode) ins;

            /*
             * Is field referencing outermost instance? if yes, ignore it
             * http://tns-www.lcs.mit.edu/manuals/java-1.1.1/guide/innerclasses/spec/innerclasses.doc10.html
             */
            if (fieldIns.name.endsWith("$0")) {
                continue;
            }

            final int opcode = ins.getOpcode();
            if (opcode == Opcodes.PUTFIELD || opcode == Opcodes.PUTSTATIC) {
                // construction of  
                //   Capturer.capture(final Object receiver, final String methodName, final Object[] methodParams)
                // call
                final InsnList il = new InsnList();

                if (opcode == Opcodes.PUTFIELD) {
                    Type fieldType = Type.getType(fieldIns.desc);
                    if (fieldType.getSize() == 1) {
                        instructions.insertBefore(fieldIns, new InsnNode(Opcodes.DUP2));
                        il.add(new InsnNode(Opcodes.POP));
                    } else if (fieldType.getSize() == 2) {
                        InsnList uglyList = new InsnList();
                        // v, w
                        uglyList.add(new InsnNode(Opcodes.DUP2_X1));
                        // w, v, w
                        uglyList.add(new InsnNode(Opcodes.POP2));
                        // w, v
                        uglyList.add(new InsnNode(Opcodes.DUP));
                        // w, v, v
                        uglyList.add(new InsnNode(Opcodes.DUP2_X2));
                        // v, v, w, v, v
                        uglyList.add(new InsnNode(Opcodes.POP2));
                        // v, v, w
                        instructions.insertBefore(fieldIns, uglyList);
                        // PUTFIELD
                        // v
                    }
                } else
                    il.add(new InsnNode(Opcodes.ACONST_NULL));

                il.add(new LdcInsnNode(this.captureId));
                il.add(new LdcInsnNode(fieldIns.owner));
                il.add(new LdcInsnNode(fieldIns.name));
                il.add(new LdcInsnNode(fieldIns.desc));

                il.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
                        PackageInfo.getNameWithSlash(FieldRegistry.class), "notifyModification",
                        "(Ljava/lang/Object;ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;)V"));

                // PUTFIELDRegistry.notifyModification also adds corresponding GETFIELD capture instructions
                this.captureId++;
                i += il.size();

                instructions.insert(fieldIns, il);
                this.captureId++;
            }
        }
    }
}

From source file:org.formulacompiler.compiler.internal.bytecode.TypeCompilerForBigDecimals.java

License:Open Source License

private final String defineOrReuseStaticConstant(String _value) {
    String result = this.constantPool.get(_value);
    if (result == null) {
        final ClassWriter cw = rootCompiler().cw();
        final GeneratorAdapter ci = rootCompiler().initializer();
        result = "C$" + Integer.toString(this.constantPool.size());
        cw.visitField(Opcodes.ACC_STATIC + Opcodes.ACC_FINAL, result, B, null, null).visitEnd();
        final BigDecimal bigValue = new BigDecimal(_value);
        if (bigValue.precision() <= MAX_LONG_PREC) {
            final long longValue = bigValue.unscaledValue().longValue();
            ci.push(longValue);//from   w  ww  .j  a v a 2  s  . c o m
            ci.push(bigValue.scale());
            ci.visitMethodInsn(Opcodes.INVOKESTATIC, BNAME, "valueOf", LI2B);
            if (needsAdjustment(bigValue)) {
                compileAdjustment(ci);
            }
        } else {
            ci.push(_value);
            compileRuntimeMethod(ci, "newBigDecimal", S2B);
            compileAdjustment(ci);
        }
        ci.visitFieldInsn(Opcodes.PUTSTATIC, rootCompiler().classInternalName(), result, B);
        this.constantPool.put(_value, result);
    }
    return result;
}

From source file:org.formulacompiler.compiler.internal.bytecode.TypeCompilerForPrecisionBigDecimals.java

License:Open Source License

final void buildStaticContext() {
    if (this.staticContextBuilt)
        return;/*ww w  .  ja v  a 2 s.com*/
    this.staticContextBuilt = true;

    final SectionCompiler root = engineCompiler().rootCompiler();
    final ClassWriter cw = root.cw();
    final FieldVisitor fv = cw.visitField(Opcodes.ACC_STATIC + Opcodes.ACC_FINAL, RUNTIME_CONTEXT_NAME,
            RUNTIME_CONTEXT_TYPE.getDescriptor(), null, null);
    fv.visitEnd();

    final GeneratorAdapter mv = root.initializer();
    final MathContext mc = numericType().mathContext();
    if (mc == MathContext.DECIMAL32) {
        compilePredefinedMathContext(mv, "DECIMAL32");
    } else if (mc == MathContext.DECIMAL64) {
        compilePredefinedMathContext(mv, "DECIMAL64");
    } else if (mc == MathContext.DECIMAL128) {
        compilePredefinedMathContext(mv, "DECIMAL128");
    } else if (mc == MathContext.UNLIMITED) {
        compilePredefinedMathContext(mv, "UNLIMITED");
    } else {
        mv.visitTypeInsn(Opcodes.NEW, RUNTIME_CONTEXT_TYPE.getInternalName());
        mv.visitInsn(Opcodes.DUP);
        mv.push(mc.getPrecision());
        final Type modeType = Type.getType(RoundingMode.class);
        mv.getStatic(modeType, mc.getRoundingMode().name(), modeType);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, RUNTIME_CONTEXT_TYPE.getInternalName(), "<init>",
                "(ILjava/math/RoundingMode;)V");
    }
    mv.visitFieldInsn(Opcodes.PUTSTATIC, root.classInternalName(), RUNTIME_CONTEXT_NAME,
            RUNTIME_CONTEXT_DESCRIPTOR);
}

From source file:org.formulacompiler.compiler.internal.bytecode.TypeCompilerForScaledLongs.java

License:Open Source License

final void buildStaticContext() {
    if (this.staticContextBuilt)
        return;//from  w w w .jav a 2  s  .  co m
    this.staticContextBuilt = true;

    final SectionCompiler root = engineCompiler().rootCompiler();
    final ClassWriter cw = root.cw();
    final FieldVisitor fv = cw.visitField(Opcodes.ACC_STATIC + Opcodes.ACC_FINAL, RUNTIME_CONTEXT_NAME,
            RUNTIME_CONTEXT_TYPE.getDescriptor(), null, null);
    fv.visitEnd();

    final GeneratorAdapter mv = root.initializer();
    mv.visitTypeInsn(Opcodes.NEW, RUNTIME_CONTEXT_TYPE.getInternalName());
    mv.visitInsn(Opcodes.DUP);
    mv.push(numericType().scale());
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, RUNTIME_CONTEXT_TYPE.getInternalName(), "<init>", "(I)V");
    mv.visitFieldInsn(Opcodes.PUTSTATIC, root.classInternalName(), RUNTIME_CONTEXT_NAME,
            RUNTIME_CONTEXT_DESCRIPTOR);
}

From source file:org.glassfish.pfl.tf.tools.enhancer.StaticInitVisitor.java

License:Open Source License

@Override
public void visitCode() {
    if (SHORT_FORM) {
        super.visitCode();
        mv.visitLdcInsn(Type.getType("L" + ecd.getClassName() + ";"));
        Type mmrType = Type.getType(MethodMonitorRegistry.class);
        String mdesc = "(Ljava/lang/Class;)V";
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, mmrType.getInternalName(), "registerClass", mdesc);
    } else {//w  ww  .  j a  va  2 s .c om
        int line = 1;
        util.info(2, "StaticInitVisitor.visitCode");
        super.visitCode();

        Label start = new Label();
        Label end = new Label();

        mv.visitLabel(start);

        LocalVariableNode thisClass = defineLocal(mv, "thisClass", Class.class, start, end);
        LocalVariableNode mnameList = defineLocal(mv, "mnameList", List.class, start, end);
        LocalVariableNode holderMap = defineLocal(mv, "holderMap", Map.class, start, end);

        generateTraceMsg(mv, "initialize the holders", line++);
        for (String str : ecd.getAnnotationToHolderName().values()) {
            generateTraceMsg(mv, "Generating to initialize holder " + str, line++);
            util.info(2, "Generating code to initialize holder " + str);
            util.newWithSimpleConstructor(mv, SynchronizedHolder.class);
            mv.visitFieldInsn(Opcodes.PUTSTATIC, ecd.getClassName(), str,
                    Type.getDescriptor(SynchronizedHolder.class));
        }

        generateTraceMsg(mv, "Store the Class of this class", line++);
        mv.visitLdcInsn(Type.getType("L" + ecd.getClassName() + ";"));
        mv.visitVarInsn(Opcodes.ASTORE, thisClass.index);

        generateTraceMsg(mv, "Create list of method names", line++);
        util.newWithSimpleConstructor(mv, ArrayList.class);
        mv.visitVarInsn(Opcodes.ASTORE, mnameList.index);

        for (String str : ecd.getMethodNames()) {
            util.info(2, "Generating code to add " + str + " to methodNames");
            mv.visitVarInsn(Opcodes.ALOAD, mnameList.index);
            mv.visitLdcInsn(str);
            mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/List", "add", "(Ljava/lang/Object;)Z");
            mv.visitInsn(Opcodes.POP);
        }

        generateTraceMsg(mv, "create map from MM annotation class to Holder and init", line++);
        util.newWithSimpleConstructor(mv, HashMap.class);
        mv.visitVarInsn(Opcodes.ASTORE, holderMap.index);

        for (Map.Entry<String, String> entry : ecd.getAnnotationToHolderName().entrySet()) {

            util.info(2,
                    "Generating code to put " + entry.getKey() + "=>" + entry.getValue() + " into holderMap");

            mv.visitVarInsn(Opcodes.ALOAD, holderMap.index);

            Type annoType = Type.getType("L" + entry.getKey() + ";");
            mv.visitLdcInsn(annoType);

            mv.visitFieldInsn(Opcodes.GETSTATIC, ecd.getClassName(), entry.getValue(),
                    Type.getDescriptor(SynchronizedHolder.class));

            mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Map", "put",
                    "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");

            mv.visitInsn(Opcodes.POP);
        }

        generateTraceMsg(mv, "register with MethodMonitorRegistry", line++);
        util.info(2, "Generating code call MethodMonitorRegistry.registerClass");
        mv.visitVarInsn(Opcodes.ALOAD, thisClass.index);
        mv.visitVarInsn(Opcodes.ALOAD, mnameList.index);
        mv.visitVarInsn(Opcodes.ALOAD, holderMap.index);

        Type mmrType = Type.getType(MethodMonitorRegistry.class);
        String mdesc = "(Ljava/lang/Class;Ljava/util/List;Ljava/util/Map;)V";
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, mmrType.getInternalName(), "registerClass", mdesc);

        mv.visitLabel(end);

        thisClass.accept(mv);
        mnameList.accept(mv);
        holderMap.accept(mv);
    }
}

From source file:org.greencheek.gc.memusage.agent.AddStaticAtomicLongInitializer.java

License:Apache License

@Override
public void visitCode() {
    super.visitCode();
    // build my static initializer by calling
    // visitFieldInsn(int opcode, String owner, String name, String desc)
    // or the/*from   w w  w  .j a v  a2 s . com*/
    for (MethodInfo method : annotatedMeasurableMethods) {
        System.out.println("Adding static initialiser for: " + method.getAnnotatedClassName() + "."
                + method.getFieldName());
        mv.visitTypeInsn(Opcodes.NEW, "java/util/concurrent/atomic/AtomicLong");
        mv.visitInsn(Opcodes.DUP);
        mv.visitInsn(Opcodes.LCONST_0);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/concurrent/atomic/AtomicLong", "<init>", "(J)V");
        mv.visitFieldInsn(Opcodes.PUTSTATIC, method.getAnnotatedClassName(), method.getFieldName(),
                "Ljava/util/concurrent/atomic/AtomicLong;");
    }
    System.out.println();
}

From source file:org.jacoco.core.internal.instr.ClassFieldProbeArrayStrategy.java

License:Open Source License

/**
 * Generates the byte code to initialize the static coverage data field
 * within this class./*from w  w  w .  j  ava  2  s .  c  om*/
 *
 * The code will push the [Z data array on the operand stack.
 *
 * @param mv
 *            generator to emit code to
 */
private int genInitializeDataField(final MethodVisitor mv, final int probeCount) {
    final int size = accessorGenerator.generateDataAccessor(classId, className, probeCount, mv);

    // Stack[0]: [Z

    mv.visitInsn(Opcodes.DUP);

    // Stack[1]: [Z
    // Stack[0]: [Z

    mv.visitFieldInsn(Opcodes.PUTSTATIC, className, InstrSupport.DATAFIELD_NAME, InstrSupport.DATAFIELD_DESC);

    // Stack[0]: [Z

    return Math.max(size, 2); // Maximum local stack size is 2
}

From source file:org.jacoco.core.internal.instr.FrameTracker.java

License:Open Source License

@Override
public void visitFieldInsn(final int opcode, final String owner, final String name, final String desc) {
    final Type t = Type.getType(desc);
    switch (opcode) {
    case Opcodes.PUTSTATIC:
        pop(t);// w ww . j  a v a2  s  . c om
        break;
    case Opcodes.PUTFIELD:
        pop(t);
        pop(1);
        break;
    case Opcodes.GETSTATIC:
        push(t);
        break;
    case Opcodes.GETFIELD:
        pop(1);
        push(t);
        break;
    default:
        throw new IllegalArgumentException();
    }
    mv.visitFieldInsn(opcode, owner, name, desc);
}

From source file:org.jacoco.core.internal.instr.InterfaceFieldProbeArrayStrategy.java

License:Open Source License

public int storeInstance(final MethodVisitor mv, final boolean clinit, final int variable) {
    if (clinit) {
        final int maxStack = accessorGenerator.generateDataAccessor(classId, className, probeCount, mv);

        // Stack[0]: [Z

        mv.visitInsn(Opcodes.DUP);/*from  w w  w .  ja  v a2 s.  c o m*/

        // Stack[1]: [Z
        // Stack[0]: [Z

        mv.visitFieldInsn(Opcodes.PUTSTATIC, className, InstrSupport.DATAFIELD_NAME,
                InstrSupport.DATAFIELD_DESC);

        // Stack[0]: [Z

        mv.visitVarInsn(Opcodes.ASTORE, variable);

        seenClinit = true;
        return Math.max(maxStack, 2);
    } else {
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, className, InstrSupport.INITMETHOD_NAME,
                InstrSupport.INITMETHOD_DESC, true);
        mv.visitVarInsn(Opcodes.ASTORE, variable);
        return 1;
    }
}

From source file:org.jacoco.core.internal.instr.InterfaceFieldProbeArrayStrategy.java

License:Open Source License

private void createClinitMethod(final ClassVisitor cv, final int probeCount) {
    final MethodVisitor mv = cv.visitMethod(InstrSupport.CLINIT_ACC, InstrSupport.CLINIT_NAME,
            InstrSupport.CLINIT_DESC, null, null);
    mv.visitCode();/*from w  w w .j ava2  s .co  m*/

    final int maxStack = accessorGenerator.generateDataAccessor(classId, className, probeCount, mv);

    // Stack[0]: [Z

    mv.visitFieldInsn(Opcodes.PUTSTATIC, className, InstrSupport.DATAFIELD_NAME, InstrSupport.DATAFIELD_DESC);

    mv.visitInsn(Opcodes.RETURN);

    mv.visitMaxs(maxStack, 0);
    mv.visitEnd();
}