Counting method callers

November 08, 2016

Written by Jens Nerche

Reading time ~1 minute

In her exciting talk Applying Java 8 Idioms to Existing Code, Trisha Gee states that demoing the refactoring to returning java.util.Optional could be a mess because so much caller statements have to be changed. You may find yourself easily in a similar situation every now and then if you have some spare time in the project and think by yourself: “Would’nt it be nice to do a quick refactoring now?” If you decide to return an Optional and get 538 compiler errors because of a wrong return type you can forget about ‘quick’. So how do you find a nice spot fitting in your refactoring time box?

It is just one simple Neo4j Cypher query if you are already using jQAssistant in your project. Let’s suppose you want to know how many callers are of method located in the de.kontext_e.techblog.service package. Here is the query:

1
2
3
4
5
6
7
8
    MATCH 
        (caller:Method:Java)-[:INVOKES]->(callee:Method:Java)<-[:DECLARES]-(t:Type) 
    WHERE 
        t.fqn=~'de.kontext_e.techblog.service.*' 
    RETURN 
        t.fqn, callee.name, count(caller) AS callers
    ORDER BY 
        callers

That’s it. Now you can assess the impact and choose wisely.

Using jQAssistant 1.8 with APOC plugin and Gradle

Gradle dependency configuration when using jQAssistant 1.8 and the APOC plugin Continue reading