Bug 565752 - QuickFix is blocked by conditional expression
Quickfix for incorrect return type is blocked by conditional expression
- Add check if expression is part of return statement or variable
declaration statement
- Add test case for assignment from conditional
Change-Id: I6d90c0c9c81658578e6b983b8d86a46a263d4174
Signed-off-by: Kenneth Styrberg <kenneth@kean.nu>
diff --git a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/core/manipulation/dom/ASTResolving.java b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/core/manipulation/dom/ASTResolving.java
index a74fab4..03ab0c0 100644
--- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/core/manipulation/dom/ASTResolving.java
+++ b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/core/manipulation/dom/ASTResolving.java
@@ -63,6 +63,7 @@
import org.eclipse.jdt.core.dom.PrimitiveType.Code;
import org.eclipse.jdt.core.dom.QualifiedName;
import org.eclipse.jdt.core.dom.QualifiedType;
+import org.eclipse.jdt.core.dom.ReturnStatement;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.SimpleType;
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
@@ -243,28 +244,22 @@
}
break;
case ASTNode.CONDITIONAL_EXPRESSION:
+ ReturnStatement parentReturnStatement= (ReturnStatement) ASTNodes.getParent(node, ASTNode.RETURN_STATEMENT);
+ if (parentReturnStatement != null) {
+ return getPossibleReferenceBinding(parentReturnStatement.getExpression());
+ }
+ VariableDeclarationStatement variableDeclarationStatement= (VariableDeclarationStatement) ASTNodes.getParent(node, ASTNode.VARIABLE_DECLARATION_STATEMENT);
+ if (variableDeclarationStatement != null) {
+ return variableDeclarationStatement.getType().resolveBinding();
+ }
ConditionalExpression expression= (ConditionalExpression) parent;
-
- if (node.getLocationInParent() == ConditionalExpression.EXPRESSION_PROPERTY) {
+ if (node.equals(expression.getExpression())) {
return parent.getAST().resolveWellKnownType("boolean"); //$NON-NLS-1$
}
-
- if (node.getLocationInParent() == ConditionalExpression.THEN_EXPRESSION_PROPERTY
- && expression.getElseExpression().resolveTypeBinding() != null) {
- return expression.getElseExpression().resolveTypeBinding();
- }
-
- if (node.getLocationInParent() == ConditionalExpression.ELSE_EXPRESSION_PROPERTY
- && expression.getThenExpression().resolveTypeBinding() != null) {
+ if (node.equals(expression.getElseExpression())) {
return expression.getThenExpression().resolveTypeBinding();
}
-
- if (node.getLocationInParent() == ConditionalExpression.THEN_EXPRESSION_PROPERTY
- || node.getLocationInParent() == ConditionalExpression.ELSE_EXPRESSION_PROPERTY) {
- return getPossibleReferenceBinding(expression);
- }
-
- break;
+ return expression.getElseExpression().resolveTypeBinding();
case ASTNode.POSTFIX_EXPRESSION:
return parent.getAST().resolveWellKnownType("int"); //$NON-NLS-1$
case ASTNode.PREFIX_EXPRESSION:
diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/TypeMismatchQuickFixTests.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/TypeMismatchQuickFixTests.java
index b00b38a..63719c8 100644
--- a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/TypeMismatchQuickFixTests.java
+++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/TypeMismatchQuickFixTests.java
@@ -1098,6 +1098,46 @@
}
@Test
+ public void testTypeMismatchInAssignment5() throws Exception {
+ // test for https://bugs.eclipse.org/bugs/show_bug.cgi?id=565752
+ IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
+ StringBuffer buf= new StringBuffer();
+ buf.append("package test1;\n");
+ buf.append("import java.util.Map;\n");
+ buf.append("public class E {\n");
+ buf.append(" static <K1, K2, V> V bar(K1 key1, Map<K1, Map<K2, V>> map) {\n");
+ buf.append(" Map<K2, V> secondLevelMap = map.get(key1);\n");
+ buf.append(" V v = secondLevelMap == null ? null : secondLevelMap.entrySet();\n");
+ buf.append(" return v;\n");
+ buf.append(" }\n");
+ buf.append("}\n");
+
+ ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
+
+ CompilationUnit astRoot= getASTRoot(cu);
+ ArrayList<IJavaCompletionProposal> proposals= collectCorrections(cu, astRoot);
+ assertNumberOfProposals(proposals, 2);
+ assertCorrectLabels(proposals);
+
+ CUCorrectionProposal proposal= (CUCorrectionProposal) proposals.get(0);
+ String preview1= getPreviewContent(proposal);
+
+ buf= new StringBuffer();
+ buf.append("package test1;\n");
+ buf.append("import java.util.Map;\n");
+ buf.append("public class E {\n");
+ buf.append(" static <K1, K2, V> V bar(K1 key1, Map<K1, Map<K2, V>> map) {\n");
+ buf.append(" Map<K2, V> secondLevelMap = map.get(key1);\n");
+ buf.append(" V v = (V) (secondLevelMap == null ? null : secondLevelMap.entrySet());\n");
+ buf.append(" return v;\n");
+ buf.append(" }\n");
+ buf.append("}\n");
+ String expected1= buf.toString();
+
+ assertEqualStringsIgnoreOrder(new String[] { preview1 }, new String[] { expected1 });
+ }
+
+ @Test
public void testTypeMismatchInExpression() throws Exception {
IPackageFragment pack0= fSourceFolder.createPackageFragment("test0", false, null);