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] CognitiveComplexity: Exception when using Map.of() #5084

Closed
AB-xdev opened this issue Jun 24, 2024 · 0 comments · Fixed by #5179
Closed

[java] CognitiveComplexity: Exception when using Map.of() #5084

AB-xdev opened this issue Jun 24, 2024 · 0 comments · Fixed by #5179
Assignees
Labels
a:bug PMD crashes or fails to analyse a file. in:type-resolution Affects the type resolution code
Milestone

Comments

@AB-xdev
Copy link

AB-xdev commented Jun 24, 2024

Affects PMD Version:

7.2.0

Description:

Crash while executing rule CognitiveComplexity on constructor with Map.of().
The created Map has the following type: Map<Class, Serializer> (Serializer is a interface)

Exception Stacktrace:

[INFO] --- pmd:3.23.0:check (default-cli) @ root ---
[INFO] PMD version: 7.2.0
Warning:  Exception applying rule CognitiveComplexity on file /home/runner/work/mockserver-neolight/mockserver-neolight/core/src/main/java/software/xdev/mockserver/serialization/WebSocketMessageSerializer.java, continuing with next rule
org.apache.commons.lang3.exception.ContextedRuntimeException: java.lang.IllegalArgumentException: Empty set
Exception Context:
	[1:Resolving type of=!debug only! [MethodCall:40:22]Map.of(
			HttpRequest.class, new HttpRequestSerializer(),
			HttpResponse.class, new HttpResponseSerializer(),
			HttpRequestAndHttpRespon(truncated)]
	[2:Rule applied on node=!debug only! [ConstructorDeclaration:38:9]public WebSocketMessageSerializer()
	{
		this.serializers = Map.of(
			HttpRequest.class, new HttpRequestSerializer(),
			HttpResponse.clas(truncated)]
---------------------------------
    at net.sourceforge.pmd.util.AssertionUtil.contexted (AssertionUtil.java:236)
    at net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode.getTypeMirror (AbstractJavaTypeNode.java:54)
    at net.sourceforge.pmd.lang.java.ast.ASTMethodCall.getTypeMirror (ASTMethodCall.java:22)
    at net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode.getTypeMirror (AbstractJavaTypeNode.java:39)
    at net.sourceforge.pmd.lang.java.ast.ASTMethodCall.getTypeMirror (ASTMethodCall.java:22)
    at net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode.forceTypeResolution (AbstractJavaTypeNode.java:29)
    at net.sourceforge.pmd.lang.java.ast.AbstractInvocationExpr.getOverloadSelectionInfo (AbstractInvocationExpr.java:27)
    at net.sourceforge.pmd.lang.java.ast.ASTMethodCall.getOverloadSelectionInfo (ASTMethodCall.java:22)
    at net.sourceforge.pmd.lang.java.metrics.internal.CognitiveComplexityVisitor.visit (CognitiveComplexityVisitor.java:245)
    at net.sourceforge.pmd.lang.java.metrics.internal.CognitiveComplexityVisitor.visit (CognitiveComplexityVisitor.java:41)
    at net.sourceforge.pmd.lang.java.ast.ASTMethodCall.acceptVisitor (ASTMethodCall.java:71)
    at net.sourceforge.pmd.lang.java.ast.AbstractJavaNode.acceptVisitor (AbstractJavaNode.java:38)
    at net.sourceforge.pmd.lang.ast.AstVisitorBase.visitChildren (AstVisitorBase.java:31)
    at net.sourceforge.pmd.lang.ast.AstVisitorBase.visitNode (AstVisitorBase.java:38)
    at net.sourceforge.pmd.lang.java.ast.JavaVisitor.visitJavaNode (JavaVisitor.java:6)
    at net.sourceforge.pmd.lang.java.ast.JavaVisitorBase.visitExpression (JavaVisitorBase.java:132)
    at net.sourceforge.pmd.lang.java.ast.JavaVisitorBase.visit (JavaVisitorBase.java:142)
    at net.sourceforge.pmd.lang.java.ast.ASTAssignmentExpression.acceptVisitor (ASTAssignmentExpression.java:56)
    at net.sourceforge.pmd.lang.java.ast.AbstractJavaNode.acceptVisitor (AbstractJavaNode.java:38)
    at net.sourceforge.pmd.lang.ast.AstVisitorBase.visitChildren (AstVisitorBase.java:31)
    at net.sourceforge.pmd.lang.ast.AstVisitorBase.visitNode (AstVisitorBase.java:38)
    at net.sourceforge.pmd.lang.java.ast.JavaVisitor.visitJavaNode (JavaVisitor.java:6)
    at net.sourceforge.pmd.lang.java.ast.JavaVisitorBase.visitStatement (JavaVisitorBase.java:281)
    at net.sourceforge.pmd.lang.java.ast.JavaVisitorBase.visit (JavaVisitorBase.java:321)
    at net.sourceforge.pmd.lang.java.ast.ASTExpressionStatement.acceptVisitor (ASTExpressionStatement.java:27)
    at net.sourceforge.pmd.lang.java.ast.AbstractJavaNode.acceptVisitor (AbstractJavaNode.java:38)
    at net.sourceforge.pmd.lang.java.metrics.internal.CognitiveComplexityVisitor.visit (CognitiveComplexityVisitor.java:226)
    at net.sourceforge.pmd.lang.java.metrics.internal.CognitiveComplexityVisitor.visit (CognitiveComplexityVisitor.java:41)
    at net.sourceforge.pmd.lang.java.ast.ASTBlock.acceptVisitor (ASTBlock.java:30)
    at net.sourceforge.pmd.lang.java.ast.AbstractJavaNode.acceptVisitor (AbstractJavaNode.java:38)
    at net.sourceforge.pmd.lang.ast.AstVisitorBase.visitChildren (AstVisitorBase.java:31)
    at net.sourceforge.pmd.lang.ast.AstVisitorBase.visitNode (AstVisitorBase.java:38)
    at net.sourceforge.pmd.lang.java.ast.JavaVisitor.visitJavaNode (JavaVisitor.java:6)
    at net.sourceforge.pmd.lang.java.ast.JavaVisitorBase.visitMethodOrCtor (JavaVisitorBase.java:33)
    at net.sourceforge.pmd.lang.java.ast.JavaVisitorBase.visit (JavaVisitorBase.java:43)
    at net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration.acceptVisitor (ASTConstructorDeclaration.java:45)
    at net.sourceforge.pmd.lang.java.ast.AbstractJavaNode.acceptVisitor (AbstractJavaNode.java:38)
    at net.sourceforge.pmd.lang.java.metrics.JavaMetrics.computeCognitive (JavaMetrics.java:469)
    at net.sourceforge.pmd.lang.metrics.Metric$1.computeFor (Metric.java:154)
    at net.sourceforge.pmd.lang.metrics.MetricsUtil.computeMetric (MetricsUtil.java:137)
    at net.sourceforge.pmd.lang.metrics.MetricsUtil.computeMetric (MetricsUtil.java:102)
    at net.sourceforge.pmd.lang.metrics.MetricsUtil.computeMetric (MetricsUtil.java:83)
    at net.sourceforge.pmd.lang.java.rule.design.CognitiveComplexityRule.visitMethod (CognitiveComplexityRule.java:56)
    at net.sourceforge.pmd.lang.java.rule.design.CognitiveComplexityRule.visit (CognitiveComplexityRule.java:48)
    at net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration.acceptVisitor (ASTConstructorDeclaration.java:45)
    at net.sourceforge.pmd.lang.java.ast.AbstractJavaNode.acceptVisitor (AbstractJavaNode.java:38)
    at net.sourceforge.pmd.lang.java.rule.AbstractJavaRule.apply (AbstractJavaRule.java:30)
    at net.sourceforge.pmd.lang.rule.RuleReference.apply (RuleReference.java:415)
    at net.sourceforge.pmd.lang.rule.internal.RuleApplicator.applyOnIndex (RuleApplicator.java:77)
    at net.sourceforge.pmd.lang.rule.internal.RuleApplicator.apply (RuleApplicator.java:57)
    at net.sourceforge.pmd.lang.rule.internal.RuleSets.apply (RuleSets.java:155)
    at net.sourceforge.pmd.lang.impl.PmdRunnable.processSource (PmdRunnable.java:140)
    at net.sourceforge.pmd.lang.impl.PmdRunnable.run (PmdRunnable.java:80)
    at java.util.concurrent.Executors$RunnableAdapter.call (Executors.java:539)
    at java.util.concurrent.FutureTask.run (FutureTask.java:264)
    at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1136)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:635)
    at java.lang.Thread.run (Thread.java:840)
Caused by: java.lang.IllegalArgumentException: Empty set
    at net.sourceforge.pmd.lang.java.types.Lub$LubJudge.lcp (Lub.java:177)
    at net.sourceforge.pmd.lang.java.types.Lub$LubJudge.lambda$lub$0 (Lub.java:159)
    at net.sourceforge.pmd.util.CollectionUtil.map (CollectionUtil.java:426)
    at net.sourceforge.pmd.util.CollectionUtil.map (CollectionUtil.java:386)
    at net.sourceforge.pmd.lang.java.types.Lub$LubJudge.lub (Lub.java:156)
    at net.sourceforge.pmd.lang.java.types.Lub$LubJudge.lub (Lub.java:112)
    at net.sourceforge.pmd.lang.java.types.Lub$LubJudge.computeLcta (Lub.java:259)
    at net.sourceforge.pmd.lang.java.types.Lub$LubJudge.lcta (Lub.java:219)
    at net.sourceforge.pmd.lang.java.types.Lub$LubJudge.lcp (Lub.java:203)
    at net.sourceforge.pmd.lang.java.types.Lub$LubJudge.lcp (Lub.java:185)
    at net.sourceforge.pmd.lang.java.types.Lub$LubJudge.lambda$lub$0 (Lub.java:159)
    at net.sourceforge.pmd.util.CollectionUtil.map (CollectionUtil.java:430)
    at net.sourceforge.pmd.util.CollectionUtil.map (CollectionUtil.java:386)
    at net.sourceforge.pmd.lang.java.types.Lub$LubJudge.lub (Lub.java:156)
    at net.sourceforge.pmd.lang.java.types.Lub$LubJudge.lub (Lub.java:112)
    at net.sourceforge.pmd.lang.java.types.Lub$LubJudge.computeLcta (Lub.java:259)
    at net.sourceforge.pmd.lang.java.types.Lub$LubJudge.lcta (Lub.java:219)
    at net.sourceforge.pmd.lang.java.types.Lub$LubJudge.lcp (Lub.java:203)
    at net.sourceforge.pmd.lang.java.types.Lub$LubJudge.lcp (Lub.java:185)
    at net.sourceforge.pmd.lang.java.types.Lub$LubJudge.lambda$lub$0 (Lub.java:159)
    at net.sourceforge.pmd.util.CollectionUtil.map (CollectionUtil.java:426)
    at net.sourceforge.pmd.util.CollectionUtil.map (CollectionUtil.java:386)
    at net.sourceforge.pmd.lang.java.types.Lub$LubJudge.lub (Lub.java:156)
    at net.sourceforge.pmd.lang.java.types.Lub.lub (Lub.java:36)
    at net.sourceforge.pmd.lang.java.types.TypeSystem.lub (TypeSystem.java:680)
    at net.sourceforge.pmd.lang.java.types.internal.infer.ReductionStep$2.solve (ReductionStep.java:41)
    at net.sourceforge.pmd.lang.java.types.internal.infer.InferenceContext.solveBatchProgressed (InferenceContext.java:479)
    at net.sourceforge.pmd.lang.java.types.internal.infer.InferenceContext.solve (InferenceContext.java:459)
    at net.sourceforge.pmd.lang.java.types.internal.infer.InferenceContext.solve (InferenceContext.java:435)
    at net.sourceforge.pmd.lang.java.types.internal.infer.Infer.instantiateImpl (Infer.java:580)
    at net.sourceforge.pmd.lang.java.types.internal.infer.Infer.instantiateMaybeNoInfer (Infer.java:545)
    at net.sourceforge.pmd.lang.java.types.internal.infer.Infer.instantiateMethod (Infer.java:389)
    at net.sourceforge.pmd.lang.java.types.internal.infer.Infer.instantiateMethodOrCtor (Infer.java:369)
    at net.sourceforge.pmd.lang.java.types.internal.infer.Infer.logInference (Infer.java:352)
    at net.sourceforge.pmd.lang.java.types.internal.infer.Infer.computeCompileTimeDecl (Infer.java:292)
    at net.sourceforge.pmd.lang.java.types.internal.infer.Infer.getCompileTimeDecl (Infer.java:254)
    at net.sourceforge.pmd.lang.java.types.internal.infer.Infer.goToInvocationWithFallback (Infer.java:187)
    at net.sourceforge.pmd.lang.java.types.internal.infer.Infer.inferInvocationRecursively (Infer.java:176)
    at net.sourceforge.pmd.lang.java.types.ast.internal.PolyResolution.inferInvocation (PolyResolution.java:263)
    at net.sourceforge.pmd.lang.java.types.ast.internal.PolyResolution.polyTypeOtherCtx (PolyResolution.java:135)
    at net.sourceforge.pmd.lang.java.types.ast.internal.PolyResolution.computePolyType (PolyResolution.java:125)
    at net.sourceforge.pmd.lang.java.types.ast.internal.LazyTypeResolver.handlePoly (LazyTypeResolver.java:267)
    at net.sourceforge.pmd.lang.java.types.ast.internal.LazyTypeResolver.visit (LazyTypeResolver.java:272)
    at net.sourceforge.pmd.lang.java.types.ast.internal.LazyTypeResolver.visit (LazyTypeResolver.java:95)
    at net.sourceforge.pmd.lang.java.ast.ASTMethodCall.acceptVisitor (ASTMethodCall.java:71)
    at net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode.getTypeMirror (AbstractJavaTypeNode.java:51)
    at net.sourceforge.pmd.lang.java.ast.ASTMethodCall.getTypeMirror (ASTMethodCall.java:22)
    at net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode.getTypeMirror (AbstractJavaTypeNode.java:39)
    at net.sourceforge.pmd.lang.java.ast.ASTMethodCall.getTypeMirror (ASTMethodCall.java:22)
    at net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode.forceTypeResolution (AbstractJavaTypeNode.java:29)
    at net.sourceforge.pmd.lang.java.ast.AbstractInvocationExpr.getOverloadSelectionInfo (AbstractInvocationExpr.java:27)
    at net.sourceforge.pmd.lang.java.ast.ASTMethodCall.getOverloadSelectionInfo (ASTMethodCall.java:22)
    at net.sourceforge.pmd.lang.java.metrics.internal.CognitiveComplexityVisitor.visit (CognitiveComplexityVisitor.java:245)
    at net.sourceforge.pmd.lang.java.metrics.internal.CognitiveComplexityVisitor.visit (CognitiveComplexityVisitor.java:41)
    at net.sourceforge.pmd.lang.java.ast.ASTMethodCall.acceptVisitor (ASTMethodCall.java:71)
    at net.sourceforge.pmd.lang.java.ast.AbstractJavaNode.acceptVisitor (AbstractJavaNode.java:38)
    at net.sourceforge.pmd.lang.ast.AstVisitorBase.visitChildren (AstVisitorBase.java:31)
    at net.sourceforge.pmd.lang.ast.AstVisitorBase.visitNode (AstVisitorBase.java:38)
    at net.sourceforge.pmd.lang.java.ast.JavaVisitor.visitJavaNode (JavaVisitor.java:6)
    at net.sourceforge.pmd.lang.java.ast.JavaVisitorBase.visitExpression (JavaVisitorBase.java:132)
    at net.sourceforge.pmd.lang.java.ast.JavaVisitorBase.visit (JavaVisitorBase.java:142)
    at net.sourceforge.pmd.lang.java.ast.ASTAssignmentExpression.acceptVisitor (ASTAssignmentExpression.java:56)
    at net.sourceforge.pmd.lang.java.ast.AbstractJavaNode.acceptVisitor (AbstractJavaNode.java:38)
    at net.sourceforge.pmd.lang.ast.AstVisitorBase.visitChildren (AstVisitorBase.java:31)
    at net.sourceforge.pmd.lang.ast.AstVisitorBase.visitNode (AstVisitorBase.java:38)
    at net.sourceforge.pmd.lang.java.ast.JavaVisitor.visitJavaNode (JavaVisitor.java:6)
    at net.sourceforge.pmd.lang.java.ast.JavaVisitorBase.visitStatement (JavaVisitorBase.java:281)
    at net.sourceforge.pmd.lang.java.ast.JavaVisitorBase.visit (JavaVisitorBase.java:321)
    at net.sourceforge.pmd.lang.java.ast.ASTExpressionStatement.acceptVisitor (ASTExpressionStatement.java:27)
    at net.sourceforge.pmd.lang.java.ast.AbstractJavaNode.acceptVisitor (AbstractJavaNode.java:38)
    at net.sourceforge.pmd.lang.java.metrics.internal.CognitiveComplexityVisitor.visit (CognitiveComplexityVisitor.java:226)
    at net.sourceforge.pmd.lang.java.metrics.internal.CognitiveComplexityVisitor.visit (CognitiveComplexityVisitor.java:41)
    at net.sourceforge.pmd.lang.java.ast.ASTBlock.acceptVisitor (ASTBlock.java:30)
    at net.sourceforge.pmd.lang.java.ast.AbstractJavaNode.acceptVisitor (AbstractJavaNode.java:38)
    at net.sourceforge.pmd.lang.ast.AstVisitorBase.visitChildren (AstVisitorBase.java:31)
    at net.sourceforge.pmd.lang.ast.AstVisitorBase.visitNode (AstVisitorBase.java:38)
    at net.sourceforge.pmd.lang.java.ast.JavaVisitor.visitJavaNode (JavaVisitor.java:6)
    at net.sourceforge.pmd.lang.java.ast.JavaVisitorBase.visitMethodOrCtor (JavaVisitorBase.java:33)
    at net.sourceforge.pmd.lang.java.ast.JavaVisitorBase.visit (JavaVisitorBase.java:43)
    at net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration.acceptVisitor (ASTConstructorDeclaration.java:45)
    at net.sourceforge.pmd.lang.java.ast.AbstractJavaNode.acceptVisitor (AbstractJavaNode.java:38)
    at net.sourceforge.pmd.lang.java.metrics.JavaMetrics.computeCognitive (JavaMetrics.java:469)
    at net.sourceforge.pmd.lang.metrics.Metric$1.computeFor (Metric.java:154)
    at net.sourceforge.pmd.lang.metrics.MetricsUtil.computeMetric (MetricsUtil.java:137)
    at net.sourceforge.pmd.lang.metrics.MetricsUtil.computeMetric (MetricsUtil.java:102)
    at net.sourceforge.pmd.lang.metrics.MetricsUtil.computeMetric (MetricsUtil.java:83)
    at net.sourceforge.pmd.lang.java.rule.design.CognitiveComplexityRule.visitMethod (CognitiveComplexityRule.java:56)
    at net.sourceforge.pmd.lang.java.rule.design.CognitiveComplexityRule.visit (CognitiveComplexityRule.java:48)
    at net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration.acceptVisitor (ASTConstructorDeclaration.java:45)
    at net.sourceforge.pmd.lang.java.ast.AbstractJavaNode.acceptVisitor (AbstractJavaNode.java:38)
    at net.sourceforge.pmd.lang.java.rule.AbstractJavaRule.apply (AbstractJavaRule.java:30)
    at net.sourceforge.pmd.lang.rule.RuleReference.apply (RuleReference.java:415)
    at net.sourceforge.pmd.lang.rule.internal.RuleApplicator.applyOnIndex (RuleApplicator.java:77)
    at net.sourceforge.pmd.lang.rule.internal.RuleApplicator.apply (RuleApplicator.java:57)
    at net.sourceforge.pmd.lang.rule.internal.RuleSets.apply (RuleSets.java:155)
    at net.sourceforge.pmd.lang.impl.PmdRunnable.processSource (PmdRunnable.java:140)
    at net.sourceforge.pmd.lang.impl.PmdRunnable.run (PmdRunnable.java:80)
    at java.util.concurrent.Executors$RunnableAdapter.call (Executors.java:539)
    at java.util.concurrent.FutureTask.run (FutureTask.java:264)
    at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1136)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:635)
    at java.lang.Thread.run (Thread.java:840)
Warning:  Exception occurred on node !debug only! [ConstructorDeclaration:38:9]public WebSocketMessageSerializer()
	{
		this.serializers = Map.of(
			HttpRequest.class, new HttpRequestSerializer(),
			HttpResponse.clas(truncated)
Warning:  There are 1 PMD processing errors:
Warning:  /home/runner/work/mockserver-neolight/mockserver-neolight/core/src/main/java/software/xdev/mockserver/serialization/WebSocketMessageSerializer.java: ContextedRuntimeException: java.lang.IllegalArgumentException: Empty set
Exception Context:
	[1:Resolving type of=!debug only! [MethodCall:40:22]Map.of(
			HttpRequest.class, new HttpRequestSerializer(),
			HttpResponse.class, new HttpResponseSerializer(),
			HttpRequestAndHttpRespon(truncated)]
	[2:Rule applied on node=!debug only! [ConstructorDeclaration:38:9]public WebSocketMessageSerializer()
	{
		this.serializers = Map.of(
			HttpRequest.class, new HttpRequestSerializer(),
			HttpResponse.clas(truncated)]
---------------------------------

Code Sample demonstrating the issue:

https://github.com/xdev-software/mockserver-neolight/blob/fe69619264fcc28c69c82ee06335f0695f274c1e/core/src/main/java/software/xdev/mockserver/serialization/WebSocketMessageSerializer.java#L36-L45

	private final Map<Class, Serializer> serializers;
	
	public WebSocketMessageSerializer()
	{
		this.serializers = Map.of(
			HttpRequest.class, new HttpRequestSerializer(),
			HttpResponse.class, new HttpResponseSerializer(),
			HttpRequestAndHttpResponse.class, new HttpRequestAndHttpResponseSerializer()
		);
	}

Steps to reproduce:

Please provide detailed steps for how we can reproduce the bug.

  1. Checkout our project: https://github.com/xdev-software/mockserver-neolight
  2. Run ./mvnw compile pmd:aggregate-pmd-no-fork -P pmd

Running PMD through: [Maven]

@AB-xdev AB-xdev added the a:bug PMD crashes or fails to analyse a file. label Jun 24, 2024
@jsotuyod jsotuyod added the in:type-resolution Affects the type resolution code label Aug 18, 2024
@oowekyala oowekyala self-assigned this Aug 25, 2024
@adangel adangel changed the title [Java] Crash while executing rule CognitiveComplexity on constructor with Map.of() [java] CognitiveComplexity: Exception when using Map.of() Aug 29, 2024
@adangel adangel added this to the 7.5.0 milestone Aug 29, 2024
adangel added a commit that referenced this issue Aug 29, 2024
Merge pull request #5179 from oowekyala:issue5084-lub-crash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:bug PMD crashes or fails to analyse a file. in:type-resolution Affects the type resolution code
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants