Bug 88312 - [1.5][dom] SourceType#createField fails for enums
Summary: [1.5][dom] SourceType#createField fails for enums
Status: RESOLVED WONTFIX
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: ---   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on: 88344
Blocks:
  Show dependency tree
 
Reported: 2005-03-17 06:39 EST by David Audel CLA
Modified: 2009-08-30 02:35 EDT (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description David Audel CLA 2005-03-17 06:39:21 EST
From : Alexander Pavlov (bug 86906 comment 3)

Another issue has risen in the I200502171619. Consider this enum:

public enum MyEnum {
    VALUE1
}

We call createField("int someField=0;\n", null, true, null).
It fails because of the following intermediate search call to make the VALUE1 
enum constant declaration a sibling:
	SourceField.findNode(CompilationUnit) line: 40
	CreateFieldOperation(CreateElementInCUOperation).insertASTNode
(ASTRewrite, ASTNode, ASTNode) line: 255
	CreateFieldOperation
(CreateElementInCUOperation).generateNewCompilationUnitAST(ICompilationUnit) 
line: 184
	CreateFieldOperation(CreateElementInCUOperation).executeOperation() 
line: 137
	CreateFieldOperation(JavaModelOperation).run(IProgressMonitor) line: 710
	Workspace.run(IWorkspaceRunnable, ISchedulingRule, int, 
IProgressMonitor) line: 1702
	CreateFieldOperation(JavaModelOperation).runOperation(IProgressMonitor) 
line: 771
	SourceType.createField(String, IJavaElement, boolean, IProgressMonitor) 
line: 117

The reason is this: the SourceType method
public ASTNode findNode(org.eclipse.jdt.core.dom.CompilationUnit ast) {
	// For field declarations, a variable declaration fragment is returned
	// Return the FieldDeclaration instead
	ASTNode node = super.findNode(ast);
	if (node == null) return null;
	return node.getParent();
}

does find the EnumConstantDeclaration but does not return one, taking its 
parent (i.e. EnumDeclaration) as the final result. A check should be made to 
handle the enum constant case properly and return the constant declaration 
itself, not its parent node (something like

if (node instanceof VariableDeclarationFragment) {
    return node.getParent();
}
else {
    return node;
}

).
Comment 1 David Audel CLA 2005-03-17 11:21:10 EST
To work, the createField() method needs to know if the inserted field is an enum
constant or a real field.
Comment 2 David Audel CLA 2006-03-28 10:05:06 EST
Defer post 3.2
Comment 3 Eclipse Webmaster CLA 2009-08-30 02:35:44 EDT
As of now 'LATER' and 'REMIND' resolutions are no longer supported.
Please reopen this bug if it is still valid for you.