| Summary: | Wrong source ranges for Java element. | ||
|---|---|---|---|
| Product: | [Eclipse Project] JDT | Reporter: | Mário Guimarães <mljrg> |
| Component: | Core | Assignee: | 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: | |||
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? 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. |
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!