stop replacement chain calls to sequences when root type changes during call chain; this-chain is not supported though

This commit is contained in:
Anna Kozlova
2014-04-01 17:04:16 +02:00
parent 73f87cd148
commit 9195d8e12a
6 changed files with 89 additions and 22 deletions
@@ -45,52 +45,45 @@ class MethodCallChainPredicate implements PsiElementPredicate {
}
private static boolean isCallChain(PsiElement element) {
PsiClass aClass1 = getQualifierExpressionType(element);
if (aClass1 == null) {
PsiClassType aClassType1 = getQualifierExpressionType(element);
if (aClassType1 == null) {
return false;
}
boolean first = true;
while (aClass1 != null) {
while (true) {
final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression)element;
final PsiReferenceExpression methodExpression = methodCallExpression.getMethodExpression();
final PsiExpression qualifierExpression = methodExpression.getQualifierExpression();
PsiClass aClass2 = getQualifierExpressionType(qualifierExpression);
PsiClassType expressionType = getQualifierExpressionType(qualifierExpression);
if (!first) {
if (aClass2 == null) {
if (expressionType == null) {
if (qualifierExpression instanceof PsiMethodCallExpression &&
((PsiMethodCallExpression)qualifierExpression).getMethodExpression().getQualifierExpression() == null) {
return false;
}
return true;
}
} else {
first = false;
}
if (!aClass1.equals(aClass2)) {
if (!aClassType1.equals(expressionType)) {
return false;
}
aClass1 = aClass2;
aClassType1 = expressionType;
element = qualifierExpression;
}
return true;
}
@Nullable
private static PsiClass getQualifierExpressionType(PsiElement element) {
private static PsiClassType getQualifierExpressionType(PsiElement element) {
if (!(element instanceof PsiMethodCallExpression)) {
return null;
}
final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression)element;
final PsiMethod method = methodCallExpression.resolveMethod();
if (method != null) {
return method.getContainingClass();
}
final PsiReferenceExpression methodExpression = methodCallExpression.getMethodExpression();
final PsiExpression qualifierExpression = methodExpression.getQualifierExpression();
if (!(qualifierExpression instanceof PsiMethodCallExpression)) {
return null;
}
final PsiType type = qualifierExpression.getType();
if (!(type instanceof PsiClassType)) {
return null;
}
final PsiClassType classType = (PsiClassType)type;
return classType.resolve();
final PsiType type = qualifierExpression != null ? qualifierExpression.getType() : null;
return type instanceof PsiClassType ? (PsiClassType)type : null;
}
}
@@ -0,0 +1,6 @@
class Test {
{
StringBuilder text = null;
text.delete(text.length() - 1, text.length()).ap<caret>pend(";\n");
}
}
@@ -0,0 +1,7 @@
class Test {
{
StringBuilder text = null;
text.delete(text.length() - 1, text.length());
text.append(";\n");
}
}
@@ -0,0 +1,12 @@
class FooBar<T> {
FooBar<List<T>> foo() {
return null;
}
FooBar<T> bar() {return this;}
void f(FooBar<String> fb) {
foo().bar().toSt<caret>ring();
}
}
@@ -0,0 +1,12 @@
class FooBar<T> {
<K> FooBar<K> foo(K k) {
return null;
}
FooBar<T> bar() {return this;}
void f(FooBar<String> fb) {
fb.foo(1).bar().toSt<caret>ring();
}
}
@@ -0,0 +1,37 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ipp.concatenation;
import com.siyeh.IntentionPowerPackBundle;
import com.siyeh.ipp.IPPTestCase;
public class MakeCallChainIntoCallSequenceTest extends IPPTestCase {
public void testBuilder() { doTest(); }
public void testTransformation() { assertIntentionNotAvailable();}
//should be probably possible?
public void testThisCollapse() { assertIntentionNotAvailable();}
@Override
protected String getIntentionName() {
return IntentionPowerPackBundle.message("make.call.chain.into.call.sequence.intention.name");
}
@Override
protected String getRelativePath() {
return "concatenation/call_chain";
}
}