001: /*
002: * Project: BeautyJ - Customizable Java Source Code Transformer
003: * Class: de.gulden.util.javasource.Field
004: * Version: 1.1
005: *
006: * Date: 2004-09-29
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: * Represents a field definition.
029: *
030: * @author Jens Gulden
031: * @version 1.0
032: */
033: public class Field extends Member implements Typed {
034:
035: // ------------------------------------------------------------------------
036: // --- field ---
037: // ------------------------------------------------------------------------
038:
039: /**
040: * The type.
041: */
042: protected Type type;
043:
044: // ------------------------------------------------------------------------
045: // --- constructor ---
046: // ------------------------------------------------------------------------
047:
048: /**
049: * Creates a new instance of Field.
050: */
051: public Field(Class c) {
052: super (c);
053: }
054:
055: // ------------------------------------------------------------------------
056: // --- methods ---
057: // ------------------------------------------------------------------------
058:
059: /**
060: * Returns the type.
061: */
062: public Type getType() {
063: return type;
064: }
065:
066: /**
067: * Sets the type.
068: */
069: public void setType(Type t) {
070: type = t;
071: }
072:
073: /**
074: * Output this object as XML.
075: *
076: * @return The XML tag.
077: * @see #initFromXML
078: */
079: public Element buildXML(Document d) {
080: Element e = super .buildXML(d);
081: e.insertBefore(type.buildXML(d), e.getFirstChild());
082: if (getCode() != null) {
083: Element ee = d.createElement("initializer");
084: ee.appendChild(getCode().buildXML(d));
085: e.appendChild(ee);
086: }
087: return e;
088: }
089:
090: /**
091: * Initialize this object from XML.
092: *
093: * @param element The XML tag.
094: * @throws IOException if an i/o error occurs
095: */
096: public void initFromXML(Element element) throws IOException {
097: // to be extended (not overwritten) by subclasses
098: super .initFromXML(element);
099:
100: // type
101: Element ty = XMLToolbox.getChildRequired(element, "type");
102: type = new Type(this );
103: type.initFromXML(ty);
104:
105: // field initializer
106: Element in = XMLToolbox.getChild(element, "initializer");
107: if (in != null) {
108: code = new Code();
109: code.initFromXML(XMLToolbox.getChildRequired(in, "code"));
110: } else {
111: code = null;
112: }
113: }
114:
115: /**
116: * Initialize this object from parsed Java code.
117: * Special way of invoking.
118: *
119: * @param rootnode The corresponding node in the abstract syntax tree (AST).
120: */
121: void initFromAST(Node rootnode, Node varnode) {
122: super .initFromAST(rootnode);
123: String className = getDeclaringClass().getName();
124: name = className + "." + varnode.getName();
125:
126: type = new Type(this );
127: type.initFromAST(rootnode, varnode); // special way of invoking (using rootnode, varnode)
128:
129: Node codenode = varnode.getChild(JJT_CODE);
130: if (codenode != null) // field has initializer
131: {
132: code = new Code();
133: code.initFromAST(varnode); // special way of invoking
134: } else {
135: code = null;
136: }
137: }
138:
139: } // end Field
|