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:org.spongepowered.common.mixin.core.entity.living.MixinEntityLiving.java

License:MIT License

@Inject(method = "clearLeashed", at = @At(value = "FIELD", target = "net/minecraft/entity/EntityLiving.isLeashed : Z", opcode = Opcodes.PUTFIELD), cancellable = true)
public void callUnleashEvent(boolean sendPacket, boolean dropLead, CallbackInfo ci) {
    final EntityUnleashEvent event = SpongeEventFactory.createEntityUnleash(Sponge.getGame(), this,
            (Entity) getLeashedToEntity());
    Sponge.getGame().getEventManager().post(event);
    if (event.isCancelled()) {
        ci.cancel();/* w  w  w.j a  va2  s . com*/
    }
}

From source file:org.spongepowered.common.mixin.core.entity.MixinEntityLiving.java

License:MIT License

@Inject(method = "clearLeashed", at = @At(value = "FIELD", target = "Lnet/minecraft/entity/EntityLiving;isLeashed:Z", opcode = Opcodes.PUTFIELD), cancellable = true)
public void callUnleashEvent(boolean sendPacket, boolean dropLead, CallbackInfo ci) {
    net.minecraft.entity.Entity entity = getLeashedToEntity();
    if (!this.worldObj.isRemote) {
        Entity leashedEntity = (Entity) (Object) this;
        UnleashEntityEvent event = SpongeEventFactory
                .createUnleashEntityEvent(entity == null ? Cause.of(NamedCause.of("Self", leashedEntity))
                        : Cause.of(NamedCause.source(entity)), leashedEntity);
        SpongeImpl.postEvent(event);//w w w.j  a va  2  s .  c o m
        if (event.isCancelled()) {
            ci.cancel();
        }
    }
}

From source file:org.spongepowered.mod.asm.util.ASMEventListenerFactory.java

License:MIT License

@SuppressWarnings("unchecked")
private static <T> Class<T> createClass(Class<T> interf, Method input, Method output) {

    String className = getClassName(interf, input, output);

    ClassWriter cwBase = new ClassWriter(0);
    CheckClassAdapter cw = new CheckClassAdapter(cwBase);

    MethodVisitor mv;//from   w  w w . j a v a2 s.c o m

    String classNameDesc = className.replace('.', '/');

    String interfaceInternalName = Type.getInternalName(interf);

    String inputName = input.getName();
    String inputMethodDescriptor = Type.getMethodDescriptor(input);

    String outputParameterTypeIntName = Type.getInternalName(output.getParameterTypes()[0]);
    String outputTargetTypeIntName = Type.getInternalName(output.getDeclaringClass());
    String outputMethodDescriptor = Type.getMethodDescriptor(output);
    String outputName = output.getName();

    boolean isOutputInterface = output.getDeclaringClass().isInterface();

    // A new class of the following form is created, with a unique name
    //
    // package org.spongepowered.mod.asm;
    // public class <className> extends java.lang.Object implements <interf>
    //
    //     private final Object target
    //
    //     public <className> (java.lang.Object target) {
    //         super();
    //         this.target = target;
    //         return;
    //     }
    //
    //     public void <inputMethod> (<inputMethodType event) {
    //         ((outputTargetType) this.target).outputMethod((outputParameteType) event);
    //         return
    //     }
    // }

    // package org.spongepowered.mod.asm;
    // public class <className> extends java.lang.Object implements <interf>
    cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER, classNameDesc, null, "java/lang/Object",
            new String[] { interfaceInternalName });

    // private final Object target
    cw.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL, "target", "Ljava/lang/Object;", null, null);

    // Constructor

    // public UniqueClass (java.lang.Object target) {
    mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "(Ljava/lang/Object;)V", null, null);
    mv.visitCode();

    // super();
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);

    // this.target = target;
    mv.visitVarInsn(Opcodes.ALOAD, 0); // Loads this
    mv.visitVarInsn(Opcodes.ALOAD, 1); // Loads target (from input)
    mv.visitFieldInsn(Opcodes.PUTFIELD, classNameDesc, "target", "Ljava/lang/Object;");

    // return;
    mv.visitInsn(Opcodes.RETURN);

    // }
    // 2 localvars due to inputs: this, target
    // 2 items on stack after double ALOAD
    mv.visitMaxs(2, 2);
    mv.visitEnd();

    // Callback method

    // public void <inputMethod> (<inputMethodType event) {
    mv = cw.visitMethod(Opcodes.ACC_PUBLIC, inputName, inputMethodDescriptor, null, null);
    mv.visitCode();

    // push((casted) this.target)
    mv.visitVarInsn(Opcodes.ALOAD, 0); // Loads this
    mv.visitFieldInsn(Opcodes.GETFIELD, classNameDesc, "target", "Ljava/lang/Object;");
    mv.visitTypeInsn(Opcodes.CHECKCAST, outputTargetTypeIntName);

    // push((casted) event)
    mv.visitVarInsn(Opcodes.ALOAD, 1); // Loads method parameter 0
    mv.visitTypeInsn(Opcodes.CHECKCAST, outputParameterTypeIntName);

    // ((outputTargetType) this.target).outputMethod((outputParameteType) event);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, outputTargetTypeIntName, outputName, outputMethodDescriptor,
            isOutputInterface);

    // return
    mv.visitInsn(Opcodes.RETURN);

    // }
    mv.visitMaxs(2, 2);
    mv.visitEnd();

    cw.visitEnd();

    byte[] bytes = cwBase.toByteArray();

    return (Class<T>) loader.defineClass(className, bytes);
}

From source file:org.springsource.loaded.test.infra.MethodPrinter.java

License:Apache License

public void visitFieldInsn(int opcode, String owner, String name, String desc) {
    if (opcode == Opcodes.GETSTATIC) {
        to.println("    GETSTATIC " + owner + "." + name + " " + desc);
    } else if (opcode == Opcodes.PUTSTATIC) {
        to.println("    PUTSTATIC " + owner + "." + name + " " + desc);
    } else if (opcode == Opcodes.GETFIELD) {
        to.println("    GETFIELD " + owner + "." + name + " " + desc);
    } else if (opcode == Opcodes.PUTFIELD) {
        to.println("    PUTFIELD " + owner + "." + name + " " + desc);
    } else {//from   w  ww.j av a2s  . c o  m
        throw new IllegalStateException(":" + opcode);
    }
}

From source file:org.teavm.flavour.regex.bytecode.MatcherClassBuilder.java

License:Apache License

private void buildConstructor(ClassVisitor cv, String className) {
    MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitCode();//w w  w  .java2  s .  c o  m

    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);

    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitInsn(Opcodes.ICONST_0);
    mv.visitFieldInsn(Opcodes.PUTFIELD, className, "state", "I");

    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitInsn(Opcodes.ICONST_M1);
    mv.visitFieldInsn(Opcodes.PUTFIELD, className, "domain", "I");

    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitInsn(Opcodes.ICONST_0);
    mv.visitFieldInsn(Opcodes.PUTFIELD, className, "index", "I");

    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(2, 1);
    mv.visitEnd();
}

From source file:org.teavm.flavour.regex.bytecode.MatcherClassBuilder.java

License:Apache License

private void buildRestartMethod(ClassVisitor cv, String className) {
    MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC, "restart", "()" + Type.getDescriptor(Matcher.class),
            null, null);/*from www. j  a v  a 2 s  .  co m*/
    mv.visitCode();

    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitInsn(Opcodes.ICONST_0);
    mv.visitFieldInsn(Opcodes.PUTFIELD, className, "domain", "I");
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitInsn(Opcodes.ICONST_0);
    mv.visitFieldInsn(Opcodes.PUTFIELD, className, "state", "I");
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitInsn(Opcodes.ARETURN);

    mv.visitMaxs(2, 1);
    mv.visitEnd();
}

From source file:org.teavm.flavour.regex.bytecode.MatcherClassBuilder.java

License:Apache License

private void buildForkMethod(ClassVisitor cv, String className) {
    MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC, "fork", "()" + Type.getDescriptor(Matcher.class),
            null, null);/*w w  w  . j  a v a2 s .  c  om*/
    mv.visitCode();

    mv.visitTypeInsn(Opcodes.NEW, className);
    mv.visitInsn(Opcodes.DUP);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, className, "<init>", "()V", false);

    mv.visitInsn(Opcodes.DUP);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, className, "domain", "I");
    mv.visitFieldInsn(Opcodes.PUTFIELD, className, "domain", "I");

    mv.visitInsn(Opcodes.DUP);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, className, "state", "I");
    mv.visitFieldInsn(Opcodes.PUTFIELD, className, "state", "I");

    mv.visitInsn(Opcodes.DUP);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, className, "index", "I");
    mv.visitFieldInsn(Opcodes.PUTFIELD, className, "index", "I");

    mv.visitInsn(Opcodes.ARETURN);

    mv.visitMaxs(2, 1);
    mv.visitEnd();
}

From source file:org.teavm.flavour.regex.bytecode.MatcherClassBuilder.java

License:Apache License

private void buildEndMethod(ClassVisitor cv, String className, Dfa dfa) {
    MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC, "end", "()" + Type.getDescriptor(Matcher.class), null,
            null);/*ww  w.  ja v a 2 s  .  c o m*/

    stateLabels = new Label[dfa.getStates().size()];
    Arrays.setAll(stateLabels, i -> new Label());
    int[] keys = new int[dfa.getStates().size()];
    Arrays.setAll(keys, IntUnaryOperator.identity());

    saveLabel = new Label();
    errorLabel = new Label();

    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, className, "state", "I");
    mv.visitLookupSwitchInsn(errorLabel, keys, stateLabels);

    for (int i = 0; i < dfa.getStates().size(); ++i) {
        mv.visitLabel(stateLabels[i]);
        DfaTransition transition = dfa.getStates().get(i).getTransition(-1);
        if (transition == null) {
            mv.visitJumpInsn(Opcodes.GOTO, errorLabel);
        } else {
            DfaState target = transition.getTarget();
            mv.visitIntInsn(Opcodes.SIPUSH, transition.getTarget().getIndex());
            mv.visitVarInsn(Opcodes.ISTORE, 1);
            mv.visitIntInsn(Opcodes.SIPUSH, !target.isTerminal() ? -1 : target.getDomains()[0]);
            mv.visitVarInsn(Opcodes.ISTORE, 2);
            debug(mv, "DFA: " + i + " .-> " + target.getIndex() + " " + Arrays.toString(target.getDomains()));
            mv.visitJumpInsn(Opcodes.GOTO, saveLabel);
        }
    }

    mv.visitLabel(errorLabel);
    debug(mv, "DFA: error");
    mv.visitInsn(Opcodes.ICONST_M1);
    mv.visitVarInsn(Opcodes.ISTORE, 1);
    mv.visitInsn(Opcodes.ICONST_M1);
    mv.visitVarInsn(Opcodes.ISTORE, 2);
    mv.visitLabel(saveLabel);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ILOAD, 1);
    mv.visitFieldInsn(Opcodes.PUTFIELD, className, "state", "I");
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ILOAD, 2);
    mv.visitFieldInsn(Opcodes.PUTFIELD, className, "domain", "I");
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitInsn(Opcodes.ARETURN);

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

From source file:org.teavm.flavour.regex.bytecode.MatcherClassBuilder.java

License:Apache License

private void buildWorkerMethod(ClassVisitor cv, String className, Dfa dfa) {
    MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC, "feed",
            "(Ljava/lang/String;IIZ)" + Type.getDescriptor(Matcher.class), null, null);
    mv.visitCode();/*from  w ww  .  ja va2  s  .c  o m*/

    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, className, "state", "I");
    mv.visitVarInsn(Opcodes.ISTORE, 5);

    errorLabel = new Label();
    saveLabel = new Label();
    loopLabel = new Label();
    continueLabel = new Label();

    mv.visitLabel(loopLabel);
    generateLengthGuard(mv);

    stateLabels = new Label[dfa.getStates().size()];
    Arrays.setAll(stateLabels, i -> new Label());
    int[] keys = new int[dfa.getStates().size()];
    Arrays.setAll(keys, IntUnaryOperator.identity());

    mv.visitVarInsn(Opcodes.ILOAD, 5);
    mv.visitLookupSwitchInsn(errorLabel, keys, stateLabels);

    mv.visitLabel(continueLabel);
    mv.visitIincInsn(2, 1);
    mv.visitJumpInsn(Opcodes.GOTO, loopLabel);

    mv.visitLabel(errorLabel);
    debug(mv, "DFA: error");
    mv.visitInsn(Opcodes.ICONST_M1);
    mv.visitVarInsn(Opcodes.ISTORE, 5);

    mv.visitLabel(saveLabel);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ILOAD, 5);
    mv.visitFieldInsn(Opcodes.PUTFIELD, className, "state", "I");
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ILOAD, 2);
    mv.visitFieldInsn(Opcodes.PUTFIELD, className, "index", "I");
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitInsn(Opcodes.ARETURN);

    for (int i = 0; i < dfa.getStates().size(); ++i) {
        mv.visitLabel(stateLabels[i]);
        DfaState state = dfa.getStates().get(i);
        generateTransitions(state, mv);
    }

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

From source file:org.teavm.flavour.regex.bytecode.MatcherClassBuilder.java

License:Apache License

private void generateTransition(MethodVisitor mv, DfaState source, DfaState target) {
    if (source.isTerminal() && source != target) {
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitInsn(Opcodes.ICONST_M1);
        mv.visitFieldInsn(Opcodes.PUTFIELD, className, "domain", "I");
    }/*from w  w w. j  av  a 2 s  . c o m*/
    mv.visitIntInsn(Opcodes.SIPUSH, target.getIndex());
    mv.visitVarInsn(Opcodes.ISTORE, 5);
    if (target.isTerminal() && source != target) {
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitIntInsn(Opcodes.SIPUSH, target.getDomains()[0]);
        mv.visitFieldInsn(Opcodes.PUTFIELD, className, "domain", "I");
    }

    debug(mv, "DFA: " + source.getIndex() + " -> " + target.getIndex() + " "
            + Arrays.toString(target.getDomains()));
    if (target.isTerminal()) {
        Label noReluctant = new Label();
        mv.visitVarInsn(Opcodes.ILOAD, 4);
        mv.visitJumpInsn(Opcodes.IFEQ, noReluctant);
        mv.visitIincInsn(2, 1);
        debug(mv, "DFA reached terminal state");
        mv.visitJumpInsn(Opcodes.GOTO, saveLabel);
        mv.visitLabel(noReluctant);
    }
    if (source.getIndex() + 1 == target.getIndex()) {
        mv.visitIincInsn(2, 1);
        generateLengthGuard(mv);
        mv.visitJumpInsn(Opcodes.GOTO, stateLabels[target.getIndex()]);
    } else {
        mv.visitJumpInsn(Opcodes.GOTO, continueLabel);
    }
}