-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
[java] Future-proof DontImportJavaLangRule #332
[java] Future-proof DontImportJavaLangRule #332
Conversation
Thanks for the PR, you are absolutely right! We should probably just rework this to be more future-proof... maybe checking how many dots are on the import starting with |
I completely agree with making this more future-proof. I was initially just trying to fit into the current approach. I was testing out a different approach that seems to incorporate the rules you mentioned: If this looks more in line with what you were thinking I can update this pull request again.
|
Amazing! I would do it slightly different, as to avoid string allocations. if (img.startsWith(IMPORT_JAVA_LANG)) {
if (img.indexOf('.', IMPORT_JAVA_LANG.length() + 1) != -1) {
// Importing from a subpackage / inner class
return data;
}
addViolation(data, node);
} I would greatly appreciate if you checked this passes all tests and update the PR. |
Nice improvement of the code. Will get this updated pull request out shortly. Thanks!! |
@@ -8,6 +8,7 @@ | |||
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule; | |||
|
|||
public class DontImportJavaLangRule extends AbstractJavaRule { | |||
private static final String IMPORT_JAVA_LANG = "java.lang."; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this extra dot at the end is causing a couple tests to fail on Travis
Looks good, just one more small fix before we get a green light and merge it :) |
There were actually two bugs, I had already fixed locally the change from "java.lang." -> "java.lang" but the logic is still failing on the following type of import:
This is because the evaluation of the img var is seeing 'java.lang.ref'. So it looks like some further work is needed to determine if this is a class or package as just looking for the '.' is enough. Will investigate further and do another push with a fix. |
I see, yes, for those cases the image is If you need any assistance on this just let me know. |
Awesome, the isImportOnDemand() was quite helpful. One question for you the original code would fail the following (note: there is not a test case for this in master) 'import java.lang.ProcessBuilder.*;' But I am think this should be ok and the new code I have does not flag a violation. What do you think, should this be a violation or not? |
@mpellegrini yes, now you mention it, the rule reporting on |
- Allow for import java.lang.ProcessBuilder.* - Update test cases to test for above scenario
@mpellegrini beautiful! Got rid of two separate false positives and made this future proof all in one PR. Thanks for the amazing job! I'll try to get this merged ASAP. Will definitely make it into PMD 5.6.0 |
Merged! Thanks for the amazing PR! Expect this in PMD 5.6.0 |
Thanks @jsotuyod. Glad I was able to contribute!! |
Please, prefix the PR title with the language it applies to within brackets, such as [java] or [apex]. If not specific to a language, you can use [core]
Before submitting a PR, please check that:
master
. The PMD team will merge back to support branches as needed.mvn test
passes.mvn checkstyle:check
passes. Check this for more infoPR Description:
In Java 7+ there was a new class added under the java.lang package. Specifically java.lang.invoke.MethodHandles.
MethodHandles provides a handy utility in which to obtain the current class name amongst other things. A technique that is gaining in popularity is to use MethodHandles to obtain the class name when getting a logger, for example
private final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
However, using this triggers a PMD violation
This pull request adds java.lang.invoke. to the existing exceptions to this rule to when using Java 7+.
Resolves #338
Resolves #339