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-2006 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.refactoring.java.plugins;
043:
044: import org.netbeans.modules.refactoring.java.spi.RefactoringVisitor;
045: import com.sun.source.tree.*;
046: import com.sun.source.util.TreePath;
047: import java.util.logging.Logger;
048: import javax.lang.model.element.*;
049: import org.netbeans.api.java.source.WorkingCopy;
050: import org.netbeans.modules.refactoring.java.RetoucheUtils;
051: import org.netbeans.modules.refactoring.java.spi.ToPhaseException;
052: import org.openide.util.Exceptions;
053:
054: /**
055: *
056: * @author Jan Becicka
057: */
058: public class CopyTransformer extends RefactoringVisitor {
059:
060: private String newName;
061: private boolean insertImport;
062: private String oldPackage;
063: private String oldName;
064: private String newPackage;
065:
066: public CopyTransformer(WorkingCopy workingCopy, String oldName,
067: String newName, boolean insertImport, String oldPackage) {
068: try {
069: setWorkingCopy(workingCopy);
070: this .newName = newName;
071: this .insertImport = insertImport;
072: this .oldPackage = oldPackage;
073: this .oldName = oldName;
074: this .newPackage = RetoucheUtils.getPackageName(workingCopy
075: .getFileObject().getParent());
076: } catch (ToPhaseException ex) {
077: Exceptions.printStackTrace(ex);
078: }
079: }
080:
081: @Override
082: public Tree visitCompilationUnit(CompilationUnitTree tree, Element p) {
083: if (!workingCopy.getTreeUtilities().isSynthetic(
084: getCurrentPath())) {
085: if (insertImport) {
086: Element el = workingCopy.getTrees().getElement(
087: getCurrentPath());
088: Tree tree2 = make.insertCompUnitImport(tree, 0, make
089: .Import(make.Identifier(oldPackage + ".*"),
090: false)); // NOI18N
091: rewrite(tree, tree2);
092: }
093: }
094: return super .visitCompilationUnit(tree, p);
095: }
096:
097: @Override
098: public Tree visitClass(ClassTree tree, Element p) {
099: if (!workingCopy.getTreeUtilities().isSynthetic(
100: getCurrentPath())) {
101: TypeElement currentClass = (TypeElement) workingCopy
102: .getTrees().getElement(getCurrentPath());
103: if (currentClass == null) {
104: Logger.getLogger(
105: "org.netbeans.modules.refactoring.java")
106: .severe(
107: "Cannot resolve tree "
108: + tree
109: + "\n file: "
110: + workingCopy.getFileObject()
111: .getPath()); // NOI18N
112: } else {
113: if (!currentClass.getNestingKind().isNested()
114: && ((workingCopy.getCompilationUnit()
115: .getTypeDecls().size() == 1) || tree
116: .getSimpleName().toString().equals(
117: oldName))) {
118: Tree nju = make.setLabel(tree, newName);
119: rewrite(tree, nju);
120: }
121: }
122: }
123: return super .visitClass(tree, p);
124: }
125:
126: @Override
127: public Tree visitIdentifier(IdentifierTree node, Element p) {
128: renameUsageIfMatch(getCurrentPath(), node, p);
129: return super .visitIdentifier(node, p);
130: }
131:
132: @Override
133: public Tree visitMemberSelect(MemberSelectTree node, Element p) {
134: renameUsageIfMatch(getCurrentPath(), node, p);
135: return super .visitMemberSelect(node, p);
136: }
137:
138: private void renameUsageIfMatch(TreePath path, Tree tree,
139: Element elementToFind) {
140: if (workingCopy.getTreeUtilities().isSynthetic(path))
141: return;
142: Element el = workingCopy.getTrees().getElement(path);
143: if (el == null)
144: return;
145:
146: if ((el instanceof TypeElement)
147: && ((TypeElement) el).getQualifiedName().toString()
148: .equals(newPackage + "." + oldName)) { // NOI18N
149: Tree nju = make.setLabel(tree, newName);
150: rewrite(tree, nju);
151: }
152: }
153:
154: }
|