mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
unguarded field access: support sync expressions (IDEA-153399)
This commit is contained in:
+15
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package com.intellij.codeInspection.concurrencyAnnotations;
|
||||
|
||||
import com.intellij.codeInsight.PsiEquivalenceUtil;
|
||||
import com.intellij.codeInsight.daemon.GroupNames;
|
||||
import com.intellij.codeInspection.BaseJavaBatchLocalInspectionTool;
|
||||
import com.intellij.codeInspection.ProblemsHolder;
|
||||
@@ -81,6 +82,20 @@ public class FieldAccessNotGuardedInspection extends BaseJavaBatchLocalInspectio
|
||||
if (containingMethod != null && containingMethod.hasModifierProperty(PsiModifier.SYNCHRONIZED)) {
|
||||
return;
|
||||
}
|
||||
|
||||
final PsiSynchronizedStatement synchronizedStatement = PsiTreeUtil.getParentOfType(expression, PsiSynchronizedStatement.class);
|
||||
if (synchronizedStatement != null) {
|
||||
final PsiExpression lockExpression = synchronizedStatement.getLockExpression();
|
||||
final PsiExpression qualifierExpression = expression.getQualifierExpression();
|
||||
if (lockExpression instanceof PsiThisExpression && qualifierExpression == null) {
|
||||
return;
|
||||
}
|
||||
if (lockExpression instanceof PsiReferenceExpression &&
|
||||
qualifierExpression != null &&
|
||||
PsiEquivalenceUtil.areElementsEquivalent(lockExpression, qualifierExpression)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (findLockTryStatement(expression, guard) != null) {
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
import javax.annotation.concurrent.GuardedBy;
|
||||
|
||||
class Example
|
||||
{
|
||||
private final Distribution distribution = new Distribution();
|
||||
|
||||
public void add(long value)
|
||||
{
|
||||
synchronized (distribution) {
|
||||
distribution.total += value;
|
||||
<error descr="Cannot resolve symbol 'total'">total</error> += value;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected static class Distribution
|
||||
{
|
||||
@GuardedBy("this")
|
||||
private long total = 0;
|
||||
}
|
||||
}
|
||||
+10
-1
@@ -24,11 +24,20 @@ import org.jetbrains.annotations.NotNull;
|
||||
public class FieldAccessedNotGuardedInspectionTest extends LightCodeInsightFixtureTestCase {
|
||||
public void testItself() throws Exception {
|
||||
myFixture.addClass("package net.jcip.annotations;\n" + getGuardedByAnnotationText());
|
||||
myFixture.testHighlighting(true, false, false, getTestName(true) + ".java");
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testJavax_itself() throws Exception {
|
||||
myFixture.addClass("package javax.annotation.concurrent;\n" + getGuardedByAnnotationText());
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testSyncOnFieldQualifier() throws Exception {
|
||||
myFixture.addClass("package javax.annotation.concurrent;\n" + getGuardedByAnnotationText());
|
||||
doTest();
|
||||
}
|
||||
|
||||
private void doTest() {
|
||||
myFixture.testHighlighting(true, false, false, getTestName(true) + ".java");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user