IDEADEV-31057 (Intention "Expand boolean use to if-else" works incorrectly with |=)
[fedora-idea.git] / plugins / IntentionPowerPak / src / com / siyeh / ipp / trivialif / ExpandBooleanPredicate.java
blobed2d104e4ce54ca763906cef8d5755e50c082d42
1 /*
2 * Copyright 2003-2008 Dave Griffith, Bas Leijdekkers
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.siyeh.ipp.trivialif;
18 import com.intellij.psi.*;
19 import com.intellij.psi.util.PsiTreeUtil;
20 import com.siyeh.ipp.base.PsiElementPredicate;
22 class ExpandBooleanPredicate implements PsiElementPredicate{
24 public boolean satisfiedBy(PsiElement element){
25 if(!(element instanceof PsiJavaToken)){
26 return false;
28 final PsiStatement containingStatement =
29 PsiTreeUtil.getParentOfType(element,
30 PsiStatement.class);
31 if(containingStatement == null){
32 return false;
34 return isBooleanReturn(containingStatement) ||
35 isBooleanAssignment(containingStatement);
38 public static boolean isBooleanReturn(PsiStatement containingStatement){
39 if(!(containingStatement instanceof PsiReturnStatement)){
40 return false;
42 final PsiReturnStatement returnStatement =
43 (PsiReturnStatement) containingStatement;
44 final PsiExpression returnValue = returnStatement.getReturnValue();
45 if (returnValue == null || returnValue instanceof PsiLiteralExpression) {
46 return false;
48 final PsiType returnType = returnValue.getType();
49 return PsiType.BOOLEAN.equals(returnType);
52 public static boolean isBooleanAssignment(PsiStatement containingStatement){
53 if(!(containingStatement instanceof PsiExpressionStatement)){
54 return false;
56 final PsiExpressionStatement expressionStatement =
57 (PsiExpressionStatement) containingStatement;
58 final PsiExpression expression = expressionStatement.getExpression();
59 if(!(expression instanceof PsiAssignmentExpression)){
60 return false;
62 final PsiAssignmentExpression assignment =
63 (PsiAssignmentExpression) expression;
64 final PsiExpression rhs = assignment.getRExpression();
65 if (rhs == null || rhs instanceof PsiLiteralExpression) {
66 return false;
68 final PsiType assignmentType = rhs.getType();
69 return PsiType.BOOLEAN.equals(assignmentType);