Search in sources :

Example 1 with FreudExtendedMatcher

use of org.freud.java.matcher.FreudExtendedMatcher 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 2 with FreudExtendedMatcher

use of org.freud.java.matcher.FreudExtendedMatcher 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 3 with FreudExtendedMatcher

use of org.freud.java.matcher.FreudExtendedMatcher in project freud by LMAX-Exchange.

the class AnnotatedElementDeclarationMatchers method hasDeclaredAnnotation.

public FreudExtendedMatcher<T> hasDeclaredAnnotation(final String annotationName, final Matcher<String> keyMatcher, final Matcher<String> valueMatcher) {
    return new FreudExtendedMatcher<T>() {

        @Override
        protected boolean matchesSafely(final AnnotatedElementDeclaration toBeAnalysed) {
            for (Annotation declaredAnnotation : toBeAnalysed.getDeclaredAnnotations()) {
                if (annotationName.equals(declaredAnnotation.getName())) {
                    if (keyMatcher != null || valueMatcher != null) {
                        for (Map.Entry<String, String> entry : declaredAnnotation.getParameterMap().entrySet()) {
                            if (keyMatcher == null || keyMatcher.matches(entry.getKey())) {
                                if (valueMatcher == null || valueMatcher.matches(entry.getValue())) {
                                    return true;
                                }
                            }
                        }
                        return false;
                    }
                    return true;
                }
            }
            return false;
        }

        @Override
        public String toString() {
            Description description = new StringDescription();
            description.appendText("HasDeclaredAnnotation(").appendText(annotationName);
            description.appendText(")");
            return description.toString();
        }

        @Override
        public void describeTo(Description description) {
            description.appendText(toString());
        }
    };
}
Also used : Description(org.hamcrest.Description) StringDescription(org.hamcrest.StringDescription) AnnotatedElementDeclaration(org.freud.analysed.javasource.AnnotatedElementDeclaration) StringDescription(org.hamcrest.StringDescription) FreudExtendedMatcher(org.freud.java.matcher.FreudExtendedMatcher) Map(java.util.Map) Annotation(org.freud.analysed.javasource.Annotation)

Example 4 with FreudExtendedMatcher

use of org.freud.java.matcher.FreudExtendedMatcher 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)

Example 5 with FreudExtendedMatcher

use of org.freud.java.matcher.FreudExtendedMatcher in project freud by LMAX-Exchange.

the class CodeBlockMatchers method hasMethodCall.

public static FreudExtendedMatcher<CodeBlock> hasMethodCall(final String methodCall) {
    return new FreudExtendedMatcher<CodeBlock>() {

        private final String methodName;

        private final String[] instanceReferences;

        {
            String[] values = methodCall.split("\\.");
            instanceReferences = new String[values.length - 1];
            System.arraycopy(values, 0, instanceReferences, 0, instanceReferences.length);
            methodName = values[values.length - 1];
        }

        @Override
        protected boolean matchesSafely(final CodeBlock toBeAnalysed) {
            List<MethodCall> methodCallList = toBeAnalysed.getMethodCallListByMethodName(methodName);
            if (methodCallList != null) {
                for (MethodCall methodCall : methodCallList) {
                    if (Arrays.equals(instanceReferences, methodCall.getInstanceReferences())) {
                        return true;
                    }
                }
            }
            return false;
        }

        @Override
        public void describeTo(final Description description) {
            description.appendText("HasMethodCall(" + methodCall + ")");
        }
    };
}
Also used : Description(org.hamcrest.Description) CodeBlock(org.freud.analysed.javasource.CodeBlock) FreudExtendedMatcher(org.freud.java.matcher.FreudExtendedMatcher) MethodCall(org.freud.analysed.javasource.MethodCall)

Aggregations

FreudExtendedMatcher (org.freud.java.matcher.FreudExtendedMatcher)5 Description (org.hamcrest.Description)5 ClassByteCodeMethod (org.freud.analysed.classbytecode.method.ClassByteCodeMethod)3 AbstractInstructionVisitor (org.freud.analysed.classbytecode.method.instruction.AbstractInstructionVisitor)3 Instruction (org.freud.analysed.classbytecode.method.instruction.Instruction)3 Map (java.util.Map)1 CastOperandStack (org.freud.analysed.classbytecode.method.instruction.CastOperandStack)1 Opcode (org.freud.analysed.classbytecode.method.instruction.Opcode)1 OperandStack (org.freud.analysed.classbytecode.method.instruction.OperandStack)1 AnnotatedElementDeclaration (org.freud.analysed.javasource.AnnotatedElementDeclaration)1 Annotation (org.freud.analysed.javasource.Annotation)1 CodeBlock (org.freud.analysed.javasource.CodeBlock)1 MethodCall (org.freud.analysed.javasource.MethodCall)1 Matcher (org.hamcrest.Matcher)1 StringDescription (org.hamcrest.StringDescription)1