KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > refactoring > java > plugins > MoveTransformer


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.refactoring.java.plugins;
21
22 import com.sun.source.tree.*;
23 import java.util.HashSet JavaDoc;
24 import java.util.Set JavaDoc;
25 import javax.lang.model.element.*;
26 import org.netbeans.api.java.source.ElementHandle;
27 import org.netbeans.api.java.source.SourceUtils;
28 import org.netbeans.api.java.source.WorkingCopy;
29 import org.netbeans.modules.refactoring.java.RetoucheUtils;
30 import org.openide.filesystems.FileObject;
31
32 /**
33  *
34  * @author Jan Becicka
35  */

36 public class MoveTransformer extends SearchVisitor {
37
38     private FileObject originalFolder;
39     private MoveRefactoringPlugin move;
40     private Set JavaDoc<Element> elementsToImport = new HashSet JavaDoc();
41     private boolean isThisFileMoving;
42     private boolean isThisFileReferencingOldPackage = false;
43     
44
45     public MoveTransformer(WorkingCopy workingCopy, MoveRefactoringPlugin move) {
46         super(workingCopy);
47         originalFolder = workingCopy.getFileObject().getParent();
48         this.move = move;
49         isThisFileMoving = move.filesToMove.contains(workingCopy.getFileObject());
50     }
51     
52     @Override JavaDoc
53     public Tree visitMemberSelect(MemberSelectTree node, Element p) {
54         if (!workingCopy.getTreeUtilities().isSynthetic(getCurrentPath())) {
55             Element el = workingCopy.getTrees().getElement(getCurrentPath());
56             if (el!=null) {
57                 if (isElementMoving(el)) {
58                     Tree nju = make.MemberSelect(make.Identifier(move.getTargetPackageName(move.filesToMove.get(index))), el);
59                     workingCopy.rewrite(node, nju);
60                 }
61             }
62         }
63         return super.visitMemberSelect(node, p);
64     }
65     
66     
67     @Override JavaDoc
68     public Tree visitIdentifier(IdentifierTree node, Element p) {
69         if (!workingCopy.getTreeUtilities().isSynthetic(getCurrentPath())) {
70             Element el = workingCopy.getTrees().getElement(getCurrentPath());
71             if (el!=null) {
72                 if (!isThisFileMoving) {
73                     if (isElementMoving(el)) {
74                         elementsToImport.add(el);
75                     }
76                 } else {
77                     if (!isThisFileReferencingOldPackage && (!isElementMoving(el) && isTopLevelClass(el)) && getPackageOf(el).toString().equals(RetoucheUtils.getPackageName(workingCopy.getFileObject().getParent()))) {
78                         isThisFileReferencingOldPackage = true;
79                     }
80                 }
81             }
82         }
83         
84         return super.visitIdentifier(node, p);
85     }
86     
87     private PackageElement getPackageOf(Element el) {
88         //return workingCopy.getElements().getPackageOf(el);
89
while (el.getKind() != ElementKind.PACKAGE)
90             el = el.getEnclosingElement();
91         return (PackageElement) el;
92     }
93     
94     
95     
96     private boolean isThisFileReferencedbyOldPackage() {
97         Set JavaDoc<FileObject> references = new HashSet JavaDoc(move.whoReferences.get(workingCopy.getFileObject()));
98         references.removeAll(move.filesToMove);
99         for (FileObject file:references) {
100             if (file.getParent().equals(originalFolder))
101                 return true;
102         }
103         return false;
104     }
105     
106 // private boolean isThisFileReferencingOldPackage() {
107
// //TODO: correctly implement
108
// return true;
109
// }
110

111     private int index;
112     private boolean isElementMoving(Element el) {
113         index=0;
114         for (ElementHandle handle:move.classes.values()) {
115             if (handle.signatureEquals(el)) {
116                 return true;
117             }
118             index++;
119         }
120         index=-1;
121         return false;
122     }
123     
124     private boolean isTopLevelClass(Element el) {
125         return (el.getKind().isClass() ||
126                 el.getKind().isInterface()) &&
127                 el.getEnclosingElement().getKind() == ElementKind.PACKAGE;
128     }
129     
130     @Override JavaDoc
131     public Tree visitCompilationUnit(CompilationUnitTree node, Element p) {
132         Tree result = super.visitCompilationUnit(node, p);
133         if (workingCopy.getTreeUtilities().isSynthetic(getCurrentPath())) {
134             return result;
135         }
136         if (isThisFileMoving) {
137             // change package statement if old and new package exist, i.e.
138
// neither old nor new package is default
139
String JavaDoc newPckg = move.getTargetPackageName(workingCopy.getFileObject());
140             if (node.getPackageName() != null && !"".equals(newPckg)) {
141                 workingCopy.rewrite(node.getPackageName(), make.Identifier(move.getTargetPackageName(workingCopy.getFileObject())));
142             } else {
143                 // in order to handle default package, we have to rewrite whole
144
// compilation unit:
145
CompilationUnitTree copy = make.CompilationUnit(
146                         "".equals(newPckg) ? null : make.Identifier(newPckg),
147                         node.getImports(),
148                         node.getTypeDecls(),
149                         node.getSourceFile()
150                 );
151                 workingCopy.rewrite(node, copy);
152             }
153             if (isThisFileReferencingOldPackage) {
154                 //add import to old package
155
node = insertImport(node, node.getPackageName().toString() + ".*");
156             }
157         }
158         for (Element el:elementsToImport) {
159             FileObject fo = SourceUtils.getFile(el, workingCopy.getClasspathInfo());
160             node = insertImport(node, move.getTargetPackageName(fo) + "." +el.getSimpleName());
161         }
162         return result;
163     }
164     
165     private CompilationUnitTree insertImport(CompilationUnitTree node, String JavaDoc imp) {
166         for (ImportTree tree: node.getImports()) {
167             if (tree.getQualifiedIdentifier().toString().equals(imp))
168                 return node;
169         }
170         CompilationUnitTree nju = make.insertCompUnitImport(node, 0, make.Import(make.Identifier(imp), false));
171         workingCopy.rewrite(node, nju);
172         return nju;
173     }
174     
175 }
176
Popular Tags