KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > core > dom > InternalASTRewrite


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.core.dom;
12 import java.util.Hashtable JavaDoc;
13 import java.util.List JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import org.eclipse.text.edits.MultiTextEdit;
17 import org.eclipse.text.edits.TextEdit;
18
19 import org.eclipse.jface.text.IDocument;
20 import org.eclipse.jface.text.TextUtilities;
21
22 import org.eclipse.jdt.core.dom.SimplePropertyDescriptor;
23 import org.eclipse.jdt.core.dom.StructuralPropertyDescriptor;
24 import org.eclipse.jdt.core.dom.rewrite.TargetSourceRangeComputer;
25 import org.eclipse.jdt.internal.core.dom.rewrite.ASTRewriteAnalyzer;
26 import org.eclipse.jdt.internal.core.dom.rewrite.LineInformation;
27 import org.eclipse.jdt.internal.core.dom.rewrite.ListRewriteEvent;
28 import org.eclipse.jdt.internal.core.dom.rewrite.NodeInfoStore;
29 import org.eclipse.jdt.internal.core.dom.rewrite.NodeRewriteEvent;
30 import org.eclipse.jdt.internal.core.dom.rewrite.RewriteEventStore;
31 import org.eclipse.jdt.internal.core.dom.rewrite.RewriteEventStore.CopySourceInfo;
32 import org.eclipse.jdt.internal.core.dom.rewrite.RewriteEventStore.PropertyLocation;
33
34 /**
35  * Internal class: not intended to be used by client.
36  * When AST modifications recording is enabled, all changes are recorded by this class.
37  */

38 class InternalASTRewrite extends NodeEventHandler {
39     
40     /** root node for the rewrite: Only nodes under this root are accepted */
41     private CompilationUnit root;
42
43     protected final RewriteEventStore eventStore;
44     protected final NodeInfoStore nodeStore;
45     protected final Hashtable JavaDoc clonedNodes;
46     
47     int cloneDepth = 0;
48     
49     /**
50      * Constructor
51      * @param root root node of the recorded ast.
52      */

53     public InternalASTRewrite(CompilationUnit root) {
54         this.root = root;
55         this.eventStore = new RewriteEventStore();
56         this.nodeStore = new NodeInfoStore(root.getAST());
57         this.clonedNodes = new Hashtable JavaDoc();
58     }
59
60     /**
61      * Performs the rewrite: The rewrite events are translated to the corresponding in text changes.
62      * @param document Document which describes the code of the AST that is passed in in the
63      * constructor. This document is accessed read-only.
64      * @param options options
65      * @throws IllegalArgumentException if the rewrite fails
66      * @return Returns the edit describing the text changes.
67      */

68     public TextEdit rewriteAST(IDocument document, Map JavaDoc options) {
69         TextEdit result = new MultiTextEdit();
70         
71         final CompilationUnit rootNode = getRootNode();
72         if (rootNode != null) {
73             TargetSourceRangeComputer xsrComputer = new TargetSourceRangeComputer() {
74                 /**
75                  * This implementation of
76                  * {@link TargetSourceRangeComputer#computeSourceRange(ASTNode)}
77                  * is specialized to work in the case of internal AST rewriting, where the
78                  * original AST has been modified from its original form. This means that
79                  * one cannot trust that the root of the given node is the compilation unit.
80                  */

81                 public SourceRange computeSourceRange(ASTNode node) {
82                     int extendedStartPosition = rootNode.getExtendedStartPosition(node);
83                     int extendedLength = rootNode.getExtendedLength(node);
84                     return new SourceRange(extendedStartPosition, extendedLength);
85                 }
86             };
87             char[] content= document.get().toCharArray();
88             LineInformation lineInfo= LineInformation.create(document);
89             String JavaDoc lineDelim= TextUtilities.getDefaultLineDelimiter(document);
90             List JavaDoc comments= rootNode.getCommentList();
91             
92             ASTRewriteAnalyzer visitor = new ASTRewriteAnalyzer(content, lineInfo, lineDelim, result, this.eventStore, this.nodeStore, comments, options, xsrComputer);
93             rootNode.accept(visitor);
94         }
95         return result;
96     }
97     
98     private void markAsMoveOrCopyTarget(ASTNode node, ASTNode newChild) {
99         ASTNode source = (ASTNode)this.clonedNodes.get(newChild);
100         if(source != null) {
101             if(this.cloneDepth == 0) {
102                 PropertyLocation propertyLocation = this.eventStore.getPropertyLocation(source, RewriteEventStore.ORIGINAL);
103                 CopySourceInfo sourceInfo =
104                     this.eventStore.markAsCopySource(
105                         propertyLocation.getParent(),
106                         propertyLocation.getProperty(),
107                         source,
108                         false);
109                 this.nodeStore.markAsCopyTarget(newChild, sourceInfo);
110             }
111         } else if((newChild.getFlags() & ASTNode.ORIGINAL) != 0) {
112             PropertyLocation propertyLocation = this.eventStore.getPropertyLocation(newChild, RewriteEventStore.ORIGINAL);
113             CopySourceInfo sourceInfo =
114                 this.eventStore.markAsCopySource(
115                     propertyLocation.getParent(),
116                     propertyLocation.getProperty(),
117                     newChild,
118                     true);
119             this.nodeStore.markAsCopyTarget(newChild, sourceInfo);
120         }
121     }
122
123     private CompilationUnit getRootNode() {
124         return this.root;
125     }
126
127     public String JavaDoc toString() {
128         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
129         buf.append("Events:\n"); //$NON-NLS-1$
130
buf.append(this.eventStore.toString());
131         return buf.toString();
132     }
133     
134     void preValueChangeEvent(ASTNode node, SimplePropertyDescriptor property) {
135         // force event creation
136
this.getNodeEvent(node, property);
137     }
138     
139     void postValueChangeEvent(ASTNode node, SimplePropertyDescriptor property) {
140         NodeRewriteEvent event = this.getNodeEvent(node, property);
141         event.setNewValue(node.getStructuralProperty(property));
142     }
143     
144     void preAddChildEvent(ASTNode node, ASTNode child, StructuralPropertyDescriptor property) {
145         if(property.isChildProperty()) {
146             NodeRewriteEvent event = this.getNodeEvent(node, property);
147             event.setNewValue(child);
148             if(child != null) {
149                 this.markAsMoveOrCopyTarget(node, child);
150             }
151         } else if(property.isChildListProperty()) {
152             // force event creation
153
this.getListEvent(node, property);
154         }
155     }
156     
157     void postAddChildEvent(ASTNode node, ASTNode child, StructuralPropertyDescriptor property) {
158         if(property.isChildListProperty()) {
159
160             ListRewriteEvent event = this.getListEvent(node, property);
161             List JavaDoc list = (List JavaDoc)node.getStructuralProperty(property);
162             int i = list.indexOf(child);
163             int s = list.size();
164             int index;
165             if(i + 1 < s) {
166                 ASTNode nextNode = (ASTNode)list.get(i + 1);
167                 index = event.getIndex(nextNode, ListRewriteEvent.NEW);
168             } else {
169                 index = -1;
170             }
171             event.insert(child, index);
172             if(child != null) {
173                 this.markAsMoveOrCopyTarget(node, child);
174             }
175         }
176     }
177     
178     void preRemoveChildEvent(ASTNode node, ASTNode child, StructuralPropertyDescriptor property) {
179         if(property.isChildProperty()) {
180             NodeRewriteEvent event = getNodeEvent(node, property);
181             event.setNewValue(null);
182         } else if(property.isChildListProperty()) {
183             ListRewriteEvent event = this.getListEvent(node, property);
184             int i = event.getIndex(child, ListRewriteEvent.NEW);
185             NodeRewriteEvent nodeEvent = (NodeRewriteEvent)event.getChildren()[i];
186             if(nodeEvent.getOriginalValue() == null) {
187                 event.revertChange(nodeEvent);
188             } else {
189                 nodeEvent.setNewValue(null);
190             }
191         }
192     }
193     
194     void preReplaceChildEvent(ASTNode node, ASTNode child, ASTNode newChild, StructuralPropertyDescriptor property) {
195         if(property.isChildProperty()) {
196             NodeRewriteEvent event = getNodeEvent(node, property);
197             event.setNewValue(newChild);
198             if(newChild != null) {
199                 this.markAsMoveOrCopyTarget(node, newChild);
200             }
201         } else if(property.isChildListProperty()) {
202             ListRewriteEvent event = this.getListEvent(node, property);
203             int i = event.getIndex(child, ListRewriteEvent.NEW);
204             NodeRewriteEvent nodeEvent = (NodeRewriteEvent)event.getChildren()[i];
205             nodeEvent.setNewValue(newChild);
206             if(newChild != null) {
207                 this.markAsMoveOrCopyTarget(node, newChild);
208             }
209         }
210     }
211     
212     
213     void preCloneNodeEvent(ASTNode node) {
214         this.cloneDepth++;
215     }
216     
217     
218     void postCloneNodeEvent(ASTNode node, ASTNode clone) {
219         if(node.ast == root.ast && clone.ast == root.ast) {
220             if((node.getFlags() & ASTNode.ORIGINAL) != 0) {
221                 this.clonedNodes.put(clone, node);
222             } else {
223                 // node can be a cloned node
224
Object JavaDoc original = this.clonedNodes.get(node);
225                 if(original != null) {
226                     this.clonedNodes.put(clone, original);
227                 }
228             }
229         }
230         this.cloneDepth--;
231     }
232     
233     private NodeRewriteEvent getNodeEvent(ASTNode node, StructuralPropertyDescriptor property) {
234         return this.eventStore.getNodeEvent(node, property, true);
235     }
236     
237     private ListRewriteEvent getListEvent(ASTNode node, StructuralPropertyDescriptor property) {
238         return this.eventStore.getListEvent(node, property, true);
239     }
240 }
241
Popular Tags