Search in sources :

Example 6 with AbstractInstructionVisitor

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

the class ClassByteCodeMethodMatchers method hasMethodInvocation.

public static FreudExtendedMatcher<ClassByteCodeMethod> hasMethodInvocation(final Class expectedOwner, final String expectedMethodName, final Class... expectedParamsDeclared) {
    return new FreudExtendedMatcher<ClassByteCodeMethod>() {

        private String expectedOwnerName;

        private String[] expectedParamNames;

        {
            expectedOwnerName = typeEncoding(expectedOwner);
            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]);
            }
        }

        @Override
        protected boolean matchesSafely(final ClassByteCodeMethod item) {
            final boolean[] found = new boolean[] { false };
            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) && Arrays.equals(expectedParamNames, args)) {
                        found[0] = true;
                    }
                }
            });
            return found[0];
        }

        @Override
        public void describeTo(final Description description) {
            description.appendText("hasMethodInvocation(" + expectedOwner.getName() + ", " + expectedMethodName + ", " + Arrays.toString(expectedParamNames) + ")");
        }
    };
}
Also used : Description(org.hamcrest.Description) 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)

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