New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a check to ban use of specified methods and constructors #5083
Comments
@raghavgautam , please share banished_methods.xml . This Check will not be robust, and most likely I would suggest you to contribute it to our experimental Checks project - https://github.com/sevntu-checkstyle/sevntu.checkstyle |
Here is the banished_methods.xml. <?xml version="1.0"?>
<!DOCTYPE BanishedMethods SYSTEM "../../../../../../../../../../src/main/resources/com/puppycrawl/tools/checkstyle/banished_methods_1_0.dtd">
<BanishedMethods>
<BanishedMethod methodName="System\.exit" argCount=".*"/>
<BanishedMethod methodName=".*assertTrue" argCount="1"/>
<BanishedMethod methodName="BannedConstructor" argCount="1"/>
</BanishedMethods> And banished_methods dtd: <?xml version="1.0" encoding="UTF-8"?>
<!-- Add the following to any file that is to be validated against this DTD:
<!DOCTYPE BanishedMethods PUBLIC
"-//Puppy Crawl//DTD Banished Methods 1.0//EN"
"http://checkstyle.sourceforge.net/dtds/banished_methods_1_0.dtd">
-->
<!ELEMENT BanishedMethods (BanishedMethod*)>
<!ELEMENT BanishedMethod EMPTY>
<!ATTLIST BanishedMethod methodName CDATA #REQUIRED
argCount CDATA #REQUIRED> |
@romani Do you have any input on the feature ? I have a question. I see that the project has moved away from sourceforge. What would be the url for the banished_methods_1_0.dtd ? |
We still use the sourceforge URL. The only one we don't use anymore is
My comments are,
I don't think we should rely on an external XML file.
Technically Unless you make your check complex and follow types defined in the file, which still leaves out types from external classes, you may have to rely mostly on the method name alone. |
@raghavgautam , Check that you propose is very unstable and uncertain and imply a lot of false positives. We could host it in https://github.com/sevntu-checkstyle/sevntu.checkstyle/tree/master/sevntu-checks but have to be changed to avoid external config file, and pass code review process and stress testing, ... . We will guide on the process. If no good ideas come up during 2 weeks, I will close this issue. |
@rnveach Thanks for the feedback !!
|
@romani Thanks for the feedback!!
Thanks for suggesting https://github.com/sevntu-checkstyle/sevntu.checkstyle/tree/master/sevntu-checks, I am looking at it's code. |
See you in sevntu project |
/var/tmp $ javac -cp ~/.m2/repository/junit/junit/4.12/junit-4.12.jar BannedConstructor.java InputBanishedMethods.java
/var/tmp $ cat BannedConstructor.java InputBanishedMethods.java
With the about java file. I want to be able to run checkstyle and get error that:
I could not find a simple and robust way to do this. So, I have written a check that can do this. I want to contribute it back. Others have discussed this on stackoverflow as well:
https://stackoverflow.com/questions/8416870/checkstyle-rule-to-prevent-invocation-of-some-methods-and-constructors
A sample output for above example is:
/var/tmp $ cat config.xml
/var/tmp $ cat banished_methods.xml
/var/tmp $ java -jar checkstyle-X.XX-all.jar -c config.xml InputBanishedMethods.java
Starting audit...
[ERROR] /Users/raghav/play/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/misc/banishedmethods/InputBanishedMethods.java:19:20: Call to exit with 1 arguments is banished. [BanishedMethods]
[ERROR] /Users/raghav/play/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/misc/banishedmethods/InputBanishedMethods.java:20:47: Call to BannedConstructor with 1 arguments is banished. [BanishedMethods]
[ERROR] /Users/raghav/play/checkstyle/src/test/resources/com/puppycrawl/tools/checkstyle/checks/misc/banishedmethods/InputBanishedMethods.java:27:26: Call to assertTrue with 1 arguments is banished. [BanishedMethods]
Audit done.
Checkstyle ends with 3 errors.
The text was updated successfully, but these errors were encountered: