Bug 70963 - [1.5] programmatically created Enum gets bad formatting
Summary: [1.5] programmatically created Enum gets bad formatting
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.0   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.1 M2   Edit
Assignee: Martin Aeschlimann CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2004-07-28 06:07 EDT by Danny Dig CLA
Modified: 2004-09-23 08:07 EDT (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Danny Dig CLA 2004-07-28 06:07:37 EDT
Build id: 200407200010 (using lattest JDT plugin export from July 27th)

I am creating an Enum declaration programatically and I set up its constants. 
However, even though I can see in the AST that the constants appear nice, 
after the rewriter does its job, the code generated for Enum doesn't have any 
separators between the enum constants. For example, if I create an enum with 
name MyEnum and constants i1, i2 and i3, the generated code will look like:

enum MyEnum {i1i2i3}

Below you'll find the code I use to create and set up the constants in the 
enum:

private void createEnumDeclaration(ASTRewrite rewrite) {
        AST ast= getInputAST();
        EnumDeclaration enumD= ast.newEnumDeclaration();
      
        // where fEnumName is a String
        SimpleName enumName= ast.newSimpleName(fEnumName);
        enumD.setName(enumName);
        List enumBody= enumD.bodyDeclarations();

        // where fFieldsToExtract is an array of SimpleNames
        for (int i= 0; i < fFieldsToExtracts.length; i++) {
            SimpleName name= fFieldsToExtracts[i];
            EnumConstantDeclaration constDecl= ast.newEnumConstantDeclaration
();
            constDecl.setName(ast.newSimpleName(name.getIdentifier());
            enumBody.add(constDecl);
        }
        if (!isExtractToSeparateCU()) {
            ListRewrite declarations= rewrite.getListRewrite(fInputTypeNode,
                    TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
            declarations.insertFirst(enumD, null);
        } else {
            createNewEnumCU(enumD);
        }
    }
Comment 1 Olivier Thomann CLA 2004-07-28 16:51:32 EDT
Instead of TypeDeclaration.BODY_DECLARATIONS_PROPERTY, should not you have
EnumDeclaration.BODY_DECLARATIONS_PROPERTY?

I also don't understand why the enumConstants are part of the body declarations.
I don't see how it is possible to get the semicolon between the constants and
the body declarations.

Jim, any idea?
Comment 2 Jim des Rivieres CLA 2004-07-28 17:03:29 EDT
Olivier, The enum constant declarations are the first elements of 
EnumDeclaration.bodyDeclarations. See NaiveASTFlattener.visit(EnumDeclaration) 
for an example of how to put the comma separators between enum constants and 
the semicolon separating the enum constants from the rest of the body decls.
Comment 3 Danny Dig CLA 2004-07-29 05:37:28 EDT
Olivier, 

replacing TypeDeclaration.BODY_DECLARATION_PROPERTY with 
EnumDeclaration.BODY_DECLARATION_PROPERTY
 
results in the following exception (below is the stack trace):

java.lang.IllegalArgumentException: TypeDeclaration has no property 
bodyDeclarations
	at java.lang.Throwable.<init>(Throwable.java)
	at java.lang.Throwable.<init>(Throwable.java)
	at 
org.eclipse.jdt.internal.core.dom.rewrite.RewriteEventStore.validateHasChildPro
perty(RewriteEventStore.java:615)
	at org.eclipse.jdt.internal.core.dom.rewrite.RewriteEventStore.getEvent
(RewriteEventStore.java:296)
	at 
org.eclipse.jdt.internal.core.dom.rewrite.RewriteEventStore.getListEvent
(RewriteEventStore.java:325)
	at org.eclipse.jdt.core.dom.rewrite.ListRewrite.getEvent
(ListRewrite.java:56)
	at org.eclipse.jdt.core.dom.rewrite.ListRewrite.internalInsertAt
(ListRewrite.java:261)
	at org.eclipse.jdt.core.dom.rewrite.ListRewrite.insertFirst
(ListRewrite.java:206)
	at 
org.eclipse.jdt.internal.corext.refactoring.structure.ExtractEnumRefactoring.cr
eateEnumDeclaration(ExtractEnumRefactoring.java:379)
	at 
org.eclipse.jdt.internal.corext.refactoring.structure.ExtractEnumRefactoring.cr
eateChange(ExtractEnumRefactoring.java:329)
	at org.eclipse.ltk.core.refactoring.CreateChangeOperation.run
(CreateChangeOperation.java:117)
	at org.eclipse.ltk.core.refactoring.PerformChangeOperation.run
(PerformChangeOperation.java:188)
	at org.eclipse.core.internal.resources.Workspace.run
(Workspace.java:1673)
	at org.eclipse.core.internal.resources.Workspace.run
(Workspace.java:1693)
	at 
org.eclipse.jdt.ui.tests.refactoring.RefactoringTest.performRefactoring
(RefactoringTest.java:177)
	at 
org.eclipse.jdt.ui.tests.refactoring.RefactoringTest.performRefactoring
(RefactoringTest.java:166)
	at org.eclipse.jdt.ui.tests.refactoring.ExtractEnumTest.canExtractEnum
(ExtractEnumTest.java:66)
	at 
org.eclipse.jdt.ui.tests.refactoring.ExtractEnumTest.testSimpleSameCU
(ExtractEnumTest.java:100)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke
(NativeMethodAccessorImpl.java:84)
	at sun.reflect.NativeMethodAccessorImpl.invoke
(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke
(DelegatingMethodAccessorImpl.java:59)
	at java.lang.reflect.Method.invoke(Method.java:390)
	at junit.framework.TestCase.runTest(TestCase.java:154)
	at junit.framework.TestCase.runBare(TestCase.java:127)
	at junit.framework.TestResult$1.protect(TestResult.java:106)
	at junit.framework.TestResult.runProtected(TestResult.java:124)
	at junit.framework.TestResult.run(TestResult.java:109)
	at junit.framework.TestCase.run(TestCase.java:118)
	at junit.framework.TestSuite.runTest(TestSuite.java:208)
	at junit.framework.TestSuite.run(TestSuite.java:203)
	at junit.extensions.TestDecorator.basicRun(TestDecorator.java:22)
	at junit.extensions.TestSetup$1.protect(TestSetup.java:19)
	at junit.framework.TestResult.runProtected(TestResult.java:124)
	at junit.extensions.TestSetup.run(TestSetup.java:23)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests
(RemoteTestRunner.java:421)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run
(RemoteTestRunner.java:305)
	at org.eclipse.pde.internal.junit.runtime.RemotePluginTestRunner.main
(RemotePluginTestRunner.java:30)
	at org.eclipse.pde.internal.junit.runtime.UITestApplication$1.run
(UITestApplication.java:92)
	at org.eclipse.swt.widgets.RunnableLock.run(RunnableLock.java:35)
	at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages
(Synchronizer.java:106)
	at org.eclipse.swt.widgets.Display.runAsyncMessages(Display.java:2749)
	at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:2434)
	at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:1435)
	at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:1406)
	at org.eclipse.ui.internal.Workbench.createAndRunWorkbench
(Workbench.java:263)
	at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:144)
	at org.eclipse.ui.internal.ide.IDEApplication.run
(IDEApplication.java:102)package p;
Comment 4 Olivier Thomann CLA 2004-08-03 13:15:58 EDT
This seems to be a bug in the rewriter that doesn't handle the enum constant
declarations specifically. It seems to consider all declarations as body
declarations.
Comment 5 Martin Aeschlimann CLA 2004-09-01 12:08:17 EDT
The bug was in the ast rewriter flattener code.
I detected the same bug in the NaiveASTFlattener: The iteration over the enum
bodyDeclarations did not update the 'prev' variable
Jeem, I added and released the missing statement as well in the NaiveASTFlattener
> 20040901
Comment 6 David Audel CLA 2004-09-23 08:07:48 EDT
Verified in I200409240100.