01: /*
02: * Author: Chris Seguin
03: *
04: * This software has been developed under the copyleft
05: * rules of the GNU General Public License. Please
06: * consult the GNU General Public License for more
07: * details about use and distribution of this software.
08: */
09: package org.acm.seguin.refactor.type;
10:
11: import net.sourceforge.jrefactory.parser.ChildrenVisitor;
12: import net.sourceforge.jrefactory.ast.ASTName;
13: import net.sourceforge.jrefactory.ast.ASTClassOrInterfaceType;
14: import net.sourceforge.jrefactory.ast.ASTGenericNameList;
15: import net.sourceforge.jrefactory.ast.ASTUnmodifiedClassDeclaration;
16: import net.sourceforge.jrefactory.ast.SimpleNode;
17: import net.sourceforge.jrefactory.parser.JavaParserTreeConstants;
18:
19: /**
20: * Walks the parse tree and updates it
21: *
22: *@author Grant Watson
23: *@created December 1, 2000
24: */
25: public class AddImplementedInterfaceVisitor extends ChildrenVisitor {
26:
27: /**
28: * Constructor for the AddImplementedInterfaceVisitor object
29: */
30: public AddImplementedInterfaceVisitor() {
31: }
32:
33: /**
34: * The ASTNameList of an ASTUnmodifiedClassDeclaration holds the names of
35: * interfaces implemented by the class. This method adds an interface name
36: * specified by the data parameter to the ASTNameList . If no ASTNameList is
37: * present, then one is created.
38: *
39: *@param node Description of Parameter
40: *@param data Description of Parameter
41: *@return Description of the Returned Value
42: */
43: public Object visit(ASTUnmodifiedClassDeclaration node, Object data) {
44: ASTName interfaceName = (ASTName) data;
45: ASTGenericNameList nameList = null;
46: // Find the ASTNameList or add one
47: for (int i = 0; i < node.jjtGetNumChildren(); i++) {
48: if (node.jjtGetChild(i) instanceof ASTGenericNameList) {
49: nameList = (ASTGenericNameList) node.jjtGetChild(i);
50: }
51: }
52: if (nameList == null) {
53: nameList = new ASTGenericNameList(
54: JavaParserTreeConstants.JJTGENERICNAMELIST);
55: node.jjtInsertChild(nameList, node.jjtGetNumChildren() - 1);
56: }
57: // Determine whether the interface name is already in the list of implemented interfaces
58: boolean exists = false;
59: for (int i = 0; i < nameList.jjtGetNumChildren(); i++) {
60: ASTName astName = (ASTName) nameList.jjtGetChild(i);
61: if (astName.equals(interfaceName)) {
62: exists = true;
63: break;
64: }
65: }
66: // If the interface is not there, then add it.
67: if (!exists) {
68: nameList.jjtAddChild(interfaceName, nameList
69: .jjtGetNumChildren());
70: }
71: return null;
72: }
73:
74: }
|