1 /* FOREGEJ - FOrmatting REfactoring GEnerating Java
2 *
3 * Copyright (C) 2003 Andreas Arrgard
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 package com.octagroup.foregej.java.lang;
20 import java.io.Reader;
21 import java.util.List;
22 import antlr.collections.AST;
23 import com.octagroup.foregej.antlr.BaseAST;
24 import com.octagroup.foregej.antlr.BaseToken;
25 import com.octagroup.foregej.antlr.HiddenToken_List;
26 import com.octagroup.foregej.java.doc.ast.AST_JAVADOC;
27 import com.octagroup.foregej.java.lang.tok.Comment;
28 import com.octagroup.foregej.java.lang.tok.TOK_JAVA_COMMENT;
29 /***
30 */
31 public class JavaAST extends BaseAST
32 {
33 /***
34 * the javadoc associated with this ast
35 */
36 private TOK_JAVA_COMMENT javadoc_;
37 /***
38 * the java recognizer used to parse data
39 */
40 protected JavaRecognizer javaRecognizer_;
41 /***
42 * Adds functionality when initializing an AST from another ast.
43 * <p>
44 * This is done by the DynASTFactory when it discovers that an AST
45 * instance is not of the correct class.
46 * </p>
47 *
48 * @param ast
49 */
50 public void initialize(AST ast)
51 {
52 super.initialize(ast);
53 hiddenAfter=((JavaAST)ast).hiddenAfter;
54 hiddenBefore=((JavaAST)ast).hiddenBefore;
55 javadoc_=((JavaAST)ast).javadoc_;
56 }
57 /***
58 * Returns the javadoc associated with this JavaAST.
59 *
60 * @return the javadoc associated with this JavaAST.
61 */
62 public TOK_JAVA_COMMENT getJavadoc()
63 {
64 return javadoc_;
65 }
66 /***
67 * Sets the javadoc asociated with this AST.
68 *
69 * @param javadoc the javadoc to set.
70 */
71 public void setJavadoc(TOK_JAVA_COMMENT javadoc)
72 {
73 javadoc_=javadoc;
74 }
75 /***
76 * Returns the javadoc AST associated with this JavaAST.
77 * <p>
78 * This method fetches the javadoc token an returns the ast from that
79 * token. If there is no javadok token associated with this ast or we
80 * failed to parse the javadoc null will be returned.
81 * </p>
82 *
83 * @return the javadoc associated with this JavaAST.
84 */
85 public AST_JAVADOC getJavadocAst()
86 {
87 if(javadoc_==null) return null;
88 return javadoc_.getAstJavaDoc();
89 }
90 /***
91 * Sets the javadoc asociated with this AST.
92 * <p>
93 * If there is a javadoc token associated with this ast it will be
94 * replaced.
95 * </p>
96 *
97 * @param javadoc the javadoc to set.
98 */
99 public void setJavadocAst(AST_JAVADOC javadoc)
100 {
101 javadoc_=new TOK_JAVA_COMMENT(javadoc);
102 }
103 /***
104 * Returns all the comments before this node.
105 *
106 * @return all the comments before this node.
107 */
108 public List getCommentsBefore()
109 {
110 return new HiddenToken_List(this, HiddenToken_List.TYPE_BEFORE,
111 new Class[]{Comment.class}, null);
112 }
113 /***
114 * Returns all the comments after this node.
115 *
116 * @return all the comments after this node.
117 */
118 public List getCommentsAfter()
119 {
120 return new HiddenToken_List(this, HiddenToken_List.TYPE_AFTER,
121 new Class[]{Comment.class}, null);
122 }
123 /***
124 * Returns true if this node has any comments attached before it.
125 *
126 * @return true if this node has any comments attached before it.
127 */
128 public boolean hasCommentsBefore()
129 {
130 return getHiddenBefore()!=null;
131 }
132 /***
133 * Returns true if this node has any comments attached after it.
134 *
135 * @return true if this node has any comments attached after it.
136 */
137 public boolean hasCommentsAfter()
138 {
139 return getHiddenAfter()!=null;
140 }
141 /***
142 * This method steals comments from the first child.
143 * <p>
144 * This metod is normally invoked from an abstract AST that combines
145 * some other asts. ie CompilationUnit or AST_METHOD_DEF
146 * </p>
147 *
148 * @param type
149 */
150 public void stealCommentsFromFirstChild(Class type)
151 {
152 //
153 // do we have a child to steal from
154 //
155 if(getFirstChild()==null) {
156 return;
157 }
158 //
159 // at the moment this token should not have any
160 // tokens before it since it should be a generated token.
161 //
162 if(getHiddenBefore()!=null) {
163 throw new IllegalStateException("Cannot steal tokens from child when there are tokens attached before ");
164 }
165 setHiddenBefore((BaseToken)((JavaAST)getFirstChild()).getHiddenBefore());
166 ((JavaAST)getFirstChild()).setHiddenBefore(null);
167 }
168 /***
169 * This method steals the javadoc from the first child.
170 */
171 public void stealJavadocFromFirstChild()
172 {
173 //
174 // do we have a child to steal from
175 //
176 if(getFirstChild()==null) {
177 return;
178 }
179 if(getJavadoc()!=null) {
180 return;
181 }
182 if(getFirstChild()!=null) {
183 setJavadoc(((JavaAST)getFirstChild()).getJavadoc());
184 ((JavaAST)getFirstChild()).setJavadoc(null);
185 }
186 }
187 /***
188 * Sets up the recognized so that it parses the supplied string.
189 *
190 * @param s the string to parse
191 */
192 public void setupRecognizer(String s)
193 {
194 javaRecognizer_=JavaRecognizer.setup(s);
195 }
196 /***
197 * Sets up the recognizer so that it parses the supplied source.
198 *
199 * @param fileName the name of the source.
200 * @param source the source to parse.
201 */
202 public void setupRecognizer(String fileName,Reader source)
203 {
204 javaRecognizer_=JavaRecognizer.setup(fileName, source);
205 }
206 /***
207 * Returns a string representation of the AST.
208 *
209 * @return
210 */
211 public String toString()
212 {
213 JavaNodeWriter nw=new JavaNodeWriter();
214 nw.setWriteComments(false);
215 nw.write(this);
216 return nw.toString();
217 }
218 }
This page was automatically generated by Maven