KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Project: BeautyJ - Customizable Java Source Code Transformer
3  * Class: de.gulden.util.javasource.Field
4  * Version: 1.1
5  *
6  * Date: 2004-09-29
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  * Represents a field definition.
29  *
30  * @author Jens Gulden
31  * @version 1.0
32  */

33 public class Field extends Member implements Typed {
34
35     // ------------------------------------------------------------------------
36
// --- field ---
37
// ------------------------------------------------------------------------
38

39     /**
40      * The type.
41      */

42     protected Type type;
43
44
45     // ------------------------------------------------------------------------
46
// --- constructor ---
47
// ------------------------------------------------------------------------
48

49     /**
50      * Creates a new instance of Field.
51      */

52     public Field(Class JavaDoc c) {
53         super(c);
54     }
55
56
57     // ------------------------------------------------------------------------
58
// --- methods ---
59
// ------------------------------------------------------------------------
60

61     /**
62      * Returns the type.
63      */

64     public Type getType() {
65         return type;
66     }
67
68     /**
69      * Sets the type.
70      */

71     public void setType(Type t) {
72         type=t;
73     }
74
75     /**
76      * Output this object as XML.
77      *
78      * @return The XML tag.
79      * @see #initFromXML
80      */

81     public Element buildXML(Document d) {
82         Element e=super.buildXML(d);
83         e.insertBefore(type.buildXML(d),e.getFirstChild());
84         if (getCode()!=null) {
85             Element ee=d.createElement("initializer");
86             ee.appendChild(getCode().buildXML(d));
87             e.appendChild(ee);
88         }
89         return e;
90     }
91
92     /**
93      * Initialize this object from XML.
94      *
95      * @param element The XML tag.
96      * @throws IOException if an i/o error occurs
97      */

98     public void initFromXML(Element element) throws IOException {
99         // to be extended (not overwritten) by subclasses
100
super.initFromXML(element);
101
102         // type
103
Element ty=XMLToolbox.getChildRequired(element,"type");
104         type=new Type(this);
105         type.initFromXML(ty);
106
107         // field initializer
108
Element in=XMLToolbox.getChild(element,"initializer");
109         if (in!=null) {
110             code=new Code();
111             code.initFromXML(XMLToolbox.getChildRequired(in,"code"));
112         }
113         else {
114             code=null;
115         }
116     }
117
118     /**
119      * Initialize this object from parsed Java code.
120      * Special way of invoking.
121      *
122      * @param rootnode The corresponding node in the abstract syntax tree (AST).
123      */

124     void initFromAST(Node rootnode, Node varnode) {
125         super.initFromAST(rootnode);
126         String JavaDoc className=getDeclaringClass().getName();
127         name=className+"."+varnode.getName();
128
129         type=new Type(this);
130         type.initFromAST(rootnode, varnode); // special way of invoking (using rootnode, varnode)
131

132         Node codenode=varnode.getChild(JJT_CODE);
133         if (codenode!=null) // field has initializer
134
{
135             code=new Code();
136             code.initFromAST(varnode); // special way of invoking
137
}
138         else {
139             code=null;
140         }
141     }
142
143 } // end Field
144
Popular Tags