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 177824 - Cannot insert method override below a guarded block
Summary: Cannot insert method override below a guarded block
Status: RESOLVED FIXED
Alias: None
Product: java
Classification: Unclassified
Component: Source (show other bugs)
Version: 6.x
Hardware: PC Linux
: P3 normal (vote)
Assignee: vezzoni
URL:
Keywords: NETFIX
Depends on:
Blocks:
 
Reported: 2009-11-30 01:46 UTC by Marian Petras
Modified: 2012-05-19 09:55 UTC (History)
2 users (show)

See Also:
Issue Type: DEFECT
Exception Reporter:


Attachments
Project reproducing the not working generation of code. (15.43 KB, application/zip)
2010-04-16 13:08 UTC, Jiri Kovalsky
Details
Diff file with the fix for this issue (21.27 KB, patch)
2012-03-25 14:05 UTC, vezzoni
Details | Diff
Diff file with the fix for this issue #2 (16.86 KB, patch)
2012-04-07 18:41 UTC, vezzoni
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Marian Petras 2009-11-30 01:46:58 UTC
I have a class with a few guarded blocks. I want to insert a new method below the last guarded block. The new method should override a method from a superclass, so I decided to use the Java code hint. I type several chars of the method's name, press Ctrl-Enter and select the proper item from a pop-up menu. After that, the pop-up disappears but the new method is not added. Instead, a notice appears in the IDE's status line: "Cannot apply changes due to guarded block."

Here is the layout of the existing code. The arrow marks the line where I am trying to add the new method to:

    -----------------------------------------
    ...
    // End of variables declaration                 <-- END OF GUARDED BLOCK

    --> TRYING TO INSERT NEW METHOD HERE <--

    /**
     * Gets default instance. Do not use directly: reserved for *.settings files only,
     * ...
    -----------------------------------------

--
JDK 6u18-b05, 64-bit, server VM
Linux (Debian Lenny), 64-bit, Intel CPU
Comment 1 Jiri Kovalsky 2010-04-16 12:59:44 UTC
I have also encountered this. I didn't even notice the warning in status bar and was very confused. In my case I was also trying to override a method but place it between generated method and variables declaration section. Both were guarded blocks - depicted by *. I placed my cursor at position 75:2 depicted by | and pressed Alt+Insert to invoke the Generate wizard.

69*  private void rbInvoiceActionPerformed(java.awt.event.ActionEvent evt) {
70       rbInvoice.setSelected(true);
71       rbReport.setSelected(false);
72       lbPreview.setIcon(invoicePreviewImage);
73       firePropertyChange("report.type", null, "invoice");
74*  }                                         
75   |
76*  // Variables declaration - do not modify                     
77   private javax.swing.JLabel lbPreview;
78   private javax.swing.JRadioButton rbInvoice;
79   private javax.swing.JRadioButton rbReport;
80*  // End of variables declaration

Marking with NETFIX keyword to give this bug more visibility and increase its chances to be fixed.
Comment 2 Jiri Kovalsky 2010-04-16 13:08:20 UTC
Created attachment 97525 [details]
Project reproducing the not working generation of code.

Simply open the project, open the ReportTypeWizardStep.java class, place cursor at position 75:1 and use Alt+Insert to override for example setDescription(String description) method. Once you check it and push Generate button nothing happens. :-(
Comment 3 vezzoni 2011-04-25 20:18:20 UTC
I'd like to NetFIX [1] this bug. Is it possible?

[1] http://wiki.netbeans.org/NetFIX"
Comment 4 Jan Lahoda 2011-04-27 11:56:15 UTC
(In reply to comment #3)
> I'd like to NetFIX [1] this bug. Is it possible?

You are surely welcome to work on this. It might be relatively simple problem, but may also be pretty complex, and I cannot tell which one without investigating the root cause.

I suspect this is a bug in the code generator (part of the java.source module). The approach that I would recommend to fix this (and similar) bugs is:
-add a new test case(s) covering the problem (in this case into the GuardedBlockTest in java.source)
-debug&fix until the test case (and other test cases in GuardedBlockTest) pass
-run:
ant -Dtest.config=generator test
in java.source directory in the NB checkout. If all tests pass (i.e. build is successful), the patch is a good candidate for review. If some tests fail, investigate why (run "ant init test-generate-html" to get an HTML results page), and either fix the regression or have a reasoning ready why the existing test was incorrect+add a patch that fixes it (it is however not very common to change existing tests).

Good luck.
Comment 5 vezzoni 2011-04-27 19:30:58 UTC
thanks Jan!

I'll follow your advice.
Comment 6 vezzoni 2012-03-25 14:05:45 UTC
Created attachment 117209 [details]
Diff file with the fix for this issue

Affected files on Java.Source project:

* org.netbeans.api.java.source.gen.GuardedBlockTest
  #testInsertMethodBeforeVariablesBug177824()

* org.netbeans.api.java.source.ModificationResult
  #processDocumentLocked(Document, Difference)
Comment 7 Jan Lahoda 2012-03-26 07:59:22 UTC
I am sorry, but I do not like the attached patch very much. Catching and ignoring exceptions is rarely the correct solution. In this case, the real question is why the code generator (the CasualDiff and the other diffing parts) tries to modify the guarded block at all, and if that can be prevented.

Thanks.
Comment 8 vezzoni 2012-03-26 14:01:25 UTC
Right Jan. 

I saw CasualDiff adding an inconsistent CHANGE diff (removing a guarded block) before the correct INSERT diff.

I'll work on it.

Thanks!
Comment 9 vezzoni 2012-04-07 18:41:17 UTC
Created attachment 117981 [details]
Diff file with the fix for this issue #2

Affected files on Java.Source project:

* org.netbeans.api.java.source.gen.GuardedBlockTest
  #testInsertMethodBeforeVariablesBug177824()

* org.netbeans.modules.java.source.save.DiffFacility
  #makeTokenListMatch(String, String, int):206
Comment 10 vezzoni 2012-04-22 22:47:28 UTC
is there some update about that submitted patch?
Comment 11 Jan Lahoda 2012-04-23 08:19:53 UTC
Seems like a reasonable approach (would be better if the difference wouldn't exist in the first place, but as the trailing whitespaces are stripped by pretty.CharBuffer.nlTerm(), this does not seem really viable, at least for now).

Can I include your name for the changeset? Thanks.
Comment 12 vezzoni 2012-04-23 14:55:02 UTC
Yes, you can do that Jan.

Regards!
Comment 13 Jan Lahoda 2012-04-24 14:30:19 UTC
Integrated in the jet-main repository:
http://hg.netbeans.org/jet-main/rev/a99f112a1760

Thank you for your contribution.
Comment 14 vezzoni 2012-04-24 16:55:00 UTC
I'm glad to help!

Thank you Jan!
Comment 15 Quality Engineering 2012-04-26 10:36:36 UTC
Integrated into 'main-golden', will be available in build *201204260400* on http://bits.netbeans.org/dev/nightly/ (upload may still be in progress)
Changeset: http://hg.netbeans.org/main-golden/rev/a99f112a1760
User: Roberto Vezzoni <vezzoni@netbeans.org>
Log: #177824: do not produce a diff for removed guarded block placeholder whitespaces
Comment 16 vezzoni 2012-04-27 13:43:26 UTC
Jan, please.

I've missed the follow instruction at integrated diff that makes the test case run:

suite.addTest(new GuardedBlockTest("testInsertMethodBeforeVariablesBug177824"));

That instruction has to be into suite() method on GuardedBlockTest unit test class. It is at 156 line on patched file.

Thanks!
Comment 17 Quality Engineering 2012-05-19 09:55:33 UTC
Integrated into 'main-golden', will be available in build *201205190400* on http://bits.netbeans.org/dev/nightly/ (upload may still be in progress)
Changeset: http://hg.netbeans.org/main-golden/rev/d7bb48785985
User: Jan Lahoda <jlahoda@netbeans.org>
Log: #177824#c16: better enable all test cases in a test class, and disable the broken ones separately.