-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add processor fixing sonar rule S4065_ThreadLocalWithInitial (#706
) * Add processor fixing sonar rule S4065_ThreadLocalWithInitial * apply requested changes * fix formatting * apply changes * rename files and remove check * fix method name
- Loading branch information
1 parent
eaf0d03
commit c11c1bb
Showing
7 changed files
with
126 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/main/java/sorald/processor/ThreadLocalWithInitial.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package sorald.processor; | ||
|
||
import java.util.List; | ||
import java.util.function.Supplier; | ||
import sorald.annotations.ProcessorAnnotation; | ||
import spoon.reflect.code.CtExpression; | ||
import spoon.reflect.code.CtInvocation; | ||
import spoon.reflect.code.CtLambda; | ||
import spoon.reflect.code.CtNewClass; | ||
import spoon.reflect.code.CtReturn; | ||
import spoon.reflect.code.CtStatement; | ||
import spoon.reflect.code.CtTypeAccess; | ||
import spoon.reflect.declaration.CtClass; | ||
import spoon.reflect.reference.CtExecutableReference; | ||
import spoon.reflect.reference.CtTypeReference; | ||
|
||
@ProcessorAnnotation(key = "S4065", description = "\"ThreadLocal.withInitial\" should be preferred") | ||
public class ThreadLocalWithInitial extends SoraldAbstractProcessor<CtNewClass<?>> { | ||
|
||
@Override | ||
protected void repairInternal(CtNewClass<?> newClass) { | ||
CtClass<?> innerClass = newClass.getAnonymousClass(); | ||
CtExecutableReference<?> initialValueMethod = findInitialValueMethod(innerClass); | ||
CtLambda<?> lambda = createSupplier(initialValueMethod); | ||
CtInvocation<?> invocation = createInitialMethod(newClass, lambda); | ||
invocation.setArguments(List.of(lambda)); | ||
newClass.replace(invocation); | ||
} | ||
|
||
private CtInvocation<?> createInitialMethod(CtNewClass<?> threadLocal, CtLambda<?> lambda) { | ||
CtTypeAccess<Object> target = getFactory().createTypeAccess(createThreadLocalRef()); | ||
CtExecutableReference<?> executableReference = | ||
getFactory() | ||
.Executable() | ||
.createReference( | ||
threadLocal.getType(), | ||
true, | ||
threadLocal.getType(), | ||
"withInitial", | ||
List.of(lambda.getType())); | ||
return getFactory().createInvocation(target, executableReference); | ||
} | ||
|
||
private CtTypeReference<Object> createThreadLocalRef() { | ||
return getFactory().createCtTypeReference(ThreadLocal.class); | ||
} | ||
|
||
private CtLambda<?> createSupplier(CtExecutableReference<?> initialValueMethod) { | ||
CtLambda<?> lambda = getFactory().createLambda(); | ||
if (initialValueMethod.getDeclaration().getBody().getStatements().size() == 1) { | ||
lambda.setExpression( | ||
getReturnStatement( | ||
initialValueMethod.getDeclaration().getBody().getStatement(0))); | ||
} else { | ||
lambda.setBody(initialValueMethod.getDeclaration().getBody()); | ||
} | ||
lambda.setType(getFactory().createCtTypeReference(Supplier.class)); | ||
return lambda; | ||
} | ||
|
||
private <T> CtExpression<T> getReturnStatement(CtStatement statement) { | ||
return ((CtReturn<T>) statement).getReturnedExpression(); | ||
} | ||
|
||
private CtExecutableReference<?> findInitialValueMethod(CtClass<?> innerClass) { | ||
return innerClass.getDeclaredExecutables().stream() | ||
.filter(v -> v.getSimpleName().equals("initialValue")) | ||
.findFirst() | ||
.get(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
Sorald fixes the violations of this rule by replacing an anonymous `ThreadLocal` class overriding `initalValue` with an invocation of `ThreadLocal.withInitial(Supplier)`. | ||
Example: | ||
```diff | ||
- ThreadLocal<String> myThreadLocal = new ThreadLocal<String>() { | ||
- @Override | ||
- protected String initialValue() { | ||
- return "Hello"; | ||
- } | ||
+ Threadlocal<String> myThreadLocal = ThreadLocal.withInitial(() -> "Hello"); | ||
``` |
13 changes: 13 additions & 0 deletions
13
...t_files/S4065_ThreadLocalWithInitial/ThreadLocalInitialWithExactlyOneReturnStatement.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
|
||
public class ThreadLocalInitialWithExactlyOneReturnStatement { | ||
|
||
public void bar() { | ||
ThreadLocal<String> threadLocal = new ThreadLocal<String>() { // Noncompliant | ||
@Override | ||
protected String initialValue() { | ||
return "hello"; | ||
} | ||
}; | ||
threadLocal.set("42"); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...4065_ThreadLocalWithInitial/ThreadLocalInitialWithExactlyOneReturnStatement.java.expected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
|
||
public class ThreadLocalInitialWithExactlyOneReturnStatement { | ||
|
||
public void bar() { | ||
ThreadLocal<String> threadLocal = ThreadLocal.withInitial(() -> "hello"); | ||
threadLocal.set("42"); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...sor_test_files/S4065_ThreadLocalWithInitial/ThreadLocalInitialWithMultipleStatements.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
|
||
public class ThreadLocalInitialWithMultipleStatements { | ||
|
||
public void bar() { | ||
ThreadLocal<String> threadLocal = new ThreadLocal<String>() { // Noncompliant | ||
@Override | ||
protected String initialValue() { | ||
String s = "hello"; | ||
return s+"42"; | ||
} | ||
}; | ||
threadLocal.set("42"); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...files/S4065_ThreadLocalWithInitial/ThreadLocalInitialWithMultipleStatements.java.expected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
|
||
public class ThreadLocalInitialWithMultipleStatements { | ||
|
||
public void bar() { | ||
ThreadLocal<String> threadLocal = ThreadLocal.withInitial(() -> {String s = "hello"; return s+"42";}); | ||
threadLocal.set("42"); | ||
} | ||
} |