Merge branch 'pr-1220'

This commit is contained in:
Andreas Dangel committed 2018-07-08 10:38:55 +02:00
commit f8e20d0ac5
4 files changed
+64 -3

No files matched your search

+1
View File
@@ -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
@@ -9,10 +9,21 @@ package net.sourceforge.pmd.lang.java.ast;
* Represents an import declaration in a Java file.
*
* <pre>
* ImportDeclaration ::= "import" [ "static" ] Name [ "." "*" ] ";"
*
* ImportDeclaration ::= "import" "static"? {@linkplain ASTName Name} ( "." "*" )? ";"
*
* </pre>
*
* @see <a href="https://docs.oracle.com/javase/specs/jls/se9/html/jls-7.html#jls-7.5">JLS 7.5</a>
*
*/
// 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".
*
* <ul>
* <li>If this is a static import, then the imported names are those
* of the accessible static members of the named type;
* <li>Otherwise, the imported names are the names of the accessible types
* of the named type or named package.
* </ul>
*/
// @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;
}
@@ -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);
@@ -440,4 +440,22 @@ public class JavaLang {
}
]]></code>
</test-code>
<test-code>
<description>#1216 [java] UnnecessaryFullyQualifiedName false positive for the same name method</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import java.awt.Component;
import java.awt.Container;
import java.util.Arrays;
import java.util.asList.*; // we don't use stream since we're on java 7
public final class Test {
public static List<String> stream(Container parent) {
return Arrays.asList("", "");
}
}
]]></code>
</test-code>
</test-data>