This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 93730

Summary: Replacing method body using TreeMaker API doesn't work in many cases
Product: java Reporter: _ deva <deva>
Component: SourceAssignee: Pavel Flaska <pflaska>
Status: RESOLVED FIXED    
Severity: blocker CC: sandipchitale
Priority: P2    
Version: 6.x   
Hardware: All   
OS: All   
Issue Type: DEFECT Exception Reporter:
Bug Depends on:    
Bug Blocks: 90451    
Attachments: Test module to reproduce the bug

Description _ deva 2007-01-31 21:44:14 UTC
If I use the following piece of code to replace method body, it replaces only 
the last statement in some cases
String bodyText = "{System.out.println(\"Hello World!\");}";
MethodTree meth = ...; //Assigned to old method tree
MethodTree newMeth = wc.getTreeMaker().Method(meth.getModifiers(), meth.getName
(), meth.getReturnType(), meth.getTypeParameters(), meth.getParameters(),   
meth.getThrows(), bodyText, (ExpressionTree)meth.getDefaultValue());
wc.rewrite(meth, newMeth);

It seems to work fine if the body has no or one statement.
Comment 1 _ deva 2007-01-31 21:45:26 UTC
Created attachment 37897 [details]
Test module to reproduce the bug
Comment 2 _ deva 2007-01-31 21:50:10 UTC
Install the attached module and follow these steps to reproduce the bug
1) Open the file TestClass.java which is part of the project
2) Select Refactor|Replace Method Body

You will notice that the body of replace() is not replaced completely
Comment 3 Pavel Flaska 2007-02-20 09:10:40 UTC
I got exception when tried to reproduce.
Comment 4 Pavel Flaska 2007-02-20 09:34:44 UTC
This is caused by block matching. Original body is matched against a new one. In
this case, the new should be printed from scratch. (Of course, matching should
also work, but it is too complex and redundant in such case.)
Comment 5 Pavel Flaska 2007-02-20 14:06:43 UTC
Checking in src/org/netbeans/modules/java/source/save/CasualDiff.java;
/cvs/java/source/src/org/netbeans/modules/java/source/save/CasualDiff.java,v 
<--  CasualDiff.java
new revision: 1.64; previous revision: 1.63
done
Checking in test/unit/src/org/netbeans/api/java/source/gen/MethodBodyTextTest.java;
/cvs/java/source/test/unit/src/org/netbeans/api/java/source/gen/MethodBodyTextTest.java,v
 <--  MethodBodyTextTest.java
new revision: 1.7; previous revision: 1.6
done