<s> package org . eclipse . jdt . core . dom . rewrite ; import java . util . HashMap ; import java . util . Iterator ; import java . util . List ; import java . util . Map ; import org . eclipse . jdt . core . IClassFile ; import org . eclipse . jdt . core . ICompilationUnit ; import org . eclipse . jdt . core . ITypeRoot ; import org . eclipse . jdt . core . JavaCore ; 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 . Block ; 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 . internal . compiler . parser . RecoveryScannerData ; import org . eclipse . jdt . internal . core . dom . rewrite . ASTRewriteAnalyzer ; import org . eclipse . jdt . internal . core . dom . rewrite . LineInformation ; import org . eclipse . jdt . internal . core . dom . rewrite . NodeInfoStore ; import org . eclipse . jdt . internal . core . dom . rewrite . NodeRewriteEvent ; import org . eclipse . jdt . internal . core . dom . rewrite . RewriteEventStore ; import org . eclipse . jdt . internal . core . dom . rewrite . TrackedNodePosition ; import org . eclipse . jdt . internal . core . dom . rewrite . RewriteEventStore . CopySourceInfo ; import org . eclipse . jdt . internal . core . dom . rewrite . RewriteEventStore . PropertyLocation ; import org . eclipse . jface . text . IDocument ; import org . eclipse . jface . text . TextUtilities ; import org . eclipse . text . edits . MultiTextEdit ; import org . eclipse . text . edits . TextEdit ; import org . eclipse . text . edits . TextEditGroup ; public class ASTRewrite { private final AST ast ; private final RewriteEventStore eventStore ; private final NodeInfoStore nodeStore ; private TargetSourceRangeComputer targetSourceRangeComputer = null ; private Object property1 = null ; private Object property2 = null ; public static ASTRewrite create ( AST ast ) { return new ASTRewrite ( ast ) ; } protected ASTRewrite ( AST ast ) { this . ast = ast ; this . eventStore = new RewriteEventStore ( ) ; this . nodeStore = new NodeInfoStore ( ast ) ; } public final AST getAST ( ) { return this . ast ; } protected final RewriteEventStore getRewriteEventStore ( ) { return this . eventStore ; } protected final NodeInfoStore getNodeStore ( ) { return this . nodeStore ; } public TextEdit rewriteAST ( IDocument document , Map options ) throws IllegalArgumentException { if ( document == null ) { throw new IllegalArgumentException ( ) ; } ASTNode rootNode = getRootNode ( ) ; if ( rootNode == null ) { return new MultiTextEdit ( ) ; } char [ ] content = document . get ( ) . toCharArray ( ) ; LineInformation lineInfo = LineInformation . create ( document ) ; String lineDelim = TextUtilities . getDefaultLineDelimiter ( document ) ; ASTNode astRoot = rootNode . getRoot ( ) ; List commentNodes = astRoot instanceof CompilationUnit ? ( ( CompilationUnit ) astRoot ) . getCommentList ( ) : null ; Map currentOptions = options == null ? JavaCore . getOptions ( ) : options ; return internalRewriteAST ( content , lineInfo , lineDelim , commentNodes , currentOptions , rootNode , ( RecoveryScannerData ) ( ( CompilationUnit ) astRoot ) . getStatementsRecoveryData ( ) ) ; } public TextEdit rewriteAST ( ) throws JavaModelException , IllegalArgumentException { ASTNode rootNode = getRootNode ( ) ; if ( rootNode == null ) { return new MultiTextEdit ( ) ; } ASTNode root = rootNode . getRoot ( ) ; if ( ! ( root instanceof CompilationUnit ) ) { throw new IllegalArgumentException ( "" ) ; } CompilationUnit astRoot = ( CompilationUnit ) root ; ITypeRoot typeRoot = astRoot . getTypeRoot ( ) ; if ( typeRoot == null || typeRoot . getBuffer ( ) == null ) { throw new IllegalArgumentException ( "" ) ; } char [ ] content = typeRoot . getBuffer ( ) . getCharacters ( ) ; LineInformation lineInfo = LineInformation . create ( astRoot ) ; String lineDelim = typeRoot . findRecommendedLineSeparator ( ) ; Map options = typeRoot . getJavaProject ( ) . getOptions ( true ) ; return internalRewriteAST ( content , lineInfo , lineDelim , astRoot . getCommentList ( ) , options , rootNode , ( RecoveryScannerData ) astRoot . getStatementsRecoveryData ( ) ) ; } private TextEdit internalRewriteAST ( char [ ] content , LineInformation lineInfo , String lineDelim , List commentNodes , Map options , ASTNode rootNode , RecoveryScannerData recoveryScannerData ) { TextEdit result = new MultiTextEdit ( ) ; TargetSourceRangeComputer sourceRangeComputer = getExtendedSourceRangeComputer ( ) ; this . eventStore . prepareMovedNodes ( sourceRangeComputer ) ; ASTRewriteAnalyzer visitor = new ASTRewriteAnalyzer ( content , lineInfo , lineDelim , result , this . eventStore , this . nodeStore , commentNodes , options , sourceRangeComputer , recoveryScannerData ) ; rootNode . accept ( visitor ) ; this . eventStore . revertMovedNodes ( ) ; return result ; } private ASTNode getRootNode ( ) { ASTNode node = null ; int start = - 1 ; int end = - 1 ; for ( Iterator iter = getRewriteEventStore ( ) . getChangeRootIterator ( ) ; iter . hasNext ( ) ; ) { ASTNode curr = ( ASTNode ) iter . next ( ) ; if ( ! RewriteEventStore . isNewNode ( curr ) ) { int currStart = curr . getStartPosition ( ) ; int currEnd = currStart + curr . getLength ( ) ; if ( node == null || currStart < start && currEnd > end ) { start = currStart ; end = currEnd ; node = curr ; } else if ( currStart < start ) { start = currStart ; } else if ( currEnd > end ) { end = currEnd ; } } } if ( node != null ) { int currStart = node . getStartPosition ( ) ; int currEnd = currStart + node . getLength ( ) ; while ( start < currStart || end > currEnd ) { node = node . getParent ( ) ; currStart = node . getStartPosition ( ) ; currEnd = currStart + node . getLength ( ) ; } ASTNode parent = node . getParent ( ) ; while ( parent != null && parent . getStartPosition ( ) == node . getStartPosition ( ) && parent . getLength ( ) == node . getLength ( ) ) { node = parent ; parent = node . getParent ( ) ; } } return node ; } public final void remove ( ASTNode node , TextEditGroup editGroup ) { if ( node == null ) { throw new IllegalArgumentException ( ) ; } StructuralPropertyDescriptor property ; ASTNode parent ; if ( RewriteEventStore . isNewNode ( node ) ) { PropertyLocation location = this . eventStore . getPropertyLocation ( node , RewriteEventStore . NEW ) ; if ( location != null ) { property = location . getProperty ( ) ; parent = location . getParent ( ) ; } else { throw new IllegalArgumentException ( "" ) ; } } else { property = node . getLocationInParent ( ) ; parent = node . getParent ( ) ; } if ( property . isChildListProperty ( ) ) { getListRewrite ( parent , ( ChildListPropertyDescriptor ) property ) . remove ( node , editGroup ) ; } else { set ( parent , property , null , editGroup ) ; } } public final void replace ( ASTNode node , ASTNode replacement , TextEditGroup editGroup ) { if ( node == null ) { throw new IllegalArgumentException ( ) ; } StructuralPropertyDescriptor property ; ASTNode parent ; if ( RewriteEventStore . isNewNode ( node ) ) { PropertyLocation location = this . eventStore . getPropertyLocation ( node , RewriteEventStore . NEW ) ; if ( location != null ) { property = location . getProperty ( ) ; parent = location . getParent ( ) ; } else { throw new IllegalArgumentException ( "" ) ; } } else { property = node . getLocationInParent ( ) ; parent = node . getParent ( ) ; } if ( property . isChildListProperty ( ) ) { getListRewrite ( parent , ( ChildListPropertyDescriptor ) property ) . replace ( node , replacement , editGroup ) ; } else { set ( parent , property , replacement , editGroup ) ; } } public final void set ( ASTNode node , StructuralPropertyDescriptor property , Object value , TextEditGroup editGroup ) { if ( node == null || property == null ) { throw new IllegalArgumentException ( ) ; } validateIsCorrectAST ( node ) ; validatePropertyType ( property , value ) ; validateIsPropertyOfNode ( property , node ) ; NodeRewriteEvent nodeEvent = this . eventStore . getNodeEvent ( node , property , true ) ; nodeEvent . setNewValue ( value ) ; if ( editGroup != null ) { this . eventStore . setEventEditGroup ( nodeEvent , editGroup ) ; } } public Object get ( ASTNode node , StructuralPropertyDescriptor property ) { if ( node == null || property == null ) { throw new IllegalArgumentException ( ) ; } if ( property . isChildListProperty ( ) ) { throw new IllegalArgumentException ( "" ) ; } return this . eventStore . getNewValue ( node , property ) ; } public final ListRewrite getListRewrite ( ASTNode node , ChildListPropertyDescriptor property ) { if ( node == null || property == null ) { throw new IllegalArgumentException ( ) ; } validateIsCorrectAST ( node ) ; validateIsListProperty ( property ) ; validateIsPropertyOfNode ( property , node ) ; return new ListRewrite ( this , node , property ) ; } public final Object getProperty ( String propertyName ) { if ( propertyName == null ) { throw new IllegalArgumentException ( ) ; } if ( this . property1 == null ) { return null ; } if ( this . property1 instanceof String ) { if ( propertyName . equals ( this . property1 ) ) { return this . property2 ; } else { return null ; } } Map m = ( Map ) this . property1 ; return m . get ( propertyName ) ; } public final ITrackedNodePosition track ( ASTNode node ) { if ( node == null ) { throw new IllegalArgumentException ( ) ; } TextEditGroup group = this . eventStore . getTrackedNodeData ( node ) ; if ( group == null ) { group = new TextEditGroup ( "internal" ) ; this . eventStore . setTrackedNodeData ( node , group ) ; } return new TrackedNodePosition ( group , node ) ; } private void validateIsExistingNode ( ASTNode node ) { if ( node . getStartPosition ( ) == - 1 ) { throw new IllegalArgumentException ( "" ) ; } } private void validateIsCorrectAST ( ASTNode node ) { if ( node . getAST ( ) != getAST ( ) ) { throw new IllegalArgumentException ( "" ) ; } } private void validateIsListProperty ( StructuralPropertyDescriptor property ) { if ( ! property . isChildListProperty ( ) ) { String message = property . getId ( ) + "" ; throw new IllegalArgumentException ( message ) ; } } private void validateIsPropertyOfNode ( StructuralPropertyDescriptor property , ASTNode node ) { if ( ! property . getNodeClass ( ) . isInstance ( node ) ) { String message = property . getId ( ) + "" + node . getClass ( ) . getName ( ) ; throw new IllegalArgumentException ( message ) ; } } private void validatePropertyType ( StructuralPropertyDescriptor prop , Object node ) { if ( prop . isChildListProperty ( ) ) { String message = "" ; throw new IllegalArgumentException ( message ) ; } } public final ASTNode createStringPlaceholder ( String code , int nodeType ) { if ( code == null ) { throw new IllegalArgumentException ( ) ; } ASTNode placeholder = getNodeStore ( ) . newPlaceholderNode ( nodeType ) ; if ( placeholder == null ) { throw new IllegalArgumentException ( "" + nodeType ) ; } getNodeStore ( ) . markAsStringPlaceholder ( placeholder , code ) ; return placeholder ; } public final ASTNode createGroupNode ( ASTNode [ ] targetNodes ) { if ( targetNodes == null || targetNodes . length == 0 ) { throw new IllegalArgumentException ( ) ; } Block res = getNodeStore ( ) . createCollapsePlaceholder ( ) ; ListRewrite listRewrite = getListRewrite ( res , Block . STATEMENTS_PROPERTY ) ; for ( int i = 0 ; i < targetNodes . length ; i ++ ) { listRewrite . insertLast ( targetNodes [ i ] , null ) ; } return res ; } private ASTNode createTargetNode ( ASTNode node , boolean isMove ) { if ( node == null ) { throw new IllegalArgumentException ( ) ; } validateIsExistingNode ( node ) ; validateIsCorrectAST ( node ) ; CopySourceInfo info = getRewriteEventStore ( ) . markAsCopySource ( node . getParent ( ) , node . getLocationInParent ( ) , node , isMove ) ; ASTNode placeholder = getNodeStore ( ) . newPlaceholderNode ( node . getNodeType ( ) ) ; if ( placeholder == null ) { throw new IllegalArgumentException ( "" + node . getClass ( ) . getName ( ) ) ; } getNodeStore ( ) . markAsCopyTarget ( placeholder , info ) ; return placeholder ; } public final ASTNode createCopyTarget ( ASTNode node ) { return createTargetNode ( node , false ) ; } public final ASTNode createMoveTarget ( ASTNode node ) { return createTargetNode ( node , true ) ; } public final TargetSourceRangeComputer getExtendedSourceRangeComputer ( ) { if ( this . targetSourceRangeComputer == null ) { this . targetSourceRangeComputer = new TargetSourceRangeComputer ( ) ; } return this . targetSourceRangeComputer ; } public final void setProperty ( String propertyName , Object data ) { if ( propertyName == null ) { throw new IllegalArgumentException ( ) ; } if ( this . property1 == null ) { if ( data == null ) { return ; } this . property1 = propertyName ; this . property2 = data ; return ; } if ( this . property1 instanceof String ) { if ( propertyName . equals ( this . property1 ) ) { if ( data == null ) { this . property1 = null ; this . property2 = null ; } else { this . property2 = data ; } return ; } if ( data == null ) { return ; } Map m = new HashMap ( 3 ) ; m . put ( this . property1 , this . property2 ) ; m . put ( propertyName , data ) ; this . property1 = m ; this . property2 = null ; return ; } Map m = ( Map ) this . property1 ; if ( data == null ) { m . remove ( propertyName ) ; if ( m . size ( ) == 1 ) { Map . Entry [ ] entries = ( Map . Entry [ ] ) m . entrySet ( ) . toArray ( new Map . Entry [ 1 ] ) ; this . property1 = entries [ 0 ] . getKey ( ) ; this . property2 = entries [ 0 ] . getValue ( ) ; } return ; } else { m . put ( propertyName , data ) ; return ; } } public final void setTargetSourceRangeComputer ( TargetSourceRangeComputer computer ) { this . targetSourceRangeComputer = computer ; } public String toString ( ) { StringBuffer buf = new StringBuffer ( ) ; buf . append ( "Events:n" ) ; if ( this . eventStore != null ) { buf . append ( this . eventStore . toString ( ) ) ; } return buf . toString ( ) ; } } </s>