From 29e2383afd572c7651b0d22963e9d2645149d169 Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Fri, 22 Sep 2017 16:20:55 +0700 Subject: [PATCH] IDEA-179370 "Constant conditions and exceptions" quickfix "Replace with 'x != null ?:'" does nothing --- .../codeInspection/ReplaceWithTernaryOperatorFix.java | 2 +- .../replaceWithTernaryOperator/afterInnerClass.java | 9 +++++++++ .../replaceWithTernaryOperator/beforeInnerClass.java | 9 +++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceWithTernaryOperator/afterInnerClass.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceWithTernaryOperator/beforeInnerClass.java diff --git a/java/java-impl/src/com/intellij/codeInspection/ReplaceWithTernaryOperatorFix.java b/java/java-impl/src/com/intellij/codeInspection/ReplaceWithTernaryOperatorFix.java index 5c93f3cddcf6..115533f1f047 100644 --- a/java/java-impl/src/com/intellij/codeInspection/ReplaceWithTernaryOperatorFix.java +++ b/java/java-impl/src/com/intellij/codeInspection/ReplaceWithTernaryOperatorFix.java @@ -55,7 +55,7 @@ public class ReplaceWithTernaryOperatorFix implements LocalQuickFix { PsiElement element = descriptor.getPsiElement(); while (true) { PsiElement parent = element.getParent(); - if (parent instanceof PsiReferenceExpression || parent instanceof PsiMethodCallExpression) { + if (parent instanceof PsiReferenceExpression || parent instanceof PsiCallExpression || parent instanceof PsiJavaCodeReferenceElement) { element = parent; } else { diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceWithTernaryOperator/afterInnerClass.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceWithTernaryOperator/afterInnerClass.java new file mode 100644 index 000000000000..19598891ea5c --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceWithTernaryOperator/afterInnerClass.java @@ -0,0 +1,9 @@ +// "Replace with 'njc != null ?:'" "true" +public class JC { + class Inner {} + + public static void main(String[] args) { + JC njc = Math.random() > 0.5 ? new JC() : null; + System.out.println(njc != null ? njc.new Inner() : null); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceWithTernaryOperator/beforeInnerClass.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceWithTernaryOperator/beforeInnerClass.java new file mode 100644 index 000000000000..97b54b51fc91 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceWithTernaryOperator/beforeInnerClass.java @@ -0,0 +1,9 @@ +// "Replace with 'njc != null ?:'" "true" +public class JC { + class Inner {} + + public static void main(String[] args) { + JC njc = Math.random() > 0.5 ? new JC() : null; + System.out.println(njc.new Inner()); + } +} \ No newline at end of file