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:cuchaz.enigma.TranslatingTypeLoader.java

License:Open Source License

private byte[] loadType(String className) {

    // NOTE: don't know if class name is obf or deobf
    ClassEntry classEntry = new ClassEntry(className);
    ClassEntry obfClassEntry = this.obfuscatingTranslator.getTranslatedClass(classEntry);

    // is this an inner class referenced directly? (ie trying to load b instead of a$b)
    if (!obfClassEntry.isInnerClass()) {
        List<ClassEntry> classChain = this.jarIndex.getObfClassChain(obfClassEntry);
        if (classChain.size() > 1) {
            System.err.println(String.format("WARNING: no class %s after inner class reconstruction. Try %s",
                    className, obfClassEntry.buildClassEntry(classChain)));
            return null;
        }// ww w .  j a va2s  .  c  om
    }

    // is this a class we should even know about?
    if (!this.jarIndex.containsObfClass(obfClassEntry)) {
        return null;
    }

    // DEBUG
    //System.out.println(String.format("Looking for %s (obf: %s)", classEntry.getName(), obfClassEntry.getName()));

    // find the class in the jar
    ClassNode node = findClassInJar(obfClassEntry);
    if (node == null) {
        // couldn't find it
        return null;
    }

    // remove <obj>.getClass() calls that are seemingly injected
    //   DUP
    //   INVOKEVIRTUAL java/lang/Object.getClass ()Ljava/lang/Class;
    //   POP
    for (MethodNode methodNode : node.methods) {
        AbstractInsnNode insnNode = methodNode.instructions.getFirst();
        while (insnNode != null) {
            if (insnNode instanceof MethodInsnNode && insnNode.getOpcode() == Opcodes.INVOKEVIRTUAL) {
                MethodInsnNode methodInsnNode = (MethodInsnNode) insnNode;
                if (methodInsnNode.name.equals("getClass") && methodInsnNode.owner.equals("java/lang/Object")
                        && methodInsnNode.desc.equals("()Ljava/lang/Class;")) {
                    AbstractInsnNode previous = methodInsnNode.getPrevious();
                    AbstractInsnNode next = methodInsnNode.getNext();
                    if (previous.getOpcode() == Opcodes.DUP && next.getOpcode() == Opcodes.POP) {
                        insnNode = previous.getPrevious();//reset the iterator so it gets the new next instruction
                        methodNode.instructions.remove(previous);
                        methodNode.instructions.remove(methodInsnNode);
                        methodNode.instructions.remove(next);
                    }
                }
            }
            insnNode = insnNode.getNext();
        }
    }

    ClassWriter writer = new ClassWriter(0);
    transformInto(node, writer);

    // we have a transformed class!
    return writer.toByteArray();
}

From source file:de.tuberlin.uebb.jbop.optimizer.utils.LoopMatcher.java

License:Open Source License

private static AbstractInsnNode getStoreOfLoopForIf(final AbstractInsnNode node) {
    if (node == null) {
        return null;
    }//w w w .  j  a v  a  2s.  c o m

    if (!NodeHelper.isIf(node)) {
        return null;
    }
    final AbstractInsnNode previous = ((JumpInsnNode) node).label.getPrevious();
    if (!NodeHelper.isGoto(previous)) {
        return null;
    }

    final AbstractInsnNode previous2 = previous.getPrevious();
    if (previous2 instanceof IincInsnNode) {
        return ((JumpInsnNode) previous).label.getPrevious();
    }
    return previous2;
}

From source file:de.tuberlin.uebb.jbop.optimizer.utils.LoopMatcher.java

License:Open Source License

private static AbstractInsnNode getStoreOfLoopForIInc(final AbstractInsnNode node) {
    if (node == null) {
        return null;
    }//from  www.jav a  2s .  c o m

    if (!(node instanceof IincInsnNode)) {
        return null;
    }
    final AbstractInsnNode next = node.getNext();
    if (NodeHelper.isGoto(next)) {
        return ((JumpInsnNode) next).label.getPrevious();
    }
    if (next instanceof LabelNode) {
        final AbstractInsnNode gotoNode = findGotoForLabel(next, true);
        if (gotoNode == null) {
            return null;
        }
        return gotoNode.getPrevious();
    }

    return null;
}

From source file:de.tuberlin.uebb.jbop.optimizer.utils.LoopMatcher.java

License:Open Source License

private static AbstractInsnNode getStoreOfLoopForGoto(final AbstractInsnNode node) {
    if (node == null) {
        return null;
    }//w  ww  .  j  a v  a2 s.com

    if (!NodeHelper.isGoto(node)) {
        return null;
    }
    if (node.getPrevious() instanceof IincInsnNode) {
        return ((JumpInsnNode) node).label.getPrevious();
    }
    return node.getPrevious();
}

From source file:de.tuberlin.uebb.jbop.optimizer.utils.LoopMatcher.java

License:Open Source License

private static Loop getTypeOneLoop(final AbstractInsnNode counter, final AbstractInsnNode jump) {
    final LabelNode labelOfJump = ((JumpInsnNode) jump).label;
    final AbstractInsnNode target = labelOfJump.getNext();
    final AbstractInsnNode label = jump.getNext();
    if (!(label instanceof LabelNode)) {
        return null;
    }//from   w w  w . j  ava 2  s.  c om

    final AbstractInsnNode ifNode = findIf(target, label);
    if (ifNode == null) {
        return null;
    }

    AbstractInsnNode previous = NodeHelper.getPrevious(ifNode);
    AbstractInsnNode endValue = previous.getPrevious();

    final int varIndex = NodeHelper.getVarIndex(counter);
    int varIndex2 = NodeHelper.getVarIndex(previous);
    if (varIndex2 == -1) {
        previous = target;
        endValue = previous.getNext();
        varIndex2 = NodeHelper.getVarIndex(counter);

    }
    if (varIndex != varIndex2) {
        return null;
    }

    final AbstractInsnNode iinc = labelOfJump.getPrevious();
    if (endValue instanceof LabelNode) {
        endValue = NodeHelper.getInsnNodeFor(0);
    }
    final AbstractInsnNode startValue = counter.getPrevious();

    final AbstractInsnNode firstOfBody = ((JumpInsnNode) ifNode).label.getNext();
    return new Loop(ifNode, startValue, endValue, iinc, firstOfBody, ifNode, counter);
}

From source file:de.tuberlin.uebb.jbop.optimizer.utils.LoopMatcher.java

License:Open Source License

private static AbstractInsnNode findGotoForLabel(final AbstractInsnNode label, final boolean up) {
    AbstractInsnNode currentNode;//from  w w w  . j ava2 s .c  o m
    if (up) {
        currentNode = label.getPrevious();
    } else {
        currentNode = label.getNext();
    }

    while (currentNode != null) {

        if (NodeHelper.isGoto(currentNode) && ((JumpInsnNode) currentNode).label == label) {
            return currentNode;
        }

        if (up) {
            currentNode = currentNode.getPrevious();
        } else {
            currentNode = currentNode.getNext();
        }

    }

    return null;
}

From source file:de.tuberlin.uebb.jbop.optimizer.utils.LoopMatcher.java

License:Open Source License

private static Loop getTypeTwoLoop(final AbstractInsnNode counter, final AbstractInsnNode label) {
    final AbstractInsnNode gotoNode = findGotoForLabel(label, false);
    if (gotoNode == null) {
        return null;
    }//from  w  w  w . j a  v  a 2s.c  o  m

    final AbstractInsnNode ifNode = findIf(counter.getNext(), gotoNode.getNext());
    if (ifNode == null) {
        return null;
    }

    AbstractInsnNode previous = ifNode.getPrevious();
    AbstractInsnNode endValue = previous.getPrevious();
    int varIndex2 = NodeHelper.getVarIndex(previous);
    if (varIndex2 == -1) {
        previous = label.getNext();
        varIndex2 = NodeHelper.getVarIndex(previous);
        endValue = previous.getNext();
    }
    final int varIndex = NodeHelper.getVarIndex(counter);
    if (varIndex != varIndex2) {
        return null;
    }

    final AbstractInsnNode startValue = counter.getPrevious();
    if (endValue instanceof LabelNode) {
        endValue = NodeHelper.getInsnNodeFor(0);
    }

    final AbstractInsnNode iinc = gotoNode.getPrevious();
    final AbstractInsnNode firstOfBody = ((JumpInsnNode) ifNode).getNext();
    return new Loop(reverse((JumpInsnNode) ifNode), startValue, endValue, iinc, firstOfBody, gotoNode.getNext(),
            counter);
}

From source file:de.tuberlin.uebb.jbop.optimizer.utils.NodeHelper.java

License:Open Source License

/**
 * Gets the previous node, skipping {@link LineNumberNode}s and {@link LabelNode}s.
 * //w  w  w  .j  a  v a2 s .  c o m
 * @param node
 *          the node
 * @return the previous node
 */
public static AbstractInsnNode getPrevious(final AbstractInsnNode node) {
    if (node == null) {
        return null;
    }
    AbstractInsnNode current = node;
    do {
        current = current.getPrevious();
        if (current != null && !(current instanceof LineNumberNode || current instanceof LabelNode)) {
            break;
        }
    } while (current != null);
    return current;
}

From source file:de.tuberlin.uebb.jbop.optimizer.utils.NodeHelper.java

License:Open Source License

/**
 * Checks if node is a value node./*  w  w  w.j  a  va  2 s. c  o m*/
 * 
 * @param node
 *          the node
 * @return true if node is value
 */
public static boolean isValue(final AbstractInsnNode node) {
    if (node == null) {
        return false;
    }
    AbstractInsnNode checkNode = node;
    if (isCast(node)) {
        checkNode = node.getPrevious();
    }
    try {
        NodeHelper.getValue(checkNode);
        return true;
    } catch (final NotANumberException nane) {
        return false;
    }
}

From source file:de.tuberlin.uebb.jbop.optimizer.utils.NodeHelper.java

License:Open Source License

/**
 * Gets the value.//w  w  w.j  a  va2s  .com
 * 
 * @param node
 *          the node
 * @return the value
 * @throws NotANumberException
 *           if node is not a number-Node
 */
public static Number getNumberValue(final AbstractInsnNode node) throws NotANumberException {
    if (node == null) {
        throw new NotANumberException();
    }
    AbstractInsnNode checkNode = node;
    if (isCast(node)) {
        checkNode = node.getPrevious();
    }
    if (isIconst(checkNode)) {
        return cast(Integer.valueOf(checkNode.getOpcode() - Opcodes.ICONST_0), checkNode, node);
    }
    if (checkNode.getOpcode() == Opcodes.LCONST_0) {
        return cast(Long.valueOf(0), checkNode, node);
    }
    if (checkNode.getOpcode() == Opcodes.LCONST_1) {
        return cast(Long.valueOf(1), checkNode, node);
    }
    if (checkNode.getOpcode() == Opcodes.FCONST_0) {
        return cast(Float.valueOf(0), checkNode, node);
    }
    if (checkNode.getOpcode() == Opcodes.FCONST_1) {
        return cast(Float.valueOf(1), checkNode, node);
    }
    if (checkNode.getOpcode() == Opcodes.FCONST_2) {
        return cast(Float.valueOf(1), checkNode, node);
    }
    if (checkNode.getOpcode() == Opcodes.DCONST_0) {
        return cast(Double.valueOf(0), checkNode, node);
    }
    if (checkNode.getOpcode() == Opcodes.DCONST_1) {
        return cast(Double.valueOf(1), checkNode, node);
    }
    if (checkNode instanceof IntInsnNode) {
        return cast(Integer.valueOf(((IntInsnNode) checkNode).operand), checkNode, node);
    }
    return cast(getConstantValue(checkNode, Number.class), checkNode, node);
}