KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Project: BeautyJ - Customizable Java Source Code Transformer
3  * Class: de.gulden.util.javasource.SourceObjectDeclared
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  * Class SourceObjectDeclared.
29  *
30  * @author Jens Gulden
31  * @version 1.0
32  */

33 public abstract class SourceObjectDeclared extends SourceObject {
34
35     // ------------------------------------------------------------------------
36
// --- fields ---
37
// ------------------------------------------------------------------------
38

39     /**
40      */

41     public Class JavaDoc declaringClass;
42
43     /**
44      */

45     public Documentation myDocumentation;
46
47     /**
48      * The modifier.
49      */

50     protected int modifier = 0;
51
52     /**
53      * The source.
54      */

55     protected String JavaDoc source;
56
57     /**
58      * The source position array.
59      */

60     protected int[] sourcePosition;
61
62
63     // ------------------------------------------------------------------------
64
// --- methods ---
65
// ------------------------------------------------------------------------
66

67     /**
68      * Returns the modifier.
69      */

70     public int getModifier() {
71         return modifier;
72     }
73
74     /**
75      * Returns the modifier string.
76      */

77     public String JavaDoc getModifierString() {
78         return java.lang.reflect.Modifier.toString(getModifier());
79     }
80
81     /**
82      * Sets the modifier.
83      */

84     public void setModifier(int m) {
85         modifier=m;
86     }
87
88     /**
89      * Returns the class within which this SourceObjectDeclared is declared.
90      */

91     public Class JavaDoc getDeclaringClass() {
92         return declaringClass;
93     }
94
95     /**
96      * Sets the class within which this SourceObjectDeclared is declared.
97      */

98     public void setDeclaringClass(Class JavaDoc c) {
99         declaringClass=c;
100     }
101
102     /**
103      * Returns the documentation.
104      */

105     public Documentation getDocumentation() {
106         return myDocumentation;
107     }
108
109     /**
110      * Sets the documentation.
111      */

112     public void setDocumentation(Documentation d) {
113         myDocumentation=d;
114     }
115
116     /**
117      * Initialize this object from XML.
118      *
119      * @param element The XML tag.
120      * @throws IOException if an i/o error occurs
121      */

122     public void initFromXML(Element element) throws IOException {
123         // to be extended (not overwritten) by subclasses
124
super.initFromXML(element);
125
126         // get modifier 'final'
127
this.modifier=0;
128         if (XMLToolbox.isYesAttribute(element,"final")) {
129             this.modifier|=java.lang.reflect.Modifier.FINAL;
130         }
131
132         // get javadoc comment
133
Element doc=XMLToolbox.getChild(element,"documentation");
134         if (doc!=null) {
135             DocumentationDeclared d=new DocumentationDeclared();
136             d.initFromXML(doc);
137             myDocumentation=d;
138         }
139         else {
140             myDocumentation=null;
141         }
142
143         if (this instanceof Typed) {
144             Typed typed=(Typed)this;
145             Type t=null;
146             if (this instanceof Member) {
147                 t=new Type((Member)this);
148             }
149             else if (this instanceof Parameter) {
150                 t=new Type(((Parameter)this).myMemberExecutable);
151             }
152             else if (this instanceof Exception JavaDoc) {
153                 t=new Type(((Exception JavaDoc)this).myMemberExecutable);
154             }
155             t.initFromXML(XMLToolbox.getChildRequired(element,"type"));
156             typed.setType(t);
157         }
158     }
159
160     /**
161      * Output this object as XML.
162      *
163      * @return The XML tag.
164      * @see #initFromXML
165      */

166     public Element buildXML(Document d) {
167         Element e=super.buildXML(d);
168
169         if (java.lang.reflect.Modifier.isFinal(modifier)) {
170             e.setAttribute("final","yes");
171         }
172         if (java.lang.reflect.Modifier.isStrict(modifier)) {
173             e.setAttribute("strictfp","yes");
174         }
175
176         // javadoc
177
Documentation doc=getDocumentation();
178         if (doc!=null) {
179             e.appendChild(doc.buildXML(d));
180         }
181
182         return e;
183     }
184
185     /**
186      * Returns the full signature of this SourceObject. This is the fully
187      * qualified name and, depending on the type of the SourceObject,
188      * the (return-)type, and the parameters' signatures
189      */

190     public String JavaDoc getSignature() {
191         String JavaDoc type;
192         if (this instanceof Typed) {
193             Typed tt=(Typed)this;
194             Type t=tt.getType();
195             type=t.getFullTypeName()+" ";
196         }
197         else {
198             type="";
199         }
200         return type+super.getSignature();
201     }
202
203     /**
204      * Returns the source.
205      */

206     public String JavaDoc getSource() {
207         return source;
208     }
209
210     /**
211      * Sets the source.
212      */

213     public void setSource(String JavaDoc source) {
214         this.source=source;
215     }
216
217     /**
218      * Returns the source position.
219      */

220     public int[] getSourcePosition() {
221         return sourcePosition;
222     }
223
224     /**
225      * Returns the source position string.
226      */

227     public String JavaDoc getSourcePositionString() {
228         return "["+getSourcePosition()[0]+":"+getSourcePosition()[1]+"]";
229     }
230
231     public String JavaDoc toString() {
232         if (getSource()!=null) {
233             return super.toString()+" "+getSource()+" "+getSourcePositionString();
234         }
235         else {
236             return super.toString();
237         }
238     }
239
240     /**
241      * Initialize this object with values from parsed Java code.
242      *
243      * @param rootnode The corresponding node in the abstract syntax tree (AST).
244      */

245     void initFromAST(Node rootnode) {
246         super.initFromAST(rootnode);
247
248         source=rootnode.getSource();
249         sourcePosition=rootnode.getSourcePosition();
250
251         // get modifier 'final'
252
this.modifier=0;
253         Node mod=rootnode.getChild(JJT_MODIFIER);
254         if (mod!=null) {
255             if (mod.hasChild(JJT_FINAL)) {
256                 this.modifier|=java.lang.reflect.Modifier.FINAL;
257             }
258             if (mod.hasChild(JJT_STRICTFP)) {
259                 this.modifier|=java.lang.reflect.Modifier.STRICT;
260             }
261         }
262
263         // get javadoc comment
264
Token t=rootnode.getStartToken();
265         t=t.specialToken;
266         if (t!=null) {
267             String JavaDoc doc=t.image;
268             if (doc.startsWith("/**")) // could be an 'ordinary'-comment (then ignore)
269
{
270                 DocumentationDeclared d=new DocumentationDeclared();
271                 d.setSourceObjectDeclared(this);
272                 doc = doc.replace('\r', ' '); // workaround: avoid problems with crlf-linefeeds
273
d.setRaw(doc);
274                 myDocumentation=d;
275             }
276             else {
277                 myDocumentation=null;
278             }
279         }
280         else {
281             myDocumentation=null;
282         }
283     }
284
285 } // end SourceObjectDeclared
286
Popular Tags