001: /*
002: * Author: Chris Seguin
003: *
004: * This software has been developed under the copyleft
005: * rules of the GNU General Public License. Please
006: * consult the GNU General Public License for more
007: * details about use and distribution of this software.
008: */
009: package org.acm.seguin.refactor;
010:
011: import java.util.LinkedList;
012: import java.util.Iterator;
013: import java.io.File;
014: import org.acm.seguin.summary.PackageSummary;
015: import org.acm.seguin.summary.FileSummary;
016: import org.acm.seguin.summary.ImportSummary;
017: import org.acm.seguin.summary.TypeSummary;
018: import org.acm.seguin.refactor.type.TypeChangeVisitor;
019:
020: /**
021: * Description of the Class
022: *
023: *@author Chris Seguin
024: */
025: public class EliminatePackageImportVisitor extends TypeChangeVisitor {
026: private PackageSummary packageSummary;
027: private LinkedList filterList = new LinkedList();
028:
029: /**
030: * Constructor for the EliminatePackageImportVisitor object
031: *
032: *@param complex Description of Parameter
033: */
034: public EliminatePackageImportVisitor(ComplexTransform complex) {
035: super (complex);
036: }
037:
038: /**
039: * Sets the Package attribute of the EliminatePackageImportVisitor object
040: *
041: *@param summary The new Package value
042: */
043: public void setPackageSummary(PackageSummary summary) {
044: packageSummary = summary;
045: }
046:
047: /**
048: * Adds a feature to the FilterClass attribute of the
049: * EliminatePackageImportVisitor object
050: *
051: *@param name The feature to be added to the FilterClass attribute
052: */
053: public void addFilterClass(String name) {
054: filterList.add(name);
055: }
056:
057: /**
058: * Gets the File Specific Transform
059: *
060: *@param summary Description of Parameter
061: *@return The FileSpecificTransform value
062: */
063: protected TransformAST getFileSpecificTransform(FileSummary summary) {
064: return new RemoveImportTransform(packageSummary);
065: }
066:
067: /**
068: * Gets the New Imports transform
069: *
070: *@param node Description of Parameter
071: *@param className Description of Parameter
072: *@return The NewImports value
073: */
074: protected AddImportTransform getNewImports(FileSummary node,
075: String className) {
076: return new AddImportTransform(packageSummary.getName(),
077: className);
078: }
079:
080: /**
081: * Gets the Remove Imports transform
082: *
083: *@param node Description of Parameter
084: *@return The transform
085: */
086: protected RemoveImportTransform getRemoveImportTransform(
087: ImportSummary node) {
088: return null;
089: }
090:
091: /**
092: * Gets the list of classes to iterate over
093: *
094: *@param node Description of Parameter
095: *@return The list
096: */
097: protected LinkedList getAppropriateClasses(FileSummary node) {
098: LinkedList list = new LinkedList();
099:
100: Iterator fileIterator = packageSummary.getFileSummaries();
101: if (fileIterator != null) {
102: while (fileIterator.hasNext()) {
103: FileSummary fileSummary = (FileSummary) fileIterator
104: .next();
105:
106: addTypesFromFile(fileSummary, list);
107: }
108: }
109:
110: return list;
111: }
112:
113: /**
114: * Gets the reference to the file where the refactored output should be sent
115: *
116: *@param node Description of Parameter
117: *@return The NewFile value
118: */
119: protected File getNewFile(FileSummary node) {
120: return node.getFile();
121: }
122:
123: /**
124: * Return the current package
125: *
126: *@return the current package of the class
127: */
128: protected String getCurrentPackage() {
129: return packageSummary.getName();
130: }
131:
132: /**
133: * Checks any preconditions
134: *
135: *@param summary Description of Parameter
136: *@return Description of the Returned Value
137: */
138: protected boolean preconditions(FileSummary summary) {
139: if (summary.getParent() == packageSummary) {
140: return false;
141: }
142:
143: Iterator iter = summary.getImports();
144: if (iter != null) {
145: while (iter.hasNext()) {
146: ImportSummary next = (ImportSummary) iter.next();
147: if (isImportingPackage(next)) {
148: return true;
149: }
150: }
151: }
152:
153: return false;
154: }
155:
156: /**
157: * Gets the RenamingTransform
158: *
159: *@param refactoring The feature to be added to the RenamingTransforms
160: * attribute
161: *@param node The feature to be added to the RenamingTransforms
162: * attribute
163: *@param className The feature to be added to the RenamingTransforms
164: * attribute
165: */
166: protected void addRenamingTransforms(ComplexTransform refactoring,
167: FileSummary node, String className) {
168: }
169:
170: /**
171: * Gets the InFilter attribute of the EliminatePackageImportVisitor object
172: *
173: *@param type Description of Parameter
174: *@return The InFilter value
175: */
176: private boolean isInFilter(TypeSummary type) {
177: Iterator iter = filterList.iterator();
178: String name = type.getName();
179: while (iter.hasNext()) {
180: if (name.equals(iter.next())) {
181: return true;
182: }
183: }
184: return false;
185: }
186:
187: /**
188: * Determines if we are importing the package that we are eliminiating
189: *
190: *@param next the import statement in question
191: *@return true if this is the import statement
192: */
193: private boolean isImportingPackage(ImportSummary next) {
194: return (next.getType() == null)
195: && (next.getPackage() == packageSummary);
196: }
197:
198: /**
199: * Adds a feature to the TypesFromFile attribute of the
200: * EliminatePackageImportVisitor object
201: *
202: *@param fileSummary The feature to be added to the TypesFromFile attribute
203: *@param list The feature to be added to the TypesFromFile attribute
204: */
205: private void addTypesFromFile(FileSummary fileSummary,
206: LinkedList list) {
207: Iterator typeIterator = fileSummary.getTypes();
208: if (typeIterator != null) {
209: while (typeIterator.hasNext()) {
210: TypeSummary next = (TypeSummary) typeIterator.next();
211: if (next.isPublic() && !isInFilter(next)) {
212: list.add(next.getName());
213: }
214: }
215: }
216: }
217: }
|