Bug 192233 - [AST] CompilationUnit.rewrite() removes whitespace between return type and method name
Summary: [AST] CompilationUnit.rewrite() removes whitespace between return type and me...
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.3   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.6 M7   Edit
Assignee: Olivier Thomann CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-06-12 11:55 EDT by Konstantin Scheglov CLA
Modified: 2010-04-26 12:25 EDT (History)
3 users (show)

See Also:


Attachments
Proposed fix + regression tests (14.22 KB, patch)
2010-03-17 12:15 EDT, Olivier Thomann CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Konstantin Scheglov CLA 2007-06-12 11:55:13 EDT
Build ID: I20070503-1400

Steps To Reproduce:
1. For code:
  List/**/getUsers();
or even
  List /**/getUsers();
but not for
  List/**/ getUsers();
2. When I use following code:

serviceRoot.recordModifications();
methodDeclaration.setReturnType2(ast.newPrimitiveType(PrimitiveType.VOID));

and later:
// prepare text edits
MultiTextEdit edits = (MultiTextEdit) serviceRoot.rewrite(document, serviceUnit.getJavaProject().getOptions(true));
// prepare new source code
edits.apply(document);
newSource = document.get();

3. Result source:
voidtheMethodName()
i.e. all whitespaces and comments removed and produce invalid source.

More information:
  Hm... Why AJDT? It should be in JDT.
Comment 1 Olivier Thomann CLA 2010-03-16 13:08:15 EDT
I have two fixes:
1) one that keeps the existing comment between the old return type and the new one.
2) one that removes the existing comment and only make sure a space is inserted. 

Markus, any preference ?
Comment 2 Markus Keller CLA 2010-03-16 15:26:57 EDT
I prefer a solution that is consistent with the API specs and with how this is handled in other places. ASTRewrite says it uses a default TargetSourceRangeComputer, so the comments should be removed in both cases.

Here's a test case I used. It also shows the same problem in method argument lists and field declarations. Apply Quick Fixes to see what the ASTRewrite does. I think we'll need to set a NoCommentSourceRangeComputer in some of the quick fixes to avoid losing comments.


package xy;

import java.util.List;

@SuppressWarnings("rawtypes")
public class Try {
    List/*important*/getUsers2() {
        return;
    }
    
    List /*important*/getUsers() {
        return fField;
    }
    
    String/**/fField;
    
    void foo(List/**/l) {
        foo("");
        
        foo(/**/q);
    }
}
Comment 3 Olivier Thomann CLA 2010-03-16 17:56:51 EDT
Since ASTReweitre(In reply to comment #2)
> I prefer a solution that is consistent with the API specs and with how this is
> handled in other places. ASTRewrite says it uses a default
> TargetSourceRangeComputer, so the comments should be removed in both cases.
Yes, I checked the doc later on and indeed it is specified to use the extended range. So comments should be removed in both cases.
I'll fix it that way.
I'll also check the two other cases you mentioned.
Comment 4 Olivier Thomann CLA 2010-03-17 12:15:13 EDT
Created attachment 162312 [details]
Proposed fix + regression tests
Comment 5 Olivier Thomann CLA 2010-03-17 12:16:50 EDT
Released for 3.6M7.
Regression tests added in:
org.eclipse.jdt.core.tests.rewrite.modifying.ASTRewritingModifyingReplaceTest#test0007
org.eclipse.jdt.core.tests.rewrite.modifying.ASTRewritingModifyingReplaceTest#test0008
org.eclipse.jdt.core.tests.rewrite.modifying.ASTRewritingModifyingReplaceTest#test0009
org.eclipse.jdt.core.tests.rewrite.modifying.ASTRewritingModifyingReplaceTest#test0010
org.eclipse.jdt.core.tests.rewrite.modifying.ASTRewritingModifyingReplaceTest#test0011
org.eclipse.jdt.core.tests.rewrite.modifying.ASTRewritingModifyingReplaceTest#test0012
org.eclipse.jdt.core.tests.rewrite.modifying.ASTRewritingModifyingReplaceTest#test0013

This fixes the initial issue with method return type, but also the field type and method arguments issues reported by Markus.
Comment 6 Frederic Fusier CLA 2010-04-26 12:25:57 EDT
Verified for 3.6M7 using I20100425-2000.