Skip to content
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] UnnecessaryImport false-positive on generic method call with on lambda #4816

Closed
blacelle opened this issue Feb 12, 2024 · 12 comments · Fixed by #4840
Closed

[java] UnnecessaryImport false-positive on generic method call with on lambda #4816

blacelle opened this issue Feb 12, 2024 · 12 comments · Fixed by #4840
Labels
a:false-positive PMD flags a piece of code that is not problematic
Milestone

Comments

@blacelle
Copy link

blacelle commented Feb 12, 2024

Affects PMD Version:
7.0.0-rc4

Rule: UnnecessaryImport

Description:

I get multiple unexpected unused imports:

[INFO] --- maven-pmd-plugin:3.21.2:check (default-cli) @ sp-core ---
[INFO] PMD version: 7.0.0-rc4
[INFO] PMD Failure: io.mitrust.sp.scope.ReportUnusuedImportFalsePositive:4 Rule:UnnecessaryImport Priority:4 Unused import 'java.util.TreeSet'.
[INFO] PMD Failure: io.mitrust.sp.scope.ReportUnusuedImportFalsePositive:6 Rule:UnnecessaryImport Priority:4 Unused import 'java.util.stream.Collectors'.
[INFO] PMD Failure: io.mitrust.sp.scope.ReportUnusuedImportFalsePositive:9 Rule:UnnecessaryImport Priority:4 Unused import 'org.jooq.Record1'.

Code Sample demonstrating the issue:

package io.mitrust.sp.scope;

import java.util.NavigableSet;
import java.util.TreeSet;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.jooq.DSLContext;
import org.jooq.Record1;

import io.mitrust.jooq.pgsql.generated.tables.MitrustScopes;

public class ReportUnusuedImportFalsePositive {

	protected <R> R onDSLContext(Function<DSLContext, R> contextConsumer) {
		return null;
	}

	protected MitrustScopes t() {
		return MitrustScopes.MITRUST_SCOPES;
	}

	public NavigableSet<String> getScopes(String useCase) {
		return onDSLContext(dsl -> dsl.select(t().SCOPE)
				.from(t())
				.where(t().USE_CASE.eq(useCase))
				.stream()
				.map(Record1::value1)
				.collect(Collectors.toCollection(TreeSet::new)));
	}

}

Expected outcome:

PMD reports a violation at line ..., but that's wrong. That's a false positive.

Running PMD through: [CLI | Ant | Maven | Gradle | Designer | Other]

@blacelle blacelle added the a:false-positive PMD flags a piece of code that is not problematic label Feb 12, 2024
@blacelle
Copy link
Author

Where can I add @SuppressWarnings("PMD.UnnecessaryImport") to workaround this?

@blacelle blacelle changed the title [java] 7.0.0-rc4 ReportUnusuedImport FalsePositive false-positive (on Lambda?) [java] 7.0.0-rc4 ReportUnusuedImport false-positive (on Lambda?) Feb 12, 2024
@adangel adangel changed the title [java] 7.0.0-rc4 ReportUnusuedImport false-positive (on Lambda?) [java] UnnecessaryImport false-positive (on Lambda?) (7.0.0-rc4) Feb 13, 2024
@adangel
Copy link
Member

adangel commented Feb 13, 2024

Thanks for reporting this issue. Yes, this seems to be a false positive.

To add a suppression until this is fixed, you'll need to use the NOPMD comment, e.g.

import java.util.TreeSet; // NOPMD false positive, see https://github.com/pmd/pmd/issues/4816

@blacelle
Copy link
Author

blacelle commented Feb 15, 2024

In the same project, I have another case of UnnecessaryImport false-positive. However, it is problematic to report as it triggers only in our CI, and not locally. The code looks more standard (i.e. not Lambda related, while many of many recent reports seems related to Lambda):

@Override
protected Optional<Map<String, ?>> doExtractAddress(streetAddress, postalCode) throws IOException {
	String addressFormatted = joinAddressParts(Stream.of(streetAddress, postalCode));

            return addressFormatted;
}

I would be happy to open a separate ticket, but the report would be awkward as I have no local reproduction scenario. I tried with -X but I did not spot particular logs out of some Cannot resolve ambiguous name eventBus, treating it as ambiguous which seems acceptable, and not related to IntStream Stream (which is one of 30 imports in given class).

@jsotuyod
Copy link
Member

@blacelle can you provide a little more context on that second scenario? You mention IntStream as the unnecessary import, but that is not used in your snippet…

Also, can you please share how is the CI running PMD? This may be down to an incomplete auxclasspath.

@blacelle
Copy link
Author

I'm sorry for the confusion @jsotuyod , this second issue happening in CI but not in local is not for IntStream but Stream.

14:24:50,665 [INFO] PMD Failure: io.xxx.address.AAA:11 Rule:UnnecessaryImport Priority:4 Unused import 'java.util.stream.Stream'.
14:24:50,665 [INFO] PMD Failure: io.xxx.address.BBB:11 Rule:UnnecessaryImport Priority:4 Unused import 'java.util.stream.Stream'.

The single usage in given class looks like the provided piece of code:

@Override
protected Optional<Map<String, ?>> doExtractAddress(streetAddress, postalCode) throws IOException {
	String addressFormatted = joinAddressParts(Stream.of(streetAddress, postalCode));

            return addressFormatted;
}

Also, can you please share how is the CI running PMD? This may be down to an incomplete auxclasspath.

I connected through ssh, and reproduced it with a simple mvn pmd:check -X.
(Note for myself: mvn pmd:check -pl :data-postprocessing -X

CI:

Picked up JAVA_TOOL_OPTIONS: -Xmx1024m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/home/circleci/heapdump.hprof
Apache Maven 3.9.6 (bc0240f3c744dd6b6ec2920b3cd08dcc295161ae)
Maven home: /opt/apache-maven
Java version: 17.0.10, vendor: Eclipse Adoptium, runtime: /usr/local/jdk-17.0.10
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "5.15.0-1050-aws", arch: "amd64", family: "unix"

Local:

Apache Maven 3.8.3 (ff8e977a158738155dc465c6a97ffaf31982d739)
Maven home: /usr/local/Cellar/maven/3.8.3/libexec
Java version: 17.0.2, vendor: Eclipse Adoptium, runtime: /Library/Java/JavaVirtualMachines/temurin-17.jdk/Contents/Home
Default locale: en_FR, platform encoding: UTF-8
OS name: "mac os x", version: "14.2", arch: "x86_64", family: "mac"

CI libs:

18:15:02,134 [DEBUG] Populating class realm plugin>org.apache.maven.plugins:maven-pmd-plugin:3.21.2
18:15:02,134 [DEBUG]   Included: org.apache.maven.plugins:maven-pmd-plugin:jar:3.21.2
18:15:02,135 [DEBUG]   Included: net.sourceforge.pmd:pmd-compat6:jar:7.0.0-rc4+with-pr4749
18:15:02,135 [DEBUG]   Included: net.sourceforge.pmd:pmd-java:jar:7.0.0-rc4
18:15:02,135 [DEBUG]   Included: org.ow2.asm:asm:jar:9.5
18:15:02,135 [DEBUG]   Included: org.apache.commons:commons-lang3:jar:3.8.1
18:15:02,135 [DEBUG]   Included: org.checkerframework:checker-qual:jar:2.5.2
18:15:02,135 [DEBUG]   Included: net.sf.saxon:Saxon-HE:jar:10.7
18:15:02,135 [DEBUG]   Included: org.pcollections:pcollections:jar:3.1.3
18:15:02,135 [DEBUG]   Included: net.sourceforge.pmd:pmd-core:jar:7.0.0-rc4
18:15:02,135 [DEBUG]   Included: org.antlr:antlr4-runtime:jar:4.9.1
18:15:02,135 [DEBUG]   Included: com.google.code.gson:gson:jar:2.8.9
18:15:02,135 [DEBUG]   Included: com.github.oowekyala.ooxml:nice-xml-messages:jar:3.1
18:15:02,135 [DEBUG]   Included: net.sourceforge.pmd:pmd-javascript:jar:7.0.0-rc4
18:15:02,135 [DEBUG]   Included: org.mozilla:rhino:jar:1.7.14
18:15:02,135 [DEBUG]   Included: net.sourceforge.pmd:pmd-jsp:jar:7.0.0-rc4
18:15:02,135 [DEBUG]   Included: org.apache.maven.shared:maven-artifact-transfer:jar:0.13.1
18:15:02,135 [DEBUG]   Included: org.sonatype.aether:aether-util:jar:1.7
18:15:02,135 [DEBUG]   Included: org.sonatype.sisu:sisu-inject-bean:jar:1.4.2
18:15:02,135 [DEBUG]   Included: org.sonatype.sisu:sisu-guice:jar:noaop:2.1.7
18:15:02,135 [DEBUG]   Included: org.codehaus.plexus:plexus-interpolation:jar:1.14
18:15:02,135 [DEBUG]   Included: org.sonatype.plexus:plexus-sec-dispatcher:jar:1.3
18:15:02,135 [DEBUG]   Included: org.sonatype.plexus:plexus-cipher:jar:1.4
18:15:02,135 [DEBUG]   Included: org.codehaus.plexus:plexus-component-annotations:jar:2.0.0
18:15:02,135 [DEBUG]   Included: org.apache.maven.shared:maven-common-artifact-filters:jar:3.3.2
18:15:02,135 [DEBUG]   Included: org.slf4j:jul-to-slf4j:jar:1.7.36
18:15:02,135 [DEBUG]   Included: org.apache.maven.doxia:doxia-sink-api:jar:1.12.0
18:15:02,135 [DEBUG]   Included: org.apache.maven.doxia:doxia-logging-api:jar:1.12.0
18:15:02,135 [DEBUG]   Included: org.apache.maven.doxia:doxia-core:jar:1.12.0
18:15:02,135 [DEBUG]   Included: org.apache.commons:commons-text:jar:1.3
18:15:02,135 [DEBUG]   Included: org.apache.httpcomponents:httpclient:jar:4.5.13
18:15:02,135 [DEBUG]   Included: commons-logging:commons-logging:jar:1.2
18:15:02,136 [DEBUG]   Included: commons-codec:commons-codec:jar:1.11
18:15:02,136 [DEBUG]   Included: org.apache.httpcomponents:httpcore:jar:4.4.14
18:15:02,136 [DEBUG]   Included: org.apache.maven.doxia:doxia-site-renderer:jar:1.11.1
18:15:02,136 [DEBUG]   Included: org.apache.maven.doxia:doxia-decoration-model:jar:1.11.1
18:15:02,136 [DEBUG]   Included: org.apache.maven.doxia:doxia-skin-model:jar:1.11.1
18:15:02,136 [DEBUG]   Included: org.apache.maven.doxia:doxia-module-xhtml:jar:1.11.1
18:15:02,136 [DEBUG]   Included: org.apache.maven.doxia:doxia-module-xhtml5:jar:1.11.1
18:15:02,136 [DEBUG]   Included: org.codehaus.plexus:plexus-velocity:jar:1.2
18:15:02,136 [DEBUG]   Included: org.apache.velocity:velocity:jar:1.7
18:15:02,136 [DEBUG]   Included: commons-lang:commons-lang:jar:2.4
18:15:02,136 [DEBUG]   Included: org.apache.velocity:velocity-tools:jar:2.0
18:15:02,136 [DEBUG]   Included: commons-beanutils:commons-beanutils:jar:1.7.0
18:15:02,136 [DEBUG]   Included: commons-digester:commons-digester:jar:1.8
18:15:02,136 [DEBUG]   Included: commons-chain:commons-chain:jar:1.1
18:15:02,136 [DEBUG]   Included: dom4j:dom4j:jar:1.1
18:15:02,136 [DEBUG]   Included: oro:oro:jar:2.0.8
18:15:02,136 [DEBUG]   Included: commons-collections:commons-collections:jar:3.2.2
18:15:02,136 [DEBUG]   Included: org.apache.maven.reporting:maven-reporting-impl:jar:3.2.0
18:15:02,136 [DEBUG]   Included: org.apache.maven.reporting:maven-reporting-api:jar:3.1.1
18:15:02,136 [DEBUG]   Included: javax.enterprise:cdi-api:jar:1.2
18:15:02,136 [DEBUG]   Included: org.eclipse.sisu:org.eclipse.sisu.inject:jar:0.9.0.M2
18:15:02,136 [DEBUG]   Included: org.apache.maven.shared:maven-shared-utils:jar:3.3.4
18:15:02,136 [DEBUG]   Included: commons-io:commons-io:jar:2.6
18:15:02,136 [DEBUG]   Included: org.apache.maven.doxia:doxia-integration-tools:jar:1.11.1
18:15:02,136 [DEBUG]   Included: junit:junit:jar:3.8.1
18:15:02,136 [DEBUG]   Included: org.codehaus.plexus:plexus-resources:jar:1.2.0
18:15:02,136 [DEBUG]   Included: org.codehaus.plexus:plexus-utils:jar:4.0.0
18:15:02,136 [DEBUG]   Included: org.codehaus.plexus:plexus-xml:jar:3.0.0
18:15:02,136 [DEBUG]   Included: org.codehaus.plexus:plexus-i18n:jar:1.0-beta-10
18:15:02,260 [DEBUG] Loading mojo org.apache.maven.plugins:maven-pmd-plugin:3.21.2:pmd from plugin realm ClassRealm[plugin>org.apache.maven.plugins:maven-pmd-plugin:3.21.2, parent: jdk.internal.loader.ClassLoaders$AppClassLoader@5ffd2b27]

Hope it helps.

@blacelle
Copy link
Author

blacelle commented Feb 16, 2024

I have another CI-only false-positive:

07:35:29,650 [INFO] PMD Failure: io.xxx.openid.profiles.filter.XXX:8 Rule:UnnecessaryImport Priority:4 Unused import 'java.util.Optional'.
07:35:29,650 [INFO] PMD Failure: io.xxx.openid.profiles.match.YYY:8 Rule:UnnecessaryImport Priority:4 Unused import 'java.util.Optional'.

while previous case was:

14:24:50,665 [INFO] PMD Failure: io.xxx.address.AAA:11 Rule:UnnecessaryImport Priority:4 Unused import 'java.util.stream.Stream'.
14:24:50,665 [INFO] PMD Failure: io.xxx.address.BBB:11 Rule:UnnecessaryImport Priority:4 Unused import 'java.util.stream.Stream'.

One (very weird, but still) pattern is given module fails with :

  • 2 classes
  • with the same Import
  • the import is at the same line of the input .java file

In the first module, both classes are similar, so it looked like chance. In the second case, the 2 classes are very different. Might be a red-herring.

edit: I have a 3rd case of this issue, but with a single concerned class in the module. However, the same module as other (CI-only) false-positives like:

09:16:26,090 [INFO] PMD Failure: io.xxx.dp.antifraud.XXX:34 Rule:ReplaceVectorWithList Priority:3 Consider replacing this Vector with the newer java.util.List.
09:16:26,090 [INFO] PMD Failure: io.xxx.dp.antifraud.XXX:34 Rule:ReplaceVectorWithList Priority:3 Consider replacing this Vector with the newer java.util.List.
09:16:26,090 [INFO] PMD Failure: io.xxx.dp.antifraud.XXX:34 Rule:ReplaceVectorWithList Priority:3 Consider replacing this Vector with the newer java.util.List.
09:16:26,090 [INFO] PMD Failure: io.xxx.dp.antifraud.XXX:35 Rule:ReplaceVectorWithList Priority:3 Consider replacing this Vector with the newer java.util.List.

while given class (or the whole module) has no use of java.util.Vector.

@blacelle
Copy link
Author

blacelle commented Feb 16, 2024

FYI I'll stop the migration of given (closed-source project from PMD 6.55.0 to 7.0.0-rc4. I opened multiple false-positive issues, but the leftovers suggests something more tricky is going on (CI-only, referring classes not used in the module, etc).

I'll be happy to start it again with 7.0.0-rc5 or based on additional feedback from PMD team.

@adangel adangel changed the title [java] UnnecessaryImport false-positive (on Lambda?) (7.0.0-rc4) [java] UnnecessaryImport false-positive on generic method call with on lambda Feb 29, 2024
@adangel adangel added this to the 7.0.0 milestone Feb 29, 2024
@adangel
Copy link
Member

adangel commented Feb 29, 2024

I found the issue with the first and probably second case: We couldn't figure out the exact method, that has been called (onDSLContext in your first example) and stopped analyzing the arguments of this method call - so we didn't see the Record1, Collectors and TreeSet references.
I think, the second example is the same (we probably couldn't figure out which method overload of joinAddressParts is being called and stopped analyzing the arguments which have a reference to Stream).

For the other cases, I don't have enough information to reproduce the issue. I'd say, once #4840 is merged and delivered, please open a new issue, if you encounter more false positive with this rule.

As for the false positives for the rule "ReplaceVectorWithList" please open another issue - this is a different rule so is off-topic for this issue.

@blacelle
Copy link
Author

blacelle commented Feb 29, 2024 via email

@jsotuyod
Copy link
Member

@blacelle just to check here…

I connected through ssh, and reproduced it with a simple mvn pmd:check -X.
(Note for myself: mvn pmd:check -pl :data-postprocessing -X

Which was it? using -pl may skip building other modules (that may be pre-built though other executions in local), producing the incomplete auxclasspath.

@blacelle
Copy link
Author

blacelle commented Mar 1, 2024

Which was it?

The command with -pl was reproducing the same false-positive as the command without -pl (but it was faster to reproducing them). In other words, I could not reproduce these issues in local, but they happens in CI, without and without -pl.

I'm kind of astonished this is not related to lambda usage, as this (and other cases reported by myself) seemed related to lambda.

@adangel
Copy link
Member

adangel commented Mar 1, 2024

Any idea why it was happening only in CI ? It may help with the various CI
only issues.

The CI could use a different Java Version, if you are using maven, you could have different jar files laying around in your $HOME/.m2/repository, the different maven version could do slightly different dependency resolution resulting in a different classpaths.

You should update your local environment to the exact versions of java (17.0.10) and maven (3.9.6) that the CI uses and start with a fresh local maven repository.

Locally you are using MacOS, but the CI is running on Linux...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:false-positive PMD flags a piece of code that is not problematic
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants