ConvertFieldToAtomicIntention#isAvailable: check that getVariable() actually returns something

Should fix EA-107511 - assert: ConvertFieldToAtomicIntention.invoke
This commit is contained in:
Tagir Valeev
2017-09-18 15:07:34 +07:00
parent df95dbba8f
commit 3bf031bdc2
3 changed files with 42 additions and 2 deletions
@@ -148,7 +148,6 @@ public class ConvertFieldToAtomicIntention extends PsiElementBaseIntentionAction
else if (var instanceof PsiField) {
((PsiField)var).setInitializer(newInitializer);
}
JavaCodeStyleManager.getInstance(var.getProject()).shortenClassReferences(var.getInitializer());
}
modifierList.setModifierProperty(PsiModifier.FINAL, true);
@@ -219,7 +218,7 @@ public class ConvertFieldToAtomicIntention extends PsiElementBaseIntentionAction
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement element) {
return myContext.isValid();
return getVariable(element) != null;
}
@Override
@@ -0,0 +1,21 @@
// "Convert to atomic" "true"
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.*;
public class InLambdas
{
public void test()
{
AtomicInteger x = new AtomicInteger();
// Also active at write point if it causes a compilation error
Runnable r1 = () -> x.getAndIncrement();
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);
};
}
}
@@ -0,0 +1,20 @@
// "Convert to atomic" "true"
import java.util.function.*;
public class InLambdas
{
public void test()
{
int x = 0;
// Also active at write point if it causes a compilation error
Runnable r1 = () -> <caret>x++;
Runnable r2 = () -> x+=2;
Runnable r3 = () -> x*=2;
Runnable r4 = () -> x = 5;
System.out.println(x /= 3);
IntSupplier s = () -> {
return x *= 2;
};
}
}