mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-05-06 05:10:22 +07:00
inline: insert sync on class object when static sync method is inlined (IDEA-65449)
This commit is contained in:
@@ -627,25 +627,30 @@ public class InlineMethodProcessor extends BaseRefactoringProcessor {
|
||||
}
|
||||
|
||||
PsiLocalVariable thisVar = null;
|
||||
if (!myMethod.hasModifierProperty(PsiModifier.STATIC)) {
|
||||
PsiClass containingClass = myMethod.getContainingClass();
|
||||
|
||||
if (containingClass != null) {
|
||||
PsiType thisType = myFactory.createType(containingClass, callSubstitutor);
|
||||
String[] names = myJavaCodeStyle.suggestVariableName(VariableKind.LOCAL_VARIABLE, null, null, thisType)
|
||||
.names;
|
||||
String thisVarName = names[0];
|
||||
thisVarName = myJavaCodeStyle.suggestUniqueVariableName(thisVarName, block.getFirstChild(), true);
|
||||
PsiExpression initializer = myFactory.createExpressionFromText("null", null);
|
||||
PsiDeclarationStatement declaration = myFactory.createVariableDeclarationStatement(thisVarName, thisType, initializer);
|
||||
declaration = (PsiDeclarationStatement)block.addAfter(declaration, null);
|
||||
thisVar = (PsiLocalVariable)declaration.getDeclaredElements()[0];
|
||||
}
|
||||
PsiClass containingClass = myMethod.getContainingClass();
|
||||
if (!myMethod.hasModifierProperty(PsiModifier.STATIC) && containingClass != null) {
|
||||
PsiType thisType = myFactory.createType(containingClass, callSubstitutor);
|
||||
String[] names = myJavaCodeStyle.suggestVariableName(VariableKind.LOCAL_VARIABLE, null, null, thisType)
|
||||
.names;
|
||||
String thisVarName = names[0];
|
||||
thisVarName = myJavaCodeStyle.suggestUniqueVariableName(thisVarName, block.getFirstChild(), true);
|
||||
PsiExpression initializer = myFactory.createExpressionFromText("null", null);
|
||||
PsiDeclarationStatement declaration = myFactory.createVariableDeclarationStatement(thisVarName, thisType, initializer);
|
||||
declaration = (PsiDeclarationStatement)block.addAfter(declaration, null);
|
||||
thisVar = (PsiLocalVariable)declaration.getDeclaredElements()[0];
|
||||
}
|
||||
|
||||
if (thisVar != null && syncNeeded(ref)) {
|
||||
String lockName = null;
|
||||
if (thisVar != null) {
|
||||
lockName = thisVar.getName();
|
||||
}
|
||||
else if (myMethod.hasModifierProperty(PsiModifier.STATIC) && containingClass != null ) {
|
||||
lockName = containingClass.getQualifiedName() + ".class";
|
||||
}
|
||||
|
||||
if (lockName != null && syncNeeded(ref)) {
|
||||
PsiSynchronizedStatement synchronizedStatement =
|
||||
(PsiSynchronizedStatement)myFactory.createStatementFromText("synchronized(" + thisVar.getName() + "){}", block);
|
||||
(PsiSynchronizedStatement)myFactory.createStatementFromText("synchronized(" + lockName + "){}", block);
|
||||
synchronizedStatement = (PsiSynchronizedStatement)CodeStyleManager.getInstance(myProject).reformat(synchronizedStatement);
|
||||
synchronizedStatement = (PsiSynchronizedStatement)block.add(synchronizedStatement);
|
||||
final PsiCodeBlock synchronizedBody = synchronizedStatement.getBody();
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
public class TestIntelliJ
|
||||
{
|
||||
|
||||
public synchronized void foo() {
|
||||
System.out.println("foo");
|
||||
}
|
||||
|
||||
public static synchronized void foo<caret>Static() {
|
||||
System.out.println("fooStatic");
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
TestIntelliJ test = new TestIntelliJ();
|
||||
test.foo();
|
||||
TestIntelliJ.fooStatic();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
public class TestIntelliJ
|
||||
{
|
||||
|
||||
public synchronized void foo() {
|
||||
System.out.println("foo");
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
TestIntelliJ test = new TestIntelliJ();
|
||||
test.foo();
|
||||
synchronized (TestIntelliJ.class) {
|
||||
System.out.println("fooStatic");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -36,6 +36,10 @@ public class InlineMethodTest extends LightCodeInsightTestCase {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testStaticSynchronized() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testSuperInsideHierarchy() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user