pmd / pmd

An extensible multilanguage static code analyzer.
https://pmd.github.io
Other
4.9k stars 1.5k forks source link

[java] Error when not common field is not declared as final in a class #5344

Open anuragagarwal561994 opened 1 week ago

anuragagarwal561994 commented 1 week ago

Affects PMD Version:

This is happening since pmd v7, but my test version is 7.7.0

Description:

When a field with not well known types are defined as non private field in an object, pmd is not able to analyse the class and breaks. While when the field is declared final, there is no such issue.

Exception Stacktrace:

20:14:25 [ERROR] Parsing failed in ParseLock#doParse()
20:14:27 java.lang.IllegalArgumentException: Invalid type reference for method or ctor type annotation: 16
20:14:27     at net.sourceforge.pmd.lang.java.symbols.internal.asm.GenericSigBase$LazyMethodType.acceptAnnotationAfterParse (GenericSigBase.java:397)
20:14:27     at net.sourceforge.pmd.lang.java.symbols.internal.asm.TypeAnnotationHelper$TypeAnnotationSetWithReferences.forEach (TypeAnnotationHelper.java:72)
20:14:27     at net.sourceforge.pmd.lang.java.symbols.internal.asm.GenericSigBase$LazyMethodType.doParse (GenericSigBase.java:256)
20:14:27     at net.sourceforge.pmd.lang.java.symbols.internal.asm.GenericSigBase$1.doParse (GenericSigBase.java:54)
20:14:27     at net.sourceforge.pmd.lang.java.symbols.internal.asm.ParseLock.getFinalStatus (ParseLock.java:33)
20:14:27     at net.sourceforge.pmd.lang.java.symbols.internal.asm.ParseLock.ensureParsed (ParseLock.java:22)
20:14:27     at net.sourceforge.pmd.lang.java.symbols.internal.asm.GenericSigBase.ensureParsed (GenericSigBase.java:76)
20:14:27     at net.sourceforge.pmd.lang.java.symbols.internal.asm.GenericSigBase$LazyMethodType.getParameterTypes (GenericSigBase.java:312)
20:14:27     at net.sourceforge.pmd.lang.java.symbols.internal.asm.ExecutableStub.getArity (ExecutableStub.java:83)
20:14:27     at net.sourceforge.pmd.lang.java.types.JMethodSig.getArity (JMethodSig.java:118)
20:14:27     at net.sourceforge.pmd.lang.java.types.TypeOps.areOverrideEquivalent (TypeOps.java:1323)
20:14:27     at net.sourceforge.pmd.lang.java.types.internal.infer.OverloadSet$ContextIndependentSet.shouldTakePrecedence (OverloadSet.java:110)
20:14:27     at net.sourceforge.pmd.lang.java.types.internal.infer.OverloadSet$ContextIndependentSet.shouldTakePrecedence (OverloadSet.java:98)
20:14:27     at net.sourceforge.pmd.lang.java.types.internal.infer.OverloadSet.add (OverloadSet.java:48)
20:14:27     at net.sourceforge.pmd.lang.java.types.internal.infer.OverloadSet$ContextIndependentSet.add (OverloadSet.java:122)
20:14:27     at net.sourceforge.pmd.lang.java.types.internal.infer.OverloadSet$ContextIndependentSet.add (OverloadSet.java:98)
20:14:27     at java.util.stream.ReduceOps$3ReducingSink.accept (ReduceOps.java:169)
20:14:27     at java.util.stream.ForEachOps$ForEachOp$OfRef.accept (ForEachOps.java:184)
20:14:27     at java.util.stream.ReferencePipeline$3$1.accept (ReferencePipeline.java:197)
20:14:27     at java.util.stream.ReferencePipeline$2$1.accept (ReferencePipeline.java:179)
20:14:27     at java.util.ArrayList$ArrayListSpliterator.forEachRemaining (ArrayList.java:1708)
20:14:27     at java.util.stream.AbstractPipeline.copyInto (AbstractPipeline.java:509)
20:14:27     at java.util.stream.AbstractPipeline.wrapAndCopyInto (AbstractPipeline.java:499)
20:14:27     at java.util.stream.ForEachOps$ForEachOp.evaluateSequential (ForEachOps.java:151)
20:14:27     at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential (ForEachOps.java:174)
20:14:27     at java.util.stream.AbstractPipeline.evaluate (AbstractPipeline.java:234)
20:14:27     at java.util.stream.ReferencePipeline.forEach (ReferencePipeline.java:596)
20:14:27     at java.util.stream.ReferencePipeline$7$1.accept (ReferencePipeline.java:276)
20:14:27     at java.util.Iterator.forEachRemaining (Iterator.java:133)
20:14:27     at java.util.Spliterators$IteratorSpliterator.forEachRemaining (Spliterators.java:1939)
20:14:27     at java.util.stream.AbstractPipeline.copyInto (AbstractPipeline.java:509)
20:14:27     at java.util.stream.AbstractPipeline.wrapAndCopyInto (AbstractPipeline.java:499)
20:14:27     at java.util.stream.ReduceOps$ReduceOp.evaluateSequential (ReduceOps.java:921)
20:14:27     at java.util.stream.AbstractPipeline.evaluate (AbstractPipeline.java:234)
20:14:27     at java.util.stream.ReferencePipeline.collect (ReferencePipeline.java:682)
20:14:27     at net.sourceforge.pmd.lang.java.types.TypeOps.getMethodsOf (TypeOps.java:1910)
20:14:27     at net.sourceforge.pmd.lang.java.types.internal.infer.ast.MethodInvocMirror.getAccessibleCandidates (MethodInvocMirror.java:73)
20:14:27     at net.sourceforge.pmd.lang.java.types.internal.infer.ast.MethodInvocMirror.getAccessibleCandidates (MethodInvocMirror.java:26)
20:14:27     at net.sourceforge.pmd.lang.java.types.internal.infer.Infer.computeCompileTimeDecl (Infer.java:303)
20:14:27     at net.sourceforge.pmd.lang.java.types.internal.infer.Infer.getCompileTimeDecl (Infer.java:281)
20:14:27     at net.sourceforge.pmd.lang.java.types.internal.infer.Infer.goToInvocationWithFallback (Infer.java:214)
20:14:27     at net.sourceforge.pmd.lang.java.types.internal.infer.Infer.inferInvocationRecursively (Infer.java:176)
20:14:27     at net.sourceforge.pmd.lang.java.types.ast.internal.PolyResolution.inferInvocation (PolyResolution.java:263)
20:14:27     at net.sourceforge.pmd.lang.java.types.ast.internal.PolyResolution.polyTypeOtherCtx (PolyResolution.java:135)
20:14:27     at net.sourceforge.pmd.lang.java.types.ast.internal.PolyResolution.computePolyType (PolyResolution.java:125)
20:14:27     at net.sourceforge.pmd.lang.java.types.ast.internal.LazyTypeResolver.handlePoly (LazyTypeResolver.java:358)
20:14:27     at net.sourceforge.pmd.lang.java.types.ast.internal.LazyTypeResolver.visit (LazyTypeResolver.java:363)
20:14:27     at net.sourceforge.pmd.lang.java.types.ast.internal.LazyTypeResolver.visit (LazyTypeResolver.java:103)
20:14:27     at net.sourceforge.pmd.lang.java.ast.ASTMethodCall.acceptVisitor (ASTMethodCall.java:71)
20:14:27     at net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode.getTypeMirror (AbstractJavaTypeNode.java:51)
20:14:27     at net.sourceforge.pmd.lang.java.ast.ASTMethodCall.getTypeMirror (ASTMethodCall.java:22)
20:14:27     at net.sourceforge.pmd.lang.java.types.internal.infer.ast.MethodInvocMirror.getAccessibleCandidates (MethodInvocMirror.java:68)
20:14:27     at net.sourceforge.pmd.lang.java.types.internal.infer.ast.MethodInvocMirror.getAccessibleCandidates (MethodInvocMirror.java:26)
20:14:27     at net.sourceforge.pmd.lang.java.types.internal.infer.Infer.computeCompileTimeDecl (Infer.java:303)
20:14:27     at net.sourceforge.pmd.lang.java.types.internal.infer.Infer.getCompileTimeDecl (Infer.java:281)
20:14:27     at net.sourceforge.pmd.lang.java.types.internal.infer.Infer.goToInvocationWithFallback (Infer.java:214)
20:14:27     at net.sourceforge.pmd.lang.java.types.internal.infer.Infer.inferInvocationRecursively (Infer.java:176)
20:14:27     at net.sourceforge.pmd.lang.java.types.ast.internal.PolyResolution.inferInvocation (PolyResolution.java:263)
20:14:27     at net.sourceforge.pmd.lang.java.types.ast.internal.PolyResolution.polyTypeOtherCtx (PolyResolution.java:135)
20:14:27     at net.sourceforge.pmd.lang.java.types.ast.internal.PolyResolution.computePolyType (PolyResolution.java:125)
20:14:27     at net.sourceforge.pmd.lang.java.types.ast.internal.LazyTypeResolver.handlePoly (LazyTypeResolver.java:358)
20:14:27     at net.sourceforge.pmd.lang.java.types.ast.internal.LazyTypeResolver.visit (LazyTypeResolver.java:363)
20:14:27     at net.sourceforge.pmd.lang.java.types.ast.internal.LazyTypeResolver.visit (LazyTypeResolver.java:103)
20:14:27     at net.sourceforge.pmd.lang.java.ast.ASTMethodCall.acceptVisitor (ASTMethodCall.java:71)
20:14:27     at net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode.getTypeMirror (AbstractJavaTypeNode.java:51)
20:14:27     at net.sourceforge.pmd.lang.java.ast.ASTMethodCall.getTypeMirror (ASTMethodCall.java:22)
20:14:27     at net.sourceforge.pmd.lang.java.types.internal.infer.ast.MethodInvocMirror.getAccessibleCandidates (MethodInvocMirror.java:68)
20:14:27     at net.sourceforge.pmd.lang.java.types.internal.infer.ast.MethodInvocMirror.getAccessibleCandidates (MethodInvocMirror.java:26)
20:14:27     at net.sourceforge.pmd.lang.java.types.internal.infer.Infer.computeCompileTimeDecl (Infer.java:303)
20:14:27     at net.sourceforge.pmd.lang.java.types.internal.infer.Infer.getCompileTimeDecl (Infer.java:281)
20:14:27     at net.sourceforge.pmd.lang.java.types.internal.infer.Infer.goToInvocationWithFallback (Infer.java:214)
20:14:27     at net.sourceforge.pmd.lang.java.types.internal.infer.Infer.inferInvocationRecursively (Infer.java:176)
20:14:27     at net.sourceforge.pmd.lang.java.types.ast.internal.PolyResolution.inferInvocation (PolyResolution.java:263)
20:14:27     at net.sourceforge.pmd.lang.java.types.ast.internal.PolyResolution.polyTypeOtherCtx (PolyResolution.java:135)
20:14:27     at net.sourceforge.pmd.lang.java.types.ast.internal.PolyResolution.computePolyType (PolyResolution.java:125)
20:14:27     at net.sourceforge.pmd.lang.java.types.ast.internal.LazyTypeResolver.handlePoly (LazyTypeResolver.java:358)
20:14:27     at net.sourceforge.pmd.lang.java.types.ast.internal.LazyTypeResolver.visit (LazyTypeResolver.java:363)
20:14:27     at net.sourceforge.pmd.lang.java.types.ast.internal.LazyTypeResolver.visit (LazyTypeResolver.java:103)
20:14:27     at net.sourceforge.pmd.lang.java.ast.ASTMethodCall.acceptVisitor (ASTMethodCall.java:71)
20:14:27     at net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode.getTypeMirror (AbstractJavaTypeNode.java:51)
20:14:27     at net.sourceforge.pmd.lang.java.ast.ASTMethodCall.getTypeMirror (ASTMethodCall.java:22)
20:14:27     at net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode.getTypeMirror (AbstractJavaTypeNode.java:39)
20:14:27     at net.sourceforge.pmd.lang.java.ast.ASTMethodCall.getTypeMirror (ASTMethodCall.java:22)
20:14:27     at net.sourceforge.pmd.lang.java.ast.InternalApiBridge.lambda$forceTypeResolutionPhase$0 (InternalApiBridge.java:92)
20:14:27     at java.util.Iterator.forEachRemaining (Iterator.java:133)
20:14:27     at net.sourceforge.pmd.lang.ast.internal.IteratorBasedNStream.forEach (IteratorBasedNStream.java:102)
20:14:27     at net.sourceforge.pmd.lang.java.ast.InternalApiBridge.forceTypeResolutionPhase (InternalApiBridge.java:90)
20:14:27     at net.sourceforge.pmd.lang.java.internal.JavaAstProcessor.lambda$process$3 (JavaAstProcessor.java:135)
20:14:27     at net.sourceforge.pmd.benchmark.TimeTracker.bench (TimeTracker.java:163)
20:14:27     at net.sourceforge.pmd.lang.java.internal.JavaAstProcessor.process (JavaAstProcessor.java:135)
20:14:27     at net.sourceforge.pmd.lang.java.internal.JavaAstProcessor.process (JavaAstProcessor.java:166)
20:14:27     at net.sourceforge.pmd.lang.java.internal.JavaAstProcessor.process (JavaAstProcessor.java:150)
20:14:27     at net.sourceforge.pmd.lang.java.ast.JavaParser.parseImpl (JavaParser.java:69)
20:14:27     at net.sourceforge.pmd.lang.java.ast.JavaParser.parseImpl (JavaParser.java:25)
20:14:27     at net.sourceforge.pmd.lang.ast.impl.javacc.JjtreeParserAdapter.parse (JjtreeParserAdapter.java:36)
20:14:27     at net.sourceforge.pmd.lang.impl.PmdRunnable.parse (PmdRunnable.java:112)
20:14:27     at net.sourceforge.pmd.lang.impl.PmdRunnable.processSource (PmdRunnable.java:132)
20:14:27     at net.sourceforge.pmd.lang.impl.PmdRunnable.run (PmdRunnable.java:80)
20:14:27     at java.util.concurrent.Executors$RunnableAdapter.call (Executors.java:572)
20:14:27     at java.util.concurrent.FutureTask.run (FutureTask.java:317)
20:14:27     at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1144)
20:14:27     at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:642)
20:14:27     at java.lang.Thread.run (Thread.java:1583)

Code Sample demonstrating the issue:

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Multimap;
import java.util.List;

public class PrebidTransformationRecorder {
    private static final int transformSize = PrebidRequestTransformation.values().length;

    private List<PrebidRequestTransformation> requestTransforms = ImmutableList.of();
    private Multimap<Integer, PrebidRequestTransformation> impTransforms = ImmutableMultimap.of();
}

enum PrebidRequestTransformation {
    US_PRIVACY_OBJECT(1),
    NATIVE_IMAGE_SIZES(2),
    VIDEO_PLAYER_SIZES(3)
}

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

Maven

oowekyala commented 1 week ago

This appears to be an issue parsing a classfile, so I need information about your dependencies. What version of Guava are you using?

When a field with not well known types are defined as non private field in an object, pmd is not able to analyse the class and breaks. While when the field is declared final, there is no such issue.

Can you clarify what you mean? What is a not well known type? The code sample you provided also has no non-private fields. Do you really mean final, or private? I don't see how adding a final modifier would change anything.

If you have time to put together a small repository with maven that shows the issue it would be a great help!

anuragagarwal561994 commented 1 week ago

Guava version used here is 33.2.1-jre

In the above if you see impTransforms field is non final but private. It can be re-declared in another method of the class.

Mostly this example should work, if not I will help you with setting up a repo.

When I say non well known type, I mean not List, Map etc. If I remove the field from the class it works, non well known is just an assumption because if I replace this with a map it works.