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 net.sourceforge.jrefactory.ast.Node;
012: import net.sourceforge.jrefactory.ast.SimpleNode;
013: import net.sourceforge.jrefactory.ast.ASTPackageDeclaration;
014: import net.sourceforge.jrefactory.ast.ASTImportDeclaration;
015: import net.sourceforge.jrefactory.ast.ASTName;
016: import net.sourceforge.jrefactory.ast.ASTTypeDeclaration;
017: import net.sourceforge.jrefactory.factory.NameFactory;
018: import org.acm.seguin.summary.Summary;
019: import org.acm.seguin.summary.PackageSummary;
020: import org.acm.seguin.summary.TypeSummary;
021: import net.sourceforge.jrefactory.parser.JavaParserTreeConstants;
022:
023: /**
024: * This object revises the import statements in the tree.
025: *
026: *@author Chris Seguin
027: */
028: public class AddImportTransform extends TransformAST {
029: private ASTName name;
030: private boolean ignorePackageName;
031:
032: /**
033: * Constructor for the AddImportTransform object
034: *
035: *@param name Description of Parameter
036: */
037: public AddImportTransform(ASTName name) {
038: this .name = name;
039: ignorePackageName = false;
040: }
041:
042: /**
043: * Constructor for the AddImportTransform object
044: *
045: *@param packageName the package name
046: *@param className the class name
047: */
048: public AddImportTransform(String packageName, String className) {
049: name = NameFactory.getName(packageName, className);
050: ignorePackageName = false;
051: }
052:
053: /**
054: * Constructor for the AddImportTransform object
055: *
056: *@param typeSummary the type symmary
057: */
058: public AddImportTransform(TypeSummary typeSummary) {
059: Summary current = typeSummary;
060: while (!(current instanceof PackageSummary)) {
061: current = current.getParent();
062: }
063: PackageSummary packageSummary = (PackageSummary) current;
064: name = NameFactory.getName(packageSummary.getName(),
065: typeSummary.getName());
066: ignorePackageName = false;
067: }
068:
069: /**
070: * Sets the IgnorePackageName attribute of the AddImportTransform object
071: *
072: *@param value The new IgnorePackageName value
073: */
074: public void setIgnorePackageName(boolean value) {
075: ignorePackageName = value;
076: }
077:
078: /**
079: * Update the syntax tree
080: *
081: *@param root Description of Parameter
082: */
083: public void update(SimpleNode root) {
084: if ((name.getNameSize() == 3)
085: && (name.getNamePart(0).equals("java"))
086: && (name.getNamePart(1).equals("lang"))) {
087: return;
088: }
089:
090: int nFirstImportSpot = findLastImport(root);
091: if (nFirstImportSpot == -1) {
092: return;
093: }
094:
095: // Create the import
096: ASTImportDeclaration importDecl = new ASTImportDeclaration(
097: JavaParserTreeConstants.JJTIMPORTDECLARATION);
098: importDecl.jjtAddFirstChild(name);
099: importDecl.setImportPackage(false);
100:
101: // Add it to the source tree
102: root.jjtInsertChild(importDecl, nFirstImportSpot);
103: }
104:
105: /**
106: * Determine where the first import should go
107: *
108: *@param root Description of Parameter
109: *@return the index where new import statements should go
110: */
111: protected int findLastImport(SimpleNode root) {
112: int last = root.jjtGetNumChildren();
113: for (int ndx = 0; ndx < last; ndx++) {
114: Node child = root.jjtGetChild(ndx);
115:
116: // Check to see if we have already imported this
117: if ((!ignorePackageName)
118: && (child instanceof ASTPackageDeclaration)) {
119: ASTPackageDeclaration decl = (ASTPackageDeclaration) child;
120: ASTName packageName = (ASTName) child
121: .jjtGetFirstChild();
122: if (packageName.getNameSize() + 1 == name.getNameSize()) {
123: boolean done = true;
124: for (int ndx2 = 0; ndx2 < packageName.getNameSize(); ndx2++) {
125: if (!packageName.getNamePart(ndx2).equals(
126: name.getNamePart(ndx2))) {
127: done = false;
128: }
129: }
130:
131: if (done) {
132: return -1;
133: }
134: }
135: }
136:
137: // Check to see if we have already imported this
138: if (child instanceof ASTImportDeclaration) {
139: ASTImportDeclaration decl = (ASTImportDeclaration) child;
140: ASTName importName = (ASTName) child.jjtGetFirstChild();
141: if (importName.equals(name)) {
142: return -1;
143: }
144: }
145:
146: // Found a type declaration, time to return the index
147: if (root.jjtGetChild(ndx) instanceof ASTTypeDeclaration) {
148: return ndx;
149: }
150: }
151:
152: // No point - there aren't any types defined.
153: return -1;
154: }
155: }
|