KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > core > dom > rewrite > TargetSourceRangeComputer


1 /*******************************************************************************
2  * Copyright (c) 2005 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.rewrite;
12
13 import org.eclipse.jdt.core.dom.ASTNode;
14 import org.eclipse.jdt.core.dom.CompilationUnit;
15
16 /**
17  * An object for computing adjusted source ranges for AST nodes
18  * that are being replaced or deleted.
19  * <p>
20  * For example, a refactoring like inline method may choose to replace
21  * calls to the method but leave intact any comments immediately preceding
22  * the calls. On the other hand, a refactoring like extract method may choose
23  * to extract not only the nodes for the selected code but also any
24  * comments preceding or following them.
25  * </p>
26  * <p>
27  * Clients should subclass if they need to influence the
28  * the source range to be affected when replacing or deleting a particular node.
29  * An instance of the subclass should be registered with
30  * {@link ASTRewrite#setTargetSourceRangeComputer(TargetSourceRangeComputer)}.
31  * During a call to {@link ASTRewrite#rewriteAST(org.eclipse.jface.text.IDocument, java.util.Map)},
32  * the {@link #computeSourceRange(ASTNode)} method on this object will be
33  * used to compute the source range for a node being deleted or replaced.
34  * </p>
35  *
36  * @since 3.1
37  */

38 public class TargetSourceRangeComputer {
39     
40     /**
41      * Reified source range. Instances are &quot;value&quot; object
42      * (cannot be modified).
43      *
44      * @since 3.1
45      */

46     public static final class SourceRange {
47         /**
48          * 0-based character index, or <code>-1</code>
49          * if no source position information is known.
50          */

51         private int startPosition;
52         
53         /**
54          * (possibly 0) length, or <code>0</code>
55          * if no source position information is known.
56          */

57         private int length;
58         
59         /**
60          * Creates a new source range.
61          *
62          * @param startPosition the 0-based character index, or <code>-1</code>
63          * if no source position information is known
64          * @param length the (possibly 0) length, or <code>0</code>
65          * if no source position information is known
66          */

67         public SourceRange(int startPosition, int length) {
68             this.startPosition = startPosition;
69             this.length = length;
70         }
71         
72         /**
73          * Returns the start position.
74          *
75          * @return the 0-based character index, or <code>-1</code>
76          * if no source position information is known
77          */

78         public int getStartPosition() {
79             return this.startPosition;
80         }
81
82         /**
83          * Returns the source length.
84          *
85          * @return a (possibly 0) length, or <code>0</code>
86          * if no source position information is known
87          */

88         public int getLength() {
89             return this.length;
90         }
91     }
92
93     /**
94      * Creates a new target source range computer.
95      */

96     public TargetSourceRangeComputer() {
97         // do nothing
98
}
99
100     /**
101      * Returns the target source range of the given node. Unlike
102      * {@link ASTNode#getStartPosition()} and {@link ASTNode#getLength()},
103      * the extended source range may include comments and whitespace
104      * immediately before or after the normal source range for the node.
105      * <p>
106      * The returned source ranges must satisfy the following conditions:
107      * <dl>
108      * <li>no two source ranges in an AST may be overlapping</li>
109      * <li>a source range of a parent node must fully cover the source ranges of its children</li>
110      * </dl>
111      * </p>
112      * <p>
113      * The default implementation uses
114      * {@link CompilationUnit#getExtendedStartPosition(ASTNode)}
115      * and {@link CompilationUnit#getExtendedLength(ASTNode)}
116      * to compute the target source range. Clients may override or
117      * extend this method to expand or contract the source range of the
118      * given node. The resulting source range must cover at least the
119      * original source range of the node.
120      * </p>
121      *
122      * @param node the node with a known source range in the compilation unit
123      * being rewritten
124      * @return the exact source range in the compilation unit being rewritten
125      * that should be replaced (or deleted)
126      */

127     public SourceRange computeSourceRange(ASTNode node) {
128         ASTNode root= node.getRoot();
129         if (root instanceof CompilationUnit) {
130             CompilationUnit cu= (CompilationUnit) root;
131             return new SourceRange(cu.getExtendedStartPosition(node), cu.getExtendedLength(node));
132         }
133         return new SourceRange(node.getStartPosition(), node.getLength());
134     }
135 }
136
Popular Tags