mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Java Type Migration: cleanup ThreadLocal conversion rule
GitOrigin-RevId: adb428623016a092b83a932ed66445fbd8400b27
This commit is contained in:
committed by
intellij-monorepo-bot
parent
6e02ada5b5
commit
63d0902a7f
+61
-71
@@ -92,18 +92,22 @@ public class ThreadLocalConversionRule extends TypeConversionRule {
|
||||
}
|
||||
else if (context instanceof PsiBinaryExpression binaryExpression) {
|
||||
final String sign = binaryExpression.getOperationSign().getText();
|
||||
return new TypeConversionDescriptor("$qualifier$" + sign + "$val$", toPrimitive("$qualifier$.get()", from, context) + " " + sign + " $val$");
|
||||
return new TypeConversionDescriptor("$qualifier$" + sign + "$val$",
|
||||
toPrimitive("$qualifier$.get()", from, context) + " " + sign + " $val$");
|
||||
}
|
||||
if (parent instanceof PsiExpressionStatement) {
|
||||
if (context instanceof PsiPostfixExpression postfixExpression) {
|
||||
final String sign = postfixExpression.getOperationSign().getText();
|
||||
|
||||
return new TypeConversionDescriptor("$qualifier$" + sign, "$qualifier$.set(" +
|
||||
getBoxedWrapper(from, to, toPrimitive("$qualifier$.get()", from, context) + " " + sign.charAt(0) + " 1",
|
||||
labeler, context, postfixExpression.getOperand().getText() +
|
||||
sign.charAt(0) +
|
||||
" 1") +
|
||||
")");
|
||||
return new TypeConversionDescriptor("$qualifier$" + sign,
|
||||
"$qualifier$.set(" +
|
||||
getBoxedWrapper(from,
|
||||
to,
|
||||
toPrimitive("$qualifier$.get()", from, context) + " " + sign.charAt(0) + " 1",
|
||||
labeler,
|
||||
context,
|
||||
postfixExpression.getOperand().getText() + sign.charAt(0) + " 1") +
|
||||
")");
|
||||
}
|
||||
else if (context instanceof PsiPrefixExpression prefixExpression) {
|
||||
final PsiJavaToken operationSign = ((PsiPrefixExpression)context).getOperationSign();
|
||||
@@ -112,12 +116,15 @@ public class ThreadLocalConversionRule extends TypeConversionRule {
|
||||
}
|
||||
final String sign = operationSign.getText();
|
||||
final PsiExpression operand = prefixExpression.getOperand();
|
||||
return new TypeConversionDescriptor(sign + "$qualifier$", "$qualifier$.set(" +
|
||||
getBoxedWrapper(from, to, toPrimitive("$qualifier$.get()", from, context) + " " + sign.charAt(0) + " 1",
|
||||
labeler, context, operand != null ? operand.getText() +
|
||||
sign.charAt(0) +
|
||||
" 1" : null) +
|
||||
")");
|
||||
return new TypeConversionDescriptor(sign + "$qualifier$",
|
||||
"$qualifier$.set(" +
|
||||
getBoxedWrapper(from,
|
||||
to,
|
||||
toPrimitive("$qualifier$.get()", from, context) + " " + sign.charAt(0) + " 1",
|
||||
labeler,
|
||||
context,
|
||||
operand != null ? operand.getText() + sign.charAt(0) + " 1" : null) +
|
||||
")");
|
||||
}
|
||||
else if (context instanceof PsiAssignmentExpression assignmentExpression) {
|
||||
final PsiJavaToken signToken = assignmentExpression.getOperationSign();
|
||||
@@ -131,23 +138,17 @@ public class ThreadLocalConversionRule extends TypeConversionRule {
|
||||
return wrapWithNewExpression(to, from, ((PsiAssignmentExpression)context).getRExpression());
|
||||
}
|
||||
}
|
||||
return new TypeConversionDescriptor("$qualifier$ = $val$", "$qualifier$.set(" +
|
||||
toBoxed("$val$", from, context) +
|
||||
")");
|
||||
return new TypeConversionDescriptor("$qualifier$ = $val$",
|
||||
"$qualifier$.set(" + toBoxed("$val$", from, context) + ")");
|
||||
}
|
||||
else {
|
||||
final PsiExpression rExpression = assignmentExpression.getRExpression();
|
||||
return new TypeConversionDescriptor("$qualifier$" + sign + "$val$", "$qualifier$.set(" +
|
||||
getBoxedWrapper(from, to, toPrimitive("$qualifier$.get()", from, context) +
|
||||
" " + sign.charAt(0) +
|
||||
" $val$", labeler, context,
|
||||
rExpression != null
|
||||
? lExpression
|
||||
.getText() +
|
||||
sign.charAt(0) +
|
||||
rExpression.getText()
|
||||
: null) +
|
||||
")");
|
||||
String boxedWrapper = getBoxedWrapper(from, to, toPrimitive("$qualifier$.get()", from, context) + " " + sign.charAt(0) + " $val$",
|
||||
labeler, context,
|
||||
rExpression != null
|
||||
? lExpression.getText() + sign.charAt(0) + rExpression.getText()
|
||||
: null);
|
||||
return new TypeConversionDescriptor("$qualifier$" + sign + "$val$", "$qualifier$.set(%s)".formatted(boxedWrapper));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -155,20 +156,16 @@ public class ThreadLocalConversionRule extends TypeConversionRule {
|
||||
}
|
||||
|
||||
private static TypeConversionDescriptor wrapWithNewExpression(PsiType to, PsiType from, PsiExpression initializer) {
|
||||
final String boxedTypeName = from instanceof PsiPrimitiveType ? ((PsiPrimitiveType)from).getBoxedTypeName() : from.getCanonicalText();
|
||||
List<PsiVariable> toMakeFinal = TypeConversionRuleUtil.getVariablesToMakeFinal(initializer);
|
||||
if (toMakeFinal == null) return null;
|
||||
return new WrappingWithInnerClassOrLambdaDescriptor("$qualifier$",
|
||||
createThreadLocalInitializerReplacement(to, from, initializer, boxedTypeName),
|
||||
createThreadLocalInitializerReplacement(to, from, initializer),
|
||||
initializer,
|
||||
toMakeFinal);
|
||||
}
|
||||
|
||||
private static @NonNls String createThreadLocalInitializerReplacement(PsiType to,
|
||||
PsiType from,
|
||||
PsiExpression initializer,
|
||||
String boxedTypeName) {
|
||||
if (PsiUtil.isLanguageLevel8OrHigher(initializer)) {
|
||||
private static @NonNls String createThreadLocalInitializerReplacement(PsiType to, PsiType from, PsiElement context) {
|
||||
if (PsiUtil.isLanguageLevel8OrHigher(context)) {
|
||||
if (from instanceof PsiPrimitiveType) {
|
||||
PsiType parameterType = ((PsiClassType)to).getParameters()[0];
|
||||
PsiPrimitiveType unboxed = PsiPrimitiveType.getUnboxedType(parameterType);
|
||||
@@ -178,48 +175,41 @@ public class ThreadLocalConversionRule extends TypeConversionRule {
|
||||
}
|
||||
return "java.lang.ThreadLocal.withInitial(() -> $qualifier$)";
|
||||
}
|
||||
return "new " +
|
||||
to.getCanonicalText() +
|
||||
"() {\n" +
|
||||
"@Override\n" +
|
||||
"protected " +
|
||||
boxedTypeName +
|
||||
" initialValue() {\n" +
|
||||
" return " +
|
||||
(PsiUtil.isLanguageLevel5OrHigher(initializer)
|
||||
? initializer.getText()
|
||||
: (from instanceof PsiPrimitiveType ? "new " +
|
||||
((PsiPrimitiveType)from).getBoxedTypeName() +
|
||||
"($qualifier$)" : "$qualifier$")) +
|
||||
";\n" +
|
||||
"}\n" +
|
||||
"}";
|
||||
final String boxedTypeName =
|
||||
from instanceof PsiPrimitiveType ? ((PsiPrimitiveType)from).getBoxedTypeName() : from.getCanonicalText();
|
||||
return ("""
|
||||
new %s() {
|
||||
@Override
|
||||
protected %s initialValue() {
|
||||
return %s;
|
||||
}
|
||||
}""").formatted(to.getCanonicalText(),
|
||||
boxedTypeName,
|
||||
from instanceof PsiPrimitiveType && !PsiUtil.isLanguageLevel5OrHigher(context)
|
||||
? "new " + ((PsiPrimitiveType)from).getBoxedTypeName() + "($qualifier$)"
|
||||
: "$qualifier$");
|
||||
}
|
||||
|
||||
private static @NonNls String toPrimitive(@NonNls String replaceByArg, PsiType from, PsiElement context) {
|
||||
return PsiUtil.isLanguageLevel5OrHigher(context)
|
||||
? replaceByArg
|
||||
: from instanceof PsiPrimitiveType ? "((" +
|
||||
((PsiPrimitiveType)from).getBoxedTypeName() +
|
||||
")" +
|
||||
replaceByArg +
|
||||
")." +
|
||||
from.getCanonicalText() +
|
||||
"Value()" : "((" + from.getCanonicalText() + ")" + replaceByArg + ")";
|
||||
if (PsiUtil.isLanguageLevel5OrHigher(context)) {
|
||||
return replaceByArg;
|
||||
}
|
||||
return from instanceof PsiPrimitiveType
|
||||
? "((" + ((PsiPrimitiveType)from).getBoxedTypeName() + ")" + replaceByArg + ")." + from.getCanonicalText() + "Value()"
|
||||
: "((" + from.getCanonicalText() + ")" + replaceByArg + ")";
|
||||
}
|
||||
|
||||
private static @NonNls String toBoxed(@NonNls String replaceByArg, PsiType from, PsiElement context) {
|
||||
return PsiUtil.isLanguageLevel5OrHigher(context)
|
||||
? replaceByArg
|
||||
: from instanceof PsiPrimitiveType ? "new " + ((PsiPrimitiveType)from).getBoxedTypeName() +
|
||||
"(" +
|
||||
replaceByArg +
|
||||
")"
|
||||
: replaceByArg;
|
||||
if (PsiUtil.isLanguageLevel5OrHigher(context)) {
|
||||
return replaceByArg;
|
||||
}
|
||||
return from instanceof PsiPrimitiveType
|
||||
? "new " + ((PsiPrimitiveType)from).getBoxedTypeName() + "(" + replaceByArg + ")"
|
||||
: replaceByArg;
|
||||
}
|
||||
|
||||
private static @NonNls String getBoxedWrapper(final PsiType from,
|
||||
final PsiType to,
|
||||
private static @NonNls String getBoxedWrapper(PsiType from,
|
||||
PsiType to,
|
||||
@NotNull @NonNls String arg,
|
||||
TypeMigrationLabeler labeler,
|
||||
PsiElement context,
|
||||
@@ -250,9 +240,9 @@ public class ThreadLocalConversionRule extends TypeConversionRule {
|
||||
private static final class WrappingWithInnerClassOrLambdaDescriptor extends ArrayInitializerAwareConversionDescriptor {
|
||||
private final List<? extends PsiVariable> myVariablesToMakeFinal;
|
||||
|
||||
private WrappingWithInnerClassOrLambdaDescriptor(@NonNls final String stringToReplace,
|
||||
@NonNls final String replaceByString,
|
||||
final PsiExpression expression,
|
||||
private WrappingWithInnerClassOrLambdaDescriptor(@NonNls String stringToReplace,
|
||||
@NonNls String replaceByString,
|
||||
PsiExpression expression,
|
||||
@NotNull List<? extends PsiVariable> toMakeFinal) {
|
||||
super(stringToReplace, replaceByString, expression);
|
||||
myVariablesToMakeFinal = toMakeFinal;
|
||||
|
||||
+4
-4
@@ -5,10 +5,10 @@ PsiReferenceExpression:myS : java.lang.ThreadLocal<java.lang.String>
|
||||
|
||||
Conversions:
|
||||
"" -> new java.lang.ThreadLocal<java.lang.String>() {
|
||||
@Override
|
||||
protected java.lang.String initialValue() {
|
||||
return "";
|
||||
}
|
||||
@Override
|
||||
protected java.lang.String initialValue() {
|
||||
return $qualifier$;
|
||||
}
|
||||
} $qualifier$ ""
|
||||
myS -> $qualifier$.get() $qualifier$ myS
|
||||
myS -> $qualifier$.get() $qualifier$ myS
|
||||
|
||||
Reference in New Issue
Block a user