Remove deprecated imports rules

Refs #3200, #3128, #2701
This commit is contained in:
Clément Fournier committed 2021-04-20 17:46:10 +02:00
1 parent 7301082d2e
commit 078e903b73
26 files changed
+54 -1261

No files matched your search

+1 -4
View File
@@ -48,7 +48,6 @@
<rule ref="category/java/bestpractices.xml/SystemPrintln"/>
<rule ref="category/java/bestpractices.xml/UnusedAssignment"/>
<rule ref="category/java/bestpractices.xml/UnusedFormalParameter"/>
<!-- <rule ref="category/java/bestpractices.xml/UnusedImports"/> -->
<rule ref="category/java/bestpractices.xml/UnusedLocalVariable"/>
<rule ref="category/java/bestpractices.xml/UnusedPrivateField"/>
<rule ref="category/java/bestpractices.xml/UnusedPrivateMethod"/>
@@ -76,8 +75,6 @@
<rule ref="category/java/codestyle.xml/ConfusingTernary"/>
<rule ref="category/java/codestyle.xml/ControlStatementBraces"/>
<rule ref="category/java/codestyle.xml/DefaultPackage"/>
<rule ref="category/java/codestyle.xml/DontImportJavaLang"/>
<!-- <rule ref="category/java/codestyle.xml/DuplicateImports"/> -->
<rule ref="category/java/codestyle.xml/EmptyMethodInAbstractClassShouldBeAbstract"/>
<rule ref="category/java/codestyle.xml/ExtendsObject"/>
<!-- <rule ref="category/java/codestyle.xml/FieldDeclarationsShouldBeAtStartOfClass"/> -->
@@ -109,6 +106,7 @@
<!-- <rule ref="category/java/codestyle.xml/UnnecessaryCast"/> -->
<rule ref="category/java/codestyle.xml/UnnecessaryConstructor"/>
<rule ref="category/java/codestyle.xml/UnnecessaryFullyQualifiedName"/>
<!-- <rule ref="category/java/codestyle.xml/UnnecessaryImport"/> -->
<rule ref="category/java/codestyle.xml/UnnecessaryLocalBeforeReturn"/>
<rule ref="category/java/codestyle.xml/UnnecessaryModifier"/>
<rule ref="category/java/codestyle.xml/UnnecessaryReturn"/>
@@ -229,7 +227,6 @@
<!-- <rule ref="category/java/errorprone.xml/FinalizeOverloaded"/> -->
<!-- <rule ref="category/java/errorprone.xml/FinalizeShouldBeProtected"/> -->
<!-- <rule ref="category/java/errorprone.xml/IdempotentOperations"/> -->
<!-- <rule ref="category/java/errorprone.xml/ImportFromSamePackage"/> -->
<!-- <rule ref="category/java/errorprone.xml/InstantiationToGetClass"/> -->
<!-- <rule ref="category/java/errorprone.xml/InvalidLogMessageFormat"/> -->
<rule ref="category/java/errorprone.xml/JUnitSpelling"/>
@@ -1,25 +0,0 @@
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.java.rule.bestpractices;
import java.util.HashSet;
import java.util.Set;
import net.sourceforge.pmd.lang.java.ast.internal.ImportWrapper;
import net.sourceforge.pmd.lang.java.rule.codestyle.UnnecessaryImportRule;
@Deprecated
public class UnusedImportsRule extends UnnecessaryImportRule {
// Note: when removing this from pmd 7, the compiled classes used
// for tests need to be moved to test/java/.../codestyle/unnecessaryimport
@Deprecated
protected Set<ImportWrapper> imports = new HashSet<>();
@Override
protected boolean justReportUnusedImports() {
return true;
}
}
@@ -1,117 +0,0 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.java.rule.codestyle;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
import net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration;
import net.sourceforge.pmd.lang.java.ast.internal.ImportWrapper;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
@Deprecated
public class DuplicateImportsRule extends AbstractJavaRule {
private static final Logger LOG = Logger.getLogger(DuplicateImportsRule.class.getName());
private Set<ImportWrapper> singleTypeImports;
private Set<ImportWrapper> importOnDemandImports;
@Override
public Object visit(ASTCompilationUnit node, Object data) {
singleTypeImports = new HashSet<>();
importOnDemandImports = new HashSet<>();
super.visit(node, data);
// this checks for things like:
// import java.io.*;
// import java.io.File;
for (ImportWrapper thisImportOnDemand : importOnDemandImports) {
for (ImportWrapper thisSingleTypeImport : singleTypeImports) {
String singleTypeFullName = thisSingleTypeImport.getFullName(); // java.io.File
String singleTypePkg = thisSingleTypeImport.getPackageName(); // java.io
String singleTypeName = thisSingleTypeImport.getName(); // File
if (thisImportOnDemand.getFullName().equals(singleTypePkg)
&& !isDisambiguationImport(node, singleTypePkg, singleTypeName)) {
addViolation(data, thisSingleTypeImport.getNode(), singleTypeFullName);
}
}
}
singleTypeImports.clear();
importOnDemandImports.clear();
return data;
}
/**
* Check whether this seemingly duplicate import is actually a
* disambiguation import.
*
* Example: import java.awt.*; import java.util.*; import java.util.List;
* //Needed because java.awt.List exists
*/
private boolean isDisambiguationImport(ASTCompilationUnit node, String singleTypePkg, String singleTypeName) {
// Loop over .* imports
for (ImportWrapper thisImportOnDemand : importOnDemandImports) {
// Skip same package
if (!thisImportOnDemand.getFullName().equals(singleTypePkg)) {
if (!thisImportOnDemand.isStaticOnDemand()) {
String fullyQualifiedClassName = thisImportOnDemand.getFullName() + "." + singleTypeName;
if (node.getClassTypeResolver().classNameExists(fullyQualifiedClassName)) {
// Class exists in another imported package
return true;
}
} else {
Class<?> importClass = node.getClassTypeResolver().loadClassOrNull(thisImportOnDemand.getFullName());
if (importClass != null) {
try {
for (Method m : importClass.getMethods()) {
if (Modifier.isStatic(m.getModifiers()) && m.getName().equals(singleTypeName)) {
// static method in another imported class
return true;
}
}
} catch (LinkageError e) {
// This is an incomplete classpath, report the missing class
LOG.log(Level.FINE, "Possible incomplete auxclasspath: Error while processing methods", e);
}
}
}
}
}
String fullyQualifiedClassName = "java.lang." + singleTypeName;
// Class might exist in another imported package
return node.getClassTypeResolver().classNameExists(fullyQualifiedClassName);
}
@Override
public Object visit(ASTImportDeclaration node, Object data) {
ImportWrapper wrapper = new ImportWrapper(node);
// blahhhh... this really wants to be ASTImportDeclaration to be
// polymorphic...
if (node.isImportOnDemand()) {
if (importOnDemandImports.contains(wrapper)) {
addViolation(data, node, node.getImportedName());
} else {
importOnDemandImports.add(wrapper);
}
} else {
if (singleTypeImports.contains(wrapper)) {
addViolation(data, node, node.getImportedName());
} else {
singleTypeImports.add(wrapper);
}
}
return data;
}
}
@@ -28,7 +28,6 @@ import net.sourceforge.pmd.lang.java.ast.TypeNode;
import net.sourceforge.pmd.lang.java.ast.internal.ImportWrapper;
import net.sourceforge.pmd.lang.java.ast.internal.PrettyPrintingUtil;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
import net.sourceforge.pmd.lang.java.rule.bestpractices.UnusedImportsRule;
public class UnnecessaryImportRule extends AbstractJavaRule {
// todo: java lang imports may be necessary if they're shadowed by a
@@ -64,10 +63,6 @@ public class UnnecessaryImportRule extends AbstractJavaRule {
private static final Pattern[] PATTERNS = { SEE_PATTERN, LINK_PATTERNS, VALUE_PATTERN, THROWS_PATTERN };
/**
* The deprecated rule {@link UnusedImportsRule} extends this class
* and overrides this.
*/
protected boolean justReportUnusedImports() {
return false;
}
@@ -1,30 +0,0 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.java.rule.errorprone;
import org.apache.commons.lang3.StringUtils;
import net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
import net.sourceforge.pmd.lang.java.symboltable.SourceFileScope;
@Deprecated
public class ImportFromSamePackageRule extends AbstractJavaRule {
@Override
public Object visit(ASTImportDeclaration importDecl, Object data) {
String packageName = importDecl.getScope().getEnclosingScope(SourceFileScope.class).getPackageName();
if (packageName != null && packageName.equals(importDecl.getPackageName())) {
addViolation(data, importDecl);
}
// special case
if (packageName == null && StringUtils.isBlank(importDecl.getPackageName())) {
addViolation(data, importDecl);
}
return data;
}
}
@@ -1412,32 +1412,6 @@ public class Foo {
</example>
</rule>
<rule name="UnusedImports"
language="java"
since="1.0"
message="Unused import ''{0}''"
class="net.sourceforge.pmd.lang.java.rule.bestpractices.UnusedImportsRule"
deprecated="true"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_bestpractices.html#unusedimports">
<description>
Reports import statements that are not used within the file. This also reports
duplicate imports, and imports from the same package. The simplest fix is just
to delete those imports.
This rule is deprecated since PMD 6.34.0. Use the rule {% rule "java/codestyle/UnnecessaryImport" %}
from category codestyle instead.
</description>
<priority>4</priority>
<example>
<![CDATA[
import java.io.File; // not referenced or required
import java.util.*; // not referenced or required
public class Foo {}
]]>
</example>
</rule>
<rule name="UnusedLocalVariable"
language="java"
since="0.1"
@@ -440,69 +440,6 @@ The rule allows methods and fields annotated with Guava's @VisibleForTesting and
</properties>
</rule>
<rule name="DontImportJavaLang"
language="java"
since="0.5"
message="Avoid importing anything from the package 'java.lang'"
deprecated="true"
class="net.sourceforge.pmd.lang.rule.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_codestyle.html#dontimportjavalang">
<description>
Avoid importing anything from the package 'java.lang'. These classes are automatically imported (JLS 7.5.3).
This rule is deprecated since PMD 6.34.0. Use the rule {% rule "java/codestyle/UnnecessaryImport" %}
from category codestyle instead.
</description>
<priority>4</priority>
<properties>
<property name="version" value="3.1" />
<property name="xpath">
<value><![CDATA[
//ImportDeclaration[@Static = false()]
[@ImportedName = "java.lang"
or (let $rest := substring-after(@ImportedName, "java.lang.")
return not($rest = '') and not(contains($rest, '.') or @ImportOnDemand = true()))]
]]></value>
</property>
</properties>
<example>
<![CDATA[
import java.lang.String; // this is unnecessary
public class Foo {}
// --- in another source code file...
import java.lang.*; // this is bad
public class Foo {}
]]>
</example>
</rule>
<rule name="DuplicateImports"
language="java"
since="0.5"
message="Avoid duplicate imports such as ''{0}''"
deprecated="true"
class="net.sourceforge.pmd.lang.java.rule.codestyle.DuplicateImportsRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_codestyle.html#duplicateimports">
<description>
Duplicate or overlapping import statements should be avoided.
This rule is deprecated since PMD 6.34.0. Use the rule {% rule "java/codestyle/UnnecessaryImport" %}
from category codestyle instead.
</description>
<priority>4</priority>
<example>
<![CDATA[
import java.lang.String;
import java.lang.*;
public class Foo {}
]]>
</example>
</rule>
<rule name="EmptyMethodInAbstractClassShouldBeAbstract"
language="java"
since="4.1"
@@ -2016,32 +2016,6 @@ public class Foo {
</example>
</rule>
<rule name="ImportFromSamePackage"
language="java"
since="1.02"
deprecated="true"
message="No need to import a type that lives in the same package"
class="net.sourceforge.pmd.lang.java.rule.errorprone.ImportFromSamePackageRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_errorprone.html#importfromsamepackage">
<description>
There is no need to import a type that lives in the same package.
This rule is deprecated since PMD 6.34.0. Use the rule {% rule "java/codestyle/UnnecessaryImport" %}
from category codestyle instead.
</description>
<priority>3</priority>
<example>
<![CDATA[
package foo;
import foo.Buz; // no need for this
import foo.*; // or this
public class Bar{}
]]>
</example>
</rule>
<rule name="InstantiationToGetClass"
language="java"
since="2.0"
@@ -10,12 +10,12 @@ These rules deal with different problems that can occur with import statements.
</description>
<!-- Rules, that have been moved into a category -->
<rule ref="category/java/bestpractices.xml/UnusedImports" deprecated="true" />
<!-- <rule ref="category/java/bestpractices.xml/UnusedImports" deprecated="true" />-->
<rule ref="category/java/errorprone.xml/ImportFromSamePackage" deprecated="true" />
<!-- <rule ref="category/java/errorprone.xml/ImportFromSamePackage" deprecated="true" />-->
<rule ref="category/java/codestyle.xml/DontImportJavaLang" deprecated="true" />
<rule ref="category/java/codestyle.xml/DuplicateImports" deprecated="true" />
<!-- <rule ref="category/java/codestyle.xml/DontImportJavaLang" deprecated="true" />-->
<!-- <rule ref="category/java/codestyle.xml/DuplicateImports" deprecated="true" />-->
<rule ref="category/java/codestyle.xml/TooManyStaticImports" deprecated="true" />
<rule ref="category/java/codestyle.xml/UnnecessaryFullyQualifiedName" deprecated="true" />
</ruleset>
@@ -12,7 +12,7 @@ directly and don't use this ruleset anymore.
<rule ref="category/java/bestpractices.xml/LooseCoupling" deprecated="true" />
<rule ref="category/java/errorprone.xml/CloneMethodMustImplementCloneable" deprecated="true" />
<rule ref="category/java/bestpractices.xml/UnusedImports" deprecated="true" />
<!-- <rule ref="category/java/bestpractices.xml/UnusedImports" deprecated="true" />-->
<rule ref="category/java/design.xml/SignatureDeclareThrowsException" deprecated="true" />
</ruleset>
@@ -1,12 +0,0 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.java.rule.bestpractices;
import net.sourceforge.pmd.testframework.PmdRuleTst;
@org.junit.Ignore("Rule has not been updated yet")
public class UnusedImportsTest extends PmdRuleTst {
// no additional unit tests
}
@@ -1,11 +0,0 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.java.rule.codestyle;
import net.sourceforge.pmd.testframework.PmdRuleTst;
public class DontImportJavaLangTest extends PmdRuleTst {
// no additional unit tests
}
@@ -1,24 +0,0 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.java.rule.codestyle;
import net.sourceforge.pmd.testframework.PmdRuleTst;
@org.junit.Ignore("Rule has not been updated yet")
public class DuplicateImportsTest extends PmdRuleTst {
/**
* This is just for testing DuplicateImports for static imports and
* disambiguation.
*/
// Do not delete this method, its needed for a test case
// see:
// /pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/DuplicateImports.xml
// #1306 False positive on duplicate when using static imports
public static void assertTrue(String message, boolean condition) {
if (!condition) {
System.out.println(message);
}
}
}
@@ -1,8 +1,8 @@
/**
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.java.rule.bestpractices.unusedimports;
package net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport;
import java.util.Arrays;
import java.util.List;
@@ -1,8 +1,8 @@
/**
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.java.rule.bestpractices.unusedimports;
package net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport;
public class ClassWithStringConstants {
@@ -2,7 +2,7 @@
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.java.rule.bestpractices.unusedimports;
package net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport;
abstract class Hello {
@@ -2,7 +2,7 @@
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.java.rule.bestpractices.unusedimports;
package net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport;
class HelloMore extends Hello {
// ...
@@ -1,8 +1,8 @@
/**
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.java.rule.bestpractices.unusedimports;
package net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport;
import java.util.Objects;
@@ -1,8 +1,8 @@
/**
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.java.rule.bestpractices.unusedimports;
package net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport;
final class PackagePrivateUtils {
private PackagePrivateUtils() {
@@ -1,8 +1,8 @@
/**
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.java.rule.bestpractices.unusedimports;
package net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport;
public class PublicUtils {
private PublicUtils() {
@@ -1,12 +0,0 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.java.rule.errorprone;
import net.sourceforge.pmd.testframework.PmdRuleTst;
@org.junit.Ignore("Rule has not been updated yet")
public class ImportFromSamePackageTest extends PmdRuleTst {
// no additional unit tests
}
@@ -1,76 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<test-data
xmlns="http://pmd.sourceforge.net/rule-tests"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/rule-tests http://pmd.sourceforge.net/rule-tests_1_0_0.xsd">
<test-code>
<description>import java.lang.String</description>
<expected-problems>1</expected-problems>
<code><![CDATA[
import java.lang.String;
public class Foo {}
]]></code>
</test-code>
<test-code>
<description>import java.lang.*</description>
<expected-problems>1</expected-problems>
<code><![CDATA[
import java.lang.*;
public class Foo {}
]]></code>
</test-code>
<test-code>
<description>import java.lang.ref/reflect/annotation/instrument/management</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import java.lang.ref.*;
import java.lang.reflect.*;
import java.lang.annotation.*;
import java.lang.instrument.*;
import java.lang.management.*;
import java.lang.ProcessBuilder.*;
public class Foo {}
]]></code>
</test-code>
<test-code>
<description>Static Java imports are OK</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import static java.lang.Process.*;
public class Foo {}
]]></code>
</test-code>
<test-code>
<description>Importing java.lang.Thread.UncaughtExceptionHandler</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import java.lang.Thread.UncaughtExceptionHandler;
public class Foo {}
]]></code>
</test-code>
<test-code>
<description>import java.lang.ProcessBuilder.Redirect: #1031 false DontImportJavaLang</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import java.lang.ProcessBuilder.Redirect;
public class Foo {}
]]></code>
</test-code>
<test-code>
<description>import java.lang.invoke.MethodHandles: #339 false DontImportJavaLang</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import java.lang.invoke.MethodHandles;
public class Foo {}
]]></code>
</test-code>
</test-data>
@@ -1,120 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<test-data
xmlns="http://pmd.sourceforge.net/rule-tests"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/rule-tests http://pmd.sourceforge.net/rule-tests_1_0_0.xsd">
<test-code>
<description>duplicate single type imports</description>
<expected-problems>1</expected-problems>
<code><![CDATA[
import java.io.File;
import java.util.*;
import java.io.File;
public class Foo {}
]]></code>
</test-code>
<test-code>
<description>duplicate wildcard imports</description>
<expected-problems>1</expected-problems>
<code><![CDATA[
import java.io.*;
import java.io.*;
public class Foo {}
]]></code>
</test-code>
<test-code>
<description>single type import after wildcard import</description>
<expected-problems>1</expected-problems>
<code><![CDATA[
import java.util.*;
import java.net.*;
import java.io.*;
import java.io.File;
public class Foo {}
]]></code>
</test-code>
<test-code>
<description>subpackage import, ok</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import javax.servlet.*;
import javax.servlet.http.*;
public class Foo {}
]]></code>
</test-code>
<test-code>
<description>674394, disambiguation import should be allowed</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import java.awt.*;
import java.util.*;
import java.util.List; //False positive
class Foo{
Color color;
List list;
Set set;
}
]]></code>
</test-code>
<test-code>
<description>674394, disambiguation import because of conflict with java.lang</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import foo.*;
import foo.System; //False positive
class Foo {
System system; //No, I do not mean java.lang.System
}
]]></code>
</test-code>
<test-code>
<description>#1306 False positive on duplicate when using static imports</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
//import static org.hamcrest.Matchers.*;
//import static org.mockito.Matchers.any; // original problem - this is needed for disambiguation
//import static org.mockito.Matchers.*;
import static org.junit.Assert.*;
import static net.sourceforge.pmd.lang.java.rule.codestyle.DuplicateImportsTest.*;
import static org.junit.Assert.assertTrue; // this import is neeeded for disambiguation - as DuplicateImportsTest
// defines assertTrue with the same signature, too.
public class DuplicateImports {
}
]]></code>
</test-code>
<test-code>
<description>negative case - static on-demand imports</description>
<expected-problems>1</expected-problems>
<expected-linenumbers>2</expected-linenumbers>
<code><![CDATA[
import static org.junit.Assert.*;
import static org.junit.Assert.assertTrue;
public class DuplicateImports {
}
]]></code>
</test-code>
<test-code>
<description>[java] DuplicateImports reported for the same import... and import static... #2546</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import charles.X.*;
import static charles.X.*;
public class DuplicateImports {}
]]></code>
</test-code>
</test-data>
@@ -409,61 +409,62 @@ public class Foo {
<description>#925 [java] UnusedImports false positive for static import</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
package net.sourceforge.pmd.lang.java.rule.bestpractices.unusedimports;
package net.sourceforge.pmd.lang.java.rule.bestpractices.unusedimports;
import static net.sourceforge.pmd.lang.java.rule.bestpractices.unusedimports.ClassWithConstants.*;
import static net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport.ClassWithConstants.*;
public class ClassWithImport {
public class ClassWithImport {
public static void main(String[] args) {
System.out.println("List 1: " + LIST1);
System.out.println("List 2: " + LIST2);
}
}
]]></code>
public static void main(String[] args) {
System.out.println("List 1: " + LIST1);
System.out.println("List 2: " + LIST2);
}
}
]]></code>
</test-code>
<test-code>
<description>#1404 [java] UnusedImports false positive for static import</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
package net.sourceforge.pmd.lang.java.rule.bestpractices.unusedimports;
package net.sourceforge.pmd.lang.java.rule.bestpractices.unusedimports;
import static net.sourceforge.pmd.lang.java.rule.bestpractices.unusedimports.ClassWithStringConstants.*;
import static net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport.ClassWithStringConstants.*;
public class ClassWithImport {
public class ClassWithImport {
public static void main(String[] args) {
if (CONST1.equals("a")) {
System.out.println("CONST1 is a");
}
}
}
]]></code>
public static void main(String[] args) {
if (CONST1.equals("a")) {
System.out.println("CONST1 is a");
}
}
}
]]></code>
</test-code>
<test-code>
<description>#1209 [java] UnusedImports false positive for static import with package-private method usage</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
package net.sourceforge.pmd.lang.java.rule.bestpractices.unusedimports;
package net.sourceforge.pmd.lang.java.rule.bestpractices.unusedimports;
import static net.sourceforge.pmd.lang.java.rule.bestpractices.unusedimports.PackagePrivateUtils.*;
import static net.sourceforge.pmd.lang.java.rule.bestpractices.unusedimports.PublicUtils.*;
import static net.sourceforge.pmd.lang.java.rule.bestpractices.unusedimports.PackagePrivateUtils.*;
import static net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport.PublicUtils.*;
public class Imports {
int importtest() {
int i = 0;
i = f1(i);
i = g1(i);
i = f2(i);
i = g2(i);
i = f3(i);
i = g3(i);
return i;
}
}
]]></code>
public class Imports {
int importtest() {
int i = 0;
i = f1(i);
i = g1(i);
i = f2(i);
i = g2(i);
i = f3(i);
i = g3(i);
return i;
}
}
]]></code>
</test-code>
<test-code>
@@ -1,74 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<test-data
xmlns="http://pmd.sourceforge.net/rule-tests"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/rule-tests http://pmd.sourceforge.net/rule-tests_1_0_0.xsd">
<test-code>
<description>simple failure</description>
<expected-problems>1</expected-problems>
<code><![CDATA[
package foo;
import foo.Bar;
public class Baz{}
]]></code>
</test-code>
<test-code>
<description>class in default package importing from sub package</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
package foo;
import foo.buz.Bar;
public class Baz{}
]]></code>
</test-code>
<test-code>
<description>class in default package importing from other package</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import java.util.*;
public class Baz{}
]]></code>
</test-code>
<test-code>
<description>class not in default package importing from default package</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
package bar;
import Foo;
public class Baz{}
]]></code>
</test-code>
<test-code>
<description>class in default package importing from default package</description>
<expected-problems>1</expected-problems>
<code><![CDATA[
import Foo;
public class Baz{}
]]></code>
</test-code>
<test-code>
<description>importing from subpackage</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
package foo.bar;
import foo.bar.baz.*;
public class Baz{}
]]></code>
</test-code>
<test-code>
<description>importing all from same package</description>
<expected-problems>1</expected-problems>
<code><![CDATA[
package foo.bar;
import foo.bar.*;
public class Baz{}
]]></code>
</test-code>
</test-data>