Example usage for org.objectweb.asm Opcodes PUTFIELD

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

Introduction

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

Prototype

int PUTFIELD

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

Click Source Link

Usage

From source file:edu.illinois.nondex.instr.WeakHashMapShufflingAdder.java

License:Open Source License

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    if ("hasNext".equals(name)) {
        return super.visitMethod(access, "originalHasNext", desc, signature, exceptions);
    }/*w ww .j av a 2s .  c o  m*/
    if ("nextEntry".equals(name)) {
        return super.visitMethod(access, "originalNextEntry", desc, signature, exceptions);
    }
    if ("<init>".equals(name)) {
        return new MethodVisitor(Opcodes.ASM5, super.visitMethod(access, name, desc, signature, exceptions)) {
            @Override
            public void visitInsn(int opcode) {
                if (opcode == Opcodes.RETURN) {
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitVarInsn(Opcodes.ALOAD, 1);
                    super.visitFieldInsn(Opcodes.PUTFIELD, "java/util/WeakHashMap$HashIterator", "this$0",
                            "Ljava/util/WeakHashMap;");
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitInsn(Opcodes.ACONST_NULL);
                    super.visitFieldInsn(Opcodes.PUTFIELD, "java/util/WeakHashMap$HashIterator", "entry",
                            "Ljava/util/WeakHashMap$Entry;");
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitInsn(Opcodes.ACONST_NULL);
                    super.visitFieldInsn(Opcodes.PUTFIELD, "java/util/WeakHashMap$HashIterator", "lastReturned",
                            "Ljava/util/WeakHashMap$Entry;");
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitFieldInsn(Opcodes.GETFIELD, "java/util/WeakHashMap$HashIterator", "this$0",
                            "Ljava/util/WeakHashMap;");
                    super.visitFieldInsn(Opcodes.GETFIELD, "java/util/WeakHashMap", "modCount", "I");
                    super.visitFieldInsn(Opcodes.PUTFIELD, "java/util/WeakHashMap$HashIterator",
                            "expectedModCount", "I");
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitInsn(Opcodes.ACONST_NULL);
                    super.visitFieldInsn(Opcodes.PUTFIELD, "java/util/WeakHashMap$HashIterator", "nextKey",
                            "Ljava/lang/Object;");
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitInsn(Opcodes.ACONST_NULL);
                    super.visitFieldInsn(Opcodes.PUTFIELD, "java/util/WeakHashMap$HashIterator", "currentKey",
                            "Ljava/lang/Object;");
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitInsn(Opcodes.ACONST_NULL);
                    super.visitFieldInsn(Opcodes.PUTFIELD, "java/util/WeakHashMap$HashIterator", "iter",
                            "Ljava/util/Iterator;");
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitVarInsn(Opcodes.ALOAD, 1);
                    super.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/util/WeakHashMap", "isEmpty", "()Z",
                            false);
                    Label l0 = new Label();
                    super.visitJumpInsn(Opcodes.IFEQ, l0);
                    super.visitInsn(Opcodes.ICONST_0);
                    Label l1 = new Label();
                    super.visitJumpInsn(Opcodes.GOTO, l1);
                    super.visitLabel(l0);
                    super.visitFrame(Opcodes.F_FULL, 2,
                            new Object[] { "java/util/WeakHashMap$HashIterator", "java/util/WeakHashMap" }, 1,
                            new Object[] { "java/util/WeakHashMap$HashIterator" });
                    super.visitVarInsn(Opcodes.ALOAD, 1);
                    super.visitFieldInsn(Opcodes.GETFIELD, "java/util/WeakHashMap", "table",
                            "[Ljava/util/WeakHashMap$Entry;");
                    super.visitInsn(Opcodes.ARRAYLENGTH);
                    super.visitLabel(l1);
                    super.visitFrame(Opcodes.F_FULL, 2,
                            new Object[] { "java/util/WeakHashMap$HashIterator", "java/util/WeakHashMap" }, 2,
                            new Object[] { "java/util/WeakHashMap$HashIterator", Opcodes.INTEGER });
                    super.visitFieldInsn(Opcodes.PUTFIELD, "java/util/WeakHashMap$HashIterator", "index", "I");
                    super.visitTypeInsn(Opcodes.NEW, "java/util/ArrayList");
                    super.visitInsn(Opcodes.DUP);
                    super.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/ArrayList", "<init>", "()V", false);
                    super.visitVarInsn(Opcodes.ASTORE, 2);
                    Label l2 = new Label();
                    super.visitLabel(l2);
                    super.visitFrame(Opcodes.F_APPEND, 1, new Object[] { "java/util/List" }, 0, null);
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/util/WeakHashMap$HashIterator",
                            "originalHasNext", "()Z", false);
                    Label l3 = new Label();
                    super.visitJumpInsn(Opcodes.IFEQ, l3);
                    super.visitVarInsn(Opcodes.ALOAD, 2);
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/util/WeakHashMap$HashIterator",
                            "originalNextEntry", "()Ljava/util/WeakHashMap$Entry;", false);
                    super.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/List", "add",
                            "(Ljava/lang/Object;)Z", true);
                    super.visitInsn(Opcodes.POP);
                    super.visitJumpInsn(Opcodes.GOTO, l2);
                    super.visitLabel(l3);
                    super.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
                    super.visitVarInsn(Opcodes.ALOAD, 2);
                    super.visitMethodInsn(Opcodes.INVOKESTATIC,
                            "edu/illinois/nondex/shuffling/ControlNondeterminism", "shuffle",
                            "(Ljava/util/List;)Ljava/util/List;", false);
                    super.visitVarInsn(Opcodes.ASTORE, 2);
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitVarInsn(Opcodes.ALOAD, 2);
                    super.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/List", "iterator",
                            "()Ljava/util/Iterator;", true);
                    super.visitFieldInsn(Opcodes.PUTFIELD, "java/util/WeakHashMap$HashIterator", "iter",
                            "Ljava/util/Iterator;");
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitInsn(Opcodes.ACONST_NULL);
                    super.visitFieldInsn(Opcodes.PUTFIELD, "java/util/WeakHashMap$HashIterator", "lastReturned",
                            "Ljava/util/WeakHashMap$Entry;");
                }
                super.visitInsn(opcode);
            }
        };
    }
    return super.visitMethod(access, name, desc, signature, exceptions);
}

From source file:edu.mit.streamjit.util.bytecode.MethodResolver.java

License:Open Source License

private void interpret(FieldInsnNode insn, FrameState frame, BBInfo block) {
    Klass k = getKlassByInternalName(insn.owner);
    Field f = k.getField(insn.name);
    switch (insn.getOpcode()) {
    case Opcodes.GETSTATIC:
        LoadInst li = new LoadInst(f);
        block.block.instructions().add(li);
        frame.stack.push(li);/*  w w w  .j a va2 s  .c  om*/
        break;
    case Opcodes.GETFIELD:
        LoadInst li2 = new LoadInst(f, frame.stack.pop());
        block.block.instructions().add(li2);
        frame.stack.push(li2);
        break;
    case Opcodes.PUTSTATIC:
        StoreInst si = new StoreInst(f, frame.stack.pop());
        block.block.instructions().add(si);
        break;
    case Opcodes.PUTFIELD:
        StoreInst si2 = new StoreInst(f, frame.stack.pop(), frame.stack.pop());
        block.block.instructions().add(si2);
        break;
    default:
        throw new UnsupportedOperationException("" + insn.getOpcode());
    }
}

From source file:edu.mit.streamjit.util.bytecode.MethodUnresolver.java

License:Open Source License

private void emit(StoreInst i, InsnList insns) {
    Value location = i.getLocation();//  www .  j av  a  2 s . com
    if (location instanceof LocalVariable) {
        load(i.getData(), insns);
        store(location, insns);
    } else {
        Field f = (Field) location;
        if (!f.isStatic())
            load(i.getInstance(), insns);
        load(i.getData(), insns);
        insns.add(new FieldInsnNode(f.isStatic() ? Opcodes.PUTSTATIC : Opcodes.PUTFIELD,
                internalName(f.getParent()), f.getName(), f.getType().getFieldType().getDescriptor()));
    }
}

From source file:edu.ubc.mirrors.holograms.HologramClassGenerator.java

License:Open Source License

@Override
public void visitEnd() {
    // Generate the static field used to store the corresponding ClassMirror
    int staticAccess = Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL | Opcodes.ACC_STATIC;
    super.visitField(staticAccess, "classMirror", classMirrorType.getDescriptor(), null, null);

    // Generate the constructor that takes a mirror instance as an Object parameter
    String constructorDesc = Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(Object.class));
    if (name.equals(getHologramType(Type.getType(Throwable.class), true).getInternalName())) {
        // This doesn't extend ObjectHologram so we have to set the field directly
        super.visitField(Opcodes.ACC_PUBLIC, "mirror", objectMirrorType.getDescriptor(), null, null);

        MethodVisitor methodVisitor = super.visitMethod(Opcodes.ACC_PUBLIC, "<init>", constructorDesc, null,
                null);//from w  w  w.  j av a 2  s . c om
        methodVisitor.visitCode();
        methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
        methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, superName, "<init>",
                Type.getMethodDescriptor(Type.VOID_TYPE));
        methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
        methodVisitor.visitVarInsn(Opcodes.ALOAD, 1);
        methodVisitor.visitTypeInsn(Opcodes.CHECKCAST, objectMirrorType.getInternalName());
        methodVisitor.visitFieldInsn(Opcodes.PUTFIELD, name, "mirror", Type.getDescriptor(ObjectMirror.class));
        methodVisitor.visitInsn(Opcodes.RETURN);
        methodVisitor.visitMaxs(2, 2);
        methodVisitor.visitEnd();

        methodVisitor = super.visitMethod(Opcodes.ACC_PUBLIC, "getMirror",
                Type.getMethodDescriptor(objectMirrorType), null, null);
        methodVisitor.visitCode();
        methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
        methodVisitor.visitFieldInsn(Opcodes.GETFIELD, name, "mirror", Type.getDescriptor(ObjectMirror.class));
        methodVisitor.visitInsn(Opcodes.ARETURN);
        methodVisitor.visitMaxs(1, 1);
        methodVisitor.visitEnd();
    } else if (!isInterface) {
        MethodVisitor methodVisitor = super.visitMethod(Opcodes.ACC_PUBLIC, "<init>", constructorDesc, null,
                null);
        methodVisitor.visitCode();
        methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
        methodVisitor.visitVarInsn(Opcodes.ALOAD, 1);
        methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, superName, "<init>", constructorDesc);
        methodVisitor.visitInsn(Opcodes.RETURN);
        methodVisitor.visitMaxs(2, 2);
        methodVisitor.visitEnd();
    }

    // Add a class initialization method to initialize the static fields,
    // if one doesn't exist already.
    if (!hasClinit) {
        InstructionAdapter mv = new InstructionAdapter(
                super.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "<clinit>", "()V", null, null));
        mv.visitCode();
        HologramMethodGenerator.initializeStaticFields(Type.getObjectType(this.name), mv);
        mv.areturn(Type.VOID_TYPE);
        mv.visitMaxs(2, 2);
        mv.visitEnd();
    }

    super.visitEnd();
}

From source file:edu.ubc.mirrors.holograms.HologramMethodGenerator.java

License:Open Source License

@Override
public void visitFieldInsn(int opcode, String owner, String name, String desc) {
    boolean isSet = (opcode == Opcodes.PUTFIELD || opcode == Opcodes.PUTSTATIC);
    boolean isStatic = (opcode == Opcodes.GETSTATIC || opcode == Opcodes.PUTSTATIC);

    Type fieldType = Type.getType(desc);
    int setValueLocal = -1;

    if (isStatic) {
        // For a static field the instance is null
        if (isSet) {
            setValueLocal = lvs.newLocal(fieldType);
            store(setValueLocal, fieldType);
        }/*from  w  w  w. j a v a 2  s.  c o m*/
        aconst(null);
        if (isSet) {
            load(setValueLocal, fieldType);
        }
    } else {
        // If this is an "uninitialized this", the mirror is the nth argument instead
        // of the mirror field on ObjectHologram.
        Object stackType = stackType(isSet ? 1 : 0);
        if (stackType == Opcodes.UNINITIALIZED_THIS) {
            // Pop the original argument
            if (isSet) {
                setValueLocal = lvs.newLocal(fieldType);
                store(setValueLocal, fieldType);
            }

            pop();
            load((methodType.getArgumentsAndReturnSizes() >> 2) - 1, instanceMirrorType);

            MethodHandle.OBJECT_HOLOGRAM_MAKE.invoke(this);

            if (isSet) {
                load(setValueLocal, fieldType);
            }
        }
    }

    getClassMirror(Type.getObjectType(owner));
    aconst(name);

    Type fieldTypeForMirrorCall = fieldType;
    int fieldSort = fieldType.getSort();
    String suffix = "";
    if (fieldSort == Type.ARRAY || fieldSort == Type.OBJECT) {
        fieldTypeForMirrorCall = hologramType;
    } else {
        suffix = HologramClassGenerator.getSortName(fieldSort);
    }

    // Call the appropriate getter/setter method on the mirror
    String methodDesc;
    if (isSet) {
        methodDesc = Type.getMethodDescriptor(Type.VOID_TYPE, hologramType, fieldTypeForMirrorCall,
                classMirrorType, stringType);
    } else {
        methodDesc = Type.getMethodDescriptor(fieldTypeForMirrorCall, hologramType, classMirrorType,
                stringType);
    }
    invokestatic(instanceHologramType.getInternalName(), (isSet ? "set" : "get") + suffix + "Field",
            methodDesc);

    if (!isSet && fieldTypeForMirrorCall.equals(hologramType)) {
        checkcast(fieldType);
    }
}

From source file:edu.umd.cs.findbugs.ba.XFactory.java

License:Open Source License

public static XField createReferencedXField(DismantleBytecode visitor) {
    int seen = visitor.getOpcode();
    if (seen != Opcodes.GETFIELD && seen != Opcodes.GETSTATIC && seen != Opcodes.PUTFIELD
            && seen != Opcodes.PUTSTATIC) {
        throw new IllegalArgumentException("Not at a field reference");
    }//from  w  w w. j  a  va  2s  .c  om
    return createXFieldUsingSlashedClassName(visitor.getClassConstantOperand(),
            visitor.getNameConstantOperand(), visitor.getSigConstantOperand(), visitor.getRefFieldIsStatic());
}

From source file:edu.umd.cs.guitar.testcase.plugin.edg.ClassDBVisitor.java

License:Open Source License

@Override
public void visitFieldInsn(int opcode, String owner, String name, String desc) {
    currentMethod.setEmpty(false);/*from  w  ww. j ava 2  s. c  om*/
    Class c;
    Field f;
    switch (opcode) {
    case Opcodes.GETFIELD:
    case Opcodes.GETSTATIC:
        c = getClass(owner);
        f = getField(c, name);
        f.setDesc(desc);
        // read after write is not a real read
        // if (!currentMethod.hasWrite(f)){
        currentMethod.addRead(f);
        labelReads.add(f);
        // }
        break;
    case Opcodes.PUTFIELD:
    case Opcodes.PUTSTATIC:
        c = getClass(owner);
        f = getField(c, name);
        f.setDesc(desc);
        currentMethod.addWrite(f);
        labelWrites.add(f);
        break;
    }
}

From source file:erjang.EFun.java

License:Apache License

static byte[] get_exported_fun_class_data(int arity) {
    /* Code template://from w w w .  j av  a2s  . c  om
     * public abstract class EFun{arity}Exported extends EFun{arity} {
     *   protected final EAtom module_name, function_name;
     *   protected EFun{arity}Exported(String m, String f) {
     *     module_name   = EAtom.intern(m);
     *     function_name = EAtom.intern(f);
     *   }
     * }
     */

    ensure(arity); // Ensure presence of superclass.
    String super_type = EFUN_TYPE.getInternalName() + arity;
    String self_type = super_type + "Exported";

    ClassWriter cw = new ClassWriter(true);
    cw.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC | Opcodes.ACC_ABSTRACT, self_type, null, super_type, null);

    cw.visitField(ACC_PROTECTED | ACC_FINAL, "module_name", EATOM_TYPE.getDescriptor(), null, null).visitEnd();

    cw.visitField(ACC_PROTECTED | ACC_FINAL, "function_name", EATOM_TYPE.getDescriptor(), null, null)
            .visitEnd();

    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PROTECTED, "<init>",
            "(Ljava/lang/String;Ljava/lang/String;)V", null, null);

    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, super_type, "<init>", "()V");

    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, EATOM_TYPE.getInternalName(), "intern",
            "(Ljava/lang/String;)Lerjang/EAtom;");
    mv.visitFieldInsn(Opcodes.PUTFIELD, self_type, "module_name", EATOM_DESC);

    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ALOAD, 2);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, EATOM_TYPE.getInternalName(), "intern",
            "(Ljava/lang/String;)Lerjang/EAtom;");
    mv.visitFieldInsn(Opcodes.PUTFIELD, self_type, "function_name", EATOM_DESC);

    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(3, 3);
    mv.visitEnd();

    make_encode_method_for_exported(cw, self_type, arity);

    cw.visitEnd();

    byte[] data = cw.toByteArray();
    return data;
}

From source file:erjang.ETuple.java

License:Apache License

private static void create_tuple_set(int n_cells, ClassAdapter cw, String this_class_name) {
    MethodVisitor mv;// w ww .j ava 2s.  co m
    mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "set", "(I" + ETERM_TYPE.getDescriptor() + ")V", null, null);
    mv.visitCode();

    Label dflt = new Label();
    Label[] labels = new Label[n_cells];
    for (int i = 0; i < n_cells; i++) {
        labels[i] = new Label();
    }

    mv.visitVarInsn(Opcodes.ILOAD, 1);
    mv.visitTableSwitchInsn(1, n_cells, dflt, labels);

    for (int zbase = 0; zbase < n_cells; zbase++) {

        mv.visitLabel(labels[zbase]);

        mv.visitVarInsn(Opcodes.ALOAD, 0); // load this
        mv.visitVarInsn(Opcodes.ALOAD, 2); // load term

        String field = "elem" + (zbase + 1);

        mv.visitFieldInsn(Opcodes.PUTFIELD, this_class_name, field, ETERM_TYPE.getDescriptor());
        mv.visitInsn(Opcodes.RETURN);
    }

    mv.visitLabel(dflt);

    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ILOAD, 1);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, ETUPLE_NAME, "bad_nth", "(I)" + ETERM_TYPE.getDescriptor());
    mv.visitInsn(Opcodes.POP);
    mv.visitInsn(Opcodes.RETURN); // make compiler happy

    mv.visitMaxs(3, 3);
    mv.visitEnd();
}

From source file:erjang.ETuple.java

License:Apache License

@Override
public Type emit_const(MethodVisitor fa) {

    Type type = Type.getType(this.getClass());

    fa.visitTypeInsn(Opcodes.NEW, type.getInternalName());
    fa.visitInsn(Opcodes.DUP);/*from   www. j a v a 2s.  c o  m*/
    fa.visitMethodInsn(Opcodes.INVOKESPECIAL, type.getInternalName(), "<init>", "()V");

    for (int i = 0; i < arity(); i++) {
        fa.visitInsn(Opcodes.DUP);

        ((EObject) elm(i + 1)).emit_const(fa);

        fa.visitFieldInsn(Opcodes.PUTFIELD, type.getInternalName(), "elem" + (i + 1),
                ETERM_TYPE.getDescriptor());
    }

    return type;
}