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

Change-Id: Ib829bdbfbd2992688f7e9c75ef257f17a4945af2
Signed-off-by: Fabrice Tiercelin <fabrice.tiercelin@yahoo.fr>
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 27b3030..d7203f1 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
@@ -244,22 +244,32 @@
 				return parent.getAST().resolveWellKnownType("boolean"); //$NON-NLS-1$
 			}
 
-			if (node.getLocationInParent() == ConditionalExpression.THEN_EXPRESSION_PROPERTY
-					&& expression.getElseExpression().resolveTypeBinding() != null) {
-				return expression.getElseExpression().resolveTypeBinding();
+			ITypeBinding parentTypeBinding= getPossibleReferenceBinding(expression);
+
+			if (parentTypeBinding != null
+					&& !parentTypeBinding.isNullType()) {
+				return parentTypeBinding;
 			}
 
-			if (node.getLocationInParent() == ConditionalExpression.ELSE_EXPRESSION_PROPERTY
-					&& expression.getThenExpression().resolveTypeBinding() != null) {
-				return expression.getThenExpression().resolveTypeBinding();
+			if (node.getLocationInParent() == ConditionalExpression.THEN_EXPRESSION_PROPERTY) {
+				ITypeBinding elseTypeBinding= expression.getElseExpression().resolveTypeBinding();
+
+				if (elseTypeBinding != null
+						&& !elseTypeBinding.isNullType()) {
+					return elseTypeBinding;
+				}
 			}
 
-			if (node.getLocationInParent() == ConditionalExpression.THEN_EXPRESSION_PROPERTY
-					|| node.getLocationInParent() == ConditionalExpression.ELSE_EXPRESSION_PROPERTY) {
-				return getPossibleReferenceBinding(expression);
+			if (node.getLocationInParent() == ConditionalExpression.ELSE_EXPRESSION_PROPERTY) {
+				ITypeBinding thenTypeBinding= expression.getThenExpression().resolveTypeBinding();
+
+				if (thenTypeBinding != null
+						&& !thenTypeBinding.isNullType()) {
+					return thenTypeBinding;
+				}
 			}
 
-			break;
+			return getPossibleReferenceBinding(expression);
 		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/ReturnTypeQuickFixTest.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/ReturnTypeQuickFixTest.java
index 3211276..b09ab1a 100644
--- a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/ReturnTypeQuickFixTest.java
+++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/ReturnTypeQuickFixTest.java
@@ -866,6 +866,63 @@
 	}
 
 	@Test
+	public void testCorrectReturnStatementInChainedConditional() throws Exception {
+		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
+
+		String input= "" //
+				+ "package test1;\n" //
+				+ "\n" //
+				+ "import java.util.Map;\n" //
+				+ "\n" //
+				+ "public class E {\n" //
+				+ "    <K1, K2, V> V foo(K1 key1, Map<K1, Map<K2, V>> aMap) {\n" //
+				+ "        Map<K2, V> newMap = aMap.get(key1);\n" //
+				+ "        return newMap == null ? null : aMap == null ? null : newMap.entrySet();\n" //
+				+ "    }\n" //
+				+ "}\n"; //
+		ICompilationUnit cu= pack1.createCompilationUnit("E.java", input, false, null);
+
+		CompilationUnit astRoot= getASTRoot(cu);
+		List<IJavaCompletionProposal> proposals= collectCorrections(cu, astRoot);
+		assertNumberOfProposals(proposals, 2);
+		assertCorrectLabels(proposals);
+
+		ASTRewriteCorrectionProposal proposal= (ASTRewriteCorrectionProposal) proposals.get(0);
+		String preview1= getPreviewContent(proposal);
+
+		String expected1= "" //
+				+ "package test1;\n" //
+				+ "\n" //
+				+ "import java.util.Map;\n" //
+				+ "\n" //
+				+ "public class E {\n" //
+				+ "    <K1, K2, V> V foo(K1 key1, Map<K1, Map<K2, V>> aMap) {\n" //
+				+ "        Map<K2, V> newMap = aMap.get(key1);\n" //
+				+ "        return (V) (newMap == null ? null : aMap == null ? null : newMap.entrySet());\n" //
+				+ "    }\n" //
+				+ "}\n"; //
+
+		proposal= (ASTRewriteCorrectionProposal) proposals.get(1);
+		String preview2= getPreviewContent(proposal);
+
+		String expected2= "" //
+				+ "package test1;\n" //
+				+ "\n" //
+				+ "import java.util.Map;\n" //
+				+ "import java.util.Map.Entry;\n" //
+				+ "import java.util.Set;\n" //
+				+ "\n" //
+				+ "public class E {\n" //
+				+ "    <K1, K2, V> Set<Entry<K2, V>> foo(K1 key1, Map<K1, Map<K2, V>> aMap) {\n" //
+				+ "        Map<K2, V> newMap = aMap.get(key1);\n" //
+				+ "        return newMap == null ? null : aMap == null ? null : newMap.entrySet();\n" //
+				+ "    }\n" //
+				+ "}\n"; //
+
+		assertEqualStringsIgnoreOrder(new String[] { preview1, preview2 }, new String[] { expected1, expected2 });
+	}
+
+	@Test
 	public void testReturnVoid() throws Exception {
 		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
 		StringBuffer buf= new StringBuffer();