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

(-)model/org/eclipse/jdt/internal/core/LocalVariable.java (+10 lines)
Lines 107-112 Link Here
107
	}
107
	}
108
	
108
	
109
	private IAnnotation getAnnotation(final org.eclipse.jdt.internal.compiler.ast.Annotation annotation, JavaElement parentElement) {
109
	private IAnnotation getAnnotation(final org.eclipse.jdt.internal.compiler.ast.Annotation annotation, JavaElement parentElement) {
110
		final int typeStart = annotation.type.sourceStart();
111
		final int typeEnd = annotation.type.sourceEnd();
112
		final int sourceStart = annotation.sourceStart();
113
		final int sourceEnd = annotation.declarationSourceEnd;
110
		class LocalVarAnnotation extends Annotation {
114
		class LocalVarAnnotation extends Annotation {
111
			IMemberValuePair[] memberValuePairs;
115
			IMemberValuePair[] memberValuePairs;
112
			public LocalVarAnnotation(JavaElement localVar, String elementName) {
116
			public LocalVarAnnotation(JavaElement localVar, String elementName) {
Lines 115-120 Link Here
115
			public IMemberValuePair[] getMemberValuePairs() throws JavaModelException {
119
			public IMemberValuePair[] getMemberValuePairs() throws JavaModelException {
116
				return this.memberValuePairs;
120
				return this.memberValuePairs;
117
			}
121
			}
122
			public ISourceRange getNameRange() throws JavaModelException {
123
				return new SourceRange(typeStart, typeEnd - typeStart + 1);
124
			}
125
			public ISourceRange getSourceRange() throws JavaModelException {
126
				return new SourceRange(sourceStart, sourceEnd - sourceStart + 1);
127
			}
118
			public boolean exists() {
128
			public boolean exists() {
119
				return this.parent.exists();
129
				return this.parent.exists();
120
			}
130
			}
(-)src/org/eclipse/jdt/core/tests/model/GetSourceTests.java (+48 lines)
Lines 238-243 Link Here
238
	}
238
	}
239
239
240
	/*
240
	/*
241
	 * Ensures that the name range for an annotation on a local variable is correct.
242
	 * (regression test for https://bugs.eclipse.org/bugs/show_bug.cgi?id=209823)
243
	 */
244
	public void testAnnotationNameRange3() throws CoreException {
245
		try {
246
			String cuSource = 
247
				"package p;\n" +
248
				"public class Y {\n" +
249
				"  void foo() {\n" +
250
				"    @MyAnnot int local;\n" +
251
				"  }\n" +
252
				"}";
253
			createFile("/P/p/Y.java", cuSource);
254
			IAnnotation annotation = getLocalVariable(getCompilationUnit("/P/p/Y.java"), "local", "local").getAnnotation("MyAnnot");
255
			assertSourceEquals(
256
				"Unexpected source'", 
257
				"MyAnnot",
258
				getNameSource(cuSource, annotation));
259
		} finally {
260
			deleteFile("/P/p/Y.java");
261
		}
262
	}
263
264
	/*
265
	 * Ensures that the source range for an annotation on a local variable is correct.
266
	 * (regression test for https://bugs.eclipse.org/bugs/show_bug.cgi?id=209823)
267
	 */
268
	public void testAnnotationSourceRange() throws CoreException {
269
		try {
270
			String cuSource = 
271
				"package p;\n" +
272
				"public class Y {\n" +
273
				"  void foo() {\n" +
274
				"    @MyAnnot int local;\n" +
275
				"  }\n" +
276
				"}";
277
			createFile("/P/p/Y.java", cuSource);
278
			IAnnotation annotation = getLocalVariable(getCompilationUnit("/P/p/Y.java"), "local", "local").getAnnotation("MyAnnot");
279
			assertSourceEquals(
280
				"Unexpected source'", 
281
				"@MyAnnot",
282
				getSource(cuSource, annotation.getSourceRange()));
283
		} finally {
284
			deleteFile("/P/p/Y.java");
285
		}
286
	}
287
288
	/*
241
	 * Ensures the name range for an anonymous class is correct.
289
	 * Ensures the name range for an anonymous class is correct.
242
	 * (regression test for bug 44450 Strange name range for anonymous classes)
290
	 * (regression test for bug 44450 Strange name range for anonymous classes)
243
	 */
291
	 */
(-)src/org/eclipse/jdt/core/tests/model/AbstractJavaModelTests.java (-2 / +5 lines)
Lines 1672-1679 Link Here
1672
				nameRange = ((IMember) element).getNameRange();
1672
				nameRange = ((IMember) element).getNameRange();
1673
				break;
1673
				break;
1674
		}
1674
		}
1675
		int start = nameRange.getOffset();
1675
		return getSource(cuSource, nameRange);
1676
		int end = start+nameRange.getLength();
1676
	}
1677
	protected String getSource(String cuSource, ISourceRange sourceRange) throws JavaModelException {
1678
		int start = sourceRange.getOffset();
1679
		int end = start+sourceRange.getLength();
1677
		String actualSource = start >= 0 && end >= start ? cuSource.substring(start, end) : "";
1680
		String actualSource = start >= 0 && end >= start ? cuSource.substring(start, end) : "";
1678
		return actualSource;
1681
		return actualSource;
1679
	}
1682
	}

Return to bug 209823