Search in sources :

Example 6 with Instruction

use of org.freud.analysed.classbytecode.method.instruction.Instruction in project freud by LMAX-Exchange.

the class ClassByteCodeMethodMatchers method containsInstructions.

public static FreudExtendedMatcher<ClassByteCodeMethod> containsInstructions(final Opcode... opcodes) {
    return new FreudExtendedMatcher<ClassByteCodeMethod>() {

        private Instruction found = null;

        @Override
        protected boolean matchesSafely(final ClassByteCodeMethod item) {
            item.findInstruction(new AbstractInstructionVisitor() {

                @Override
                public void noArgInstruction(final Instruction instruction) {
                    for (int i = 0; i < opcodes.length; i++) {
                        Opcode opcode = opcodes[i];
                        if (instruction.getOpcode() == opcode) {
                            found = instruction;
                            break;
                        }
                    }
                }
            });
            return found != null;
        }

        @Override
        public void describeTo(final Description description) {
            description.appendText("containsInstructions(");
            for (int i = 0; i < opcodes.length; i++) {
                Opcode opcode = opcodes[i];
                description.appendText(opcode.name());
                description.appendText(", ");
            }
            description.appendText(") found");
        }
    };
}
Also used : Description(org.hamcrest.Description) ClassByteCodeMethod(org.freud.analysed.classbytecode.method.ClassByteCodeMethod) Opcode(org.freud.analysed.classbytecode.method.instruction.Opcode) Instruction(org.freud.analysed.classbytecode.method.instruction.Instruction) FreudExtendedMatcher(org.freud.java.matcher.FreudExtendedMatcher) AbstractInstructionVisitor(org.freud.analysed.classbytecode.method.instruction.AbstractInstructionVisitor)

Example 7 with Instruction

use of org.freud.analysed.classbytecode.method.instruction.Instruction in project freud by LMAX-Exchange.

the class ClassByteCodeMethodMatchers method methodInvokedWithParams.

public static FreudExtendedMatcher<ClassByteCodeMethod> methodInvokedWithParams(final Class expectedOwner, final String expectedMethodName, final Matcher<OperandStack>... expectedParamsPassed) {
    return new FreudExtendedMatcher<ClassByteCodeMethod>() {

        private String expectedOwnerName;

        {
            expectedOwnerName = typeEncoding(expectedOwner);
        }

        @Override
        protected boolean matchesSafely(final ClassByteCodeMethod item) {
            final boolean[] found = new boolean[1];
            found[0] = false;
            item.findInstruction(new AbstractInstructionVisitor() {

                @Override
                public void methodInvocation(final Instruction instruction, final String owner, final String methodName, final String... args) {
                    if (!found[0] && expectedOwnerName.equals(owner) && expectedMethodName.equals(methodName)) {
                        Instruction prevInstruction = item.getInstruction(instruction.getInstructionIndex() - 1);
                        OperandStack operandStack = prevInstruction.getOperandStack();
                        found[0] = true;
                        for (int i = expectedParamsPassed.length - 1; i >= 0; i--) {
                            Matcher<OperandStack> matcher = expectedParamsPassed[i];
                            if (!matcher.matches(operandStack)) {
                                found[0] = false;
                                break;
                            }
                            operandStack = operandStack.next();
                        }
                    }
                }
            });
            return found[0];
        }

        @Override
        public void describeTo(final Description description) {
            description.appendText("methodInvokedWithParams(" + expectedOwner.getName() + ", " + expectedMethodName + ")");
        }
    };
}
Also used : CastOperandStack(org.freud.analysed.classbytecode.method.instruction.CastOperandStack) OperandStack(org.freud.analysed.classbytecode.method.instruction.OperandStack) Description(org.hamcrest.Description) Matcher(org.hamcrest.Matcher) FreudExtendedMatcher(org.freud.java.matcher.FreudExtendedMatcher) ClassByteCodeMethod(org.freud.analysed.classbytecode.method.ClassByteCodeMethod) Instruction(org.freud.analysed.classbytecode.method.instruction.Instruction) FreudExtendedMatcher(org.freud.java.matcher.FreudExtendedMatcher) AbstractInstructionVisitor(org.freud.analysed.classbytecode.method.instruction.AbstractInstructionVisitor)

Example 8 with Instruction

use of org.freud.analysed.classbytecode.method.instruction.Instruction in project freud by LMAX-Exchange.

the class AsmMethod method visitMethodInsn.

public void visitMethodInsn(final int opcode, final String owner, final String name, final String desc) {
    final Matcher matcher = METHOD_DESC_PATTERN.matcher(desc);
    if (matcher.matches()) {
        final String argsAsString = matcher.group(1);
        final ArrayList<String> argsContainer = new ArrayList<String>();
        String returnType = matcher.group(2);
        parseArgs(argsAsString, argsContainer);
        String[] args = argsContainer.toArray(new String[argsContainer.size()]);
        final Instruction instruction = new MethodInvocationInstruction(instructionList.size(), OPCODES_ARRAY[opcode], currentLineNumber, "L" + owner + ";", name, args, returnType);
        updateCurrentState(instruction);
    }
}
Also used : Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) MethodInvocationInstruction(org.freud.analysed.classbytecode.method.instruction.MethodInvocationInstruction) Instruction(org.freud.analysed.classbytecode.method.instruction.Instruction) ReferenceOperandInstruction(org.freud.analysed.classbytecode.method.instruction.ReferenceOperandInstruction) VarInstruction(org.freud.analysed.classbytecode.method.instruction.VarInstruction) JumpInstruction(org.freud.analysed.classbytecode.method.instruction.JumpInstruction) ConstInstruction(org.freud.analysed.classbytecode.method.instruction.ConstInstruction) FieldInstruction(org.freud.analysed.classbytecode.method.instruction.FieldInstruction) IntOperandInstruction(org.freud.analysed.classbytecode.method.instruction.IntOperandInstruction) MethodInvocationInstruction(org.freud.analysed.classbytecode.method.instruction.MethodInvocationInstruction)

Example 9 with Instruction

use of org.freud.analysed.classbytecode.method.instruction.Instruction in project freud by LMAX-Exchange.

the class AsmMethod method visitIntInsn.

public void visitIntInsn(final int opcodeUsed, final int operand) {
    final Opcode opcode = OPCODES_ARRAY[opcodeUsed];
    final Instruction instruction;
    if (opcode == Opcode.NEWARRAY) {
        instruction = new ReferenceOperandInstruction(instructionList.size(), opcode, currentLineNumber, NEWARRAY_TYPES[operand]);
    } else {
        instruction = new IntOperandInstruction(instructionList.size(), opcode, currentLineNumber, operand);
    }
    updateCurrentState(instruction);
}
Also used : IntOperandInstruction(org.freud.analysed.classbytecode.method.instruction.IntOperandInstruction) ReferenceOperandInstruction(org.freud.analysed.classbytecode.method.instruction.ReferenceOperandInstruction) Opcode(org.freud.analysed.classbytecode.method.instruction.Opcode) Instruction(org.freud.analysed.classbytecode.method.instruction.Instruction) ReferenceOperandInstruction(org.freud.analysed.classbytecode.method.instruction.ReferenceOperandInstruction) VarInstruction(org.freud.analysed.classbytecode.method.instruction.VarInstruction) JumpInstruction(org.freud.analysed.classbytecode.method.instruction.JumpInstruction) ConstInstruction(org.freud.analysed.classbytecode.method.instruction.ConstInstruction) FieldInstruction(org.freud.analysed.classbytecode.method.instruction.FieldInstruction) IntOperandInstruction(org.freud.analysed.classbytecode.method.instruction.IntOperandInstruction) MethodInvocationInstruction(org.freud.analysed.classbytecode.method.instruction.MethodInvocationInstruction)

Example 10 with Instruction

use of org.freud.analysed.classbytecode.method.instruction.Instruction in project freud by LMAX-Exchange.

the class AsmMethod method visitIincInsn.

public void visitIincInsn(final int var, final int increment) {
    final Instruction instruction = new IntOperandInstruction(instructionList.size(), Opcode.IINC, currentLineNumber, increment);
    updateCurrentState(instruction);
}
Also used : IntOperandInstruction(org.freud.analysed.classbytecode.method.instruction.IntOperandInstruction) Instruction(org.freud.analysed.classbytecode.method.instruction.Instruction) ReferenceOperandInstruction(org.freud.analysed.classbytecode.method.instruction.ReferenceOperandInstruction) VarInstruction(org.freud.analysed.classbytecode.method.instruction.VarInstruction) JumpInstruction(org.freud.analysed.classbytecode.method.instruction.JumpInstruction) ConstInstruction(org.freud.analysed.classbytecode.method.instruction.ConstInstruction) FieldInstruction(org.freud.analysed.classbytecode.method.instruction.FieldInstruction) IntOperandInstruction(org.freud.analysed.classbytecode.method.instruction.IntOperandInstruction) MethodInvocationInstruction(org.freud.analysed.classbytecode.method.instruction.MethodInvocationInstruction)

Aggregations

Instruction (org.freud.analysed.classbytecode.method.instruction.Instruction)16 ConstInstruction (org.freud.analysed.classbytecode.method.instruction.ConstInstruction)10 FieldInstruction (org.freud.analysed.classbytecode.method.instruction.FieldInstruction)10 IntOperandInstruction (org.freud.analysed.classbytecode.method.instruction.IntOperandInstruction)10 JumpInstruction (org.freud.analysed.classbytecode.method.instruction.JumpInstruction)10 MethodInvocationInstruction (org.freud.analysed.classbytecode.method.instruction.MethodInvocationInstruction)10 ReferenceOperandInstruction (org.freud.analysed.classbytecode.method.instruction.ReferenceOperandInstruction)10 VarInstruction (org.freud.analysed.classbytecode.method.instruction.VarInstruction)10 AbstractInstructionVisitor (org.freud.analysed.classbytecode.method.instruction.AbstractInstructionVisitor)6 Opcode (org.freud.analysed.classbytecode.method.instruction.Opcode)4 ClassByteCodeMethod (org.freud.analysed.classbytecode.method.ClassByteCodeMethod)3 FreudExtendedMatcher (org.freud.java.matcher.FreudExtendedMatcher)3 Description (org.hamcrest.Description)3 OperandStack (org.freud.analysed.classbytecode.method.instruction.OperandStack)2 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1 CastOperandStack (org.freud.analysed.classbytecode.method.instruction.CastOperandStack)1 Label (org.freud.analysed.classbytecode.method.instruction.Label)1 Matcher (org.hamcrest.Matcher)1 Type (org.objectweb.asm.Type)1