Saturday, October 20, 2018

java - How to check if there are any invocations of a method in the code base



Let's say I have a class Foo and a method evilMethod:



class Foo {

public void evilMethod() {
// do sth
}

}


Is there a way to write a test that would fail any time there is any invocation of this method in any class on the classpath? I mean, not when the method is invoked at runtime, but when it's declared in the code.



For example, the precense of this code in any class on the classpath should make the test fail:



if(false) {
Foo f = new Foo();
f.evilMethod(); // test fails because of that

}


What I want to achieve: I would like to annotate methods that should not be called in certain project, but need to be placed there for the use in another, and then write a test that makes sure annotated methods are not invoked anywhere indeed.


Answer



I don't know if it is fixes your problem but FindBugs does a static code analysis for Java projects. Maybe you can define a rule there that covers your issue...


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...