diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index fcec8682a8..5dceab44c2 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -25,6 +25,7 @@ This is a minor release. * [#1215](https://github.com/pmd/pmd/issues/1215): \[doc] TOC links don't work? * java-codestyle * [#1211](https://github.com/pmd/pmd/issues/1211): \[java] CommentDefaultAccessModifier false positive with nested interfaces (regression from 6.4.0) + * [#1216](https://github.com/pmd/pmd/issues/1216): \[java] UnnecessaryFullyQualifiedName false positive for the same name method * java-design * [#1217](https://github.com/pmd/pmd/issues/1217): \[java] CyclomaticComplexityRule counts ?-operator twice diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTImportDeclaration.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTImportDeclaration.java index 3be5d48746..326105a7df 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTImportDeclaration.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTImportDeclaration.java @@ -9,10 +9,21 @@ package net.sourceforge.pmd.lang.java.ast; * Represents an import declaration in a Java file. * *
- *   ImportDeclaration ::= "import" [ "static" ] Name [ "." "*" ] ";"
+ *
+ * ImportDeclaration ::= "import" "static"? {@linkplain ASTName Name} ( "." "*" )? ";"
+ *
  * 
* + * @see JLS 7.5 + * */ +// TODO should this really be a type node? +// E.g. for on-demand imports, what's the type of this node? There's no type name, just a package name +// for on-demand static imports? +// for static imports of a field? the type of the field or the type of the enclosing type? +// for static imports of a method? +// I don't think we can work out a spec without surprising corner cases, and #1207 will abstract +// things away anyway, so I think we should make it a regular node public class ASTImportDeclaration extends AbstractJavaTypeNode { private boolean isImportOnDemand; @@ -27,18 +38,47 @@ public class ASTImportDeclaration extends AbstractJavaTypeNode { super(p, id); } + + /** + * @deprecated Will be made private with 7.0.0 + */ + @Deprecated public void setImportOnDemand() { isImportOnDemand = true; } + + // @formatter:off + /** + * Returns true if this is an import-on-demand declaration, + * aka "wildcard import". + * + * + */ + // @formatter:on public boolean isImportOnDemand() { return isImportOnDemand; } + + /** + * @deprecated Will be made private with 7.0.0 + */ + @Deprecated public void setStatic() { isStatic = true; } + + /** + * Returns true if this is a static import. If this import is not on-demand, + * {@link #getImportedSimpleName()} returns the name of the imported member. + */ public boolean isStatic() { return isStatic; } @@ -52,7 +92,7 @@ public class ASTImportDeclaration extends AbstractJavaTypeNode { /** * Returns the full name of the import. For on-demand imports, this is the name without - * the final asterisk. + * the final dot and asterisk. */ public String getImportedName() { return jjtGetChild(0).getImage(); @@ -106,6 +146,8 @@ public class ASTImportDeclaration extends AbstractJavaTypeNode { * auxclasspath is not correctly set, as this method depends on correct * type resolution. */ + // TODO deprecate? This is only used in a test. I don't think it's really + // useful and it gives work to ClassTypeResolver. public Package getPackage() { return this.pkg; } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/UnnecessaryFullyQualifiedNameRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/UnnecessaryFullyQualifiedNameRule.java index 0300cd4d1d..37c2b06d9d 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/UnnecessaryFullyQualifiedNameRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/UnnecessaryFullyQualifiedNameRule.java @@ -133,7 +133,7 @@ public class UnnecessaryFullyQualifiedNameRule extends AbstractJavaRule { matches.add(importDeclaration); } } - } else { + } else if (!importDeclaration.isImportOnDemand()) { // last part matches? if (nameParts[nameParts.length - 1].equals(importParts[importParts.length - 1])) { matches.add(importDeclaration); diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/UnnecessaryFullyQualifiedName.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/UnnecessaryFullyQualifiedName.xml index fbc79c8550..4077e9beeb 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/UnnecessaryFullyQualifiedName.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/UnnecessaryFullyQualifiedName.xml @@ -440,4 +440,22 @@ public class JavaLang { } ]]> + + + #1216 [java] UnnecessaryFullyQualifiedName false positive for the same name method + 0 + stream(Container parent) { + return Arrays.asList("", ""); + } + } + ]]> +