18.6.13

Find class method dependencies in 8 seconds

Scouring through large amounts of source code using just "grep" is soooo 1990.  With git, pipes, and scripting languages, you can combine the convenience of grep with simpler and more flexible parsing tools.   Here's a couple of ways to find dependencies given a java class name, without having to load all your code into an IDE.

Scenario

You want to know which classes reference a particular compilation unit in your code.
But more and you want to know which methods in those classes are making the references.

How to do it with class info only using grep + perl/python/ettc

A way to start is to run: 

git grep -l MyClassName | grep ".java" | perl -pe 's!.*/!!'


This will give all the matching classes.

How to do it with method specific information using "git -p"

Using git's code parser, you can run

git grep -p MyClassName | grep java

This will return a several lines per match: One for the match, and another specifically for the method which CONTAINS the match.  You can parse out the methods by scanning one row above the match itself. 

Moral of the story

You don't have to use sed for everything.  Instead, consider

1) Piping into python and perl which can do a wonderful job reading from standard in and stream editing a file.  

2) Better yet, git comes stock with programmer specific grep utilities that understand the semantics of common programming languages.

That is all :)


3 comments: