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 net.sourceforge.jrefactory.parser.ChildrenVisitor;
012: import net.sourceforge.jrefactory.ast.SimpleNode;
013: import net.sourceforge.jrefactory.ast.ASTMethodDeclaration;
014: import net.sourceforge.jrefactory.ast.ASTConstructorDeclaration;
015: import net.sourceforge.jrefactory.ast.ASTFormalParameters;
016: import net.sourceforge.jrefactory.ast.SimpleNode;
017: import net.sourceforge.jrefactory.ast.Node;
018: import net.sourceforge.jrefactory.ast.ASTMethodDeclarator;
019: import java.util.Iterator;
020: import net.sourceforge.jrefactory.ast.ASTFormalParameter;
021: import net.sourceforge.jrefactory.ast.ASTType;
022: import net.sourceforge.jrefactory.ast.ASTReferenceType;
023: import net.sourceforge.jrefactory.ast.ASTClassOrInterfaceType;
024: import net.sourceforge.jrefactory.ast.ASTPrimitiveType;
025: import net.sourceforge.jrefactory.ast.ASTName;
026: import org.acm.seguin.summary.ParameterSummary;
027: import org.acm.seguin.summary.MethodSummary;
028: import net.sourceforge.jrefactory.ast.ASTTypeParameters;
029: import net.sourceforge.jrefactory.ast.ASTAnnotation;
030:
031: /**
032: * A visitor that is able to identify the method that we are operating on
033: *
034: *@author Chris Seguin
035: */
036: abstract class IdentifyMethodVisitor extends ChildrenVisitor {
037: /**
038: * The method we are searching for
039: */
040: private MethodSummary methodSummary;
041:
042: /**
043: * Constructor for the IdentifyMethodVisitor object
044: *
045: *@param init Description of Parameter
046: */
047: public IdentifyMethodVisitor(MethodSummary init) {
048: methodSummary = init;
049: }
050:
051: /**
052: * Have we found the method declaration that we are going to move?
053: *
054: *@param next Description of Parameter
055: *@return The Found value
056: */
057: protected boolean isFound(SimpleNode next) {
058: if (next instanceof ASTMethodDeclaration) {
059: int childNo = ((ASTMethodDeclaration) next)
060: .skipAnnotationsAndTypeParameters();
061: return checkDeclaration((ASTMethodDeclarator) next
062: .jjtGetChild(childNo + 1));
063: }
064: if (next instanceof ASTConstructorDeclaration) {
065: return checkDeclaration((ASTConstructorDeclaration) next);
066: }
067:
068: return false;
069: }
070:
071: /**
072: * Checks a single variable declaration to see if it is the one we are
073: * looking for
074: *
075: *@param next the method declaration that we are checking
076: *@return true if we have found the method
077: */
078: protected boolean checkDeclaration(ASTMethodDeclarator decl) {
079: if (decl.getName().equals(methodSummary.getName())) {
080: return checkParameters((ASTFormalParameters) decl
081: .jjtGetFirstChild());
082: }
083: return false;
084: }
085:
086: /**
087: * Checks a single variable declaration to see if it is the one we are
088: * looking for
089: *
090: *@param next the method declaration that we are checking
091: *@return true if we have found the method
092: */
093: protected boolean checkDeclaration(ASTConstructorDeclaration decl) {
094: if (methodSummary.isConstructor()) {
095: int childNo = decl.skipAnnotations();
096: return checkParameters((ASTFormalParameters) decl
097: .jjtGetChild(childNo));
098: }
099:
100: return false;
101: }
102:
103: /**
104: * Description of the Method
105: *
106: *@param params Description of Parameter
107: *@return Description of the Returned Value
108: */
109: protected boolean checkParameters(ASTFormalParameters params) {
110: int length = params.jjtGetNumChildren();
111: Iterator iter = methodSummary.getParameters();
112:
113: if (iter == null) {
114: return (length == 0);
115: }
116:
117: int ndx;
118: for (ndx = 0; (iter.hasNext()) && (ndx < length); ndx++) {
119: ASTFormalParameter param = (ASTFormalParameter) params
120: .jjtGetChild(ndx);
121: int childNo = param.skipAnnotations();
122: ASTType type = (ASTType) param.jjtGetChild(childNo);
123: String name;
124:
125: if (type.jjtGetFirstChild() instanceof ASTPrimitiveType) {
126: name = ((ASTPrimitiveType) type.jjtGetFirstChild())
127: .getName();
128: } else {
129: // (type.jjtGetFirstChild() instanceof ASTReferenceType) {
130: ASTReferenceType reference = (ASTReferenceType) type
131: .jjtGetFirstChild();
132: if (reference.jjtGetFirstChild() instanceof ASTClassOrInterfaceType) {
133: name = ((ASTClassOrInterfaceType) reference
134: .jjtGetFirstChild()).getName();
135: } else {
136: name = ((ASTPrimitiveType) type.jjtGetFirstChild())
137: .getName();
138: }
139: }
140:
141: ParameterSummary paramSummary = (ParameterSummary) iter
142: .next();
143: String typeName = paramSummary.getTypeDecl().getLongName();
144:
145: if (!name.equals(typeName)) {
146: return false;
147: }
148: }
149:
150: return (!iter.hasNext()) && (ndx == length);
151: }
152: }
|