inline: insert sync on class object when static sync method is inlined (IDEA-65449)

This commit is contained in:
anna
2011-02-14 14:13:10 +01:00
parent 9b28b9cf01
commit 13d5d02daa
4 changed files with 57 additions and 16 deletions

View File

@@ -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();

View File

@@ -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();
}
}

View File

@@ -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");
}
}
}

View File

@@ -36,6 +36,10 @@ public class InlineMethodTest extends LightCodeInsightTestCase {
doTest();
}
public void testStaticSynchronized() throws Exception {
doTest();
}
public void testSuperInsideHierarchy() throws Exception {
doTest();
}