Detect all injection points in Java (Guice) via static code analysis

Viewed 299

I'm working on a large scale project in Java that uses Guice as its dependency injection framework. Is it possible to find all places in that code that a specific object type should be injected to? Clarification: via static code analysis, not during runtime.

Specifically, I have a method that is annotated @Provides. I want to detect all places in the code that are bounded to this method return value.

Update:

According to this, there isn't any existing tool for this job.

There isn't any static analysis of Guice to see if you actually have bindings for the annotations you said you did. This means that failures in Guice happen at run-time. Given that developers can fat-finger strings not referenced as constants, Guice also allows you to use your own annotations.

1 Answers

Caveat: This solution is hardly complete nor robust (as is also mentioned in the comments), but it might be sufficient in the absence of a better solution.

The simple solution I have found for this is using a regular expression. For example, if the "provider" method returns a class Foo, then the expression should be:

@Inject[\s\S]+?\W(Foo)

This does not cover, for example, nested injection patterns, such as parameters for methods annotated with @Provides.

Related