diff --git a/java/java-tests/testData/codeInsight/invertIfCondition/afterEmptyParenthesis.java b/java/java-tests/testData/codeInsight/invertIfCondition/afterEmptyParenthesis.java new file mode 100644 index 000000000000..f7fcfffc82bc --- /dev/null +++ b/java/java-tests/testData/codeInsight/invertIfCondition/afterEmptyParenthesis.java @@ -0,0 +1,11 @@ +// "Invert 'if' condition" "true" +class A { + public boolean foo() { + if (!()) { + return true; + } + else { + return false; + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/invertIfCondition/afterNull.java b/java/java-tests/testData/codeInsight/invertIfCondition/afterNull.java new file mode 100644 index 000000000000..208552c5bd18 --- /dev/null +++ b/java/java-tests/testData/codeInsight/invertIfCondition/afterNull.java @@ -0,0 +1,11 @@ +// "Invert 'if' condition" "true" +class A { + public boolean foo() { + if (!null) { + return true; + } + else { + return false; + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/invertIfCondition/beforeEmptyParenthesis.java b/java/java-tests/testData/codeInsight/invertIfCondition/beforeEmptyParenthesis.java new file mode 100644 index 000000000000..87b2ce7888b8 --- /dev/null +++ b/java/java-tests/testData/codeInsight/invertIfCondition/beforeEmptyParenthesis.java @@ -0,0 +1,9 @@ +// "Invert 'if' condition" "true" +class A { + public boolean foo() { + if (()) + return false; + else + return true; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/invertIfCondition/beforeNull.java b/java/java-tests/testData/codeInsight/invertIfCondition/beforeNull.java new file mode 100644 index 000000000000..584af553ad79 --- /dev/null +++ b/java/java-tests/testData/codeInsight/invertIfCondition/beforeNull.java @@ -0,0 +1,9 @@ +// "Invert 'if' condition" "true" +class A { + public boolean foo() { + if (null) + return false; + else + return true; + } +} \ No newline at end of file diff --git a/java/openapi/src/com/intellij/codeInsight/CodeInsightServicesUtil.java b/java/openapi/src/com/intellij/codeInsight/CodeInsightServicesUtil.java index c9f28724dc17..a93a15836113 100644 --- a/java/openapi/src/com/intellij/codeInsight/CodeInsightServicesUtil.java +++ b/java/openapi/src/com/intellij/codeInsight/CodeInsightServicesUtil.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2017 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. @@ -78,15 +78,18 @@ public class CodeInsightServicesUtil { } } else if (booleanExpression instanceof PsiLiteralExpression) { - return booleanExpression.getText().equals("true") ? - factory.createExpressionFromText("false", null) : - factory.createExpressionFromText("true", null); + Object value = ((PsiLiteralExpression)booleanExpression).getValue(); + if (value instanceof Boolean) { + return factory.createExpressionFromText(String.valueOf(!((Boolean)value)), booleanExpression); + } } if (booleanExpression instanceof PsiParenthesizedExpression) { PsiExpression operand = ((PsiParenthesizedExpression)booleanExpression).getExpression(); - operand.replace(invertCondition(operand)); - return booleanExpression; + if (operand != null) { + operand.replace(invertCondition(operand)); + return booleanExpression; + } } PsiPrefixExpression result = (PsiPrefixExpression)factory.createExpressionFromText("!(a)", null);