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.
16 package com
.intellij
.refactoring
.util
.duplicates
;
18 import com
.intellij
.codeInsight
.PsiEquivalenceUtil
;
19 import com
.intellij
.psi
.*;
20 import com
.intellij
.util
.IncorrectOperationException
;
25 public class ConditionalReturnStatementValue
implements ReturnValue
{
26 PsiExpression myReturnValue
;
28 public ConditionalReturnStatementValue(final PsiExpression returnValue
) {
29 myReturnValue
= returnValue
;
32 public boolean isEquivalent(ReturnValue other
) {
33 if (!(other
instanceof ConditionalReturnStatementValue
)) return false;
34 PsiExpression otherReturnValue
= ((ConditionalReturnStatementValue
) other
).myReturnValue
;
35 if (otherReturnValue
== null || myReturnValue
== null) return myReturnValue
== null && otherReturnValue
== null;
36 return PsiEquivalenceUtil
.areElementsEquivalent(myReturnValue
, otherReturnValue
);
39 public PsiStatement
createReplacement(final PsiMethod extractedMethod
, PsiMethodCallExpression methodCallExpression
) throws IncorrectOperationException
{
40 final PsiElementFactory elementFactory
= JavaPsiFacade
.getInstance(methodCallExpression
.getProject()).getElementFactory();
41 PsiIfStatement statement
;
42 if (myReturnValue
== null) {
43 statement
= (PsiIfStatement
)elementFactory
.createStatementFromText("if(a) return;", null);
46 statement
= (PsiIfStatement
)elementFactory
.createStatementFromText("if(a) return b;", null);
47 final PsiReturnStatement thenBranch
= (PsiReturnStatement
)statement
.getThenBranch();
48 assert thenBranch
!= null;
49 final PsiExpression returnValue
= thenBranch
.getReturnValue();
50 assert returnValue
!= null;
51 returnValue
.replace(myReturnValue
);
54 final PsiExpression condition
= statement
.getCondition();
55 assert condition
!= null;
56 condition
.replace(methodCallExpression
);
57 return (PsiStatement
)statement
.getManager().getCodeStyleManager().reformat(statement
);