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

Collapse All | Expand All

(-)src/org/eclipse/jdt/core/tests/dom/BatchASTCreationTests.java (+21 lines)
Lines 2070-2073 Link Here
2070
			}, 
2070
			}, 
2071
			"Lp1/X~Y;");
2071
			"Lp1/X~Y;");
2072
	}
2072
	}
2073
	
2074
	/* 
2075
	 * Ensures that a duplicate local variable binding can be created using its key in ASTRequestor#createBindings
2076
	 * (regression test for bug 149590 [model] bindings for duplicate local variables share same key)
2077
	 */
2078
	public void test084() throws CoreException {
2079
		assertBindingCreated(
2080
			new String[] {
2081
				"/P/p1/X.java",
2082
				"package p1;\n" +
2083
				"public class X {\n" +
2084
				"  void foo() {\n" +
2085
				"    int i;\n" +
2086
				"    int i;\n" +
2087
				"  }\n" +
2088
				"}",
2089
			},
2090
			"Lp1/X;.foo()V#i#1");
2091
	}
2092
2093
2073
}
2094
}
(-)src/org/eclipse/jdt/core/tests/dom/ASTConverterTestAST3_2.java (-17 / +13 lines)
Lines 8305-8311 Link Here
8305
8305
8306
	/**
8306
	/**
8307
	 * http://dev.eclipse.org/bugs/show_bug.cgi?id=144858
8307
	 * http://dev.eclipse.org/bugs/show_bug.cgi?id=144858
8308
	 * TODO (frederic) check that keys are different (PR 149590)
8309
	 */
8308
	 */
8310
	public void test0660() throws JavaModelException {
8309
	public void test0660() throws JavaModelException {
8311
		ICompilationUnit workingCopy = null;
8310
		ICompilationUnit workingCopy = null;
Lines 8336-8344 Link Here
8336
			assertNotNull("No binding", variableBinding);
8335
			assertNotNull("No binding", variableBinding);
8337
			assertEquals("Wrong name", "x", variableBinding.getName());
8336
			assertEquals("Wrong name", "x", variableBinding.getName());
8338
			// (PR 149590)
8337
			// (PR 149590)
8339
			// String key = variableBinding.getKey();
8338
			String key = variableBinding.getKey();
8340
8339
8341
			node = getASTNode(unit, 0, 0, 0);
8340
			node = getASTNode(unit, 0, 0, 1);
8342
			assertEquals("Not a variable declaration statement", ASTNode.VARIABLE_DECLARATION_STATEMENT, node.getNodeType());
8341
			assertEquals("Not a variable declaration statement", ASTNode.VARIABLE_DECLARATION_STATEMENT, node.getNodeType());
8343
			statement = (VariableDeclarationStatement) node;
8342
			statement = (VariableDeclarationStatement) node;
8344
			fragments = statement.fragments();
8343
			fragments = statement.fragments();
Lines 8348-8355 Link Here
8348
			assertNotNull("No binding", variableBinding);
8347
			assertNotNull("No binding", variableBinding);
8349
			assertEquals("Wrong name", "x", variableBinding.getName());
8348
			assertEquals("Wrong name", "x", variableBinding.getName());
8350
			// (PR 149590)
8349
			// (PR 149590)
8351
			// String key2 = variableBinding.getKey();
8350
			String key2 = variableBinding.getKey();
8352
			//assertFalse("Keys should not be equals", key2.equals(key));
8351
			assertFalse("Keys should not be equals", key2.equals(key));
8353
		} finally {
8352
		} finally {
8354
			if (workingCopy != null)
8353
			if (workingCopy != null)
8355
				workingCopy.discardWorkingCopy();
8354
				workingCopy.discardWorkingCopy();
Lines 8358-8364 Link Here
8358
8357
8359
	/**
8358
	/**
8360
	 * http://dev.eclipse.org/bugs/show_bug.cgi?id=144858
8359
	 * http://dev.eclipse.org/bugs/show_bug.cgi?id=144858
8361
	 * TODO (frederic) check that keys are different (PR 149590)
8362
	 */
8360
	 */
8363
	public void test0661() throws JavaModelException {
8361
	public void test0661() throws JavaModelException {
8364
		ICompilationUnit workingCopy = null;
8362
		ICompilationUnit workingCopy = null;
Lines 8392-8398 Link Here
8392
			assertNotNull("No binding", variableBinding);
8390
			assertNotNull("No binding", variableBinding);
8393
			assertEquals("Wrong name", "x", variableBinding.getName());
8391
			assertEquals("Wrong name", "x", variableBinding.getName());
8394
			// (PR 149590)
8392
			// (PR 149590)
8395
			// String key = variableBinding.getKey();
8393
			String key = variableBinding.getKey();
8396
8394
8397
			node = getASTNode(unit, 0, 0, 1);
8395
			node = getASTNode(unit, 0, 0, 1);
8398
			assertEquals("Not a try statement", ASTNode.TRY_STATEMENT, node.getNodeType());
8396
			assertEquals("Not a try statement", ASTNode.TRY_STATEMENT, node.getNodeType());
Lines 8405-8412 Link Here
8405
			assertNotNull("No binding", variableBinding);
8403
			assertNotNull("No binding", variableBinding);
8406
			assertEquals("Wrong name", "x", variableBinding.getName());
8404
			assertEquals("Wrong name", "x", variableBinding.getName());
8407
			// (PR 149590)
8405
			// (PR 149590)
8408
			// String key2 = variableBinding.getKey();
8406
			String key2 = variableBinding.getKey();
8409
			//assertFalse("Keys should not be equals", key2.equals(key));
8407
			assertFalse("Keys should not be equals", key2.equals(key));
8410
		} finally {
8408
		} finally {
8411
			if (workingCopy != null)
8409
			if (workingCopy != null)
8412
				workingCopy.discardWorkingCopy();
8410
				workingCopy.discardWorkingCopy();
Lines 8415-8421 Link Here
8415
8413
8416
	/**
8414
	/**
8417
	 * http://dev.eclipse.org/bugs/show_bug.cgi?id=144858
8415
	 * http://dev.eclipse.org/bugs/show_bug.cgi?id=144858
8418
	 * TODO (frederic) check that keys are different (PR 149590)
8419
	 */
8416
	 */
8420
	public void test0662() throws JavaModelException {
8417
	public void test0662() throws JavaModelException {
8421
		ICompilationUnit workingCopy = null;
8418
		ICompilationUnit workingCopy = null;
Lines 8451-8457 Link Here
8451
			assertNotNull("No binding", variableBinding);
8448
			assertNotNull("No binding", variableBinding);
8452
			assertEquals("Wrong name", "x", variableBinding.getName());
8449
			assertEquals("Wrong name", "x", variableBinding.getName());
8453
			// (PR 149590)
8450
			// (PR 149590)
8454
			// String key = variableBinding.getKey();
8451
			String key = variableBinding.getKey();
8455
8452
8456
			node = getASTNode(unit, 0, 0, 1);
8453
			node = getASTNode(unit, 0, 0, 1);
8457
			assertEquals("Not an if statement", ASTNode.IF_STATEMENT, node.getNodeType());
8454
			assertEquals("Not an if statement", ASTNode.IF_STATEMENT, node.getNodeType());
Lines 8470-8477 Link Here
8470
			assertEquals("Wrong name", "x", variableBinding.getName());
8467
			assertEquals("Wrong name", "x", variableBinding.getName());
8471
8468
8472
			// (PR 149590)
8469
			// (PR 149590)
8473
			// String key2 = variableBinding.getKey();
8470
			String key2 = variableBinding.getKey();
8474
			//assertFalse("Keys should not be equals", key2.equals(key));
8471
			assertFalse("Keys should not be equals", key2.equals(key));
8475
		} finally {
8472
		} finally {
8476
			if (workingCopy != null)
8473
			if (workingCopy != null)
8477
				workingCopy.discardWorkingCopy();
8474
				workingCopy.discardWorkingCopy();
Lines 8480-8486 Link Here
8480
8477
8481
	/**
8478
	/**
8482
	 * http://dev.eclipse.org/bugs/show_bug.cgi?id=144858
8479
	 * http://dev.eclipse.org/bugs/show_bug.cgi?id=144858
8483
	 * TODO (frederic) check that keys are different (PR 149590)
8484
	 */
8480
	 */
8485
	public void test0663() throws JavaModelException {
8481
	public void test0663() throws JavaModelException {
8486
		ICompilationUnit workingCopy = null;
8482
		ICompilationUnit workingCopy = null;
Lines 8519-8525 Link Here
8519
			assertNotNull("No binding", variableBinding);
8515
			assertNotNull("No binding", variableBinding);
8520
			assertEquals("Wrong name", "i", variableBinding.getName());
8516
			assertEquals("Wrong name", "i", variableBinding.getName());
8521
			// (PR 149590)
8517
			// (PR 149590)
8522
			// String key = variableBinding.getKey();
8518
			String key = variableBinding.getKey();
8523
8519
8524
			Block block = (Block) statement.getBody();
8520
			Block block = (Block) statement.getBody();
8525
			List statements = block.statements();
8521
			List statements = block.statements();
Lines 8540-8547 Link Here
8540
			assertEquals("Wrong name", "i", variableBinding.getName());
8536
			assertEquals("Wrong name", "i", variableBinding.getName());
8541
8537
8542
			// (PR 149590)
8538
			// (PR 149590)
8543
			// String key2 = variableBinding.getKey();
8539
			String key2 = variableBinding.getKey();
8544
			//assertFalse("Keys should not be equals", key2.equals(key));
8540
			assertFalse("Keys should not be equals", key2.equals(key));
8545
		} finally {
8541
		} finally {
8546
			if (workingCopy != null)
8542
			if (workingCopy != null)
8547
				workingCopy.discardWorkingCopy();
8543
				workingCopy.discardWorkingCopy();
(-)compiler/org/eclipse/jdt/internal/compiler/lookup/LocalVariableBinding.java (-2 / +22 lines)
Lines 10-15 Link Here
10
 *******************************************************************************/
10
 *******************************************************************************/
11
package org.eclipse.jdt.internal.compiler.lookup;
11
package org.eclipse.jdt.internal.compiler.lookup;
12
12
13
import org.eclipse.jdt.core.compiler.CharOperation;
13
import org.eclipse.jdt.internal.compiler.ast.ASTNode;
14
import org.eclipse.jdt.internal.compiler.ast.ASTNode;
14
import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration;
15
import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration;
15
import org.eclipse.jdt.internal.compiler.ast.Annotation;
16
import org.eclipse.jdt.internal.compiler.ast.Annotation;
Lines 57-70 Link Here
57
	}
58
	}
58
	
59
	
59
	/*
60
	/*
60
	 * declaringUniqueKey # scopeIndex / varName
61
	 * declaringUniqueKey # scopeIndex(0-based) # varName [# occurrenceCount(0-based)]
61
	 * p.X { void foo() { int local; } } --> Lp/X;.foo()V#1/local
62
	 * p.X { void foo() { int local; int local;} } --> Lp/X;.foo()V#1#local#1
62
	 */
63
	 */
63
	public char[] computeUniqueKey(boolean isLeaf) {
64
	public char[] computeUniqueKey(boolean isLeaf) {
64
		StringBuffer buffer = new StringBuffer();
65
		StringBuffer buffer = new StringBuffer();
65
		
66
		
66
		// declaring method or type
67
		// declaring method or type
67
		BlockScope scope = this.declaringScope;
68
		BlockScope scope = this.declaringScope;
69
		int occurenceCount = 0;
68
		if (scope != null) {
70
		if (scope != null) {
69
			// the scope can be null. See https://bugs.eclipse.org/bugs/show_bug.cgi?id=185129
71
			// the scope can be null. See https://bugs.eclipse.org/bugs/show_bug.cgi?id=185129
70
			MethodScope methodScope = scope instanceof MethodScope ? (MethodScope) scope : scope.enclosingMethodScope();
72
			MethodScope methodScope = scope instanceof MethodScope ? (MethodScope) scope : scope.enclosingMethodScope();
Lines 83-93 Link Here
83
	
85
	
84
			// scope index
86
			// scope index
85
			getScopeKey(scope, buffer);
87
			getScopeKey(scope, buffer);
88
			
89
			// find number of occurences of a variable with the same name in the scope
90
			LocalVariableBinding[] locals = scope.locals;
91
			for (int i = 0; i < scope.localIndex; i++) { // use linear search assuming the number of locals per scope is low
92
				LocalVariableBinding local = locals[i];
93
				if (CharOperation.equals(this.name, local.name)) {
94
					if (this == local)
95
						break;
96
					occurenceCount++;
97
				}
98
			}
86
		}
99
		}
87
		// variable name
100
		// variable name
88
		buffer.append('#');
101
		buffer.append('#');
89
		buffer.append(this.name);
102
		buffer.append(this.name);
90
		
103
		
104
		// add occurence count to avoid same key for duplicate variables
105
		// (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=149590)
106
		if (occurenceCount > 0) {
107
			buffer.append('#');
108
			buffer.append(occurenceCount);
109
		}
110
		
91
		int length = buffer.length();
111
		int length = buffer.length();
92
		char[] uniqueKey = new char[length];
112
		char[] uniqueKey = new char[length];
93
		buffer.getChars(0, length, uniqueKey, 0);
113
		buffer.getChars(0, length, uniqueKey, 0);
(-)model/org/eclipse/jdt/internal/core/util/BindingKeyParser.java (-2 / +11 lines)
Lines 430-436 Link Here
430
		// default is to do nothing
430
		// default is to do nothing
431
	}
431
	}
432
	
432
	
433
	public void consumeLocalVar(char[] varName) {
433
	public void consumeLocalVar(char[] varName, int occurrenceCount) {
434
		// default is to do nothing
434
		// default is to do nothing
435
	}
435
	}
436
	
436
	
Lines 695-701 Link Here
695
			}
695
			}
696
			parseLocalVariable();
696
			parseLocalVariable();
697
		} else {
697
		} else {
698
		 	consumeLocalVar(varName);
698
			int occurrenceCount = 0;
699
			if (this.scanner.isAtLocalVariableStart()) {
700
			 	if (this.scanner.nextToken() != Scanner.LOCAL_VAR) {
701
			 		malformedKey();
702
					return;
703
			 	}
704
				char[] occurrence = this.scanner.getTokenSource();
705
				occurrenceCount = Integer.parseInt(new String(occurrence));
706
			}
707
		 	consumeLocalVar(varName, occurrenceCount);
699
		}
708
		}
700
 	}
709
 	}
701
	
710
	
(-)model/org/eclipse/jdt/internal/core/util/BindingKeyResolver.java (-2 / +3 lines)
Lines 223-235 Link Here
223
 			}
223
 			}
224
	}
224
	}
225
225
226
	public void consumeLocalVar(char[] varName) {
226
	public void consumeLocalVar(char[] varName, int occurrenceCount) {
227
		if (this.scope == null) {
227
		if (this.scope == null) {
228
			this.scope = this.methodBinding.sourceMethod().scope;
228
			this.scope = this.methodBinding.sourceMethod().scope;
229
		}
229
		}
230
	 	for (int i = 0; i < this.scope.localIndex; i++) {
230
	 	for (int i = 0; i < this.scope.localIndex; i++) {
231
			LocalVariableBinding local = this.scope.locals[i];
231
			LocalVariableBinding local = this.scope.locals[i];
232
			if (CharOperation.equals(varName, local.name)) {
232
			if (CharOperation.equals(local.name, varName)
233
					&& occurrenceCount-- == 0) {
233
				this.methodBinding = null;
234
				this.methodBinding = null;
234
				this.compilerBinding = local;
235
				this.compilerBinding = local;
235
				return;
236
				return;
(-)model/org/eclipse/jdt/internal/core/util/KeyKind.java (-1 / +1 lines)
Lines 53-59 Link Here
53
		this.flags |= F_LOCAL;
53
		this.flags |= F_LOCAL;
54
	}
54
	}
55
55
56
	public void consumeLocalVar(char[] varName) {
56
	public void consumeLocalVar(char[] varName, int occurrenceCount) {
57
		this.flags |= F_LOCAL_VAR;
57
		this.flags |= F_LOCAL_VAR;
58
	}
58
	}
59
59

Return to bug 149590