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

Bug 183113

Summary: [ast rewrite] Wrong code formatting of AST rewrite
Product: [Eclipse Project] JDT Reporter: Eddie Man <superman_eddie>
Component: CoreAssignee: David Audel <david_audel>
Status: CLOSED WORKSFORME QA Contact:
Severity: normal    
Priority: P3 CC: rgrunber
Version: 3.1   
Target Milestone: ---   
Hardware: PC   
OS: All   
Whiteboard: stalebug

Description Eddie Man CLA 2007-04-19 02:22:12 EDT
The number literal of field declaration is using the array initializer formatting.

Steps
===========
1. Set the array initializer's braces formatting to next line.
(In preferences, Java > Code Style > Formatter > Edit > Braces > Array initializer)
2. Create a new class with 1 attribute (private int attribute;)
3. Using the AST API to set the initializer of the attribute to a number literal.
    CompilationUnit unit;
    ...
    unit.recordModifications();
    ...
    FieldDeclaration field;
    ...
    VariableDeclarationFragment fragment = (VariableDeclarationFragment) field.fragments().get(0);
    fragment.setInitializer(field.getAST().newNumberLiteral("3");
    TextEdit textEdit = unit.rewrite(document, JavaCore.getOptions());
    textEdit.apply(document);

Result:
private int attribute =
3;
Comment 1 Eddie Man CLA 2007-06-08 05:12:27 EDT
Is there have any work around?
Comment 2 Eclipse Genie CLA 2020-08-28 17:35:31 EDT
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.
Comment 3 Roland Grunberg CLA 2020-09-11 15:21:14 EDT
In ChainCompletionTest (jdt.ui), I tried the following test :

	@Test
	public void testTemporary () throws Exception {
		Map<String, String> options= JavaCore.getOptions();
		options.put(DefaultCodeFormatterConstants.FORMATTER_BRACE_POSITION_FOR_ARRAY_INITIALIZER, DefaultCodeFormatterConstants.NEXT_LINE);

		StringBuffer buf= new StringBuffer();
		buf.append("package test;\n" +
				"\n" +
				"public class TestTemporary {\n" +
				"  private int attribute; \n" +
				"}");

		ICompilationUnit cu= getCompilationUnit(pkg, buf, "TestTemporary.java");
		CompilationUnit cuRoot= ASTCreator.createAST(cu, null);
		final FieldDeclaration[] fieldArr= new FieldDeclaration[1];
		cuRoot.accept(new ASTVisitor() {
			@Override
			public boolean visit(org.eclipse.jdt.core.dom.FieldDeclaration node) {
				fieldArr[0]= node;
				return false;
			}
		});

		FieldDeclaration field = fieldArr[0];
		IDocument document= new Document(cu.getSource());
	    cuRoot.recordModifications();
	    VariableDeclarationFragment fragment = (VariableDeclarationFragment) field.fragments().get(0);
	    fragment.setInitializer(field.getAST().newNumberLiteral("3"));
	    TextEdit textEdit = cuRoot.rewrite(document, options);
	    textEdit.apply(document);

	    String expected = "package test;\n" +
				"\n" +
				"public class TestTemporary {\n" +
				"  private int attribute = 3; \n" +
				"}";

	    assertEquals(expected, document.get());
	}

It seems to pass (the '3' literal is placed on the same line), which would indicate this is no longer an issue.