View | Details | Raw Unified | Return to bug 304006 | Differences between
and this patch

Collapse All | Expand All

(-)codeassist/org/eclipse/jdt/internal/codeassist/CompletionEngine.java (-114 / +123 lines)
Lines 6710-6842 Link Here
6710
		if (enclosingNode == null || !(enclosingNode instanceof IfStatement)) return;
6710
		if (enclosingNode == null || !(enclosingNode instanceof IfStatement)) return;
6711
6711
6712
		IfStatement ifStatement = (IfStatement)enclosingNode;
6712
		IfStatement ifStatement = (IfStatement)enclosingNode;
6713
6713
		while (true) {
6714
		if (!(ifStatement.condition instanceof InstanceOfExpression)) return;
6714
			if (!(ifStatement.condition instanceof InstanceOfExpression)) return;
6715
6715
	
6716
		InstanceOfExpression instanceOfExpression = (InstanceOfExpression) ifStatement.condition;
6716
			InstanceOfExpression instanceOfExpression = (InstanceOfExpression) ifStatement.condition;
6717
6717
	
6718
		TypeReference instanceOfType = instanceOfExpression.type;
6718
			TypeReference instanceOfType = instanceOfExpression.type;
6719
6719
	
6720
		if (instanceOfType.resolvedType == null) return;
6720
			if (instanceOfType.resolvedType == null) return;
6721
6721
	
6722
		boolean findFromAnotherReceiver = false;
6722
			boolean findFromAnotherReceiver = false;
6723
6723
	
6724
		char[][] receiverName = null;
6724
			char[][] receiverName = null;
6725
		int receiverStart = -1;
6725
			int receiverStart = -1;
6726
		int receiverEnd = -1;
6726
			int receiverEnd = -1;
6727
6727
	
6728
		if (receiver instanceof QualifiedNameReference) {
6728
			if (receiver instanceof QualifiedNameReference) {
6729
			QualifiedNameReference qualifiedNameReference = (QualifiedNameReference) receiver;
6729
				QualifiedNameReference qualifiedNameReference = (QualifiedNameReference) receiver;
6730
6730
	
6731
			receiverName = qualifiedNameReference.tokens;
6731
				receiverName = qualifiedNameReference.tokens;
6732
6732
	
6733
			if (receiverName.length != 1) return;
6733
				if (receiverName.length != 1) return;
6734
6734
	
6735
			receiverStart = (int) (qualifiedNameReference.sourcePositions[0] >>> 32);
6735
				receiverStart = (int) (qualifiedNameReference.sourcePositions[0] >>> 32);
6736
			receiverEnd = (int) qualifiedNameReference.sourcePositions[qualifiedNameReference.tokens.length - 1] + 1;
6736
				receiverEnd = (int) qualifiedNameReference.sourcePositions[qualifiedNameReference.tokens.length - 1] + 1;
6737
6737
	
6738
			// if (local instanceof X) local.|
6738
				// if (local instanceof X) local.|
6739
			// if (field instanceof X) field.|
6739
				// if (field instanceof X) field.|
6740
			if (instanceOfExpression.expression instanceof SingleNameReference &&
6741
					((SingleNameReference)instanceOfExpression.expression).binding == qualifiedBinding &&
6742
					(qualifiedBinding instanceof LocalVariableBinding || qualifiedBinding instanceof FieldBinding)) {
6743
				findFromAnotherReceiver = true;
6744
			}
6745
6746
			// if (this.field instanceof X) field.|
6747
			if (instanceOfExpression.expression instanceof FieldReference) {
6748
				FieldReference fieldReference = (FieldReference)instanceOfExpression.expression;
6749
6750
				if (fieldReference.receiver instanceof ThisReference &&
6751
						qualifiedBinding instanceof FieldBinding &&
6752
						fieldReference.binding == qualifiedBinding) {
6753
							findFromAnotherReceiver = true;
6754
				}
6755
			}
6756
		} else if (receiver instanceof FieldReference) {
6757
			FieldReference fieldReference1 = (FieldReference) receiver;
6758
6759
			receiverStart = fieldReference1.sourceStart;
6760
			receiverEnd = fieldReference1.sourceEnd + 1;
6761
6762
			if (fieldReference1.receiver instanceof ThisReference) {
6763
6764
				receiverName = new char[][] {THIS, fieldReference1.token};
6765
6766
				// if (field instanceof X) this.field.|
6767
				if (instanceOfExpression.expression instanceof SingleNameReference &&
6740
				if (instanceOfExpression.expression instanceof SingleNameReference &&
6768
						((SingleNameReference)instanceOfExpression.expression).binding == fieldReference1.binding) {
6741
						((SingleNameReference)instanceOfExpression.expression).binding == qualifiedBinding &&
6742
						(qualifiedBinding instanceof LocalVariableBinding || qualifiedBinding instanceof FieldBinding)) {
6769
					findFromAnotherReceiver = true;
6743
					findFromAnotherReceiver = true;
6770
				}
6744
				}
6771
6745
	
6772
				// if (this.field instanceof X) this.field.|
6746
				// if (this.field instanceof X) field.|
6773
				if (instanceOfExpression.expression instanceof FieldReference) {
6747
				if (instanceOfExpression.expression instanceof FieldReference) {
6774
					FieldReference fieldReference2 = (FieldReference)instanceOfExpression.expression;
6748
					FieldReference fieldReference = (FieldReference)instanceOfExpression.expression;
6775
6749
	
6776
					if (fieldReference2.receiver instanceof ThisReference &&
6750
					if (fieldReference.receiver instanceof ThisReference &&
6777
							fieldReference2.binding == fieldReference1.binding) {
6751
							qualifiedBinding instanceof FieldBinding &&
6752
							fieldReference.binding == qualifiedBinding) {
6778
								findFromAnotherReceiver = true;
6753
								findFromAnotherReceiver = true;
6779
					}
6754
					}
6780
				}
6755
				}
6756
			} else if (receiver instanceof FieldReference) {
6757
				FieldReference fieldReference1 = (FieldReference) receiver;
6758
	
6759
				receiverStart = fieldReference1.sourceStart;
6760
				receiverEnd = fieldReference1.sourceEnd + 1;
6761
	
6762
				if (fieldReference1.receiver instanceof ThisReference) {
6763
	
6764
					receiverName = new char[][] {THIS, fieldReference1.token};
6765
	
6766
					// if (field instanceof X) this.field.|
6767
					if (instanceOfExpression.expression instanceof SingleNameReference &&
6768
							((SingleNameReference)instanceOfExpression.expression).binding == fieldReference1.binding) {
6769
						findFromAnotherReceiver = true;
6770
					}
6771
	
6772
					// if (this.field instanceof X) this.field.|
6773
					if (instanceOfExpression.expression instanceof FieldReference) {
6774
						FieldReference fieldReference2 = (FieldReference)instanceOfExpression.expression;
6775
	
6776
						if (fieldReference2.receiver instanceof ThisReference &&
6777
								fieldReference2.binding == fieldReference1.binding) {
6778
									findFromAnotherReceiver = true;
6779
						}
6780
					}
6781
				}
6781
			}
6782
			}
6782
		}
6783
	
6783
6784
			if (findFromAnotherReceiver) {
6784
		if (findFromAnotherReceiver) {
6785
				TypeBinding receiverTypeBinding = instanceOfType.resolvedType;
6785
			TypeBinding receiverTypeBinding = instanceOfType.resolvedType;
6786
				char[] castedReceiver = null;
6786
			char[] castedReceiver = null;
6787
	
6787
6788
				char[] castedTypeChars = CharOperation.concatWith(instanceOfType.getTypeName(), '.');
6788
			char[] castedTypeChars = CharOperation.concatWith(instanceOfType.getTypeName(), '.');
6789
				if(this.source != null) {
6789
			if(this.source != null) {
6790
					int memberRefStart = this.startPosition;
6790
				int memberRefStart = this.startPosition;
6791
	
6791
6792
					char[] receiverChars = CharOperation.subarray(this.source, receiverStart, receiverEnd);
6792
				char[] receiverChars = CharOperation.subarray(this.source, receiverStart, receiverEnd);
6793
					char[] dotChars = CharOperation.subarray(this.source, receiverEnd, memberRefStart);
6793
				char[] dotChars = CharOperation.subarray(this.source, receiverEnd, memberRefStart);
6794
	
6794
6795
					castedReceiver =
6795
				castedReceiver =
6796
					CharOperation.concat(
6797
						CharOperation.concat(
6796
						CharOperation.concat(
6798
							'(',
6799
							CharOperation.concat(
6797
							CharOperation.concat(
6800
								CharOperation.concat('(', castedTypeChars, ')'),
6798
								'(',
6801
								receiverChars),
6799
								CharOperation.concat(
6802
							')'),
6800
									CharOperation.concat('(', castedTypeChars, ')'),
6803
						dotChars);
6801
									receiverChars),
6804
			} else {
6802
								')'),
6805
				castedReceiver =
6803
							dotChars);
6806
					CharOperation.concat(
6804
				} else {
6805
					castedReceiver =
6807
						CharOperation.concat(
6806
						CharOperation.concat(
6808
							'(',
6809
							CharOperation.concat(
6807
							CharOperation.concat(
6810
								CharOperation.concat('(', castedTypeChars, ')'),
6808
								'(',
6811
								CharOperation.concatWith(receiverName, '.')),
6809
								CharOperation.concat(
6812
							')'),
6810
									CharOperation.concat('(', castedTypeChars, ')'),
6813
						DOT);
6811
									CharOperation.concatWith(receiverName, '.')),
6812
								')'),
6813
							DOT);
6814
				}
6815
	
6816
				if (castedReceiver == null) return;
6817
	
6818
				int oldStartPosition = this.startPosition;
6819
				this.startPosition = receiverStart;
6820
	
6821
				findFieldsAndMethods(
6822
						this.completionToken,
6823
						receiverTypeBinding,
6824
						scope,
6825
						fieldsFound,
6826
						methodsFound,
6827
						invocationSite,
6828
						invocationScope,
6829
						false,
6830
						false,
6831
						null,
6832
						null,
6833
						null,
6834
						false,
6835
						castedReceiver,
6836
						receiverStart,
6837
						receiverEnd);
6838
	
6839
				this.startPosition = oldStartPosition;
6840
			}
6841
			// traverse the enclosing node to find the instanceof expression corresponding
6842
			// to the completion node (if any)
6843
			// https://bugs.eclipse.org/bugs/show_bug.cgi?id=304006
6844
			if (ifStatement.thenStatement instanceof IfStatement) {
6845
				ifStatement = (IfStatement) ifStatement.thenStatement;
6846
			} else {
6847
				break;
6814
			}
6848
			}
6815
6816
			if (castedReceiver == null) return;
6817
6818
			int oldStartPosition = this.startPosition;
6819
			this.startPosition = receiverStart;
6820
6821
			findFieldsAndMethods(
6822
					this.completionToken,
6823
					receiverTypeBinding,
6824
					scope,
6825
					fieldsFound,
6826
					methodsFound,
6827
					invocationSite,
6828
					invocationScope,
6829
					false,
6830
					false,
6831
					null,
6832
					null,
6833
					null,
6834
					false,
6835
					castedReceiver,
6836
					receiverStart,
6837
					receiverEnd);
6838
6839
			this.startPosition = oldStartPosition;
6840
		}
6849
		}
6841
	}
6850
	}
6842
	private void findFieldsAndMethodsFromFavorites(
6851
	private void findFieldsAndMethodsFromFavorites(
(-)codeassist/org/eclipse/jdt/internal/codeassist/complete/CompletionParser.java (-35 / +72 lines)
Lines 563-570 Link Here
563
			if(expression == this.assistNode
563
			if(expression == this.assistNode
564
				|| (expression instanceof Assignment	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=287939
564
				|| (expression instanceof Assignment	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=287939
565
					&& ((Assignment)expression).expression == this.assistNode
565
					&& ((Assignment)expression).expression == this.assistNode
566
					&& ((this.expressionPtr > 0 && this.expressionStack[this.expressionPtr - 1] instanceof InstanceOfExpression)
566
					&& ((this.expressionPtr > 0 && stackHasInstanceOfExpression(this.expressionStack, this.expressionPtr - 1))
567
						|| (this.elementPtr >= 0 && this.elementObjectInfoStack[this.elementPtr] instanceof InstanceOfExpression)))
567
							// In case of error in compilation unit, expression stack might not have instanceof exp, so try elementObjectInfoStack
568
						|| (this.elementPtr >= 0 && stackHasInstanceOfExpression(this.elementObjectInfoStack, this.elementPtr))))
568
				|| (expression instanceof AllocationExpression
569
				|| (expression instanceof AllocationExpression
569
					&& ((AllocationExpression)expression).type == this.assistNode)
570
					&& ((AllocationExpression)expression).type == this.assistNode)
570
				|| (expression instanceof AND_AND_Expression
571
				|| (expression instanceof AND_AND_Expression
Lines 1064-1070 Link Here
1064
	}
1065
	}
1065
}
1066
}
1066
private Statement buildMoreCompletionEnclosingContext(Statement statement) {
1067
private Statement buildMoreCompletionEnclosingContext(Statement statement) {
1067
1068
	IfStatement ifStatement = null;
1068
	int blockIndex = lastIndexOfElement(K_BLOCK_DELIMITER);
1069
	int blockIndex = lastIndexOfElement(K_BLOCK_DELIMITER);
1069
	int controlIndex = lastIndexOfElement(K_CONTROL_STATEMENT_DELIMITER);
1070
	int controlIndex = lastIndexOfElement(K_CONTROL_STATEMENT_DELIMITER);
1070
	int index;
1071
	int index;
Lines 1077-1118 Link Here
1077
		int instanceOfIndex = lastIndexOfElement(K_BETWEEN_INSTANCEOF_AND_RPAREN);
1078
		int instanceOfIndex = lastIndexOfElement(K_BETWEEN_INSTANCEOF_AND_RPAREN);
1078
		index = blockIndex != -1 && instanceOfIndex < blockIndex ? blockIndex : instanceOfIndex;
1079
		index = blockIndex != -1 && instanceOfIndex < blockIndex ? blockIndex : instanceOfIndex;
1079
	}
1080
	}
1080
	if (index != -1 && this.elementInfoStack[index] == IF && this.elementObjectInfoStack[index] != null) {
1081
	while (index >= 0) {
1081
		Expression condition = (Expression)this.elementObjectInfoStack[index];
1082
		// Try to find an enclosing if statement even if one is not found immediately preceding the completion node.
1082
1083
		if (index != -1 && this.elementInfoStack[index] == IF && this.elementObjectInfoStack[index] != null) {
1083
		// If currentElement is a RecoveredLocalVariable then it can be contained in the if statement
1084
			Expression condition = (Expression)this.elementObjectInfoStack[index];
1084
		if (this.currentElement instanceof RecoveredLocalVariable &&
1085
	
1085
				this.currentElement.parent instanceof RecoveredBlock) {
1086
			// If currentElement is a RecoveredLocalVariable then it can be contained in the if statement
1086
			RecoveredLocalVariable recoveredLocalVariable = (RecoveredLocalVariable) this.currentElement;
1087
			if (this.currentElement instanceof RecoveredLocalVariable &&
1087
			if (recoveredLocalVariable.localDeclaration.initialization == null &&
1088
					this.currentElement.parent instanceof RecoveredBlock) {
1088
					statement instanceof Expression &&
1089
				RecoveredLocalVariable recoveredLocalVariable = (RecoveredLocalVariable) this.currentElement;
1089
					condition.sourceStart < recoveredLocalVariable.localDeclaration.sourceStart) {
1090
				if (recoveredLocalVariable.localDeclaration.initialization == null &&
1090
				this.currentElement.add(statement, 0);
1091
						statement instanceof Expression &&
1091
1092
						condition.sourceStart < recoveredLocalVariable.localDeclaration.sourceStart) {
1092
				statement = recoveredLocalVariable.updatedStatement(0, new HashSet());
1093
					this.currentElement.add(statement, 0);
1093
1094
	
1094
				// RecoveredLocalVariable must be removed from its parent because the IfStatement will be added instead
1095
					statement = recoveredLocalVariable.updatedStatement(0, new HashSet());
1095
				RecoveredBlock recoveredBlock =  (RecoveredBlock) recoveredLocalVariable.parent;
1096
	
1096
				recoveredBlock.statements[--recoveredBlock.statementCount] = null;
1097
					// RecoveredLocalVariable must be removed from its parent because the IfStatement will be added instead
1097
1098
					RecoveredBlock recoveredBlock =  (RecoveredBlock) recoveredLocalVariable.parent;
1098
				this.currentElement = recoveredBlock;
1099
					recoveredBlock.statements[--recoveredBlock.statementCount] = null;
1099
1100
	
1101
					this.currentElement = recoveredBlock;
1102
	
1103
				}
1100
			}
1104
			}
1105
			if (statement instanceof AND_AND_Expression && this.assistNode instanceof Statement) {
1106
				statement = (Statement) this.assistNode;
1107
			}
1108
			ifStatement =
1109
				new IfStatement(
1110
						condition,
1111
						statement,
1112
						condition.sourceStart,
1113
						statement.sourceEnd);
1114
			index--;
1115
			break;
1101
		}
1116
		}
1102
		if (statement instanceof AND_AND_Expression && this.assistNode instanceof Statement) {
1117
		index--;
1103
			statement = (Statement) this.assistNode;
1118
	}
1119
	if (ifStatement == null) {
1120
		return statement;
1121
	}
1122
	// collect all if statements with instanceof expressions that enclose the completion node
1123
	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=304006
1124
	while (index >= 0) {
1125
		if (this.elementInfoStack[index] == IF && this.elementObjectInfoStack[index] instanceof InstanceOfExpression) {
1126
			InstanceOfExpression condition = (InstanceOfExpression)this.elementObjectInfoStack[index];
1127
			ifStatement =
1128
				new IfStatement(
1129
						condition,
1130
						ifStatement,
1131
						condition.sourceStart,
1132
						ifStatement.sourceEnd);
1104
		}
1133
		}
1105
		IfStatement ifStatement =
1134
		index--;
1106
			new IfStatement(
1107
					condition,
1108
					statement,
1109
					condition.sourceStart,
1110
					statement.sourceEnd);
1111
		this.enclosingNode = ifStatement;
1112
		return ifStatement;
1113
	}
1135
	}
1114
1136
	this.enclosingNode = ifStatement;
1115
	return statement;
1137
	return ifStatement;
1116
}
1138
}
1117
private void buildMoreGenericsCompletionContext(ASTNode node, boolean consumeTypeArguments) {
1139
private void buildMoreGenericsCompletionContext(ASTNode node, boolean consumeTypeArguments) {
1118
	int kind = topKnownElementKind(COMPLETION_OR_ASSIST_PARSER);
1140
	int kind = topKnownElementKind(COMPLETION_OR_ASSIST_PARSER);
Lines 4840-4843 Link Here
4840
		return field;
4862
		return field;
4841
	}
4863
	}
4842
}
4864
}
4865
4866
/*
4867
 * To find out if the given stack has an instanceof expression
4868
 * at the given startIndex or at one prior to that
4869
 */
4870
private boolean stackHasInstanceOfExpression(Object[] stackToSearch, int startIndex) {
4871
	int indexInstanceOf = startIndex;
4872
	while (indexInstanceOf >= 0) {
4873
		if (stackToSearch[indexInstanceOf] instanceof InstanceOfExpression) {
4874
			return true;
4875
		}
4876
		indexInstanceOf--;
4877
	}
4878
	return false;
4879
}
4843
}
4880
}
(-)src/org/eclipse/jdt/core/tests/model/CompletionTests.java (-4 / +241 lines)
Lines 2390-2397 Link Here
2390
	int relevance1 = R_DEFAULT + R_RESOLVED + R_INTERESTING + R_CASE + R_NON_STATIC + R_NON_RESTRICTED;
2390
	int relevance1 = R_DEFAULT + R_RESOLVED + R_INTERESTING + R_CASE + R_NON_STATIC + R_NON_RESTRICTED;
2391
	int start1 = str.lastIndexOf("equal") + "".length();
2391
	int start1 = str.lastIndexOf("equal") + "".length();
2392
	int end1 = start1 + "equal".length();
2392
	int end1 = start1 + "equal".length();
2393
	int start2 = str.lastIndexOf("a.equal");
2394
	int end2 = start2 + "a.equal".length();
2395
	int start3 = str.lastIndexOf("a.");
2396
	int end3 = start3 + "a".length();
2393
	assertResults(
2397
	assertResults(
2394
			"equals[METHOD_REF]{equals(), Ljava.lang.Object;, (Ljava.lang.Object;)Z, equals, (obj), replace["+start1+", "+end1+"], token["+start1+", "+end1+"], " + (relevance1) + "}",
2398
			"equals[METHOD_REF]{equals(), Ljava.lang.Object;, (Ljava.lang.Object;)Z, equals, (obj), replace["+start1+", "+end1+"], token["+start1+", "+end1+"], " + (relevance1) + "}\n" +
2399
			"equalsFoo[METHOD_REF_WITH_CASTED_RECEIVER]{((CompletionAfterInstanceOf)a).equalsFoo(), Ltest.CompletionAfterInstanceOf;, ()V, Ltest.CompletionAfterInstanceOf;, equalsFoo, null, replace["+start2+", "+end2+"], token["+start1+", "+end1+"], receiver["+start3+", "+end3+"], " + (relevance1) + "}",
2395
			requestor.getResults());
2400
			requestor.getResults());
2396
}
2401
}
2397
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=193909
2402
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=193909
Lines 2525-2532 Link Here
2525
	int relevance1 = R_DEFAULT + R_RESOLVED + R_INTERESTING + R_CASE + R_NON_STATIC + R_NON_RESTRICTED;
2530
	int relevance1 = R_DEFAULT + R_RESOLVED + R_INTERESTING + R_CASE + R_NON_STATIC + R_NON_RESTRICTED;
2526
	int start1 = str.lastIndexOf("equal") + "".length();
2531
	int start1 = str.lastIndexOf("equal") + "".length();
2527
	int end1 = start1 + "equal".length();
2532
	int end1 = start1 + "equal".length();
2533
	int start2 = str.lastIndexOf("a.equal");
2534
	int end2 = start2 + "a.equal".length();
2535
	int start3 = str.lastIndexOf("a.");
2536
	int end3 = start3 + "a".length();
2528
	assertResults(
2537
	assertResults(
2529
			"equals[METHOD_REF]{equals(), Ljava.lang.Object;, (Ljava.lang.Object;)Z, equals, (obj), replace["+start1+", "+end1+"], token["+start1+", "+end1+"], " + (relevance1) + "}",
2538
			"equals[METHOD_REF]{equals(), Ljava.lang.Object;, (Ljava.lang.Object;)Z, equals, (obj), replace["+start1+", "+end1+"], token["+start1+", "+end1+"], " + (relevance1) + "}\n" +
2539
			"equalsFoo[METHOD_REF_WITH_CASTED_RECEIVER]{((CompletionAfterInstanceOf)a).equalsFoo(), Ltest.CompletionAfterInstanceOf;, ()V, Ltest.CompletionAfterInstanceOf;, equalsFoo, null, replace["+start2+", "+end2+"], token["+start1+", "+end1+"], receiver["+start3+", "+end3+"], " + (relevance1) + "}",
2530
			requestor.getResults());
2540
			requestor.getResults());
2531
}
2541
}
2532
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=193909
2542
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=193909
Lines 2555-2562 Link Here
2555
	int relevance1 = R_DEFAULT + R_RESOLVED + R_INTERESTING + R_CASE + R_NON_STATIC + R_NON_RESTRICTED;
2565
	int relevance1 = R_DEFAULT + R_RESOLVED + R_INTERESTING + R_CASE + R_NON_STATIC + R_NON_RESTRICTED;
2556
	int start1 = str.lastIndexOf("equal") + "".length();
2566
	int start1 = str.lastIndexOf("equal") + "".length();
2557
	int end1 = start1 + "equal".length();
2567
	int end1 = start1 + "equal".length();
2568
	int start2 = str.lastIndexOf("a.equal");
2569
	int end2 = start2 + "a.equal".length();
2570
	int start3 = str.lastIndexOf("a.");
2571
	int end3 = start3 + "a".length();
2558
	assertResults(
2572
	assertResults(
2559
			"equals[METHOD_REF]{equals(), Ljava.lang.Object;, (Ljava.lang.Object;)Z, equals, (obj), replace["+start1+", "+end1+"], token["+start1+", "+end1+"], " + (relevance1) + "}",
2573
			"equals[METHOD_REF]{equals(), Ljava.lang.Object;, (Ljava.lang.Object;)Z, equals, (obj), replace["+start1+", "+end1+"], token["+start1+", "+end1+"], " + (relevance1) + "}\n" +
2574
			"equalsFoo[METHOD_REF_WITH_CASTED_RECEIVER]{((CompletionAfterInstanceOf)a).equalsFoo(), Ltest.CompletionAfterInstanceOf;, ()V, Ltest.CompletionAfterInstanceOf;, equalsFoo, null, replace["+start2+", "+end2+"], token["+start1+", "+end1+"], receiver["+start3+", "+end3+"], " + (relevance1) + "}",
2560
			requestor.getResults());
2575
			requestor.getResults());
2561
}
2576
}
2562
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=193909
2577
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=193909
Lines 2587-2594 Link Here
2587
	int relevance1 = R_DEFAULT + R_RESOLVED + R_INTERESTING + R_CASE + R_NON_STATIC + R_NON_RESTRICTED;
2602
	int relevance1 = R_DEFAULT + R_RESOLVED + R_INTERESTING + R_CASE + R_NON_STATIC + R_NON_RESTRICTED;
2588
	int start1 = str.lastIndexOf("equal") + "".length();
2603
	int start1 = str.lastIndexOf("equal") + "".length();
2589
	int end1 = start1 + "equal".length();
2604
	int end1 = start1 + "equal".length();
2605
	int start2 = str.lastIndexOf("a.equal");
2606
	int end2 = start2 + "a.equal".length();
2607
	int start3 = str.lastIndexOf("a.");
2608
	int end3 = start3 + "a".length();
2590
	assertResults(
2609
	assertResults(
2591
			"equals[METHOD_REF]{equals(), Ljava.lang.Object;, (Ljava.lang.Object;)Z, equals, (obj), replace["+start1+", "+end1+"], token["+start1+", "+end1+"], " + (relevance1) + "}",
2610
			"equals[METHOD_REF]{equals(), Ljava.lang.Object;, (Ljava.lang.Object;)Z, equals, (obj), replace["+start1+", "+end1+"], token["+start1+", "+end1+"], " + (relevance1) + "}\n" +
2611
			"equalsFoo[METHOD_REF_WITH_CASTED_RECEIVER]{((CompletionAfterInstanceOf)a).equalsFoo(), Ltest.CompletionAfterInstanceOf;, ()V, Ltest.CompletionAfterInstanceOf;, equalsFoo, null, replace["+start2+", "+end2+"], token["+start1+", "+end1+"], receiver["+start3+", "+end3+"], " + (relevance1) + "}",
2592
			requestor.getResults());
2612
			requestor.getResults());
2593
}
2613
}
2594
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=193909
2614
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=193909
Lines 21454-21457 Link Here
21454
			"AN_INT_VALUE2[FIELD_REF]{AN_INT_VALUE2, Ltest.CompletionAfterCase2;, I, AN_INT_VALUE2, null, " + (R_DEFAULT + R_INTERESTING + R_CASE + R_UNQUALIFIED + R_NON_RESTRICTED + R_EXACT_EXPECTED_TYPE + R_FINAL) + "}",
21474
			"AN_INT_VALUE2[FIELD_REF]{AN_INT_VALUE2, Ltest.CompletionAfterCase2;, I, AN_INT_VALUE2, null, " + (R_DEFAULT + R_INTERESTING + R_CASE + R_UNQUALIFIED + R_NON_RESTRICTED + R_EXACT_EXPECTED_TYPE + R_FINAL) + "}",
21455
			requestor.getResults());
21475
			requestor.getResults());
21456
}
21476
}
21477
21478
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=304006
21479
// To verify that autocast works correctly after an instanceof even if there are other expressions in between
21480
// instanceof and the place where code assist is requested.
21481
public void testBug304006a() throws JavaModelException {
21482
	this.workingCopies = new ICompilationUnit[1];
21483
	this.workingCopies[0] = getWorkingCopy(
21484
		"/Completion/src/test/CompletionAfterInstanceOf.java",
21485
		"package test;\n" +
21486
		"public class CompletionAfterInstanceOf {\n" +
21487
		"	public int foo() { return 1; }\n" +
21488
		"	public int returnZero(){ return 0;}\n" +
21489
		"	void bar(Object a){\n" +
21490
		"       int i = 1;\n" +
21491
		"		if (a instanceof CompletionAfterInstanceOf) {" +
21492
		"			if (i == 1)\n" +
21493
		"				i =  a.r\n" +
21494
		"	}\n" +
21495
		"}\n");
21496
21497
	CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true, false, true, true, true, true);
21498
	requestor.allowAllRequiredProposals();
21499
	String str = this.workingCopies[0].getSource();
21500
	String completeBehind = "a.r";
21501
	int cursorLocation = str.lastIndexOf(completeBehind) + completeBehind.length();
21502
	this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner);
21503
21504
	int relevance1 = R_DEFAULT + R_RESOLVED + R_INTERESTING + R_CASE + R_NON_STATIC + R_NON_RESTRICTED + R_EXACT_EXPECTED_TYPE;
21505
	int start1 = str.lastIndexOf("r") + "".length();
21506
	int end1 = start1 + "r".length();
21507
	int start2 = str.lastIndexOf("a.r");
21508
	int end2 = start2 + "a.r".length();
21509
	int start3 = str.lastIndexOf("a.");
21510
	int end3 = start3 + "a".length();
21511
	
21512
	assertResults(
21513
			"expectedTypesSignatures={I}\n" +
21514
			"expectedTypesKeys={I}",
21515
			requestor.getContext());
21516
	assertResults(
21517
			"returnZero[METHOD_REF_WITH_CASTED_RECEIVER]{((CompletionAfterInstanceOf)a).returnZero(), Ltest.CompletionAfterInstanceOf;, ()I, Ltest.CompletionAfterInstanceOf;, returnZero, null, replace["+start2+", "+end2+"], token["+start1+", "+end1+"], receiver["+start3+", "+end3+"], " + (relevance1) + "}",
21518
			requestor.getResults());
21519
}
21520
21521
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=304006
21522
// To verify that autocast works correctly after an instanceof even if there are other expressions in between
21523
// instanceof and the place where code assist is requested.
21524
public void testBug304006b() throws JavaModelException {
21525
	this.workingCopies = new ICompilationUnit[1];
21526
	this.workingCopies[0] = getWorkingCopy(
21527
		"/Completion/src/test/CompletionAfterInstanceOf.java",
21528
		"package test;\n" +
21529
		"public class CompletionAfterInstanceOf {\n" +
21530
		"	public int foo() { return 1; }\n" +
21531
		"	public int returnZero(){ return 0;}\n" +
21532
		"	void bar(Object a){\n" +
21533
		"       int i = 1;\n" +
21534
		"		if (a instanceof CompletionAfterInstanceOf) {" +
21535
		"			if (i == 1)\n" +
21536
		"				a.r\n" +
21537
		"	}\n" +
21538
		"}\n");
21539
21540
	CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true, false, true, true, true, true);
21541
	requestor.allowAllRequiredProposals();
21542
	String str = this.workingCopies[0].getSource();
21543
	String completeBehind = "a.r";
21544
	int cursorLocation = str.lastIndexOf(completeBehind) + completeBehind.length();
21545
	this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner);
21546
21547
	int relevance1 = R_DEFAULT + R_RESOLVED + R_INTERESTING + R_CASE + R_NON_STATIC + R_NON_RESTRICTED;
21548
	int start1 = str.lastIndexOf("r") + "".length();
21549
	int end1 = start1 + "r".length();
21550
	int start2 = str.lastIndexOf("a.r");
21551
	int end2 = start2 + "a.r".length();
21552
	int start3 = str.lastIndexOf("a.");
21553
	int end3 = start3 + "a".length();
21554
	
21555
	assertResults(
21556
			"returnZero[METHOD_REF_WITH_CASTED_RECEIVER]{((CompletionAfterInstanceOf)a).returnZero(), Ltest.CompletionAfterInstanceOf;, ()I, Ltest.CompletionAfterInstanceOf;, returnZero, null, replace["+start2+", "+end2+"], token["+start1+", "+end1+"], receiver["+start3+", "+end3+"], " + (relevance1) + "}",
21557
			requestor.getResults());
21558
}
21559
21560
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=304006
21561
// To verify that autocast works correctly even when the completion node
21562
// is preceeded by a number of unrelated instanceof expressions
21563
public void testBug304006c() throws JavaModelException {
21564
	this.workingCopies = new ICompilationUnit[1];
21565
	this.workingCopies[0] = getWorkingCopy(
21566
		"/Completion/src/test/CompletionAfterInstanceOf.java",
21567
		"package test;\n" +
21568
		"public class CompletionAfterInstanceOf {\n" +
21569
		"	public int foo() { return 1; }\n" +
21570
		"	public int returnZero(){ return 0;}\n" +
21571
		"	void bar(Object abc, Object xyz){\n" +
21572
		"       int i = 1, j;\n" +
21573
		"		if(i == 1)\n" +
21574
		"			if (abc instanceof CompletionAfterInstanceOf)\n" +
21575
		"				if(xyz instanceof CompletionAfterInstanceOf){\n" +
21576
		"					j = 1;\n" +
21577
		"					if(j == 1)\n" +
21578
		"						i = abc.r \n" +
21579
		"				}\n" +
21580
		"	}\n" +
21581
		"}\n");
21582
21583
	CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true, false, true, true, true, true);
21584
	requestor.allowAllRequiredProposals();
21585
	String str = this.workingCopies[0].getSource();
21586
	String completeBehind = "abc.r";
21587
	int cursorLocation = str.lastIndexOf(completeBehind) + completeBehind.length();
21588
	this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner);
21589
21590
	int relevance1 = R_DEFAULT + R_RESOLVED + R_INTERESTING + R_CASE + R_NON_STATIC + R_NON_RESTRICTED + R_EXACT_EXPECTED_TYPE;
21591
	int start1 = str.lastIndexOf("r") + "".length();
21592
	int end1 = start1 + "r".length();
21593
	int start2 = str.lastIndexOf("abc.r");
21594
	int end2 = start2 + "abc.r".length();
21595
	int start3 = str.lastIndexOf("abc.");
21596
	int end3 = start3 + "abc".length();
21597
	
21598
	assertResults(
21599
			"expectedTypesSignatures={I}\n" +
21600
			"expectedTypesKeys={I}",
21601
			requestor.getContext());
21602
	assertResults(
21603
			"returnZero[METHOD_REF_WITH_CASTED_RECEIVER]{((CompletionAfterInstanceOf)abc).returnZero(), Ltest.CompletionAfterInstanceOf;, ()I, Ltest.CompletionAfterInstanceOf;, returnZero, null, replace["+start2+", "+end2+"], token["+start1+", "+end1+"], receiver["+start3+", "+end3+"], " + (relevance1) + "}",
21604
			requestor.getResults());
21605
}
21606
21607
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=304006
21608
// To verify that autocast works correctly even when the completion node is preceeded by a number 
21609
// of unrelated instanceof expressions. This case has errors in compilation unit. 
21610
public void testBug304006d() throws JavaModelException {
21611
	this.workingCopies = new ICompilationUnit[1];
21612
	this.workingCopies[0] = getWorkingCopy(
21613
		"/Completion/src/test/CompletionAfterInstanceOf.java",
21614
		"package test;\n" +
21615
		"public class CompletionAfterInstanceOf {\n" +
21616
		"	public int foo() { return 1; }\n" +
21617
		"	public int returnZero(){ return 0;}\n" +
21618
		"	void bar(Object abc, Object xyz){\n" +
21619
		"       int i = 1, j;\n" +
21620
		"		j = \n" +
21621
		"		if(i == 1)\n" +
21622
		"			if (abc instanceof CompletionAfterInstanceOf)\n" +
21623
		"				if(xyz instanceof CompletionAfterInstanceOf){\n" +
21624
		"					j = 1;\n" +
21625
		"					if(j == 1)\n" +
21626
		"						xyz.r \n" +
21627
		"				}\n" +
21628
		"	}\n" +
21629
		"}\n");
21630
21631
	CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true, false, true, true, true, true);
21632
	requestor.allowAllRequiredProposals();
21633
	String str = this.workingCopies[0].getSource();
21634
	String completeBehind = "xyz.r";
21635
	int cursorLocation = str.lastIndexOf(completeBehind) + completeBehind.length();
21636
	this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner);
21637
21638
	int relevance1 = R_DEFAULT + R_RESOLVED + R_INTERESTING + R_CASE + R_NON_STATIC + R_NON_RESTRICTED;
21639
	int start1 = str.lastIndexOf("r") + "".length();
21640
	int end1 = start1 + "r".length();
21641
	int start2 = str.lastIndexOf("xyz.r");
21642
	int end2 = start2 + "xyz.r".length();
21643
	int start3 = str.lastIndexOf("xyz.");
21644
	int end3 = start3 + "xyz".length();
21645
	
21646
	assertResults(
21647
			"returnZero[METHOD_REF_WITH_CASTED_RECEIVER]{((CompletionAfterInstanceOf)xyz).returnZero(), Ltest.CompletionAfterInstanceOf;, ()I, Ltest.CompletionAfterInstanceOf;, returnZero, null, replace["+start2+", "+end2+"], token["+start1+", "+end1+"], receiver["+start3+", "+end3+"], " + (relevance1) + "}",
21648
			requestor.getResults());
21649
}
21650
21651
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=304006
21652
// To verify that autocast works correctly after an instanceof even if there are other expressions in between
21653
// instanceof and the place where code assist is requested. This is a case with errors in the compilation unit.
21654
public void testBug304006e() throws JavaModelException {
21655
	this.workingCopies = new ICompilationUnit[1];
21656
	this.workingCopies[0] = getWorkingCopy(
21657
		"/Completion/src/test/CompletionAfterInstanceOf.java",
21658
		"package test;\n" +
21659
		"public class CompletionAfterInstanceOf {\n" +
21660
		"	public int foo() { return 1; }\n" +
21661
		"	public int returnZero(){ return 0;}\n" +
21662
		"	void bar(Object a){\n" +
21663
		"       int i = 1;\n" +
21664
		"       int i = \n" +
21665
		"		if (a instanceof CompletionAfterInstanceOf) {" +
21666
		"			if (i == 1)\n" +
21667
		"				i =  a.r\n" +
21668
		"	}\n" +
21669
		"}\n");
21670
21671
	CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true, false, true, true, true, true);
21672
	requestor.allowAllRequiredProposals();
21673
	String str = this.workingCopies[0].getSource();
21674
	String completeBehind = "a.r";
21675
	int cursorLocation = str.lastIndexOf(completeBehind) + completeBehind.length();
21676
	this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner);
21677
21678
	int relevance1 = R_DEFAULT + R_RESOLVED + R_INTERESTING + R_CASE + R_NON_STATIC + R_NON_RESTRICTED + R_EXACT_EXPECTED_TYPE;
21679
	int start1 = str.lastIndexOf("r") + "".length();
21680
	int end1 = start1 + "r".length();
21681
	int start2 = str.lastIndexOf("a.r");
21682
	int end2 = start2 + "a.r".length();
21683
	int start3 = str.lastIndexOf("a.");
21684
	int end3 = start3 + "a".length();
21685
	
21686
	assertResults(
21687
			"expectedTypesSignatures={I}\n" +
21688
			"expectedTypesKeys={I}",
21689
			requestor.getContext());
21690
	assertResults(
21691
			"returnZero[METHOD_REF_WITH_CASTED_RECEIVER]{((CompletionAfterInstanceOf)a).returnZero(), Ltest.CompletionAfterInstanceOf;, ()I, Ltest.CompletionAfterInstanceOf;, returnZero, null, replace["+start2+", "+end2+"], token["+start1+", "+end1+"], receiver["+start3+", "+end3+"], " + (relevance1) + "}",
21692
			requestor.getResults());
21693
}
21457
}
21694
}

Return to bug 304006