01: package org.acm.seguin.refactor.type;
02:
03: import net.sourceforge.jrefactory.ast.SimpleNode;
04: import net.sourceforge.jrefactory.ast.ASTPackageDeclaration;
05: import net.sourceforge.jrefactory.ast.ASTName;
06: import net.sourceforge.jrefactory.factory.NameFactory;
07: import org.acm.seguin.refactor.TransformAST;
08: import net.sourceforge.jrefactory.parser.JavaParserTreeConstants;
09:
10: /**
11: * This object revises the package statement
12: *
13: *@author Chris Seguin
14: *@created October 23, 1999
15: */
16: public class ChangePackageTransform extends TransformAST {
17: private ASTName name;
18:
19: /**
20: * Constructor for the ChangePackageTransform object
21: *
22: *@param name Description of Parameter
23: */
24: public ChangePackageTransform(ASTName name) {
25: this .name = name;
26: }
27:
28: /**
29: * Constructor for the ChangePackageTransform object
30: *
31: *@param packageName Description of Parameter
32: */
33: public ChangePackageTransform(String packageName) {
34: name = NameFactory.getName(packageName, null);
35: }
36:
37: /**
38: * Update the syntax tree
39: *
40: *@param root the root of the syntax tree
41: */
42: public void update(SimpleNode root) {
43: if (name.getName().length() == 0) {
44: removePackage(root);
45: } else {
46: addPackage(root);
47: }
48: }
49:
50: /**
51: * Adds a feature to the Package attribute of the ChangePackageTransform
52: * object
53: *
54: *@param root The feature to be added to the Package attribute
55: */
56: private void addPackage(SimpleNode root) {
57: SimpleNode first = (SimpleNode) root.jjtGetFirstChild();
58:
59: ASTPackageDeclaration pack = new ASTPackageDeclaration(
60: JavaParserTreeConstants.JJTPACKAGEDECLARATION);
61: pack.jjtAddChild(name, 0);
62:
63: if (first instanceof ASTPackageDeclaration) {
64: root.jjtAddChild(pack, 0);
65: } else {
66: root.jjtInsertChild(pack, 0);
67: }
68: }
69:
70: /**
71: * Description of the Method
72: *
73: *@param root Description of Parameter
74: */
75: private void removePackage(SimpleNode root) {
76: SimpleNode first = (SimpleNode) root.jjtGetFirstChild();
77: if (first instanceof ASTPackageDeclaration) {
78: root.jjtDeleteChild(0);
79: }
80: }
81: }
|