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
.codeInsight
;
18 import com
.intellij
.openapi
.diagnostic
.Logger
;
19 import com
.intellij
.psi
.*;
20 import com
.intellij
.psi
.tree
.IElementType
;
21 import com
.intellij
.util
.IncorrectOperationException
;
26 public class CodeInsightServicesUtil
{
27 private static final Logger LOG
= Logger
.getInstance("#com.intellij.codeInsight.CodeInsightServicesUtil");
29 private static final IElementType
[] ourTokenMap
= new IElementType
[]{
30 JavaTokenType
.EQEQ
, JavaTokenType
.NE
,
31 JavaTokenType
.LT
, JavaTokenType
.GE
,
32 JavaTokenType
.LE
, JavaTokenType
.GT
,
33 JavaTokenType
.OROR
, JavaTokenType
.ANDAND
36 public static PsiExpression
invertCondition(PsiExpression booleanExpression
) throws IncorrectOperationException
{
37 PsiElementFactory factory
= JavaPsiFacade
.getInstance(booleanExpression
.getProject()).getElementFactory();
39 if (booleanExpression
instanceof PsiBinaryExpression
) {
40 PsiBinaryExpression expression
= (PsiBinaryExpression
) booleanExpression
;
41 PsiJavaToken operationSign
= expression
.getOperationSign();
42 for (int i
= 0; i
< ourTokenMap
.length
; i
++) {
43 IElementType tokenType
= ourTokenMap
[i
];
44 if (operationSign
.getTokenType() == tokenType
) {
45 expression
= (PsiBinaryExpression
)expression
.copy();
46 expression
.getOperationSign().replace(createOperationToken(factory
, ourTokenMap
[i
+ (i
% 2 == 0 ?
1 : -1)]));
47 if (tokenType
== JavaTokenType
.OROR
|| tokenType
== JavaTokenType
.ANDAND
) {
48 expression
.getLOperand().replace(invertCondition(expression
.getLOperand()));
49 expression
.getROperand().replace(invertCondition(expression
.getROperand()));
54 } else if (booleanExpression
instanceof PsiPrefixExpression
) {
55 PsiPrefixExpression expression
= (PsiPrefixExpression
) booleanExpression
;
56 PsiJavaToken operationSign
= expression
.getOperationSign();
57 if (operationSign
.getTokenType() == JavaTokenType
.EXCL
) {
58 PsiExpression operand
= expression
.getOperand();
59 if (operand
instanceof PsiParenthesizedExpression
) {
60 operand
= ((PsiParenthesizedExpression
) operand
).getExpression();
64 } else if (booleanExpression
instanceof PsiLiteralExpression
) {
65 return booleanExpression
.getText().equals("true") ?
66 factory
.createExpressionFromText("false", null) :
67 factory
.createExpressionFromText("true", null);
70 if (booleanExpression
instanceof PsiParenthesizedExpression
) {
71 PsiExpression operand
= ((PsiParenthesizedExpression
) booleanExpression
).getExpression();
72 operand
.replace(invertCondition(operand
));
73 return booleanExpression
;
76 PsiPrefixExpression result
= (PsiPrefixExpression
)factory
.createExpressionFromText("!(a)", null);
77 if (!(booleanExpression
instanceof PsiBinaryExpression
)) {
78 result
.getOperand().replace(booleanExpression
);
80 PsiParenthesizedExpression e
= (PsiParenthesizedExpression
) result
.getOperand();
81 e
.getExpression().replace(booleanExpression
);
87 private static PsiElement
createOperationToken(PsiElementFactory factory
, IElementType tokenType
) throws IncorrectOperationException
{
89 if (tokenType
== JavaTokenType
.EQEQ
) {
92 else if (tokenType
== JavaTokenType
.NE
) {
95 else if (tokenType
== JavaTokenType
.LT
) {
98 else if (tokenType
== JavaTokenType
.LE
) {
101 else if (tokenType
== JavaTokenType
.GT
) {
104 else if (tokenType
== JavaTokenType
.GE
) {
107 else if (tokenType
== JavaTokenType
.ANDAND
) {
110 else if (tokenType
== JavaTokenType
.OROR
) {
114 LOG
.error("Unknown token type");
118 PsiBinaryExpression expression
= (PsiBinaryExpression
) factory
.createExpressionFromText("a" + s
+ "b", null);
119 return expression
.getOperationSign();