View | Details | Raw Unified | Return to bug 217287
Collapse All | Expand All

(-)src/org/eclipse/jdt/core/tests/dom/ASTModelBridgeTests.java (+23 lines)
Lines 1393-1398 Link Here
1393
	}
1393
	}
1394
1394
1395
	/*
1395
	/*
1396
	 * Ensures that the IJavaElement of an IBinding representing a local variable in an initializer is correct.
1397
	 * (regression test for https://bugs.eclipse.org/bugs/show_bug.cgi?id=217287 )
1398
	 */
1399
	public void testLocalVariable5() throws JavaModelException {
1400
		ASTNode node = buildAST(
1401
			"public class X {\n" +
1402
			"  {\n" +
1403
			"    int /*start*/local/*end*/;\n" +
1404
			"  }\n" +
1405
			"}"
1406
		);
1407
		IBinding binding = ((VariableDeclaration) node).resolveBinding();
1408
		assertNotNull("No binding", binding);
1409
		IJavaElement element = binding.getJavaElement();
1410
		IJavaElement expected = getLocalVariable(this.workingCopy, "local", "local");
1411
		assertEquals(
1412
			"Unexpected Java element",
1413
			expected,
1414
			element
1415
		);
1416
	}
1417
1418
	/*
1396
	 * Ensures that the IJavaElement of an IBinding representing a member type is correct.
1419
	 * Ensures that the IJavaElement of an IBinding representing a member type is correct.
1397
	 */
1420
	 */
1398
	public void testMemberType() throws JavaModelException {
1421
	public void testMemberType() throws JavaModelException {
(-)model/org/eclipse/jdt/internal/core/util/Util.java (+25 lines)
Lines 1291-1296 Link Here
1291
	}
1291
	}
1292
	
1292
	
1293
	/**
1293
	/**
1294
	 * Returns the IInitializer that contains the given local variable in the given type
1295
	 */
1296
	public static JavaElement getUnresolvedJavaElement(int localSourceStart, int localSourceEnd, JavaElement type) {
1297
		try {
1298
			if (!(type instanceof IType))
1299
				return null;
1300
			IInitializer[] initializers = ((IType) type).getInitializers();
1301
			for (int i = 0; i < initializers.length; i++) {
1302
				IInitializer initializer = initializers[i];
1303
				ISourceRange sourceRange = initializer.getSourceRange();
1304
				if (sourceRange != null) {
1305
					int initializerStart = sourceRange.getOffset();
1306
					int initializerEnd = initializerStart + sourceRange.getLength();
1307
					if (initializerStart <= localSourceStart && localSourceEnd <= initializerEnd) {
1308
						return (JavaElement) initializer;
1309
					}
1310
				}
1311
			}
1312
			return null;
1313
		} catch (JavaModelException e) {
1314
			return null;
1315
		}
1316
	}
1317
	
1318
	/**
1294
	 * Return the java element corresponding to the given compiler binding.
1319
	 * Return the java element corresponding to the given compiler binding.
1295
	 */
1320
	 */
1296
	public static JavaElement getUnresolvedJavaElement(MethodBinding methodBinding, WorkingCopyOwner workingCopyOwner, BindingsToNodesMap bindingsToNodes) {
1321
	public static JavaElement getUnresolvedJavaElement(MethodBinding methodBinding, WorkingCopyOwner workingCopyOwner, BindingsToNodesMap bindingsToNodes) {
(-)dom/org/eclipse/jdt/core/dom/VariableBinding.java (-5 / +29 lines)
Lines 13-20 Link Here
13
13
14
import org.eclipse.jdt.core.IJavaElement;
14
import org.eclipse.jdt.core.IJavaElement;
15
import org.eclipse.jdt.core.util.IModifierConstants;
15
import org.eclipse.jdt.core.util.IModifierConstants;
16
import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
16
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
17
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
17
import org.eclipse.jdt.internal.compiler.impl.Constant;
18
import org.eclipse.jdt.internal.compiler.impl.Constant;
19
import org.eclipse.jdt.internal.compiler.impl.ReferenceContext;
18
import org.eclipse.jdt.internal.compiler.lookup.FieldBinding;
20
import org.eclipse.jdt.internal.compiler.lookup.FieldBinding;
19
import org.eclipse.jdt.internal.compiler.lookup.LocalVariableBinding;
21
import org.eclipse.jdt.internal.compiler.lookup.LocalVariableBinding;
20
import org.eclipse.jdt.internal.compiler.lookup.TagBits;
22
import org.eclipse.jdt.internal.compiler.lookup.TagBits;
Lines 212-221 Link Here
212
			}
214
			}
213
		}
215
		}
214
		// local variable
216
		// local variable
215
		IMethodBinding declaringMethod = getDeclaringMethod();
216
		if (declaringMethod == null) return null;
217
		JavaElement method = (JavaElement) declaringMethod.getJavaElement();
218
		if (method == null) return null;
219
		if (!(this.resolver instanceof DefaultBindingResolver)) return null;
217
		if (!(this.resolver instanceof DefaultBindingResolver)) return null;
220
		VariableDeclaration localVar = (VariableDeclaration) ((DefaultBindingResolver) this.resolver).bindingsToAstNodes.get(this);
218
		VariableDeclaration localVar = (VariableDeclaration) ((DefaultBindingResolver) this.resolver).bindingsToAstNodes.get(this);
221
		if (localVar == null) return null;
219
		if (localVar == null) return null;
Lines 236-243 Link Here
236
			sourceStart = node.getStartPosition();
234
			sourceStart = node.getStartPosition();
237
			sourceLength = node.getLength();
235
			sourceLength = node.getLength();
238
		}
236
		}
237
		int sourceEnd = sourceStart+sourceLength-1;
239
		char[] typeSig = this.binding.type.genericTypeSignature();
238
		char[] typeSig = this.binding.type.genericTypeSignature();
240
		return new LocalVariable(method, localVar.getName().getIdentifier(), sourceStart, sourceStart+sourceLength-1, nameStart, nameStart+nameLength-1, new String(typeSig), ((LocalVariableBinding) this.binding).declaration.annotations);
239
		JavaElement parent = null;
240
		IMethodBinding declaringMethod = getDeclaringMethod();
241
		if (declaringMethod == null) {
242
			ReferenceContext referenceContext = ((LocalVariableBinding) binding).declaringScope.referenceContext();
243
			if (referenceContext instanceof TypeDeclaration){
244
				// Local variable is declared inside an initializer
245
				TypeDeclaration typeDeclaration = (TypeDeclaration) referenceContext;
246
				JavaElement typeHandle = null;
247
				if (this.resolver instanceof DefaultBindingResolver) {
248
					DefaultBindingResolver defaultBindingResolver = (DefaultBindingResolver) this.resolver;
249
					typeHandle = Util.getUnresolvedJavaElement(
250
						typeDeclaration.binding,
251
						defaultBindingResolver.workingCopyOwner,
252
						defaultBindingResolver.getBindingsToNodesMap());
253
				} else {
254
					typeHandle = Util.getUnresolvedJavaElement(typeDeclaration.binding, null, null);
255
				}
256
				parent = Util.getUnresolvedJavaElement(sourceStart, sourceEnd, typeHandle);
257
			} else {
258
				return null;
259
			}
260
		} else {
261
			parent = (JavaElement) declaringMethod.getJavaElement();
262
		}
263
		if (parent == null) return null;
264
		return new LocalVariable(parent, localVar.getName().getIdentifier(), sourceStart, sourceEnd, nameStart, nameStart+nameLength-1, new String(typeSig), ((LocalVariableBinding) this.binding).declaration.annotations);
241
	}
265
	}
242
266
243
	/*
267
	/*
(-)codeassist/org/eclipse/jdt/internal/codeassist/InternalExtendedCompletionContext.java (-27 / +3 lines)
Lines 14-23 Link Here
14
import java.util.Map;
14
import java.util.Map;
15
15
16
import org.eclipse.jdt.core.ICompilationUnit;
16
import org.eclipse.jdt.core.ICompilationUnit;
17
import org.eclipse.jdt.core.IInitializer;
18
import org.eclipse.jdt.core.IJavaElement;
17
import org.eclipse.jdt.core.IJavaElement;
19
import org.eclipse.jdt.core.ISourceRange;
20
import org.eclipse.jdt.core.IType;
21
import org.eclipse.jdt.core.ITypeRoot;
18
import org.eclipse.jdt.core.ITypeRoot;
22
import org.eclipse.jdt.core.JavaModelException;
19
import org.eclipse.jdt.core.JavaModelException;
23
import org.eclipse.jdt.core.WorkingCopyOwner;
20
import org.eclipse.jdt.core.WorkingCopyOwner;
Lines 227-255 Link Here
227
			// Local variable is declared inside an initializer
224
			// Local variable is declared inside an initializer
228
			TypeDeclaration typeDeclaration = (TypeDeclaration) referenceContext;
225
			TypeDeclaration typeDeclaration = (TypeDeclaration) referenceContext;
229
			
226
			
230
			IType type = (IType)this.getJavaElementOfCompilationUnit(typeDeclaration, typeDeclaration.binding);
227
			JavaElement type = this.getJavaElementOfCompilationUnit(typeDeclaration, typeDeclaration.binding);
231
			if (type != null) {
228
			parent = Util.getUnresolvedJavaElement(local.sourceStart, local.sourceEnd, type);
232
				try {
233
					IInitializer[] initializers = type.getInitializers();
234
					if (initializers != null) {
235
						done : for (int i = 0; i < initializers.length; i++) {
236
							IInitializer initializer = initializers[i];
237
							ISourceRange sourceRange = initializer.getSourceRange();
238
							if (sourceRange != null) {
239
								int initializerStart = sourceRange.getOffset();
240
								int initializerEnd = initializerStart + sourceRange.getLength();
241
								if (initializerStart <= local.sourceStart &&
242
										local.sourceEnd <= initializerEnd) {
243
									parent = (JavaElement)initializer;
244
									break done;
245
								}
246
							}
247
						}
248
					}
249
				} catch (JavaModelException e) {
250
					return null;
251
				}
252
			}
253
		}
229
		}
254
		if (parent == null) return null;
230
		if (parent == null) return null;
255
		
231
		
Lines 263-269 Link Here
263
				Util.typeSignature(local.type),
239
				Util.typeSignature(local.type),
264
				binding.declaration.annotations);
240
				binding.declaration.annotations);
265
	}
241
	}
266
	
242
267
	private JavaElement getJavaElementOfCompilationUnit(Binding binding) {
243
	private JavaElement getJavaElementOfCompilationUnit(Binding binding) {
268
		if (!this.hasComputedEnclosingJavaElements) {
244
		if (!this.hasComputedEnclosingJavaElements) {
269
			computeEnclosingJavaElements();
245
			computeEnclosingJavaElements();

Return to bug 217287