<s> package org . eclipse . jdt . internal . core ; import org . eclipse . core . resources . IResource ; import org . eclipse . core . resources . IWorkspace ; import org . eclipse . core . runtime . jobs . ISchedulingRule ; import org . eclipse . jdt . core . ICompilationUnit ; import org . eclipse . jdt . core . IJavaElement ; import org . eclipse . jdt . core . IJavaModelStatus ; import org . eclipse . jdt . core . IJavaModelStatusConstants ; import org . eclipse . jdt . core . JavaModelException ; import org . eclipse . jdt . core . dom . AST ; import org . eclipse . jdt . core . dom . ASTNode ; import org . eclipse . jdt . core . dom . ASTParser ; import org . eclipse . jdt . core . dom . ChildListPropertyDescriptor ; import org . eclipse . jdt . core . dom . CompilationUnit ; import org . eclipse . jdt . core . dom . StructuralPropertyDescriptor ; import org . eclipse . jdt . core . dom . rewrite . ASTRewrite ; import org . eclipse . jdt . core . dom . rewrite . ListRewrite ; import org . eclipse . jdt . internal . core . util . Util ; import org . eclipse . text . edits . TextEdit ; public abstract class CreateElementInCUOperation extends JavaModelOperation { protected CompilationUnit cuAST ; protected static final int INSERT_LAST = 1 ; protected static final int INSERT_AFTER = 2 ; protected static final int INSERT_BEFORE = 3 ; protected int insertionPolicy = INSERT_LAST ; protected IJavaElement anchorElement = null ; protected boolean creationOccurred = true ; public CreateElementInCUOperation ( IJavaElement parentElement ) { super ( null , new IJavaElement [ ] { parentElement } ) ; initializeDefaultPosition ( ) ; } protected void checkCanceled ( ) { if ( ! this . isNested ) { super . checkCanceled ( ) ; } } public void createAfter ( IJavaElement sibling ) { setRelativePosition ( sibling , INSERT_AFTER ) ; } public void createBefore ( IJavaElement sibling ) { setRelativePosition ( sibling , INSERT_BEFORE ) ; } protected void executeOperation ( ) throws JavaModelException { try { beginTask ( getMainTaskName ( ) , getMainAmountOfWork ( ) ) ; JavaElementDelta delta = newJavaElementDelta ( ) ; ICompilationUnit unit = getCompilationUnit ( ) ; generateNewCompilationUnitAST ( unit ) ; if ( this . creationOccurred ) { unit . save ( null , false ) ; boolean isWorkingCopy = unit . isWorkingCopy ( ) ; if ( ! isWorkingCopy ) setAttribute ( HAS_MODIFIED_RESOURCE_ATTR , TRUE ) ; worked ( 1 ) ; this . resultElements = generateResultHandles ( ) ; if ( ! isWorkingCopy && ! Util . isExcluded ( unit ) && unit . getParent ( ) . exists ( ) ) { for ( int i = 0 ; i < this . resultElements . length ; i ++ ) { delta . added ( this . resultElements [ i ] ) ; } addDelta ( delta ) ; } } } finally { done ( ) ; } } protected abstract StructuralPropertyDescriptor getChildPropertyDescriptor ( ASTNode parent ) ; protected abstract ASTNode generateElementAST ( ASTRewrite rewriter , ICompilationUnit cu ) throws JavaModelException ; protected void generateNewCompilationUnitAST ( ICompilationUnit cu ) throws JavaModelException { this . cuAST = parse ( cu ) ; AST ast = this . cuAST . getAST ( ) ; ASTRewrite rewriter = ASTRewrite . create ( ast ) ; ASTNode child = generateElementAST ( rewriter , cu ) ; if ( child != null ) { ASTNode parent = ( ( JavaElement ) getParentElement ( ) ) . findNode ( this . cuAST ) ; if ( parent == null ) parent = this . cuAST ; insertASTNode ( rewriter , parent , child ) ; TextEdit edits = rewriter . rewriteAST ( ) ; applyTextEdit ( cu , edits ) ; } worked ( 1 ) ; } protected abstract IJavaElement generateResultHandle ( ) ; protected IJavaElement [ ] generateResultHandles ( ) { return new IJavaElement [ ] { generateResultHandle ( ) } ; } protected ICompilationUnit getCompilationUnit ( ) { return getCompilationUnitFor ( getParentElement ( ) ) ; } protected int getMainAmountOfWork ( ) { return 2 ; } public abstract String getMainTaskName ( ) ; protected ISchedulingRule getSchedulingRule ( ) { IResource resource = getCompilationUnit ( ) . getResource ( ) ; IWorkspace workspace = resource . getWorkspace ( ) ; return workspace . getRuleFactory ( ) . modifyRule ( resource ) ; } protected void initializeDefaultPosition ( ) { } protected void insertASTNode ( ASTRewrite rewriter , ASTNode parent , ASTNode child ) throws JavaModelException { StructuralPropertyDescriptor propertyDescriptor = getChildPropertyDescriptor ( parent ) ; if ( propertyDescriptor instanceof ChildListPropertyDescriptor ) { ChildListPropertyDescriptor childListPropertyDescriptor = ( ChildListPropertyDescriptor ) propertyDescriptor ; ListRewrite rewrite = rewriter . getListRewrite ( parent , childListPropertyDescriptor ) ; switch ( this . insertionPolicy ) { case INSERT_BEFORE : ASTNode element = ( ( JavaElement ) this . anchorElement ) . findNode ( this . cuAST ) ; if ( childListPropertyDescriptor . getElementType ( ) . isAssignableFrom ( element . getClass ( ) ) ) rewrite . insertBefore ( child , element , null ) ; else rewrite . insertLast ( child , null ) ; break ; case INSERT_AFTER : element = ( ( JavaElement ) this . anchorElement ) . findNode ( this . cuAST ) ; if ( childListPropertyDescriptor . getElementType ( ) . isAssignableFrom ( element . getClass ( ) ) ) rewrite . insertAfter ( child , element , null ) ; else rewrite . insertLast ( child , null ) ; break ; case INSERT_LAST : rewrite . insertLast ( child , null ) ; break ; } } else { rewriter . set ( parent , propertyDescriptor , child , null ) ; } } protected CompilationUnit parse ( ICompilationUnit cu ) throws JavaModelException { cu . makeConsistent ( this . progressMonitor ) ; ASTParser parser = ASTParser . newParser ( AST . JLS4 ) ; parser . setSource ( cu ) ; return ( CompilationUnit ) parser . createAST ( this . progressMonitor ) ; } protected void setAlteredName ( String newName ) { } protected void setRelativePosition ( IJavaElement sibling , int policy ) throws IllegalArgumentException { if ( sibling == null ) { this . anchorElement = null ; this . insertionPolicy = INSERT_LAST ; } else { this . anchorElement = sibling ; this . insertionPolicy = policy ; } } public IJavaModelStatus verify ( ) { if ( getParentElement ( ) == null ) { return new JavaModelStatus ( IJavaModelStatusConstants . NO_ELEMENTS_TO_PROCESS ) ; } if ( this . anchorElement != null ) { IJavaElement domPresentParent = this . anchorElement . getParent ( ) ; if ( domPresentParent . getElementType ( ) == IJavaElement . IMPORT_CONTAINER ) { domPresentParent = domPresentParent . getParent ( ) ; } if ( ! domPresentParent . equals ( getParentElement ( ) ) ) { return new JavaModelStatus ( IJavaModelStatusConstants . INVALID_SIBLING , this . anchorElement ) ; } } return JavaModelStatus . VERIFIED_OK ; } } </s>