View Javadoc

1   /*
2    * Copyright (c) 2001-2003 The XDoclet team
3    * All rights reserved.
4    */
5   package xjavadoc;
6   
7   import java.lang.reflect.Modifier;
8   import java.util.*;
9   
10  /***
11   * Describe what this class does
12   *
13   * @author    Ara Abrahamian
14   * @author    Aslak Hellesøy
15   * @created   7. mars 2003
16   */
17  public abstract class AbstractProgramElement implements XProgramElement
18  {
19  	final static List  EMPTY_LIST = Collections.unmodifiableList( new LinkedList() );
20  
21      private XJavaDoc _xJavaDoc;
22  	private AbstractClass _containingClass;
23  	private int        _modifiers = 0;
24  	private String     _modifierString;
25  	private XDoc       _doc;
26  	private Token      _token;
27  	private Token      _javadocToken;
28      private final XTagFactory _tagFactory;
29  
30      protected AbstractProgramElement( AbstractClass containingClass, XTagFactory tagFactory )
31      {
32          _xJavaDoc = containingClass.getXJavaDoc();
33          _containingClass = containingClass;
34          _tagFactory = tagFactory;
35      }
36  
37      protected AbstractProgramElement( XJavaDoc xJavaDoc,  XTagFactory tagFactory )
38      {
39          _xJavaDoc = xJavaDoc;
40          _containingClass = null;
41          _tagFactory = tagFactory;
42      }
43  
44      public XJavaDoc getXJavaDoc() {
45          return _xJavaDoc;
46      }
47  
48  	public final boolean isFinal()
49  	{
50  		return ( _modifiers & Modifier.FINAL ) != 0;
51  	}
52  
53  	public final boolean isAbstract()
54  	{
55  		return ( _modifiers & Modifier.ABSTRACT ) != 0;
56  	}
57  
58  	public final boolean isPackagePrivate()
59  	{
60  		return !isPrivate() && !isProtected() && !isPublic();
61  	}
62  
63  	public final boolean isPrivate()
64  	{
65  		return ( _modifiers & Modifier.PRIVATE ) != 0;
66  	}
67  
68  	public final boolean isProtected()
69  	{
70  		return ( _modifiers & Modifier.PROTECTED ) != 0;
71  	}
72  
73  	public final boolean isPublic()
74  	{
75  		return ( _modifiers & Modifier.PUBLIC ) != 0;
76  	}
77  
78  	public final boolean isStatic()
79  	{
80  		return ( _modifiers & Modifier.STATIC ) != 0;
81  	}
82  
83  	/***
84  	 * Get the doc. If this is a binary, primitive or unknown, null is returned.
85  	 *
86  	 * @return   the class level doc
87  	 */
88  	public final XDoc getDoc()
89  	{
90  		if( _doc == null )
91  		{
92  			if( _token == null )
93  			{
94  				// We're not from source (we're binary, primitive or unknown)
95  				_doc = new XDoc( null, this, _tagFactory );
96  			}
97  			else
98  			{
99  				if( _javadocToken != null )
100 				{
101 					_doc = new XDoc( _javadocToken, this, _tagFactory );
102 				}
103 				else
104 				{
105 					// there was no doc in the original source. Create it.
106 					// We have to create a new token and attach it to _token as specialToken
107 					// The pre and post tokens are only to ensure proper line breaks before and after
108 					Token preJavadocToken = Token.newToken( NodeParserConstants.DEFAULT );
109 
110 					preJavadocToken.image = "\n\n";
111 
112 					_javadocToken = Token.newToken( NodeParserConstants.FORMAL_COMMENT );
113 					_javadocToken.image = "";
114 
115 					Token postJavadocToken = Token.newToken( NodeParserConstants.DEFAULT );
116 
117 					postJavadocToken.image = "\n";
118 
119 					// Link the new tokens properly
120 					preJavadocToken.next = _javadocToken;
121 					_javadocToken.next = postJavadocToken;
122 
123 					_token.specialToken = preJavadocToken;
124 					_doc = new XDoc( _javadocToken, this, _tagFactory );
125 				}
126 			}
127 			// Help the GC. We don't need the tokens anymore.
128 			_token = null;
129 			_javadocToken = null;
130 		}
131 		return _doc;
132 	}
133 
134 	/***
135 	 * Get modifiers string.
136 	 *
137 	 * @return
138 	 */
139 	public final String getModifiers()
140 	{
141 		if( _modifierString == null )
142 		{
143 			_modifierString = Modifier.toString( _modifiers );
144 		}
145 		return _modifierString;
146 	}
147 
148 	/***
149 	 * Get the modifier specifier integer.
150 	 *
151 	 * @return
152 	 */
153 	public final int getModifierSpecifier()
154 	{
155 		return _modifiers;
156 	}
157 
158 	public final XClass getContainingClass()
159 	{
160 		return getContainingAbstractClass();
161 	}
162 
163 	public final AbstractClass getContainingAbstractClass()
164 	{
165 		return _containingClass;
166 	}
167 
168 	public XPackage getContainingPackage()
169 	{
170 		if( _containingClass != null )
171 		{
172 			return getContainingClass().getContainingPackage();
173 		}
174 		else
175 		{
176 			return null;
177 		}
178 	}
179 
180 	/***
181 	 * Sets the Token where we start. Useful for doc mutation.
182 	 *
183 	 * @param token  The new Token value
184 	 */
185 	public final void setToken( Token token )
186 	{
187 		if( _token == null && token != null )
188 		{
189 			_token = token;
190 			setJavaDoc();
191 		}
192 	}
193 
194 	public final void addModifier( int modifier )
195 	{
196 		_modifiers |= modifier;
197 	}
198 
199 	public int compareTo( Object o )
200 	{
201 		XProgramElement other = ( XProgramElement ) o;
202 
203 		return getName().compareTo( other.getName() );
204 	}
205 
206 	/***
207 	 * update javadoc
208 	 */
209 	public void updateDoc()
210 	{
211 		if( _doc != null )
212 		{
213 			_doc.updateToken();
214 		}
215 	}
216 
217     protected XTagFactory getTagFactory() {
218         return _tagFactory;
219     }
220 
221 	void reset()
222 	{
223 		_doc = null;
224 		_containingClass = null;
225 		_token = null;
226 		_javadocToken = null;
227 	}
228 
229 	private final void setJavaDoc()
230 	{
231 		Token javadoc = null;
232 
233 		Token tt = _token.specialToken;
234 
235 		if( tt != null )
236 		{
237 			while( tt.specialToken != null )
238 			{
239 				tt = tt.specialToken;
240 			}
241 			while( tt != null )
242 			{
243 				if( tt.kind == NodeParserConstants.FORMAL_COMMENT )
244 				{
245 					// it's JavaDoc
246 					javadoc = tt;
247 				}
248 				else if( tt.kind == NodeParserConstants.SINGLE_LINE_COMMENT || tt.kind == NodeParserConstants.MULTI_LINE_COMMENT )
249 				{
250 					// reset it. some other comment is standalone or followed what could have been a javadoc comment
251 					javadoc = null;
252 				}
253 				tt = tt.next;
254 			}
255 		}
256 		if( javadoc != null )
257 		{
258 			// There was javadoc here!
259 			if( _doc != null )
260 			{
261 				throw new IllegalStateException( "Doc has already been set!!" );
262 			}
263 			else
264 			{
265 				_javadocToken = javadoc;
266 			}
267 		}
268 	}
269 
270 }