Example usage for org.objectweb.asm.commons LocalVariablesSorter newLocal

List of usage examples for org.objectweb.asm.commons LocalVariablesSorter newLocal

Introduction

In this page you can find the example usage for org.objectweb.asm.commons LocalVariablesSorter newLocal.

Prototype

public int newLocal(final Type type) 

Source Link

Document

Constructs a new local variable of the given type.

Usage

From source file:net.sourceforge.cobertura.instrument.pass3.InjectCodeClassInstrumenter.java

License:GNU General Public License

/**
 * <p>Instrumenting a code in a single method. Special conditions for processing 'static initialization block'.</p>
 * <p/>/*from  ww w  .  j ava 2s . c o  m*/
 * <p>This method also uses {@link ShiftVariableMethodAdapter} that is used firstly to calculate the index of internal
 * variable injected to store information about last 'processed' jump or switch in runtime ( {@link ShiftVariableMethodAdapter#calculateFirstStackVariable(int, String)} ),
 * and then is used to inject code responsible for keeping the variable and shifting (+1) all previously seen variables.
 */
@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
    if (ignoredMethods.contains(name + desc)) {
        return mv;
    }
    if ((access & Opcodes.ACC_STATIC) != 0) {
        mv = new GenerateCallCoberturaInitMethodVisitor(mv, classMap.getClassName());
        if ("<clinit>".equals(name)) {
            wasStaticInitMethodVisited = true;
        }
    }
    FindTouchPointsMethodAdapter instrumenter = new FindTouchPointsMethodAdapter(mv, classMap.getClassName(),
            name, desc, eventIdGenerator, duplicatedLinesMap, lineIdGenerator);
    instrumenter.setTouchPointListener(touchPointListener);
    instrumenter.setIgnoreRegexp(getIgnoreRegexp());
    LocalVariablesSorter sorter = new LocalVariablesSorter(access, desc, instrumenter);
    int variable = sorter.newLocal(Type.INT_TYPE);
    touchPointListener.setLastJumpIdVariableIndex(variable);
    return sorter;
    //return new ShiftVariableMethodAdapter(instrumenter, access, desc, 1);
}

From source file:org.jtsan.LocalVarsSaver.java

License:Apache License

private static ArrayList<Integer> newLocalVarsFromTypes(ArrayList<Type> types, LocalVariablesSorter lvs) {
    ArrayList<Integer> vars = new ArrayList<Integer>();
    for (Type type : types) {
        vars.add(lvs.newLocal(type));
    }/*from  ww w . j a v a 2s.co m*/
    return vars;
}

From source file:portablejim.veinminer.asm.ItemInWorldManagerTransformer.java

License:Open Source License

private int insertCallAfterTryHarvestBlockFunction(MethodNode curMethod, String obfuscatedClassName,
        int startIndex) throws IndexOutOfBoundsException {
    LocalVariablesSorter varSorter = new LocalVariablesSorter(curMethod.access, curMethod.desc, curMethod);

    String worldType = typemap.get(getCorrectName("theWorld"));
    String playerType = typemap.get(getCorrectName("thisPlayerMP"));

    while (!isMethodWithName(curMethod.instructions.get(startIndex), "tryHarvestBlock")) {
        ++startIndex;//from www . ja  v  a 2 s  . c om
    }

    do {
        --startIndex;
    } while (curMethod.instructions.get(startIndex).getType() == AbstractInsnNode.VAR_INSN);

    int blockVarIndex = varSorter.newLocal(Type.getType(BlockID.class));
    curMethod.instructions.insert(curMethod.instructions.get(startIndex),
            buildBlockIdFunctionCall(obfuscatedClassName, worldType, blockVarIndex));
    ++startIndex;

    while (!isMethodWithName(curMethod.instructions.get(startIndex), "tryHarvestBlock")) {
        ++startIndex;
    }

    // Add variable to store result
    int newVarIndex = varSorter.newLocal(Type.BOOLEAN_TYPE);
    VarInsnNode newVar = new VarInsnNode(Opcodes.ISTORE, newVarIndex);
    curMethod.instructions.insert(curMethod.instructions.get(startIndex), newVar);
    ++startIndex;

    // Add in function call to call function
    InsnList veinMinerFunctionCall = new InsnList();
    veinMinerFunctionCall
            .add(new FieldInsnNode(Opcodes.GETSTATIC, targetClassName, "instance", targetClassType));
    veinMinerFunctionCall.add(new VarInsnNode(Opcodes.ALOAD, 0));
    veinMinerFunctionCall.add(new FieldInsnNode(Opcodes.GETFIELD, obfuscatedClassName.replace(".", "/"),
            getCorrectName("theWorld"), typemap.get(getCorrectName("theWorld"))));
    veinMinerFunctionCall.add(new VarInsnNode(Opcodes.ALOAD, 0));
    veinMinerFunctionCall.add(new FieldInsnNode(Opcodes.GETFIELD, obfuscatedClassName.replace(".", "/"),
            getCorrectName("thisPlayerMP"), typemap.get(getCorrectName("thisPlayerMP"))));
    veinMinerFunctionCall.add(new VarInsnNode(Opcodes.ILOAD, 1));
    veinMinerFunctionCall.add(new VarInsnNode(Opcodes.ILOAD, 2));
    veinMinerFunctionCall.add(new VarInsnNode(Opcodes.ILOAD, 3));
    veinMinerFunctionCall.add(new VarInsnNode(Opcodes.ILOAD, newVarIndex));
    veinMinerFunctionCall.add(new VarInsnNode(Opcodes.ALOAD, blockVarIndex));

    String blockIdClassType = String.format("L%s;", blockIdClassName);
    veinMinerFunctionCall.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, targetClassName, targetMethodName,
            String.format(targetMethodType, worldType, playerType, blockIdClassType)));
    curMethod.instructions.insert(curMethod.instructions.get(startIndex), veinMinerFunctionCall);
    ++startIndex;

    // Get rid of un-needed POP.
    while (curMethod.instructions.get(startIndex).getOpcode() != Opcodes.POP) {
        ++startIndex;
    }
    curMethod.instructions.remove(curMethod.instructions.get(startIndex));

    return startIndex;
}