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

Bug 109926

Summary: [formatter] Annotation not formatted propertly
Product: [Eclipse Project] JDT Reporter: Olivier Thomann <Olivier_Thomann>
Component: CoreAssignee: JDT-Core-Inbox <jdt-core-inbox>
Status: CLOSED WONTFIX QA Contact:
Severity: normal    
Priority: P5 CC: brian.vosburgh, ghoren, jgarms, LifarV, martinae, pkothari, ppatil, sunny.yee, te
Version: 3.2Keywords: helpwanted
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
Whiteboard: stalebug

Description Olivier Thomann CLA 2005-09-19 16:09:10 EDT
From Eclipse newsgroups,
I'm writing an customized Java editor, and using CompilationUnit.rewrite()
method with formatting options to save back to a Java file.  My editor tries to
write out a new normal annotation, and format the expressions in its array
initializer on separate lines.  
This is the formatting output that I want to see, e.g.

   @Wpa.Action(
       forwardConfigs = {
           @Wpa.ForwardConfig( forward="success",                layout="MyLayout",
               localized=true
           )
       }
   )

The formatting options that I use doesn't seem to have any affect, and writes
out the entire normal annotation on one line, e.g.

   @Wpa.Action(forwardConfigs = {@Wpa.ForwardConfig(forward="success_cart",    
             layout="athp", localized=true)})

I tried using only ICompilationUnit.getJavaProject().getOptions(true).

TextEdit edits = unit.rewrite(sourceDocument,
icu.getJavaProject().getOptions(true));

Also I tried setting some of the formatting options and passing in the formating
options Map to the rewrite() method without success.

formattingOptions = icu.getJavaProject().getOptions(true);

formattingOptions.put(
DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_EXPRESSIONS_IN_ARRAY_INITIALIZER,
  DefaultCodeFormatterConstants.createAlignmentValue(true,      
DefaultCodeFormatterConstants.WRAP_NEXT_PER_LINE,      
DefaultCodeFormatterConstants.INDENT_DEFAULT));
 
formattingOptions.put(
DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_OPENING_BRACE_IN_ARRAY_INITIALIZER,
             JavaCore.INSERT);
           formattingOptions.put(
DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_ANNOTATION,
   JavaCore.INSERT);

formattingOptions.put(
DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_BEFORE_CLOSING_BRACE_IN_ARRAY_INITIALIZER,
   JavaCore.INSERT);
                      formattingOptions.put(
                  
DefaultCodeFormatterConstants.FORMATTER_BRACE_POSITION_FOR_ARRAY_INITIALIZER,
                       DefaultCodeFormatterConstants.createAlignmentValue(true,
                               DefaultCodeFormatterConstants.WRAP_NEXT_PER_LINE,
                               DefaultCodeFormatterConstants.INDENT_DEFAULT));

Thanks for any help.

Sunny Yee
Comment 1 Olivier Thomann CLA 2005-10-03 13:03:50 EDT
I thought this was an issue with the code formatter, but it looks more like an
issue with the DOM rewriter.
Martin, could you please look at this one? It looks like the rewriter is not
using the formatter's option when the expresion is an array initializer.
Comment 2 Martin Aeschlimann CLA 2005-10-03 13:20:36 EDT
There special code in the rewriter that sets the line length to unlimited. I
have to investigate why.
Comment 3 Olivier Thomann CLA 2005-10-03 13:22:47 EDT
In this case, the force option is set for the array initializer line wrapping.
So even if the length of the line is unlimited, it should still wrap it,
shouldn't it?
Comment 4 Martin Aeschlimann CLA 2005-10-04 03:36:13 EDT
Sunny, can you give more details on how the annotation nodes were created?
Are they all new and were added as a whole, or did you just make modifications
on an existing one?

When making modifications, the AST rewriter doesn't follow all the formatter
preferences. It's a limitation of the rewriter that isn't easy to change.
Comment 5 Sunny Yee CLA 2005-10-04 14:31:30 EDT
Martin,

I update the AST with new NormalAnnotation nodes, and also by modifying existing
NormalAnnotation nodes.  I see problems in the formatting on both, so even with
all new nodes, I see formatting to one line.   In the following example, if the
@Wpa.Action doesn't exist for a particular class method, I created it new along
with the list of name-value pairs.

    @Wpa.Action(forwardConfigs = {@Wpa.ForwardConfig(forward="success",
layout="athp", layoutActions={com.hp.SampleLayoutAction.class},
layoutProperties={@Wpa.LayoutProperty(name="pageTitle", value="demo page 1")})
    })

Psudeo code:

// adding a new @Wpa.Action node if it doesn't exists
if (noWpaActionNode == false) {
    wpaActionAnn = ast.newNormalAnnotation();
    wpaActionAnn.setTypeName(ast.newName(JpfConstants.WPA_ACTION_ANNO));
				
    methodDecl.modifiers().add(pos, wpaActionAnn);
}

// Add a new 'forwardConfigs={}' member pair if it doesn't exists.

MemberValuePair forwardConfigsPair = null; 
List<MemberValuePair> forwardConfigsList = wpaActionAnn.values();
for (MemberValuePair pair: forwardConfigsList) {
    if pair.getName().getFullyQualifiedName().
            equals(JpfConstants.FORWARD_CONFIG_MEMBER_NAME)) {
	// found 'forwardsConfig' node
	forwardConfigsPair = pair;
	break;
				
    }
}

// 'forwardConfigs' member value pair doesn't exists, so create one.
if (noForwardConfigsPair == true) {
    forwardConfigsPair = ast.newMemberValuePair();
    forwardConfigsPair.setName(
            ast.newSimpleName(JpfConstants.FORWARD_CONFIG_MEMBER_NAME));
				
    // Create empty array.
    ArrayInitializer arrayInit = ast.newArrayInitializer();
    forwardConfigsPair.setValue(arrayInit);
				
    wpaActionAnn.values().add(forwardConfigsPair);
}
			
// Code to add to array ...
Comment 6 Gary Horen CLA 2005-11-10 18:54:31 EST
Formatting of annotations is very important to us; we do a lot of programmatic 
insertion of them into source files.
Comment 7 Gary Horen CLA 2006-01-05 20:27:29 EST
*** Bug 122838 has been marked as a duplicate of this bug. ***
Comment 8 Martin Aeschlimann CLA 2006-02-10 14:41:59 EST
It seems to me that the rewriter uses the formatter, but we don't have the formatter settings that can do exactly what you want.

In my formatter profile I configured:
New Lines:
  Insert new lines after open brace of array initailizer : true
  Insert new lines after open brace of array initailizer : true
Line Wrapping:
  Extension - Array Initialiers: Always wrap except first, indent one

This is my code sample to format:
---
package pack;

@MyAnnot(x1 = "", y1 = {@MyAnnot2(z1 = "")})
public class Y {
}

@interface MyAnnot {
	String x1();	
	MyAnnot2[] y1();
}

@interface MyAnnot2 {
	String z1();
}
--
This is what I get (select all, Source > Format)
---
package pack;

@MyAnnot(x1 = "", y1 = {
	@MyAnnot2(z1 = "")
}) public class Y {
}

@interface MyAnnot {
	String x1();

	MyAnnot2[] y1();
}

@interface MyAnnot2 {
	String z1();
}



When removing the y1 = {...} line and using quick fix, the result is:
package pack;

@MyAnnot(x1 = "", y1 = {
	@MyAnnot2
})
public class Y {
}

@interface MyAnnot {
	String x1();

	MyAnnot2[] y1();
}

@interface MyAnnot2 {
	String z1();
}

So it seems the rewriter correctly uses the formatter for the new node.

It seems to me that this is a formatter issue. Olivier did I miss something?
Also note the strange placement of 'public' just after the closing parenthesis.
Comment 9 Frederic Fusier CLA 2008-08-18 08:07:02 EDT
Ownership has changed for the formatter, but I surely will not have enough time to fix your bug during the 3.5 development process, hence set its priority to P5.
Please provide a patch if you definitely need the bug to be fixed in this version and I'll have a look at it...
TIA
Comment 10 Raoul CLA 2008-09-01 04:57:31 EDT
Hy,

I had this bug too, to resolve it you have only to :
   - delete "site.xml" where in you eclipse folder.
   - run eclipse : "eclipse.exe -clean"

Normaly it work.

Best regards,
Aurélie
Comment 11 Eclipse Genie CLA 2019-12-23 08:15:18 EST
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.