<s> package org . eclipse . jdt . internal . core ; import org . eclipse . jdt . core . ICompilationUnit ; import org . eclipse . jdt . core . IJavaElement ; import org . eclipse . jdt . core . IJavaModelStatusConstants ; import org . eclipse . jdt . core . IType ; import org . eclipse . jdt . core . JavaModelException ; import org . eclipse . jdt . core . dom . ASTNode ; import org . eclipse . jdt . core . dom . SimpleName ; import org . eclipse . jdt . core . dom . rewrite . ASTRewrite ; import org . eclipse . jdt . internal . core . util . Messages ; public class CreateInitializerOperation extends CreateTypeMemberOperation { protected int numberOfInitializers = 1 ; public CreateInitializerOperation ( IType parentElement , String source ) { super ( parentElement , source , false ) ; } protected ASTNode generateElementAST ( ASTRewrite rewriter , ICompilationUnit cu ) throws JavaModelException { ASTNode node = super . generateElementAST ( rewriter , cu ) ; if ( node . getNodeType ( ) != ASTNode . INITIALIZER ) throw new JavaModelException ( new JavaModelStatus ( IJavaModelStatusConstants . INVALID_CONTENTS ) ) ; return node ; } protected IJavaElement generateResultHandle ( ) { try { getType ( ) . getCompilationUnit ( ) . close ( ) ; if ( this . anchorElement == null ) { return getType ( ) . getInitializer ( this . numberOfInitializers ) ; } else { IJavaElement [ ] children = getType ( ) . getChildren ( ) ; int count = 0 ; for ( int i = 0 ; i < children . length ; i ++ ) { IJavaElement child = children [ i ] ; if ( child . equals ( this . anchorElement ) ) { if ( child . getElementType ( ) == IJavaElement . INITIALIZER && this . insertionPolicy == CreateElementInCUOperation . INSERT_AFTER ) { count ++ ; } return getType ( ) . getInitializer ( count ) ; } else if ( child . getElementType ( ) == IJavaElement . INITIALIZER ) { count ++ ; } } } } catch ( JavaModelException e ) { } return null ; } public String getMainTaskName ( ) { return Messages . operation_createInitializerProgress ; } protected SimpleName rename ( ASTNode node , SimpleName newName ) { return null ; } protected void initializeDefaultPosition ( ) { IType parentElement = getType ( ) ; try { IJavaElement [ ] elements = parentElement . getInitializers ( ) ; if ( elements != null && elements . length > 0 ) { this . numberOfInitializers = elements . length ; createAfter ( elements [ elements . length - 1 ] ) ; } else { elements = parentElement . getChildren ( ) ; if ( elements != null && elements . length > 0 ) { createBefore ( elements [ 0 ] ) ; } } } catch ( JavaModelException e ) { } } } </s>