Example usage for org.objectweb.asm.tree AbstractInsnNode getPrevious

List of usage examples for org.objectweb.asm.tree AbstractInsnNode getPrevious

Introduction

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

Prototype

public AbstractInsnNode getPrevious() 

Source Link

Document

Returns the previous instruction in the list to which this instruction belongs, if any.

Usage

From source file:org.jacoco.playground.filter.InsnSequenceMatcher.java

License:Open Source License

private AbstractInsnNode getPrevious(final AbstractInsnNode node) {
    return skipIgnoredBackward(node.getPrevious());
}

From source file:org.jooby.internal.apitool.Insn.java

License:Apache License

public static AbstractInsnNode first(AbstractInsnNode node) {
    AbstractInsnNode result = node;/*  w w w .j  a  va 2  s  .co m*/
    AbstractInsnNode it = node;
    while (it != null) {
        result = it;
        it = it.getPrevious();
    }
    return result;
}

From source file:org.mutabilitydetector.checkers.CollectionTypeWrappedInUnmodifiableIdiomChecker.java

License:Apache License

private AbstractInsnNode lastMeaningfulNode(AbstractInsnNode node) {
    AbstractInsnNode previous = node.getPrevious();
    return (previous instanceof LabelNode) || (previous instanceof LineNumberNode)
            ? lastMeaningfulNode(previous)
            : previous;//  w w  w.j  a va2  s  .  c  o m
}

From source file:org.parboiled.transform.process.LabellingGenerator.java

License:Apache License

@Override
public void process(@Nonnull final ParserClassNode classNode, @Nonnull final RuleMethod method)
        throws Exception {
    Objects.requireNonNull(classNode, "classNode");
    Objects.requireNonNull(method, "method");
    // super methods have flag moved to the overriding method
    Preconditions.checkState(!method.isSuperMethod());

    final InsnList instructions = method.instructions;

    AbstractInsnNode retInsn = instructions.getLast();
    while (retInsn.getOpcode() != ARETURN)
        retInsn = retInsn.getPrevious();

    final LabelNode label = new LabelNode();
    final CodeBlock block = CodeBlock.newCodeBlock().dup().ifnull(label).ldc(getLabelText(method))
            .invokeinterface(CodegenUtils.p(Rule.class), "label", CodegenUtils.sig(Rule.class, String.class))
            .label(label);//from   w w  w  .  j a v  a  2s.co  m

    instructions.insertBefore(retInsn, block.getInstructionList());
}

From source file:org.parboiled.transform.process.ReturnInstructionUnifier.java

License:Apache License

@Override
public void process(@Nonnull final ParserClassNode classNode, @Nonnull final RuleMethod method)
        throws Exception {
    Objects.requireNonNull(classNode, "classNode");
    Objects.requireNonNull(method, "method");

    AbstractInsnNode current = method.instructions.getLast();

    // find last return
    while (current.getOpcode() != ARETURN)
        current = current.getPrevious();

    final LabelNode lastReturnLabel = new LabelNode();
    method.instructions.insertBefore(current, lastReturnLabel);

    // iterate backwards up to first instructions
    while ((current = current.getPrevious()) != null) {

        // replace returns with gotos
        if (current.getOpcode() != ARETURN)
            continue;

        final JumpInsnNode insn = new JumpInsnNode(GOTO, lastReturnLabel);
        method.instructions.set(current, insn);
        current = insn;//from w  w  w .  j a v a  2 s. c o m
    }
}

From source file:org.parboiled.transform.process.VarFramingGenerator.java

License:Apache License

@Override
public void process(@Nonnull final ParserClassNode classNode, @Nonnull final RuleMethod method)
        throws Exception {
    Objects.requireNonNull(classNode, "classNode");
    Objects.requireNonNull(method, "method");
    final InsnList instructions = method.instructions;

    AbstractInsnNode ret = instructions.getLast();
    while (ret.getOpcode() != ARETURN)
        ret = ret.getPrevious();

    final CodeBlock block = CodeBlock.newCodeBlock();

    block.newobj(CodegenUtils.p(VarFramingMatcher.class)).dup_x1().swap();

    createVarFieldArray(block, method);/*  w  ww  . j av a  2  s .c o  m*/

    block.invokespecial(CodegenUtils.p(VarFramingMatcher.class), "<init>",
            CodegenUtils.sig(void.class, Rule.class, Var[].class));

    instructions.insertBefore(ret, block.getInstructionList());

    method.setBodyRewritten();
}

From source file:pl.clareo.coroutines.core.MethodTransformer.java

License:Apache License

private static FrameNode findPreviousFrame(AbstractInsnNode insn) {
    AbstractInsnNode prevInsn = insn.getPrevious();
    while (prevInsn != null) {
        if (prevInsn.getType() == AbstractInsnNode.FRAME) {
            return (FrameNode) prevInsn;
        }//from   ww  w. j  a  v a2 s. c  om
        prevInsn = prevInsn.getPrevious();
    }
    return null;
}

From source file:pxb.android.dex2jar.optimize.B.java

License:Apache License

/**
 * <pre>// w  w w.  j  a  v  a 2  s  .c  o m
 * BEFORE:
 *     LDC Ljavax/servlet/GenericServlet;.class
 *     ASTORE 1
 *     LDC "/javax/servlet/LocalStrings.properties"
 *     ASTORE 2
 *     ALOAD 1
 *     ALOAD 2
 *     INVOKEVIRTUAL Ljava/lang/Class;.getResourceAsStream (Ljava/lang/String;)Ljava/io/InputStream;
 *     ASTORE 1
 *     NEW Ljava/util/PropertyResourceBundle;
 *     DUP
 *     ALOAD 1
 *     INVOKESPECIAL Ljava/util/PropertyResourceBundle;.&lt;init> (Ljava/io/InputStream;)V
 *     ASTORE 0
 *     ALOAD 0
 *     PUTSTATIC Ljavax/servlet/GenericServlet;.lStrings : Ljava/util/ResourceBundle;
 * </pre>
 * 
 * <pre>
 * AFTER:
 *     LDC Ljavax/servlet/GenericServlet;.class
 *     LDC "/javax/servlet/LocalStrings.properties"
 *     INVOKEVIRTUAL Ljava/lang/Class;.getResourceAsStream (Ljava/lang/String;)Ljava/io/InputStream;
 *     ASTORE 1
 *     NEW Ljava/util/PropertyResourceBundle;
 *     DUP
 *     ALOAD 1
 *     INVOKESPECIAL Ljava/util/PropertyResourceBundle;.&lt;init> (Ljava/io/InputStream;)V
 *     ASTORE 0
 *     ALOAD 0
 *     PUTSTATIC Ljavax/servlet/GenericServlet;.lStrings : Ljava/util/ResourceBundle;
 * </pre>
 * 
 * @param block
 */
private void doLdc(Block block) {
    Map<Integer, LdcInsnNode> map = new HashMap<Integer, LdcInsnNode>();
    AbstractInsnNode p = block.first.getNext();
    while (p != null && p != block.last) {
        if (p.getOpcode() == Opcodes.LDC) {
            AbstractInsnNode q = p.getNext();
            if (isWrite(q)) {
                Integer var = var(q);
                if (block.out.get(var) == null || block.out.get(var) != q) {
                    map.put(var, (LdcInsnNode) p);
                    insnList.remove(q); // remove store
                    q = p.getPrevious();
                    insnList.remove(p); // remove ldc
                    p = q;
                }
            }
        } else if (isRead(p)) {
            Integer var = var(p);
            if (block.out.get(var) == null || block.out.get(var) != p) {
                LdcInsnNode ldc = map.get(var);
                if (ldc != null) {
                    AbstractInsnNode _ldc_copy = ldc.clone(null);
                    insnList.insert(p, _ldc_copy);
                    insnList.remove(p);
                    p = _ldc_copy;
                }
            }
        } else if (isWrite(p)) {
            Integer var = var(p);
            map.remove(var);
        }
        p = p.getNext();
    }
}

From source file:pxb.android.dex2jar.optimize.B.java

License:Apache License

/**
 * //w  w w .j av a2s .c om
 * 
 * NEWINVOKESPECIAL??
 * 
 * <pre>
 * BEFORE:
 *     NEW Ljava/util/PropertyResourceBundle;
 *     ASTORE 0
 *     LDC Ljavax/servlet/GenericServlet;.class
 *     ASTORE 1
 *     LDC "/javax/servlet/LocalStrings.properties"
 *     ASTORE 2
 *     ALOAD 1
 *     ALOAD 2
 *     INVOKEVIRTUAL Ljava/lang/Class;.getResourceAsStream (Ljava/lang/String;)Ljava/io/InputStream;
 *     ASTORE 1
 *     ALOAD 0
 *     ALOAD 1
 *     INVOKESPECIAL Ljava/util/PropertyResourceBundle;.&lt;init> (Ljava/io/InputStream;)V
 *     ALOAD 0
 *     PUTSTATIC Ljavax/servlet/GenericServlet;.lStrings : Ljava/util/ResourceBundle;
 * </pre>
 * 
 * 
 * <pre>
 * AFTER:
 *     LDC Ljavax/servlet/GenericServlet;.class
 *     ASTORE 1
 *     LDC "/javax/servlet/LocalStrings.properties"
 *     ASTORE 2
 *     ALOAD 1
 *     ALOAD 2
 *     INVOKEVIRTUAL Ljava/lang/Class;.getResourceAsStream (Ljava/lang/String;)Ljava/io/InputStream;
 *     ASTORE 1
 *     NEW Ljava/util/PropertyResourceBundle;
 *     DUP
 *     ALOAD 1
 *     INVOKESPECIAL Ljava/util/PropertyResourceBundle;.&lt;init> (Ljava/io/InputStream;)V
 *     ASTORE 0
 *     ALOAD 0
 *     PUTSTATIC Ljavax/servlet/GenericServlet;.lStrings : Ljava/util/ResourceBundle;
 * </pre>
 * 
 * @param block
 */
private void doNew(Block block) {
    Map<String, AbstractInsnNode> map = new HashMap<String, AbstractInsnNode>();
    AbstractInsnNode p = block.first.getNext();
    while (p != null && p != block.last) {
        switch (p.getOpcode()) {
        case Opcodes.NEW: {
            AbstractInsnNode store = p.getNext();
            if (store instanceof VarInsnNode) {
                map.put(((TypeInsnNode) p).desc + var(store), p);
                p = store.getNext();
            } else {
                p = store;
            }
            break;
        }
        case Opcodes.INVOKESPECIAL: {
            MethodInsnNode m = (MethodInsnNode) p;
            p = p.getNext();
            if (m.name.equals("<init>")) {
                int length = Type.getArgumentTypes(m.desc).length;
                AbstractInsnNode q = m.getPrevious();
                while (length-- > 0) {
                    q = q.getPrevious();
                }
                AbstractInsnNode _new = map.remove(m.owner + var(q));
                if (_new != null) {
                    AbstractInsnNode _store = _new.getNext();
                    insnList.remove(_new);// remove new
                    insnList.remove(_store); // remove store
                    insnList.insertBefore(q, _new);
                    insnList.insert(_new, new InsnNode(DUP));
                    insnList.remove(q);
                    insnList.insert(m, _store);
                }
            }
            break;
        }
        default:
            p = p.getNext();
        }
    }
}

From source file:pxb.android.dex2jar.optimize.B.java

License:Apache License

/**
 * <pre>/*from   www. j av a  2  s  .  c om*/
 * BEFORE:
 *     LDC Ljavax/servlet/GenericServlet;.class
 *     LDC "/javax/servlet/LocalStrings.properties"
 *     INVOKEVIRTUAL Ljava/lang/Class;.getResourceAsStream (Ljava/lang/String;)Ljava/io/InputStream;
 *     ASTORE 1
 *     NEW Ljava/util/PropertyResourceBundle;
 *     DUP
 *     ALOAD 1
 *     INVOKESPECIAL Ljava/util/PropertyResourceBundle;.&lt;init> (Ljava/io/InputStream;)V
 *     ASTORE 0
 *     ALOAD 0
 *     PUTSTATIC Ljavax/servlet/GenericServlet;.lStrings : Ljava/util/ResourceBundle;
 * </pre>
 * 
 * <pre>
 * AFTER:
 *     LDC Ljavax/servlet/GenericServlet;.class
 *     LDC "/javax/servlet/LocalStrings.properties"
 *     INVOKEVIRTUAL Ljava/lang/Class;.getResourceAsStream (Ljava/lang/String;)Ljava/io/InputStream;
 *     ASTORE 1
 *     NEW Ljava/util/PropertyResourceBundle;
 *     DUP
 *     ALOAD 1
 *     INVOKESPECIAL Ljava/util/PropertyResourceBundle;.&lt;init> (Ljava/io/InputStream;)V
 *     PUTSTATIC Ljavax/servlet/GenericServlet;.lStrings : Ljava/util/ResourceBundle;
 * </pre>
 * 
 * @param block
 */
private void doVar(Block block) {
    AbstractInsnNode p = block.first.getNext();
    while (p != null && p != block.last) {
        if (isWrite(p)) {
            AbstractInsnNode q = p.getNext();
            if (isRead(q)) {
                if (isSameVar(p, q)) {
                    int var = var(p);
                    boolean canDel = true;
                    for (AbstractInsnNode i = q.getNext(); i != null && i != block.last; i = i.getNext()) {
                        if (isRead(i) && var == var(i)) {
                            canDel = false;
                            break;
                        }
                        if (isWrite(i) && var == var(i)) {
                            canDel = true;
                            break;
                        }
                    }
                    if (canDel && block.out.get(var) != p) {
                        AbstractInsnNode t = q.getNext();
                        insnList.remove(p);
                        insnList.remove(q);
                        p = t.getPrevious();
                    }
                }
            }
        }
        p = p.getNext();
    }
}