Skip to content

Commit

Permalink
SONARJAVA-5265 S6906 ClassCastException: IdentifierTreeImpl cannot be…
Browse files Browse the repository at this point in the history
… cast to MemberSelectExpressionTree (#4977)
  • Loading branch information
alban-auzeill authored Jan 10, 2025
1 parent 9a6a82a commit ba8cbb0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.RunnableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class VirtualThreadNotSynchronizedCheckSample {

Expand Down Expand Up @@ -158,4 +161,14 @@ void increaseCoverageMethodHasNoBlock(Fooable fooable) {
interface Fooable {
void foo();
}

public static abstract class DefaultThreadPoolExecutor extends ThreadPoolExecutor {
protected DefaultThreadPoolExecutor() {
super(2,4,20,TimeUnit.SECONDS, null);
}
void submit(RunnableFuture<Void> runnableFuture) {
execute( runnableFuture ); // Compliant, custom ThreadPoolExecutor
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,15 @@ public void visitMethodInvocation(MethodInvocationTree tree) {
}

private static boolean isRunnableInVirtualThread(MethodInvocationTree tree) {
return switch (tree.methodSymbol().name()) {
case "start", "unstarted" -> isCallToOfVirtual(tree);
case "execute", "submit" -> isCallToExecutorServiceWithVirtualTasks(tree);
default -> true;
};
return tree.methodSelect() instanceof MemberSelectExpressionTree methodSelect &&
switch (tree.methodSymbol().name()) {
case "start", "unstarted" -> isCallToOfVirtual(methodSelect.expression());
case "execute", "submit" -> isCallToExecutorServiceWithVirtualTasks(methodSelect.expression());
default -> true;
};
}

private static boolean isCallToOfVirtual(MethodInvocationTree tree) {
var callSiteExpression = ((MemberSelectExpressionTree) tree.methodSelect()).expression();
private static boolean isCallToOfVirtual(ExpressionTree callSiteExpression) {
if (callSiteExpression.symbolType().is(OF_VIRTUAL)) {
return true;
}
Expand All @@ -126,8 +126,7 @@ private static boolean isCallToOfVirtual(MethodInvocationTree tree) {
.anyMatch(it -> it.symbolType().is(OF_VIRTUAL));
}

private static boolean isCallToExecutorServiceWithVirtualTasks(MethodInvocationTree tree) {
var callSiteExpression = ((MemberSelectExpressionTree) tree.methodSelect()).expression();
private static boolean isCallToExecutorServiceWithVirtualTasks(ExpressionTree callSiteExpression) {
if (isCallToExecutorServiceBuilderWithVirtualTasks(callSiteExpression)) {
return true;
}
Expand Down

0 comments on commit ba8cbb0

Please sign in to comment.