Example usage for org.objectweb.asm.tree MethodNode MethodNode

List of usage examples for org.objectweb.asm.tree MethodNode MethodNode

Introduction

In this page you can find the example usage for org.objectweb.asm.tree MethodNode MethodNode.

Prototype

public MethodNode(final int access, final String name, final String descriptor, final String signature,
        final String[] exceptions) 

Source Link

Document

Constructs a new MethodNode .

Usage

From source file:com.seovic.pof.PortableTypeGenerator.java

License:Apache License

private void implementReadExternal() {
    int index = 0;

    MethodNode mn = new MethodNode(ACC_PRIVATE, "readExternal", "(Lcom/tangosol/io/pof/PofReader;)V", null,
            new String[] { "java/io/IOException" });
    mn.visitCode();//from   w w  w .j a  v  a  2  s . com

    for (int version : properties.keySet()) {
        mn.visitVarInsn(ALOAD, 1);
        mn.visitMethodInsn(INVOKEINTERFACE, "com/tangosol/io/pof/PofReader", "getVersionId", "()I");
        mn.visitIntInsn(BIPUSH, version);
        Label l = new Label();
        mn.visitJumpInsn(IF_ICMPLT, l);

        SortedSet<FieldNode> fields = properties.get(version);
        for (FieldNode fn : fields) {
            Type type = Type.getType(fn.desc);

            if (isDebugEnabled()) {
                mn.visitLdcInsn("reading attribute " + index + " (" + fn.name + ") from POF stream");
                mn.visitIntInsn(BIPUSH, 7);
                mn.visitMethodInsn(INVOKESTATIC, "com/tangosol/net/CacheFactory", "log",
                        "(Ljava/lang/String;I)V");
            }
            mn.visitVarInsn(ALOAD, 0);
            mn.visitVarInsn(ALOAD, 1);
            mn.visitIntInsn(BIPUSH, index++);

            ReadMethod readMethod = getReadMethod(fn, type);
            readMethod.createTemplate(mn, fn, type);
            mn.visitMethodInsn(INVOKEINTERFACE, "com/tangosol/io/pof/PofReader", readMethod.getName(),
                    readMethod.getDescriptor());
            if (type.getSort() == Type.OBJECT || "readObjectArray".equals(readMethod.getName())) {
                mn.visitTypeInsn(CHECKCAST, type.getInternalName());
            }
            mn.visitFieldInsn(PUTFIELD, cn.name, fn.name, fn.desc);
        }

        mn.visitLabel(l);
        mn.visitFrame(F_SAME, 0, null, 0, null);
    }

    mn.visitInsn(RETURN);
    mn.visitMaxs(0, 0);
    mn.visitEnd();

    if (!hasMethod(mn)) {
        cn.methods.add(mn);
    }
    LOG.debug("Implemented method: " + mn.name);
}

From source file:com.seovic.pof.PortableTypeGenerator.java

License:Apache License

private void implementWriteExternal() {
    int index = 0;

    MethodNode mn = new MethodNode(ACC_PRIVATE, "writeExternal", "(Lcom/tangosol/io/pof/PofWriter;)V", null,
            new String[] { "java/io/IOException" });
    mn.visitCode();//ww w.j  ava 2  s. c om

    for (int version : properties.keySet()) {
        SortedSet<FieldNode> fields = properties.get(version);
        for (FieldNode fn : fields) {
            addPofIndex(fn, index);
            Type type = Type.getType(fn.desc);

            if (isDebugEnabled()) {
                mn.visitLdcInsn("writing attribute " + index + " (" + fn.name + ") to POF stream");
                mn.visitIntInsn(BIPUSH, 7);
                mn.visitMethodInsn(INVOKESTATIC, "com/tangosol/net/CacheFactory", "log",
                        "(Ljava/lang/String;I)V");
            }
            mn.visitVarInsn(ALOAD, 1);
            mn.visitIntInsn(BIPUSH, index++);
            mn.visitVarInsn(ALOAD, 0);
            mn.visitFieldInsn(GETFIELD, cn.name, fn.name, fn.desc);

            WriteMethod writeMethod = getWriteMethod(fn, type);
            writeMethod.pushUniformTypes(mn);
            mn.visitMethodInsn(INVOKEINTERFACE, "com/tangosol/io/pof/PofWriter", writeMethod.getName(),
                    writeMethod.getDescriptor());
        }
    }

    mn.visitInsn(RETURN);
    mn.visitMaxs(0, 0);
    mn.visitEnd();

    if (!hasMethod(mn)) {
        cn.methods.add(mn);
    }
    LOG.debug("Implemented method: " + mn.name);
}

From source file:com.sun.tdk.jcov.instrument.BlockCodeMethodAdapter.java

License:Open Source License

public BlockCodeMethodAdapter(final MethodVisitor mv, final DataMethodWithBlocks method,
        final InstrumentationParams params) {
    super(new MethodNode(method.getAccess(), method.getName(), method.getVmSignature(), method.getSignature(),
            method.getExceptions()), method);
    this.nextVisitor = mv;
    this.insnToBB = new IdentityHashMap<AbstractInsnNode, SimpleBasicBlock>();
    this.exits = new ArrayList<DataExit>();
    this.params = params;
}

From source file:com.sun.tdk.jcov.instrument.BranchCodeMethodAdapter.java

License:Open Source License

public BranchCodeMethodAdapter(final MethodVisitor mv, final DataMethodWithBlocks method,
        InstrumentationParams params) {/*from w ww  . ja v a  2  s  .  c  om*/
    super(new MethodNode(method.getAccess(), method.getName(), method.getVmSignature(), method.getSignature(),
            method.getExceptions()), method);
    this.nextVisitor = mv;
    this.insnToBB = new IdentityHashMap<AbstractInsnNode, BasicBlock>();
    this.exits = new ArrayList<DataExit>();
    this.src = new ArrayList<DataBlock>();
    this.params = params;
}

From source file:com.sun.tdk.jcov.instrument.DeferringMethodClassAdapter.java

License:Open Source License

public MethodVisitor visitMethodCoverage(final int access, final String name, final String desc,
        final String signature, final String[] exceptions) {
    if (!InstrumentationOptions.isSkipped(k.getFullname(), name, access) && params.isDynamicCollect()
            && params.isInstrumentNative() && (access & ACC_NATIVE) != 0) {
        // Visit the native method, but change the access flags and rename it with a prefix
        int accessNative = access;
        if ((accessNative & ACC_STATIC) == 0) {
            // not static, then it needs to be final
            accessNative |= ACC_FINAL;/*from  w  w w .  j a v  a  2 s .  c  o m*/
        }
        accessNative &= ~(ACC_PUBLIC | ACC_PROTECTED);
        accessNative |= ACC_PRIVATE;
        MethodVisitor mvNative = cv.visitMethod(accessNative, InstrumentationOptions.nativePrefix + name, desc,
                signature, exceptions);
        if (mvNative == null) {
            throw new InternalError("Should not happen!");
        }

        // Write the native method, then visit and write the wrapper method
        MethodNode methodWrapper = new MethodNode(access & ~ACC_NATIVE, name, desc, signature, exceptions);
        DataMethodEntryOnly meth = new DataMethodEntryOnly(k, access, name, desc, signature, exceptions);
        return new NativeWrappingMethodAdapter(mvNative, methodWrapper, cv, meth, params);
    }
    MethodVisitor mv = cv.visitMethod(access, name, desc, signature, exceptions);
    //This code is executed both in dynamic and static mode
    if (InstrumentationOptions.isSkipped(k.getFullname(), name, access) || mv == null
            || (access & ACC_ABSTRACT) != 0 || (access & ACC_NATIVE) != 0) {
        // build method with no content, and we are done
        if ((access & ACC_ABSTRACT) != 0 && params.isInstrumentAbstract()
                || ((access & ACC_NATIVE) != 0 && params.isInstrumentNative())
                || InstrumentationOptions.isSkipped(k.getFullname(), name, access)) {
            DataMethod meth = new DataMethodInvoked(k, access, name, desc, signature, exceptions);
            return new MethodAnnotationAdapter(mv, meth);
        }
        return mv;
    }

    // method could not be instrumented
    // java.lang.VerifyError: (class: sun/awt/X11/XWindowPeer, method: handleButtonPressRelease signature: (IJ)V) Mismatched stack types
    // temporary use only method coverage
    if (k.getFullname().equals("sun/awt/X11/XWindowPeer") && name.equals("handleButtonPressRelease")) {
        DataMethodEntryOnly meth = new DataMethodEntryOnly(k, access, name, desc, signature, exceptions);
        return new EntryCodeMethodAdapter(mv, meth, params);
    }

    switch (params.getMode()) {
    case METHOD: {
        DataMethodEntryOnly meth = new DataMethodEntryOnly(k, access, name, desc, signature, exceptions);
        return new EntryCodeMethodAdapter(mv, meth, params);
    }
    case BLOCK: {
        DataMethodWithBlocks meth = new DataMethodWithBlocks(k, access, name, desc, signature, exceptions);
        return new BlockCodeMethodAdapter(mv, meth, params);
    }
    case BRANCH: {
        DataMethodWithBlocks meth = new DataMethodWithBlocks(k, access, name, desc, signature, exceptions);
        return new BranchCodeMethodAdapter(mv, meth, params);
    }
    default:
        break;
    }

    return null;
}

From source file:com.toolazydogs.maiden.agent.asm.BeginEndMethodVisitor.java

License:Apache License

public BeginEndMethodVisitor(MethodVisitor visitor, int access, String name, String desc, String signature,
        String[] exceptions) {//ww w  .jav  a  2s  .com
    assert visitor != null;
    assert name != null;
    assert desc != null;

    if (LOGGER.isLoggable(Level.CONFIG)) {
        LOGGER.config("visitor: " + visitor);
        LOGGER.config("access: " + access);
        LOGGER.config("name: " + name);
        LOGGER.config("desc: " + desc);
        LOGGER.config("signature: " + signature);
        for (String exception : exceptions)
            LOGGER.config("exception: " + exception);
    }

    this.visitor = visitor;

    // this MethodNode instance is used to fill in hte gaps of frames and maxes
    methodNode = new MethodNode(access, name, desc, signature, exceptions);

    // We use this instance to keep track of local variables that may need to be created.
    lvs = new LocalVariablesSorter(access, desc, methodNode);
}

From source file:de.jiac.micro.config.analysis.MethodAnalyser.java

License:Open Source License

MethodAnalyser(ClassInfoCreator parent, int access, String name, String desc, String signature,
        String[] exceptions) {//  www. j  a  va  2 s  .c  o  m
    super(new MethodNode(access, name, desc, signature, exceptions));
    this.parent = parent;
    this.methodKey = new MethodKey(name, desc);
}

From source file:de.sanandrew.core.manpack.transformer.AnnotationChecker.java

License:Creative Commons License

private static MethodNode getSignature(String method) {
    String split[] = method.split(" ");

    int accLvl = getAccessLevelOpcode(split[0]);
    Matcher mtch = ASMNames.OWNERNAME.matcher(split[1]);
    if (!mtch.find()) {
        throw new RuntimeException("SAP-Method signature invalid!");
    }//  w  ww  . j  a  v  a 2s . c o  m

    String name = mtch.group(2);
    String desc = split[2];
    String sig = split.length > 3 ? split[3] : null;
    String throwing[] = split.length > 4 ? split[4].split(";") : null;

    return new MethodNode(accLvl, name, desc, sig, throwing);
}

From source file:de.sanandrew.core.manpack.transformer.TransformBadPotionsATN.java

License:Creative Commons License

private byte[] transformPotion(byte[] bytes) {
    ClassNode cn = ASMHelper.createClassNode(bytes);

    if (ASMHelper.hasClassMethodName(cn, ASMNames.M_isBadEffect)) {
        return bytes;
    }/* w w w  .j a  v a2s  . co m*/

    MethodNode method = new MethodNode(Opcodes.ACC_PUBLIC, ASMNames.M_isBadEffect, "()Z", null, null);
    method.visitCode();
    Label l0 = new Label();
    method.visitLabel(l0);
    method.visitVarInsn(Opcodes.ALOAD, 0);
    method.visitFieldInsn(Opcodes.GETFIELD, "net/minecraft/potion/Potion", ASMNames.F_isBadEffect, "Z");
    method.visitInsn(Opcodes.IRETURN);
    Label l1 = new Label();
    method.visitLabel(l1);
    method.visitLocalVariable("this", "Lnet/minecraft/potion/Potion;", null, l0, l1, 0);
    method.visitMaxs(0, 0);
    method.visitEnd();

    cn.methods.add(method);

    bytes = ASMHelper.createBytes(cn, ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);

    return bytes;
}

From source file:de.sanandrew.core.manpack.transformer.TransformELBAttackingPlayer.java

License:Creative Commons License

private static byte[] transformAttackingPlayer(byte[] bytes) {
    ClassNode clazz = ASMHelper.createClassNode(bytes);

    /** ADD GETTER FOR ATTACKING PLAYER **/
    {//ww  w . j a  va 2  s .c om
        MethodNode method = new MethodNode(Opcodes.ACC_PUBLIC, "_SAP_getAttackingPlayer",
                "()Lnet/minecraft/entity/player/EntityPlayer;", null, null);
        method.visitCode();
        Label l0 = new Label();
        method.visitLabel(l0);
        method.visitVarInsn(Opcodes.ALOAD, 0);
        method.visitFieldInsn(Opcodes.GETFIELD, "net/minecraft/entity/EntityLivingBase",
                ASMNames.F_attackingPlayer, "Lnet/minecraft/entity/player/EntityPlayer;");
        method.visitInsn(Opcodes.ARETURN);
        Label l1 = new Label();
        method.visitLabel(l1);
        method.visitLocalVariable("this", "Lnet/minecraft/entity/EntityLivingBase;", null, l0, l1, 0);
        method.visitMaxs(0, 0);
        method.visitEnd();
        clazz.methods.add(method);
    }

    /** ADD SETTER FOR ATTACKING PLAYER **/
    {
        MethodNode method = new MethodNode(Opcodes.ACC_PUBLIC, "_SAP_setAttackingPlayer",
                "(Lnet/minecraft/entity/player/EntityPlayer;)V", null, null);
        method.visitCode();
        Label l0 = new Label();
        method.visitLabel(l0);
        method.visitVarInsn(Opcodes.ALOAD, 0);
        method.visitVarInsn(Opcodes.ALOAD, 1);
        method.visitFieldInsn(Opcodes.PUTFIELD, "net/minecraft/entity/EntityLivingBase",
                ASMNames.F_attackingPlayer, "Lnet/minecraft/entity/player/EntityPlayer;");
        Label l1 = new Label();
        method.visitLabel(l1);
        method.visitInsn(Opcodes.RETURN);
        Label l2 = new Label();
        method.visitLabel(l2);
        method.visitLocalVariable("this", "Lnet/minecraft/entity/EntityLivingBase;", null, l0, l2, 0);
        method.visitLocalVariable("player", "Lnet/minecraft/entity/player/EntityPlayer;", null, l0, l2, 1);
        method.visitMaxs(0, 0);
        method.visitEnd();
        clazz.methods.add(method);
    }

    /** ADD GETTER FOR RECENTLY HIT **/
    {
        MethodNode method = new MethodNode(Opcodes.ACC_PUBLIC, "_SAP_getRecentlyHit", "()I", null, null);
        method.visitCode();
        Label l0 = new Label();
        method.visitLabel(l0);
        method.visitVarInsn(Opcodes.ALOAD, 0);
        method.visitFieldInsn(Opcodes.GETFIELD, "net/minecraft/entity/EntityLivingBase", ASMNames.F_recentlyHit,
                "I");
        method.visitInsn(Opcodes.IRETURN);
        Label l1 = new Label();
        method.visitLabel(l1);
        method.visitLocalVariable("this", "Lnet/minecraft/entity/EntityLivingBase;", null, l0, l1, 0);
        method.visitMaxs(0, 0);
        method.visitEnd();
        clazz.methods.add(method);
    }

    /** ADD SETTER FOR RECENTLY HIT **/
    {
        MethodNode method = new MethodNode(Opcodes.ACC_PUBLIC, "_SAP_setRecentlyHit", "(I)V", null, null);
        method.visitCode();
        Label l0 = new Label();
        method.visitLabel(l0);
        method.visitVarInsn(Opcodes.ALOAD, 0);
        method.visitVarInsn(Opcodes.ILOAD, 1);
        method.visitFieldInsn(Opcodes.PUTFIELD, "net/minecraft/entity/EntityLivingBase", ASMNames.F_recentlyHit,
                "I");
        Label l1 = new Label();
        method.visitLabel(l1);
        method.visitInsn(Opcodes.RETURN);
        Label l2 = new Label();
        method.visitLabel(l2);
        method.visitLocalVariable("this", "Lnet/minecraft/entity/EntityLivingBase;", null, l0, l2, 0);
        method.visitLocalVariable("hit", "I", null, l0, l2, 1);
        method.visitMaxs(0, 0);
        method.visitEnd();
        clazz.methods.add(method);
    }

    bytes = ASMHelper.createBytes(clazz, /*ClassWriter.COMPUTE_FRAMES |*/ ClassWriter.COMPUTE_MAXS);

    return bytes;
}