Search in sources :

Example 1 with AbstractInstructionVisitor

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

the class ClassByteCodeDsl method hasMethodInvocation.

public static boolean hasMethodInvocation(final ClassByteCodeMethod analysed, final Class expectedOwner, final String expectedMethodName, final Class... expectedParamsDeclared) {
    final boolean[] found = new boolean[] { false };
    final String expectedOwnerName = typeEncoding(expectedOwner);
    final String[] expectedParamNames = (expectedParamsDeclared.length == 0) ? EMPTY_ARGS : new String[expectedParamsDeclared.length];
    for (int i = 0, size = expectedParamsDeclared.length; i < size; i++) {
        expectedParamNames[i] = typeEncoding(expectedParamsDeclared[i]);
    }
    found[0] = false;
    analysed.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) && Arrays.equals(expectedParamNames, args)) {
                found[0] = true;
            }
        }
    });
    return found[0];
}
Also used : Instruction(org.freud.analysed.classbytecode.method.instruction.Instruction) AbstractInstructionVisitor(org.freud.analysed.classbytecode.method.instruction.AbstractInstructionVisitor)

Example 2 with AbstractInstructionVisitor

use of org.freud.analysed.classbytecode.method.instruction.AbstractInstructionVisitor 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 3 with AbstractInstructionVisitor

use of org.freud.analysed.classbytecode.method.instruction.AbstractInstructionVisitor 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 4 with AbstractInstructionVisitor

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

the class ClassByteCodeDsl method methodInvokedWithParams.

public static boolean methodInvokedWithParams(final ClassByteCodeMethod analysed, final Class expectedOwner, final String expectedMethodName, final Class... expectedParamTypes) {
    final String expectedOwnerName = typeEncoding(expectedOwner);
    final boolean[] found = new boolean[1];
    found[0] = false;
    analysed.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 = analysed.getInstruction(instruction.getInstructionIndex() - 1);
                OperandStack operandStack = prevInstruction.getOperandStack();
                found[0] = true;
                for (int i = expectedParamTypes.length - 1; i >= 0; i--) {
                    final String expectedType = typeEncoding(expectedParamTypes[i]);
                    if (!expectedType.equals(operandStack.getOperandType())) {
                        found[0] = false;
                        break;
                    }
                    operandStack = operandStack.next();
                }
            }
        }
    });
    return found[0];
}
Also used : OperandStack(org.freud.analysed.classbytecode.method.instruction.OperandStack) Instruction(org.freud.analysed.classbytecode.method.instruction.Instruction) AbstractInstructionVisitor(org.freud.analysed.classbytecode.method.instruction.AbstractInstructionVisitor)

Example 5 with AbstractInstructionVisitor

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

the class ClassByteCodeDsl method containsInstructions.

public static boolean containsInstructions(final ClassByteCodeMethod analysed, final Opcode... opcodes) {
    final Instruction[] found = new Instruction[1];
    analysed.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[0] = instruction;
                    break;
                }
            }
        }
    });
    return found[0] != null;
}
Also used : Opcode(org.freud.analysed.classbytecode.method.instruction.Opcode) Instruction(org.freud.analysed.classbytecode.method.instruction.Instruction) AbstractInstructionVisitor(org.freud.analysed.classbytecode.method.instruction.AbstractInstructionVisitor)

Aggregations

AbstractInstructionVisitor (org.freud.analysed.classbytecode.method.instruction.AbstractInstructionVisitor)6 Instruction (org.freud.analysed.classbytecode.method.instruction.Instruction)6 ClassByteCodeMethod (org.freud.analysed.classbytecode.method.ClassByteCodeMethod)3 FreudExtendedMatcher (org.freud.java.matcher.FreudExtendedMatcher)3 Description (org.hamcrest.Description)3 Opcode (org.freud.analysed.classbytecode.method.instruction.Opcode)2 OperandStack (org.freud.analysed.classbytecode.method.instruction.OperandStack)2 CastOperandStack (org.freud.analysed.classbytecode.method.instruction.CastOperandStack)1 Matcher (org.hamcrest.Matcher)1