Search in sources :

Example 1 with MethodCall

use of org.freud.analysed.javasource.MethodCall in project freud by LMAX-Exchange.

the class CodeBlockJdom method getMethodCallByMethodNameMap.

@Override
@SuppressWarnings("unchecked")
public Map<String, List<MethodCall>> getMethodCallByMethodNameMap() {
    if (methodCallByMethodNameMap == null) {
        methodCallByMethodNameMap = new HashMap<String, List<MethodCall>>();
        JXPathContext context = JXPathContext.newContext(codeBlockElement);
        List<Element> methodCallElementList = context.selectNodes("//" + JavaSourceTokenType.METHOD_CALL.getName());
        for (Element methodCallElement : methodCallElementList) {
            final MethodCall methodCall = new MethodCallJdom(methodCallElement);
            List<MethodCall> methodCallList = methodCallByMethodNameMap.get(methodCall.getMethodName());
            if (methodCallList == null) {
                methodCallList = new LinkedList<MethodCall>();
                methodCallByMethodNameMap.put(methodCall.getMethodName(), methodCallList);
            }
            methodCallList.add(methodCall);
        }
    }
    return methodCallByMethodNameMap;
}
Also used : JXPathContext(org.apache.commons.jxpath.JXPathContext) Element(org.jdom.Element) List(java.util.List) LinkedList(java.util.LinkedList) MethodCall(org.freud.analysed.javasource.MethodCall)

Example 2 with MethodCall

use of org.freud.analysed.javasource.MethodCall 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

MethodCall (org.freud.analysed.javasource.MethodCall)2 LinkedList (java.util.LinkedList)1 List (java.util.List)1 JXPathContext (org.apache.commons.jxpath.JXPathContext)1 CodeBlock (org.freud.analysed.javasource.CodeBlock)1 FreudExtendedMatcher (org.freud.java.matcher.FreudExtendedMatcher)1 Description (org.hamcrest.Description)1 Element (org.jdom.Element)1