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 93740 - Trying to replace constructor body results in StringIndexOutOfBoundsException: String index out of range: -1
Summary: Trying to replace constructor body results in StringIndexOutOfBoundsException...
Status: RESOLVED FIXED
Alias: None
Product: java
Classification: Unclassified
Component: Source (show other bugs)
Version: 6.x
Hardware: All All
: P2 blocker (vote)
Assignee: Pavel Flaska
URL:
Keywords:
Depends on:
Blocks: 90451
  Show dependency tree
 
Reported: 2007-01-31 22:42 UTC by _ deva
Modified: 2007-02-19 12:20 UTC (History)
1 user (show)

See Also:
Issue Type: DEFECT
Exception Reporter:


Attachments
Test module project to reproduce the bug (10.43 KB, application/x-compressed)
2007-01-31 22:43 UTC, _ deva
Details

Note You need to log in before you can comment on or make changes to this bug.
Description _ deva 2007-01-31 22:42:25 UTC
If I use the following piece of code to replace constructor body 
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);
The following exception is thrown
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
	at java.lang.String.substring(String.java:1932)
	at org.netbeans.modules.java.source.save.CasualDiff.diffBlock
(CasualDiff.java:604)
	at org.netbeans.modules.java.source.save.CasualDiff.diffTree
(CasualDiff.java:2048)
	at org.netbeans.modules.java.source.save.CasualDiff.diffTree
(CasualDiff.java:2225)
	at org.netbeans.modules.java.source.save.CasualDiff.diffMethodDef
(CasualDiff.java:547)
	at org.netbeans.modules.java.source.save.CasualDiff.diffTree
(CasualDiff.java:2041)
	at org.netbeans.modules.java.source.save.CasualDiff.diffList
(CasualDiff.java:1735)
	at org.netbeans.modules.java.source.save.CasualDiff.diffClassDef
(CasualDiff.java:413)
	at org.netbeans.modules.java.source.save.CasualDiff.diffTree
(CasualDiff.java:2038)
	at org.netbeans.modules.java.source.save.CasualDiff.diffList
(CasualDiff.java:1735)
	at org.netbeans.modules.java.source.save.CasualDiff.diffTopLevel
(CasualDiff.java:217)
	at org.netbeans.modules.java.source.save.CasualDiff.diffTree
(CasualDiff.java:2032)
	at org.netbeans.modules.java.source.save.CasualDiff.diffTree
(CasualDiff.java:2221)
	at org.netbeans.modules.java.source.save.CasualDiff.diff
(CasualDiff.java:114)
	at org.netbeans.modules.java.source.save.Commit.commit(Commit.java:243)
Comment 1 _ deva 2007-01-31 22:43:25 UTC
Created attachment 37899 [details]
Test module project to reproduce the bug
Comment 2 _ deva 2007-01-31 22:59:48 UTC
Build and 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 see the StringIndexOutOfBoundsException. If you try again you do not 
see the exception. My reasoning below will explain why this is hapenning

The new method tree obtained by TreeMaker.method() doesn't contain the 
synthetic statement super(), but in the code CasualDiff.diffBlock(), only the 
old block tree is checked for synthetic statement but both the old and new 
tree statements are advanced for comparison. Hence you will see the exception.

When you try again second time, the old tree doesn't have synthetic statement 
and the replacement of body works fine. [Note that replaced code is not 
indented properly]


Comment 3 Pavel Flaska 2007-02-19 12:20:38 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.63; previous revision: 1.62
done