Java Type Migration: add cast if necessary when converting to ThreadLocal (IDEA-248610)

GitOrigin-RevId: 15b83a4b1281356a007783e13d8a50e44870e6cf
This commit is contained in:
Bas Leijdekkers
2023-03-07 09:05:49 +01:00
committed by intellij-monorepo-bot
parent 18c1ad1b54
commit 93a9db98dc
3 changed files with 15 additions and 0 deletions

View File

@@ -169,6 +169,13 @@ public class ThreadLocalConversionRule extends TypeConversionRule {
PsiExpression initializer,
String boxedTypeName) {
if (PsiUtil.isLanguageLevel8OrHigher(initializer)) {
if (from instanceof PsiPrimitiveType) {
PsiType parameterType = ((PsiClassType)to).getParameters()[0];
PsiPrimitiveType unboxed = PsiPrimitiveType.getUnboxedType(parameterType);
if (unboxed != null && !from.equals(unboxed)) {
return "java.lang.ThreadLocal.withInitial(() -> (" + unboxed.getCanonicalText() + ")$qualifier$)";
}
}
return "java.lang.ThreadLocal.withInitial(() -> $qualifier$)";
}
return "new " +

View File

@@ -0,0 +1,4 @@
// "Convert to 'ThreadLocal'" "true"
class T {
private static final ThreadLocal<Long> l = ThreadLocal.withInitial(() -> (long) 1); // choose "Convert to ThreadLocal" intention
}

View File

@@ -0,0 +1,4 @@
// "Convert to 'ThreadLocal'" "true"
class T {
private static final long <caret>l = 1; // choose "Convert to ThreadLocal" intention
}