Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 156025

Summary: JDOMMember setComments misses new line
Product: [Modeling] EMF Reporter: Alain Picard <picard>
Component: ToolsAssignee: Dave Steinberg <davidms>
Status: RESOLVED DUPLICATE QA Contact:
Severity: normal    
Priority: P3 CC: Ed.Merks
Version: 2.2   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
Whiteboard:
Attachments:
Description Flags
Test code to reproduce bug none

Description Alain Picard CLA 2006-09-01 22:36:32 EDT
Source compilation unit:
  /**
   * @Unmodifiable
   */
  private static NodeStructure nodeStructure;

Original target compilation unit:
  private static NodeStructure nodeStructure;

PullRule:
    <merge:pull 
	  	sourceMarkup="^Unmod$"		
      sourceGet="Member/getComment"
      targetPut="Member/setComment"/>

New target code:
  /**
   * @Unmodifiable
   */	private static NodeStructure nodeStructure;

Notice the missing linefeed. This in turn is triggering a bug with the editor code folding option that makes the code invisible (reported separately)
Comment 1 Ed Merks CLA 2006-09-02 08:54:24 EDT
I can't reproduce this and our code is rife with examples just like it.  Using the JDOM facade, this just delegates directly to JDOM, so if this were a bug, it would be a JDOM bug.  The AST facade is not complete and is not intended to be used, so if that's the cause, it will be addressed as we complete that implementation.  So I'll return this pending enough information for me to reproduce the problem.
Comment 2 Alain Picard CLA 2006-09-02 10:15:46 EDT
Created attachment 49293 [details]
Test code to reproduce bug

contains the source, target and rule.xml file to test the bug
Comment 3 Alain Picard CLA 2006-09-02 10:18:19 EDT
I have added a simple attachment to reproduce the bug.

Here is the code that I use to drive the test case:
		JControlModel controlModel = new JControlModel();
		controlModel.initialize(new JDOMFacadeHelper(), url.toString());
		JMerger merger = new JMerger(controlModel);
		merger.setSourceCompilationUnit(merger.createCompilationUnitForInputStream(file1.getContents(/* force */true)));
		merger.setTargetCompilationUnit(merger.createCompilationUnitForInputStream(file2.getContents(/* force */true)));
		merger.merge();
		
		String file2Content = merger.getTargetCompilationUnit().getContents();


and the result that i get from the test case:
package EDU.purdue.jtb.syntaxtree;

import com.castortech.annotation.*;

public class MergerSource {
  /**
   * @Unmodifiable
   */	private static com.castortech.common.syntaxrule.NodeStructure nodeStructure;
}

which shows the problem reported earlier. 

Hopefully you will now be able to reproduce the error this time.
Comment 4 Ed Merks CLA 2006-09-02 10:50:59 EDT
Alain,

I understand now; it should have been clear to me from the initial description that the target has no comment at all. Unfortunately this behavior is just the way JDOM works. I.e., JDOMMember.getComment() doesn't include the trailing new line (or new lines and white space for that matter) and setComment will insert only the characters it's asked to insert, which is the results of getComment(). I'll return this as a duplicate of the AST facade work, since JDOM is deprecated and fixing JDOM-specific issues is not something we can address.  We will try to ensure that the AST facade will do a better job of this.  

The JDT formatter can be used to correct any formatting errors in the result.  Here's some snips from the EMF generator to help you set that up...

    Map options = CodeFormatterProfileParser.parse(profileFile);
    CodeFormatter codeFormatt = ToolFactory.createCodeFormatter(codeFormatterOptions);

        IDocument doc = new Document(contents);
        TextEdit edit = codeFormatter.format(CodeFormatter.K_COMPILATION_UNIT, doc.get(), 0, doc.get().length(), 0, null);
  
        if (edit != null)
        {
          try
          {
            edit.apply(doc);
            contents = doc.get();
          }
          catch (Exception exception)
          {
            CodeGenEcorePlugin.INSTANCE.log(exception);
          }
        }

*** This bug has been marked as a duplicate of 124372 ***
Comment 5 Alain Picard CLA 2006-09-02 11:46:40 EDT
It would be appreciate if the test case i submitted is added to the test suite for the AST, so that we can be sure this gets tested.

Thanks.
Comment 6 Ed Merks CLA 2006-09-02 12:13:14 EDT
Alain,

We'll be sure you review all the duplicates to ensure that all the issues in them are addressed.  Since you are on CC list of that bugzilla, you can retest when it's complete.  I expect we will start working on in later in the month and have something available by October.