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.method;
010:
011: import java.util.Iterator;
012: import java.util.LinkedList;
013: import net.sourceforge.jrefactory.ast.*;
014: import net.sourceforge.jrefactory.build.BuildExpression;
015: import org.acm.seguin.summary.*;
016: import net.sourceforge.jrefactory.parser.JavaParserTreeConstants;
017:
018: /**
019: * Adds an abstract method to the class
020: *
021: *@author Chris Seguin
022: */
023: public class InvokeMovedMethodTransform extends AddNewMethod {
024: private Summary dest;
025:
026: /**
027: * Constructor for the InvokeMovedMethodTransform object
028: *
029: *@param init The signature of the method that we are adding
030: *@param destination Description of Parameter
031: */
032: public InvokeMovedMethodTransform(MethodSummary init,
033: Summary destination) {
034: super (init);
035: dest = destination;
036: }
037:
038: /**
039: * Determines if the method is abstract
040: *
041: *@return true if the method is abstract
042: */
043: protected boolean isAbstract() {
044: return false;
045: }
046:
047: /**
048: * Adds the body of the method
049: *
050: *@param methodDecl The feature to be added to the Body attribute
051: *@param index The feature to be added to the Body attribute
052: */
053: protected void addBody(SimpleNode methodDecl, int index) {
054: ASTBlock block = new ASTBlock(JavaParserTreeConstants.JJTBLOCK);
055: methodDecl.jjtAddChild(block, index);
056:
057: if (dest instanceof ParameterSummary) {
058: ParameterSummary paramSummary = (ParameterSummary) dest;
059:
060: ASTBlockStatement blockStatement = new ASTBlockStatement(
061: JavaParserTreeConstants.JJTBLOCKSTATEMENT);
062: block.jjtAddChild(blockStatement, 0);
063:
064: ASTStatement statement = new ASTStatement(0);
065: blockStatement.jjtAddChild(statement, 0);
066:
067: ASTStatementExpression stateExpress = new ASTStatementExpression(
068: JavaParserTreeConstants.JJTSTATEMENTEXPRESSION);
069: statement.jjtAddChild(stateExpress, 0);
070:
071: ASTPrimaryExpression primaryExpression = new ASTPrimaryExpression(
072: JavaParserTreeConstants.JJTPRIMARYEXPRESSION);
073: stateExpress.jjtAddChild(primaryExpression, 0);
074:
075: ASTPrimaryPrefix prefix = new ASTPrimaryPrefix(
076: JavaParserTreeConstants.JJTPRIMARYPREFIX);
077: primaryExpression.jjtAddChild(prefix, 0);
078:
079: ASTName name = new ASTName();
080: name.addNamePart(paramSummary.getName());
081: name.addNamePart(methodSummary.getName());
082: primaryExpression.jjtAddChild(name, 0);
083:
084: ASTPrimarySuffix suffix = new ASTPrimarySuffix(
085: JavaParserTreeConstants.JJTPRIMARYSUFFIX);
086: primaryExpression.jjtAddChild(suffix, 1);
087:
088: ASTArguments args = new ASTArguments(
089: JavaParserTreeConstants.JJTARGUMENTS);
090: suffix.jjtAddChild(args, 0);
091:
092: ASTArgumentList argList = new ASTArgumentList(
093: JavaParserTreeConstants.JJTARGUMENTLIST);
094: args.jjtAddChild(argList, 0);
095:
096: int count = 0;
097: Iterator iter = methodSummary.getParameters();
098: if (iter != null) {
099: while (iter.hasNext()) {
100: ParameterSummary next = (ParameterSummary) iter
101: .next();
102: if (!next.equals(paramSummary)) {
103: ASTExpression expr = buildExpression(next
104: .getName());
105: argList.jjtAddChild(expr, count);
106: count++;
107: }
108: }
109: }
110:
111: if (isObjectReferenced()) {
112: argList.jjtAddChild(buildExpression("this"), count);
113: }
114: }
115: }
116:
117: /**
118: * Determines if this object is referenced
119: *
120: *@return The ObjectReferenced value
121: */
122: private boolean isObjectReferenced() {
123: return ObjectReference.isReferenced(methodSummary);
124: }
125:
126: /**
127: * Builds an expression
128: *
129: *@param name the name at the base of the expression
130: *@return the expression
131: */
132: private ASTExpression buildExpression(String name) {
133: BuildExpression be = new BuildExpression();
134: return be.buildName(name);
135: }
136: }
|