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.type;
010:
011: import java.io.File;
012: import net.sourceforge.jrefactory.ast.*;
013: import org.acm.seguin.pretty.PrettyPrintFile;
014: import org.acm.seguin.refactor.RefactoringException;
015: import org.acm.seguin.summary.PackageSummary;
016: import org.acm.seguin.summary.Summary;
017: import org.acm.seguin.summary.TypeDeclSummary;
018: import org.acm.seguin.summary.TypeSummary;
019: import org.acm.seguin.summary.query.TopLevelDirectory;
020: import net.sourceforge.jrefactory.parser.JavaParserTreeConstants;
021:
022: /**
023: * This object creates an interface from nothing. It is responsible for
024: * building up the parse tree from scratch to create a new interface.
025: *
026: *@author Grant Watson
027: *@created November 28, 2000
028: */
029: public class CreateNewInterface {
030: private String m_interfaceName;
031: private String m_packageName;
032: private Summary m_summary;
033:
034: /**
035: * Constructor for the CreateNewInterface object
036: *
037: *@param interfaceName Description of Parameter
038: *@param packageName Description of Parameter
039: *@param summary Description of Parameter
040: */
041: public CreateNewInterface(Summary summary, String packageName,
042: String interfaceName) {
043: m_summary = summary;
044: m_packageName = packageName;
045: m_interfaceName = interfaceName;
046: }
047:
048: /**
049: * Constructor for the CreateNewInterface object
050: */
051: CreateNewInterface() {
052: m_interfaceName = null;
053: m_packageName = null;
054: }
055:
056: /**
057: * Creates the the designated class
058: *
059: *@return Description of the Returned Value
060: *@exception RefactoringException Description of Exception
061: */
062: public File run() throws RefactoringException {
063: if (m_packageName == null) {
064: throw new RefactoringException("No package name specified");
065: }
066:
067: if (m_interfaceName == null) {
068: throw new RefactoringException(
069: "No interface name specified");
070: }
071:
072: // Create the AST
073: ASTCompilationUnit root = new ASTCompilationUnit(0);
074:
075: // Create the package statement
076: int nextIndex = 0;
077:
078: if ((m_packageName != null) && (m_packageName.length() > 0)) {
079: ASTPackageDeclaration packDecl = createPackageDeclaration();
080: root.jjtAddChild(packDecl, 0);
081: nextIndex++;
082: }
083:
084: // Create the class
085: ASTTypeDeclaration td = createTypeDeclaration();
086: root.jjtAddChild(td, nextIndex);
087:
088: // Print this new one
089: File dest = print(m_interfaceName, root);
090: return dest;
091: }
092:
093: /**
094: * Creates the package declaration
095: *
096: *@return the package declaration
097: */
098: ASTPackageDeclaration createPackageDeclaration() {
099: ASTPackageDeclaration packDecl = new ASTPackageDeclaration(
100: JavaParserTreeConstants.JJTPACKAGEDECLARATION);
101: ASTName packName = new ASTName();
102: packName.fromString(m_packageName);
103: packDecl.jjtAddChild(packName, 0);
104:
105: return packDecl;
106: }
107:
108: /**
109: * Creates the type declaration
110: *
111: *@return the modified class
112: */
113: ASTTypeDeclaration createTypeDeclaration() {
114: ASTTypeDeclaration td = new ASTTypeDeclaration();
115: ASTInterfaceDeclaration id = createModifiedClass();
116: td.jjtAddChild(id, 0);
117:
118: return td;
119: }
120:
121: /**
122: * Creates the modified class
123: *
124: *@return the modified class
125: */
126: ASTInterfaceDeclaration createModifiedClass() {
127: ASTInterfaceDeclaration id = new ASTInterfaceDeclaration(
128: JavaParserTreeConstants.JJTINTERFACEDECLARATION);
129: id.addModifier("public");
130: ASTUnmodifiedInterfaceDeclaration uid = createClassBody(m_interfaceName);
131: id.jjtAddChild(uid, 0);
132: return id;
133: }
134:
135: /**
136: * Creates the body. The protection level is package so it can be easily
137: * tested.
138: *
139: *@param parentName Description of Parameter
140: *@return the class
141: */
142: ASTUnmodifiedInterfaceDeclaration createClassBody(String parentName) {
143: ASTUnmodifiedInterfaceDeclaration uid = new ASTUnmodifiedInterfaceDeclaration(
144: JavaParserTreeConstants.JJTUNMODIFIEDINTERFACEDECLARATION);
145: uid.setName(parentName);
146: ASTInterfaceBody ib = new ASTInterfaceBody(
147: JavaParserTreeConstants.JJTINTERFACEBODY);
148: uid.jjtAddChild(ib, 0);
149: return uid;
150: }
151:
152: /**
153: * Prints the file
154: *
155: *@param root Description of Parameter
156: *@param interfaceName Description of Parameter
157: *@return Description of the Returned Value
158: */
159: File print(String interfaceName, SimpleNode root) {
160: File parent = TopLevelDirectory.getPackageDirectory(m_summary,
161: m_packageName);
162:
163: // Create directory if it doesn't exist
164: if (!parent.exists()) {
165: parent.mkdir();
166: }
167: File destFile = new File(parent, interfaceName + ".java");
168:
169: try {
170: (new PrettyPrintFile()).apply(destFile, root);
171: } catch (Throwable thrown) {
172: thrown.printStackTrace(System.out);
173: }
174:
175: return destFile;
176: }
177:
178: /**
179: * Gets the package summary
180: *
181: *@param base Description of Parameter
182: *@return the package summary
183: */
184: private PackageSummary getPackageSummary(Summary base) {
185: Summary current = base;
186: while (!(current instanceof PackageSummary)) {
187: current = current.getParent();
188: }
189: return (PackageSummary) current;
190: }
191:
192: /**
193: * Gets the SameParent attribute of the AddAbstractParent object
194: *
195: *@param one Description of Parameter
196: *@param two Description of Parameter
197: *@return The SameParent value
198: */
199: private boolean isSameParent(TypeSummary one, TypeSummary two) {
200: if (isObject(one)) {
201: return isObject(two);
202: }
203:
204: if (isObject(two)) {
205: return false;
206: }
207:
208: return one.equals(two);
209: }
210:
211: /**
212: * Gets the Object attribute of the AddAbstractParent object
213: *
214: *@param item Description of Parameter
215: *@return The Object value
216: */
217: private boolean isObject(TypeSummary item) {
218: if (item == null) {
219: return true;
220: }
221:
222: if (item.getName().equals("Object")) {
223: return true;
224: }
225:
226: return false;
227: }
228: }
|