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.field;
010:
011: import net.sourceforge.jrefactory.ast.ASTFieldDeclaration;
012: import net.sourceforge.jrefactory.ast.ASTName;
013: import net.sourceforge.jrefactory.ast.ASTPrimitiveType;
014: import net.sourceforge.jrefactory.ast.ASTType;
015: import net.sourceforge.jrefactory.ast.ASTReferenceType;
016: import net.sourceforge.jrefactory.ast.ASTClassOrInterfaceType;
017: import net.sourceforge.jrefactory.ast.SimpleNode;
018: import org.acm.seguin.refactor.Refactoring;
019: import org.acm.seguin.summary.FileSummary;
020: import org.acm.seguin.summary.PackageSummary;
021: import org.acm.seguin.summary.Summary;
022: import org.acm.seguin.summary.TypeSummary;
023: import org.acm.seguin.summary.query.GetTypeSummary;
024:
025: /**
026: * Base class for all field refactorings
027: *
028: *@author Chris Seguin
029: */
030: abstract class FieldRefactoring extends Refactoring {
031: /**
032: * The name of the field
033: */
034: protected String field;
035: /**
036: * The type summary that contains the field
037: */
038: protected TypeSummary typeSummary;
039:
040: /**
041: * Constructor for the FieldRefactoring object
042: */
043: public FieldRefactoring() {
044: super ();
045: }
046:
047: /**
048: * Sets the Class attribute of the PullupFieldRefactoring object
049: *
050: *@param packageName the package name
051: *@param className the class name
052: */
053: public void setClass(String packageName, String className) {
054: setClass(GetTypeSummary.query(PackageSummary
055: .getPackageSummary(packageName), className));
056: }
057:
058: /**
059: * Sets the Class attribute of the PullupFieldRefactoring object
060: *
061: *@param init The new Class value
062: */
063: public void setClass(TypeSummary init) {
064: typeSummary = init;
065: }
066:
067: /**
068: * Sets the Field attribute of the PullupFieldRefactoring object
069: *
070: *@param fieldName The new Field value
071: */
072: public void setField(String fieldName) {
073: field = fieldName;
074: }
075:
076: /**
077: * Determines if the specified type is in java.lang package
078: *
079: *@param type the type
080: *@return true if it is in the package
081: */
082: protected boolean isInJavaLang(ASTName type) {
083: return (type.getNameSize() == 3)
084: && (type.getNamePart(0).equals("java"))
085: && (type.getNamePart(1).equals("lang"));
086: }
087:
088: /**
089: * Determines if the specified type is in java.lang package
090: *
091: *@param type the type
092: *@return true if it is in the package
093: */
094: protected boolean isInJavaLang(TypeSummary type) {
095: return getPackage(type).getName().equals("java.lang");
096: }
097:
098: /**
099: * Gets the package summary for the specific object
100: *
101: *@param current the summary
102: *@return the package summary
103: */
104: protected PackageSummary getPackage(Summary current) {
105: while (!(current instanceof PackageSummary)) {
106: current = current.getParent();
107: }
108: return (PackageSummary) current;
109: }
110:
111: /**
112: * Gets the package summary for the specific object
113: *
114: *@param current the summary
115: *@return the package summary
116: */
117: protected FileSummary getFileSummary(Summary current) {
118: while (!(current instanceof FileSummary)) {
119: current = current.getParent();
120: }
121: return (FileSummary) current;
122: }
123:
124: /**
125: * Gets the FieldType attribute of the PullupFieldRefactoring object
126: *
127: *@param node Description of Parameter
128: *@param fileSummary Description of Parameter
129: *@return The FieldType value
130: */
131: protected Object getFieldType(SimpleNode node,
132: FileSummary fileSummary) {
133: ASTFieldDeclaration field = (ASTFieldDeclaration) node
134: .jjtGetFirstChild();
135: int childNo = field.skipAnnotations();
136: ASTType type = (ASTType) field.jjtGetChild(childNo);
137: if (type.jjtGetFirstChild() instanceof ASTPrimitiveType) {
138: return null;
139: }
140: ASTClassOrInterfaceType name = null;
141: if (type.jjtGetFirstChild() instanceof ASTReferenceType) {
142: ASTReferenceType reference = (ASTReferenceType) type
143: .jjtGetFirstChild();
144: if (reference.jjtGetFirstChild() instanceof ASTClassOrInterfaceType) {
145: name = (ASTClassOrInterfaceType) reference
146: .jjtGetFirstChild();
147: } else {
148: return null;
149: }
150: }
151: if (name.getNameSize() == 1) {
152: return GetTypeSummary.query(fileSummary, name.getName());
153: } else {
154: return name;
155: }
156: }
157: }
|