001: /*
002: * Project: BeautyJ - Customizable Java Source Code Transformer
003: * Class: de.gulden.util.javasource.SourceObjectDeclared
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: * Class SourceObjectDeclared.
029: *
030: * @author Jens Gulden
031: * @version 1.0
032: */
033: public abstract class SourceObjectDeclared extends SourceObject {
034:
035: // ------------------------------------------------------------------------
036: // --- fields ---
037: // ------------------------------------------------------------------------
038:
039: /**
040: */
041: public Class declaringClass;
042:
043: /**
044: */
045: public Documentation myDocumentation;
046:
047: /**
048: * The modifier.
049: */
050: protected int modifier = 0;
051:
052: /**
053: * The source.
054: */
055: protected String source;
056:
057: /**
058: * The source position array.
059: */
060: protected int[] sourcePosition;
061:
062: // ------------------------------------------------------------------------
063: // --- methods ---
064: // ------------------------------------------------------------------------
065:
066: /**
067: * Returns the modifier.
068: */
069: public int getModifier() {
070: return modifier;
071: }
072:
073: /**
074: * Returns the modifier string.
075: */
076: public String getModifierString() {
077: return java.lang.reflect.Modifier.toString(getModifier());
078: }
079:
080: /**
081: * Sets the modifier.
082: */
083: public void setModifier(int m) {
084: modifier = m;
085: }
086:
087: /**
088: * Returns the class within which this SourceObjectDeclared is declared.
089: */
090: public Class getDeclaringClass() {
091: return declaringClass;
092: }
093:
094: /**
095: * Sets the class within which this SourceObjectDeclared is declared.
096: */
097: public void setDeclaringClass(Class c) {
098: declaringClass = c;
099: }
100:
101: /**
102: * Returns the documentation.
103: */
104: public Documentation getDocumentation() {
105: return myDocumentation;
106: }
107:
108: /**
109: * Sets the documentation.
110: */
111: public void setDocumentation(Documentation d) {
112: myDocumentation = d;
113: }
114:
115: /**
116: * Initialize this object from XML.
117: *
118: * @param element The XML tag.
119: * @throws IOException if an i/o error occurs
120: */
121: public void initFromXML(Element element) throws IOException {
122: // to be extended (not overwritten) by subclasses
123: super .initFromXML(element);
124:
125: // get modifier 'final'
126: this .modifier = 0;
127: if (XMLToolbox.isYesAttribute(element, "final")) {
128: this .modifier |= java.lang.reflect.Modifier.FINAL;
129: }
130:
131: // get javadoc comment
132: Element doc = XMLToolbox.getChild(element, "documentation");
133: if (doc != null) {
134: DocumentationDeclared d = new DocumentationDeclared();
135: d.initFromXML(doc);
136: myDocumentation = d;
137: } else {
138: myDocumentation = null;
139: }
140:
141: if (this instanceof Typed) {
142: Typed typed = (Typed) this ;
143: Type t = null;
144: if (this instanceof Member) {
145: t = new Type((Member) this );
146: } else if (this instanceof Parameter) {
147: t = new Type(((Parameter) this ).myMemberExecutable);
148: } else if (this instanceof Exception) {
149: t = new Type(((Exception) this ).myMemberExecutable);
150: }
151: t.initFromXML(XMLToolbox.getChildRequired(element, "type"));
152: typed.setType(t);
153: }
154: }
155:
156: /**
157: * Output this object as XML.
158: *
159: * @return The XML tag.
160: * @see #initFromXML
161: */
162: public Element buildXML(Document d) {
163: Element e = super .buildXML(d);
164:
165: if (java.lang.reflect.Modifier.isFinal(modifier)) {
166: e.setAttribute("final", "yes");
167: }
168: if (java.lang.reflect.Modifier.isStrict(modifier)) {
169: e.setAttribute("strictfp", "yes");
170: }
171:
172: // javadoc
173: Documentation doc = getDocumentation();
174: if (doc != null) {
175: e.appendChild(doc.buildXML(d));
176: }
177:
178: return e;
179: }
180:
181: /**
182: * Returns the full signature of this SourceObject. This is the fully
183: * qualified name and, depending on the type of the SourceObject,
184: * the (return-)type, and the parameters' signatures
185: */
186: public String getSignature() {
187: String type;
188: if (this instanceof Typed) {
189: Typed tt = (Typed) this ;
190: Type t = tt.getType();
191: type = t.getFullTypeName() + " ";
192: } else {
193: type = "";
194: }
195: return type + super .getSignature();
196: }
197:
198: /**
199: * Returns the source.
200: */
201: public String getSource() {
202: return source;
203: }
204:
205: /**
206: * Sets the source.
207: */
208: public void setSource(String source) {
209: this .source = source;
210: }
211:
212: /**
213: * Returns the source position.
214: */
215: public int[] getSourcePosition() {
216: return sourcePosition;
217: }
218:
219: /**
220: * Returns the source position string.
221: */
222: public String getSourcePositionString() {
223: return "[" + getSourcePosition()[0] + ":"
224: + getSourcePosition()[1] + "]";
225: }
226:
227: public String toString() {
228: if (getSource() != null) {
229: return super .toString() + " " + getSource() + " "
230: + getSourcePositionString();
231: } else {
232: return super .toString();
233: }
234: }
235:
236: /**
237: * Initialize this object with values from parsed Java code.
238: *
239: * @param rootnode The corresponding node in the abstract syntax tree (AST).
240: */
241: void initFromAST(Node rootnode) {
242: super .initFromAST(rootnode);
243:
244: source = rootnode.getSource();
245: sourcePosition = rootnode.getSourcePosition();
246:
247: // get modifier 'final'
248: this .modifier = 0;
249: Node mod = rootnode.getChild(JJT_MODIFIER);
250: if (mod != null) {
251: if (mod.hasChild(JJT_FINAL)) {
252: this .modifier |= java.lang.reflect.Modifier.FINAL;
253: }
254: if (mod.hasChild(JJT_STRICTFP)) {
255: this .modifier |= java.lang.reflect.Modifier.STRICT;
256: }
257: }
258:
259: // get javadoc comment
260: Token t = rootnode.getStartToken();
261: t = t.specialToken;
262: if (t != null) {
263: String doc = t.image;
264: if (doc.startsWith("/**")) // could be an 'ordinary'-comment (then ignore)
265: {
266: DocumentationDeclared d = new DocumentationDeclared();
267: d.setSourceObjectDeclared(this );
268: doc = doc.replace('\r', ' '); // workaround: avoid problems with crlf-linefeeds
269: d.setRaw(doc);
270: myDocumentation = d;
271: } else {
272: myDocumentation = null;
273: }
274: } else {
275: myDocumentation = null;
276: }
277: }
278:
279: } // end SourceObjectDeclared
|