Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 349959

Summary: Wrong source ranges for Java element.
Product: [Eclipse Project] JDT Reporter: Mário Guimarães <mljrg>
Component: CoreAssignee: JDT-Core-Inbox <jdt-core-inbox>
Status: CLOSED DUPLICATE QA Contact:
Severity: normal    
Priority: P3 CC: amj87.iitr, markus.kell.r, Olivier_Thomann
Version: 3.6.2   
Target Milestone: ---   
Hardware: PC   
OS: Windows 7   
Whiteboard:

Description Mário Guimarães CLA 2011-06-21 11:43:34 EDT
Build Identifier: M20110210-1200

Hello,

the methods ISourceReference.getSource() and ISourceReference.getSourceRange() when called for an IMethod consider the non-Javadoc comments before the method. This does not match the Javadoc for org.eclipse.jdt.core.dom.MethodDeclaration. 
I two solutions for this:
1) Change the javadocs to match the current behavior (please, don't choose this)
2) Or, make the methods in ISourceReference match the same source range defined by ASTNode.getStartPosition() and ASTNode.getLength().
Optionally, if you think useful, extend ISourceReference, say via ISourceReference2, and offer methods getExtendedSource() and getExtendedSourceRange(). These two new methods would then match the range defined by CompilationUnit.getExtendedStartPosition(ASTNode) and CompilationUnit.getExtendedLength(ASTNode).

I've observed this issue for methods, however it may be happening with other Java elements.

Regards,
Mário

Reproducible: Always

Steps to Reproduce:
1. Create the following code:

// some comments
public void foo() {
 ...
}

2. Get an IMethod reference to foo()

3. Call ISourceReference.getSource() on that IMethod reference.
You will see that the source will include the comments before the method.

4. Now, get a MethodDeclaration reference for the above method. Now if you check the result values of getStartPosition() and getLength() on that reference they will not match the source range in 3.
Problem: mismatching source ranges!
Comment 1 Ayushman Jain CLA 2011-06-22 08:24:31 EDT
The source range for a java element always consists of the leading and trailing comments. So,

// comment1

//comment2
void foo() {
...
}
//comment3

//comment4

will show a source range from start of comment 2 to the end of comment 3.

The MethodDeclaration on the other hand is an AST node, and not a java element, and only includes the void foo().

This has always been the case. Also, any change in this will be a breaking change. Why do you want to have the source range from the java element and the AST node to be the same?
Comment 2 Mário Guimarães CLA 2011-06-22 13:19:57 EDT
Hello,

I think that ISourceReference.getSource() should have been written to be consistent with ASTNode --- I really don't see the usefulness of having those non-javadoc comments outside the method when calling getSource(); as it is, this requires the programmer parse those comments or to create an AST just to get the *strict* source.

Anyway, I got surprised knowing about this, but fortunately I did a workaround in my code. So, you may close this issue and avoid breaking the API.

Cheers.
Comment 3 Markus Keller CLA 2013-03-07 12:44:45 EST

*** This bug has been marked as a duplicate of bug 54204 ***