mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
AtomicConversionRule: also support non-void context for updateAndGet (IDEA-CR-24025)
This commit is contained in:
+25
-29
@@ -209,36 +209,32 @@ public class AtomicConversionRule extends TypeConversionRule {
|
||||
boolean voidContext = parent instanceof PsiExpressionStatement ||
|
||||
(parent instanceof PsiLambdaExpression && PsiType.VOID.equals(LambdaUtil.getFunctionalInterfaceReturnType(
|
||||
(PsiFunctionalExpression)parent)));
|
||||
if (operationSign == JavaTokenType.EQ) {
|
||||
if (!voidContext) return null;
|
||||
final PsiExpression lExpression = ((PsiAssignmentExpression)context).getLExpression();
|
||||
if (lExpression instanceof PsiReferenceExpression) {
|
||||
final PsiElement element = ((PsiReferenceExpression)lExpression).resolve();
|
||||
if (element instanceof PsiVariable && ((PsiVariable)element).hasModifierProperty(PsiModifier.FINAL)) {
|
||||
return wrapWithNewExpression(to, from, ((PsiAssignmentExpression)context).getRExpression(), element);
|
||||
}
|
||||
}
|
||||
return new TypeConversionDescriptor("$qualifier$ = $val$", "$qualifier$.set($val$)");
|
||||
}
|
||||
if (PsiUtil.isLanguageLevel8OrHigher(context)) {
|
||||
final String name =
|
||||
JavaCodeStyleManager.getInstance(context.getProject()).suggestUniqueVariableName("v", context, false);
|
||||
return new TypeConversionDescriptor("$qualifier$" + sign + "$val$",
|
||||
"$qualifier$.updateAndGet(" +
|
||||
name +
|
||||
" -> " +
|
||||
getBoxedWrapper(from, to, name + " " + sign.charAt(0) + " $val$)"));
|
||||
}
|
||||
if (voidContext) {
|
||||
if (operationSign == JavaTokenType.EQ) {
|
||||
final PsiExpression lExpression = ((PsiAssignmentExpression)context).getLExpression();
|
||||
if (lExpression instanceof PsiReferenceExpression) {
|
||||
final PsiElement element = ((PsiReferenceExpression)lExpression).resolve();
|
||||
if (element instanceof PsiVariable && ((PsiVariable)element).hasModifierProperty(PsiModifier.FINAL)) {
|
||||
return wrapWithNewExpression(to, from, ((PsiAssignmentExpression)context).getRExpression(), element);
|
||||
}
|
||||
}
|
||||
return new TypeConversionDescriptor("$qualifier$ = $val$", "$qualifier$.set($val$)");
|
||||
}
|
||||
else {
|
||||
if (PsiUtil.isLanguageLevel8OrHigher(context)) {
|
||||
final String name =
|
||||
JavaCodeStyleManager.getInstance(context.getProject()).suggestUniqueVariableName("v", context, false);
|
||||
return new TypeConversionDescriptor("$qualifier$" + sign + "$val$", "$qualifier$.updateAndGet("
|
||||
+ name + " -> " + getBoxedWrapper(from, to, name + " " + sign.charAt(0) + " $val$)")); }
|
||||
else {
|
||||
if (context.getParent() instanceof PsiStatement) {
|
||||
return new TypeConversionDescriptor("$qualifier$" + sign + "$val$", "$qualifier$.set(" +
|
||||
getBoxedWrapper(from, to, "$qualifier$.get() " +
|
||||
sign.charAt(0) +
|
||||
" $val$") +
|
||||
")");
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
} //else should be a conflict
|
||||
return new TypeConversionDescriptor("$qualifier$" + sign + "$val$",
|
||||
"$qualifier$.set(" +
|
||||
getBoxedWrapper(from, to, "$qualifier$.get() " + sign.charAt(0) + " $val$") +
|
||||
")");
|
||||
}
|
||||
}
|
||||
else if (context instanceof PsiPostfixExpression) {
|
||||
final String sign = ((PsiPostfixExpression)context).getOperationSign().getText();
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
// "Convert to atomic" "true"
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.function.*;
|
||||
|
||||
public class InLambdas
|
||||
{
|
||||
public void test()
|
||||
@@ -10,5 +12,9 @@ public class InLambdas
|
||||
Runnable r2 = () -> x.addAndGet(2);
|
||||
Runnable r3 = () -> x.updateAndGet(v -> v * 2);
|
||||
Runnable r4 = () -> x.set(5);
|
||||
System.out.println(x.updateAndGet(v -> v / 3));
|
||||
IntSupplier s = () -> {
|
||||
return x.updateAndGet(v -> v * 2);
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,7 @@
|
||||
// "Convert to atomic" "true"
|
||||
|
||||
import java.util.function.*;
|
||||
|
||||
public class InLambdas
|
||||
{
|
||||
public void test()
|
||||
@@ -8,5 +11,9 @@ public class InLambdas
|
||||
Runnable r2 = () -> x+=2;
|
||||
Runnable r3 = () -> x*=2;
|
||||
Runnable r4 = () -> x = 5;
|
||||
System.out.println(x /= 3);
|
||||
IntSupplier s = () -> {
|
||||
return x *= 2;
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user