2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com
.siyeh
.ipp
.expression
;
19 import com
.intellij
.psi
.*;
20 import com
.intellij
.psi
.util
.PropertyUtil
;
21 import com
.intellij
.util
.IncorrectOperationException
;
22 import com
.siyeh
.ipp
.base
.Intention
;
23 import com
.siyeh
.ipp
.base
.PsiElementPredicate
;
24 import org
.jetbrains
.annotations
.NotNull
;
27 * @author Konstantin Bulenkov
29 public class FlipSetterCallIntention
extends Intention
{
30 private static final PsiElementPredicate PREDICATE
= new SetterCallPredicate();
32 protected void processIntention(@NotNull PsiElement element
) throws IncorrectOperationException
{
33 if (element
instanceof PsiMethodCallExpression
) {
34 flipCalls((PsiMethodCallExpression
)element
);
39 protected PsiElementPredicate
getElementPredicate() {
43 private static void flipCalls(PsiMethodCallExpression call
) {
44 PsiExpression qualifierExpression
= call
.getMethodExpression().getQualifierExpression();
45 if (qualifierExpression
== null) return;
46 final String qualifier1
= qualifierExpression
.getText();
47 if (qualifier1
== null || qualifier1
.length() == 0) return;
48 final PsiMethodCallExpression param
= (PsiMethodCallExpression
)call
.getArgumentList().getExpressions()[0];
49 qualifierExpression
= param
.getMethodExpression().getQualifierExpression();
50 if (qualifierExpression
== null) return;
51 final String qualifier2
= qualifierExpression
.getText();
52 final PsiMethod setter
= call
.resolveMethod();
53 final PsiMethod getter
= param
.resolveMethod();
55 if (getter
== null || setter
== null) return;
57 final PsiMethod get
= PropertyUtil
.findPropertyGetter(setter
.getContainingClass(), PropertyUtil
.getPropertyName(setter
), false, true);
58 final PsiMethod set
= PropertyUtil
.findPropertySetter(getter
.getContainingClass(), PropertyUtil
.getPropertyName(getter
), false, true);
60 if (get
== null || set
== null) return;
62 StringBuilder text
= new StringBuilder();
63 text
.append(qualifier2
).append(".").append(set
.getName())
65 .append(qualifier1
).append(".").append(get
.getName()).append("()")
67 final PsiExpression newExpression
=
68 JavaPsiFacade
.getElementFactory(call
.getProject()).createExpressionFromText(text
.toString(), call
.getContext());
69 call
.replace(newExpression
);
73 //static PsiMethodCallExpression getSetGetMethodCallAtCaret(Editor editor, PsiFile file) {
74 // int offset = editor.getCaretModel().getOffset();
75 // PsiElement element = file.findElementAt(offset);
76 // if (element == null) return null;
77 // PsiMethodCallExpression expression = PsiTreeUtil.getParentOfType(element, PsiMethodCallExpression.class);
78 // if (expression != null && atSameLine(expression, editor) && isSetGetMethodCall(expression)) {
84 private static boolean isSetGetMethodCall(PsiMethodCallExpression call
) {
85 final PsiExpression
[] params
= call
.getArgumentList().getExpressions();
86 if (params
.length
!= 1) return false;
87 if (! (params
[0] instanceof PsiMethodCallExpression
)) return false;
88 final PsiMethodCallExpression call2
= (PsiMethodCallExpression
)params
[0];
90 //check expressions are simple properties
91 final PsiElement methodElement
= call
.getMethodExpression().resolve();
92 final PsiElement param
= call2
.getMethodExpression().resolve();
93 if (!(methodElement
instanceof PsiMethod
)
94 || !(param
instanceof PsiMethod
)
95 ||!PropertyUtil
.isSimplePropertySetter((PsiMethod
)methodElement
)
96 ||!PropertyUtil
.isSimplePropertyGetter((PsiMethod
)param
)) {
99 final PsiMethod setter1
= (PsiMethod
)methodElement
;
100 final PsiMethod getter2
= (PsiMethod
)param
;
102 //check types compatibility
103 if (! call
.getArgumentList().getExpressionTypes()[0].equals(getter2
.getReturnType())) return false;
105 //check both classes have getters/setters
106 final PsiMethod getter1
= PropertyUtil
.findPropertyGetter(setter1
.getContainingClass(), PropertyUtil
.getPropertyName(setter1
), false, true);
107 if (getter1
== null) return false;
109 final PsiMethod setter2
= PropertyUtil
.findPropertyGetter(getter2
.getContainingClass(), PropertyUtil
.getPropertyName(getter2
), false, true);
110 if (setter2
== null) return false;
115 static class SetterCallPredicate
implements PsiElementPredicate
{
116 public boolean satisfiedBy(PsiElement element
) {
117 return (element
instanceof PsiMethodCallExpression
118 && isSetGetMethodCall((PsiMethodCallExpression
)element
));