001: /*
002: * Project: BeautyJ - Customizable Java Source Code Transformer
003: * Class: de.gulden.util.javasource.MemberExecutable
004: * Version: 1.0
005: *
006: * Date: 2002-10-27
007: *
008: * Note: Contains auto-generated Javadoc comments created by BeautyJ.
009: *
010: * This is licensed under the GNU General Public License (GPL)
011: * and comes with NO WARRANTY. See file license.txt for details.
012: *
013: * Author: Jens Gulden
014: * Email: beautyj@jensgulden.de
015: */
016:
017: package de.gulden.util.javasource;
018:
019: import de.gulden.util.javasource.jjt.Node;
020: import de.gulden.util.javasource.jjt.*;
021: import de.gulden.util.xml.XMLToolbox;
022: import javax.xml.parsers.*;
023: import org.w3c.dom.*;
024: import java.io.*;
025: import java.util.*;
026:
027: /**
028: * Class MemberExecutable.
029: *
030: * @author Jens Gulden
031: * @version 1.0
032: */
033: public abstract class MemberExecutable extends Member {
034:
035: // ------------------------------------------------------------------------
036: // --- fields ---
037: // ------------------------------------------------------------------------
038: /**
039: * The parameters of this executable member.
040: */
041: public Vector myParameter;
042:
043: /**
044: * The exceptions thrown by this executable member.
045: */
046: public Vector myException;
047:
048: // ------------------------------------------------------------------------
049: // --- constructor ---
050: // ------------------------------------------------------------------------
051: /**
052: * Creates a new instance of MemberExecutable.
053: */
054: protected MemberExecutable(Class c) {
055: super (c);
056: myParameter = new Vector();
057: myException = new Vector();
058: }
059:
060: // ------------------------------------------------------------------------
061: // --- methods ---
062: // ------------------------------------------------------------------------
063: /**
064: * Returns the parameters.
065: */
066: public NamedIterator getParameters() {
067: return new NamedIterator(myParameter);
068: }
069:
070: /**
071: * Returns the exceptions.
072: */
073: public NamedIterator getExceptions() {
074: return new NamedIterator(myException);
075: }
076:
077: /**
078: * Output this object as XML.
079: *
080: * @return The XML tag.
081: * @see #initFromXML
082: */
083: public Element buildXML(Document d) {
084: Element e = super .buildXML(d);
085: NamedIterator it = getParameters();
086: while (it.hasMore()) {
087: Parameter p = (Parameter) it.next();
088: e.appendChild(p.buildXML(d));
089: }
090: it = getExceptions();
091: while (it.hasMore()) {
092: Exception exc = (Exception) it.next();
093: e.appendChild(exc.buildXML(d));
094: }
095: if (code != null) {
096: e.appendChild(code.buildXML(d));
097: }
098: return e;
099: }
100:
101: /**
102: * Initialize this object from XML.
103: *
104: * @param element The XML tag.
105: * @throws IOException if an i/o error occurs
106: */
107: public void initFromXML(Element element) throws IOException {
108: // to be extended (not overwritten) by subclasses
109: super .initFromXML(element);
110:
111: // parameters
112: myParameter.removeAllElements();
113: NodeList nl = XMLToolbox.getChildren(element, "parameter");
114: for (int i = 0; i < nl.getLength(); i++) {
115: Parameter pa = new Parameter(this );
116: pa.initFromXML((Element) nl.item(i));
117: myParameter.addElement(pa);
118: }
119:
120: // exceptions
121: myException.removeAllElements();
122: nl = XMLToolbox.getChildren(element, "throws");
123: for (int i = 0; i < nl.getLength(); i++) {
124: Exception ex = new Exception(this );
125: ex.initFromXML((Element) nl.item(i));
126: myException.addElement(ex);
127: }
128:
129: // code
130: Element co = XMLToolbox.getChild(element, "code");
131: if (co != null) {
132: code = new Code();
133: code.initFromXML(co);
134: } else {
135: code = null;
136: }
137: }
138:
139: /**
140: * Returns the parameter signature.
141: */
142: public String getParameterSignature() {
143: StringBuffer pb = new StringBuffer();
144: boolean first = true;
145: for (NamedIterator it = getParameters(); it.hasMore();) {
146: Parameter p = (Parameter) it.next();
147: if (!first) {
148: pb.append(",");
149: } else {
150: first = false;
151: }
152: pb.append(p.getType().getFullTypeName());
153: }
154: return "(" + pb.toString() + ")";
155: }
156:
157: /**
158: * Get the full signature of this executable member.
159: */
160: public String getSignature() {
161: return super .getSignature() + getParameterSignature();
162: }
163:
164: /**
165: * Initialize this object from parsed Java code.
166: *
167: * @param rootnode The corresponding node in the abstract syntax tree (AST).
168: */
169: void initFromAST(Node rootnode) {
170: super .initFromAST(rootnode);
171:
172: Node r = rootnode.getChild(JJT_CODE);
173: if (r != null) {
174: code = new Code();
175: code.initFromAST(rootnode); // pass rootnode, not r (see Code.initFromAST)
176: } else {
177: code = null; // abstract declaration
178: }
179:
180: Node[] nodes;
181:
182: myParameter.removeAllElements();
183: nodes = rootnode.getChildren(JJT_PARAMETER);
184: for (int i = 0; i < nodes.length; i++) {
185: Parameter c = new Parameter(this );
186: c.initFromAST(nodes[i]);
187: myParameter.addElement(c);
188: }
189:
190: myException.removeAllElements();
191: Node t = rootnode.getChild(JJT_THROWS);
192: if (t != null) {
193: nodes = t.getChildren(JJT_NAME);
194: for (int i = 0; i < nodes.length; i++) {
195: Exception ex = new Exception(this );
196: ex.initFromAST(nodes[i]);
197: myException.addElement(ex);
198: }
199: }
200: }
201:
202: } // end MemberExecutable
|