KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > gulden > util > javasource > MemberExecutable


1 /*
2  * Project: BeautyJ - Customizable Java Source Code Transformer
3  * Class: de.gulden.util.javasource.MemberExecutable
4  * Version: 1.0
5  *
6  * Date: 2002-10-27
7  *
8  * Note: Contains auto-generated Javadoc comments created by BeautyJ.
9  *
10  * This is licensed under the GNU General Public License (GPL)
11  * and comes with NO WARRANTY. See file license.txt for details.
12  *
13  * Author: Jens Gulden
14  * Email: beautyj@jensgulden.de
15  */

16
17 package de.gulden.util.javasource;
18
19 import de.gulden.util.javasource.jjt.Node;
20 import de.gulden.util.javasource.jjt.*;
21 import de.gulden.util.xml.XMLToolbox;
22 import javax.xml.parsers.*;
23 import org.w3c.dom.*;
24 import java.io.*;
25 import java.util.*;
26
27 /**
28  * Class MemberExecutable.
29  *
30  * @author Jens Gulden
31  * @version 1.0
32  */

33 public abstract class MemberExecutable extends Member {
34
35     // ------------------------------------------------------------------------
36
// --- fields ---
37
// ------------------------------------------------------------------------
38
/**
39      * The parameters of this executable member.
40      */

41     public Vector myParameter;
42
43     /**
44      * The exceptions thrown by this executable member.
45      */

46     public Vector myException;
47
48
49     // ------------------------------------------------------------------------
50
// --- constructor ---
51
// ------------------------------------------------------------------------
52
/**
53      * Creates a new instance of MemberExecutable.
54      */

55     protected MemberExecutable(Class JavaDoc c) {
56         super(c);
57         myParameter=new Vector();
58         myException=new Vector();
59     }
60
61
62     // ------------------------------------------------------------------------
63
// --- methods ---
64
// ------------------------------------------------------------------------
65
/**
66      * Returns the parameters.
67      */

68     public NamedIterator getParameters() {
69         return new NamedIterator(myParameter);
70     }
71
72     /**
73      * Returns the exceptions.
74      */

75     public NamedIterator getExceptions() {
76         return new NamedIterator(myException);
77     }
78
79     /**
80      * Output this object as XML.
81      *
82      * @return The XML tag.
83      * @see #initFromXML
84      */

85     public Element buildXML(Document d) {
86                 Element e=super.buildXML(d);
87         NamedIterator it=getParameters();
88         while (it.hasMore()) {
89             Parameter p=(Parameter)it.next();
90             e.appendChild(p.buildXML(d));
91         }
92         it=getExceptions();
93         while (it.hasMore()) {
94             Exception JavaDoc exc=(Exception JavaDoc)it.next();
95             e.appendChild(exc.buildXML(d));
96         }
97         if (code!=null) {
98             e.appendChild(code.buildXML(d));
99         }
100         return e;
101     }
102
103     /**
104      * Initialize this object from XML.
105      *
106      * @param element The XML tag.
107      * @throws IOException if an i/o error occurs
108      */

109     public void initFromXML(Element element) throws IOException {
110         // to be extended (not overwritten) by subclasses
111
super.initFromXML(element);
112         
113         // parameters
114
myParameter.removeAllElements();
115         NodeList nl=XMLToolbox.getChildren(element,"parameter");
116         for (int i=0;i<nl.getLength();i++) {
117             Parameter pa=new Parameter(this);
118             pa.initFromXML((Element)nl.item(i));
119             myParameter.addElement(pa);
120         }
121         
122         // exceptions
123
myException.removeAllElements();
124         nl=XMLToolbox.getChildren(element,"throws");
125         for (int i=0;i<nl.getLength();i++) {
126             Exception JavaDoc ex=new Exception JavaDoc(this);
127             ex.initFromXML((Element)nl.item(i));
128             myException.addElement(ex);
129         }
130         
131         // code
132
Element co=XMLToolbox.getChild(element,"code");
133         if (co!=null) {
134             code=new Code();
135             code.initFromXML(co);
136         }
137         else {
138             code=null;
139         }
140     }
141
142     /**
143      * Returns the parameter signature.
144      */

145     public String JavaDoc getParameterSignature() {
146         StringBuffer JavaDoc pb=new StringBuffer JavaDoc();
147         boolean first=true;
148         for (NamedIterator it=getParameters();it.hasMore();) {
149             Parameter p=(Parameter)it.next();
150             if (!first) {
151                 pb.append(",");
152             }
153             else {
154                 first=false;
155             }
156             pb.append(p.getType().getFullTypeName());
157         }
158         return "("+pb.toString()+")";
159     }
160
161     /**
162      * Get the full signature of this executable member.
163      */

164     public String JavaDoc getSignature() {
165         return super.getSignature()+getParameterSignature();
166     }
167
168     /**
169      * Initialize this object from parsed Java code.
170      *
171      * @param rootnode The corresponding node in the abstract syntax tree (AST).
172      */

173     void initFromAST(Node rootnode) {
174         super.initFromAST(rootnode);
175         
176         Node r=rootnode.getChild(JJT_CODE);
177         if (r!=null) {
178             code=new Code();
179             code.initFromAST(rootnode); // pass rootnode, not r (see Code.initFromAST)
180
}
181         else {
182             code=null; // abstract declaration
183
}
184         
185         Node[] nodes;
186         
187         myParameter.removeAllElements();
188         nodes=rootnode.getChildren(JJT_PARAMETER);
189         for (int i=0;i<nodes.length;i++) {
190             Parameter c=new Parameter(this);
191             c.initFromAST(nodes[i]);
192             myParameter.addElement(c);
193         }
194         
195         myException.removeAllElements();
196         Node t=rootnode.getChild(JJT_THROWS);
197         if (t!=null) {
198             nodes=t.getChildren(JJT_NAME);
199             for (int i=0;i<nodes.length;i++) {
200                 Exception JavaDoc ex=new Exception JavaDoc(this);
201                 ex.initFromAST(nodes[i]);
202                 myException.addElement(ex);
203             }
204         }
205     }
206
207 } // end MemberExecutable
208
Popular Tags