001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.visualweb.insync.java;
043:
044: import com.sun.source.tree.ClassTree;
045: import com.sun.source.tree.ExpressionTree;
046: import com.sun.source.tree.IdentifierTree;
047: import com.sun.source.tree.LiteralTree;
048: import com.sun.source.tree.MemberSelectTree;
049: import com.sun.source.tree.MethodInvocationTree;
050: import com.sun.source.tree.MethodTree;
051: import com.sun.source.tree.ReturnTree;
052: import com.sun.source.tree.Tree;
053: import com.sun.source.tree.VariableTree;
054: import com.sun.source.util.TreePath;
055: import com.sun.source.util.TreePathScanner;
056: import java.util.HashMap;
057: import java.util.List;
058: import javax.lang.model.element.Element;
059: import org.netbeans.api.java.source.ElementHandle;
060: import org.netbeans.api.java.source.TreeMaker;
061: import org.netbeans.api.java.source.WorkingCopy;
062:
063: /**
064: *
065: * @author jdeva
066: */
067: public class Refactor {
068:
069: public static class ElementRenamer extends
070: TreePathScanner<Tree, Element> {
071: private WorkingCopy workingCopy;
072: private String newName;
073:
074: /** Creates a new instance of Refactor */
075: public ElementRenamer(WorkingCopy workingCopy, String newName) {
076: this .newName = newName;
077: this .workingCopy = workingCopy;
078: }
079:
080: @Override
081: public Tree visitIdentifier(IdentifierTree node,
082: Element elemToRename) {
083: renameIfMatch(getCurrentPath(), node, elemToRename);
084: return super .visitIdentifier(node, elemToRename);
085: }
086:
087: @Override
088: public Tree visitMemberSelect(MemberSelectTree node,
089: Element elemToRename) {
090: renameIfMatch(getCurrentPath(), node, elemToRename);
091: return super .visitMemberSelect(node, elemToRename);
092: }
093:
094: @Override
095: public Tree visitMethod(MethodTree tree, Element elemToRename) {
096: renameIfMatch(getCurrentPath(), tree, elemToRename);
097: return super .visitMethod(tree, elemToRename);
098: }
099:
100: @Override
101: public Tree visitVariable(VariableTree tree,
102: Element elemToRename) {
103: renameIfMatch(getCurrentPath(), tree, elemToRename);
104: return super .visitVariable(tree, elemToRename);
105: }
106:
107: private void renameIfMatch(TreePath path, Tree tree,
108: Element elemToRename) {
109: if (workingCopy.getTreeUtilities().isSynthetic(path))
110: return;
111: Element el = workingCopy.getTrees().getElement(path);
112: if (el != null && el.equals(elemToRename)) {
113: Tree nju = workingCopy.getTreeMaker().setLabel(tree,
114: newName);
115: workingCopy.rewrite(tree, nju);
116: }
117: }
118: }
119:
120: public static class LiteralRenamer extends
121: TreePathScanner<Tree, Void> {
122: private WorkingCopy workingCopy;
123: private String newName, oldName;
124:
125: public LiteralRenamer(WorkingCopy workingCopy, String oldName,
126: String newName) {
127: this .oldName = oldName;
128: this .newName = newName;
129: this .workingCopy = workingCopy;
130: }
131:
132: @Override
133: public Tree visitLiteral(LiteralTree tree, Void v) {
134: renameIfMatch(tree);
135: return super .visitLiteral(tree, v);
136: }
137:
138: private void renameIfMatch(LiteralTree tree) {
139: if (tree.getKind() == Tree.Kind.STRING_LITERAL
140: && oldName.equals(tree.getValue())) {
141: Tree nju = workingCopy.getTreeMaker().Literal(newName);
142: workingCopy.rewrite(tree, nju);
143: }
144: }
145: }
146:
147: /**
148: * Visitor class to update all return statements returning the old string literal
149: * with the new string literal
150: * */
151: public static class ReturnStatementLiteralRenamer extends
152: TreePathScanner<Tree, Void> {
153: private WorkingCopy workingCopy;
154: private String newName, oldName;
155:
156: public ReturnStatementLiteralRenamer(WorkingCopy workingCopy,
157: String oldName, String newName) {
158: this .oldName = oldName;
159: this .newName = newName;
160: this .workingCopy = workingCopy;
161: }
162:
163: @Override
164: public Tree visitReturn(ReturnTree tree, Void v) {
165: renameIfMatch(tree);
166: return super .visitReturn(tree, v);
167: }
168:
169: private void renameIfMatch(ReturnTree tree) {
170: ExpressionTree expr = tree.getExpression();
171: if (expr.getKind() == Tree.Kind.STRING_LITERAL) {
172: LiteralTree literal = (LiteralTree) expr;
173: if (oldName.equals(literal.getValue())) {
174: Tree newLiteral = workingCopy.getTreeMaker()
175: .Literal(newName);
176: workingCopy.rewrite(literal, newLiteral);
177: }
178: }
179: }
180: }
181:
182: public static class ElementsRenamer extends
183: TreePathScanner<Tree, Void> {
184: private final WorkingCopy workingCopy;
185: private final TreeMaker make;
186: private HashMap<Element, String> elementAndNames = new HashMap<Element, String>();
187:
188: /** Creates a new instance of Refactor */
189: public ElementsRenamer(WorkingCopy workingCopy,
190: HashMap<? extends ElementHandle, String> handleAndNames) {
191: this .workingCopy = workingCopy;
192: make = workingCopy.getTreeMaker();
193: for (ElementHandle elemHandle : handleAndNames.keySet()) {
194: Element elem = elemHandle.resolve(workingCopy);
195: elementAndNames.put(elem, handleAndNames
196: .get(elemHandle));
197: }
198: }
199:
200: @Override
201: public Tree visitIdentifier(IdentifierTree tree, Void v) {
202: renameIfMatch(getCurrentPath(), tree);
203: return super .visitIdentifier(tree, v);
204: }
205:
206: @Override
207: public Tree visitMemberSelect(MemberSelectTree tree, Void v) {
208: renameIfMatch(getCurrentPath(), tree);
209: return super .visitMemberSelect(tree, v);
210: }
211:
212: @Override
213: public Tree visitMethod(MethodTree tree, Void v) {
214: renameIfMatch(getCurrentPath(), tree);
215: return super .visitMethod(tree, v);
216: }
217:
218: @Override
219: public Tree visitVariable(VariableTree tree, Void v) {
220: renameIfMatch(getCurrentPath(), tree);
221: return super .visitVariable(tree, v);
222: }
223:
224: private void renameIfMatch(TreePath path, Tree tree) {
225: if (workingCopy.getTreeUtilities().isSynthetic(path)) {
226: return;
227: }
228: Element el = workingCopy.getTrees().getElement(path);
229: if (el != null && elementAndNames.containsKey(el)) {
230: Tree nju = make.setLabel(tree, elementAndNames.get(el));
231: workingCopy.rewrite(tree, nju);
232: }
233: }
234: }
235: }
|