KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > javadoc > AutoCommenter


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.tasklist.javadoc;
21
22 import java.awt.Image JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.lang.reflect.Modifier JavaDoc;
26
27 import org.openide.src.*;
28 import org.openide.cookies.SourceCookie;
29 import org.openide.loaders.DataObject;
30 import java.util.EventListener JavaDoc;
31 import javax.swing.text.StyledDocument JavaDoc;
32 import org.netbeans.modules.tasklist.client.Suggestion;
33 import org.netbeans.modules.tasklist.client.SuggestionManager;
34 import org.netbeans.modules.tasklist.client.SuggestionPerformer;
35 import org.netbeans.modules.tasklist.client.SuggestionAgent;
36 import org.netbeans.modules.tasklist.providers.SuggestionContext;
37 import org.netbeans.modules.tasklist.javadoc.ext.JavaTagNames;
38 import org.openide.text.Line;
39 import org.openide.text.NbDocument;
40 import org.openide.util.Utilities;
41
42 /**
43  * This file has been shamelessly stolen from the javadoc module.
44  * I at first plugged my tasklist functionality into the javadoc
45  * module, but since I had to make a couple of modifications etc.
46  * I figured that meant this functionality will have to be delivered
47  * as a patch, which means few people will ever see it.
48  * So for now, just replicate the code and make a few modifications
49  * (for example, the old AutoCommenter creates Swing List models).
50  * I have marked the places I changed with a pair of
51  *
52  * <p>
53  * Specific changes made:
54  * <ul>
55  * <li> Replaced ResourceUtils references with NbBundle direct calls
56  * <li> Added a resolution list (to parallel the errorList)
57  * which lists the specific errors fixed (where the strings are
58  * labelled FIX_ instead of ERR_).
59  * (These were the errors preceeded by a checkOnly flag)
60  * </ul>
61  * <p>
62  * Original javadoc:
63  * <p>
64  * Contains static methods for generating default JavaDoc comments for
65  * java data object hierarchy elements.
66  *
67  * Checks for Comment errors in JavaDataObjects
68  *
69  * @author Petr Hrebejk
70  */

71 public final class AutoCommenter extends Object JavaDoc implements JavaTagNames {
72     public static final int JDC_OK = 1;
73     public static final int JDC_MISSING = 2;
74     public static final int JDC_ERROR = 4;
75     
76     private ArrayList JavaDoc elements;
77     private final DataObject dataObject;
78     /** Utility field holding the PropertyChangeListener. */
79     private AutoCommentChangeListener autoCommentChangeListener = null;
80     private SuggestionContext env;
81     private SuggestionManager man;
82
83     /**
84      *
85      * Creates Auto commenter for data objects. NOTE: Does NOT NOT NOT NOT
86      * keep listening on the data objects etc.
87      */

88     public AutoCommenter(SuggestionManager man, DataObject dobj, SuggestionContext env ) {
89         this.man = man;
90         dataObject = dobj;
91         this.env = env;
92         elements = new ArrayList JavaDoc();
93     }
94
95     private void prepareListModel(ArrayList JavaDoc listModel, int mask, boolean pckg, int err_mask ) {
96         Iterator JavaDoc it = elements.iterator();
97
98         while( it.hasNext() ) {
99             AutoCommenterElement el = (AutoCommenterElement)it.next();
100
101             if ( acceptElement( el, mask, pckg, err_mask ) ) {
102                 listModel.add( el );
103             }
104         }
105     }
106     
107     static boolean acceptElement(AutoCommenterElement el, int mask, boolean pckg, int err_mask ) {
108         // Test whether the element is accepted by error mask
109
if ((el.getErrorNumber() & err_mask) == 0)
110             return false;
111
112         // Test whether the element is accepted by access mask
113
int access = JavaDocUtils.getEffectiveAccess((MemberElement)el.getSrcElement());
114         if (access == 0)
115             return pckg;
116         else
117             return (access & mask) > 0;
118     }
119
120     ArrayList JavaDoc prepareListModel( int mask, boolean pckg, int err_mask ) {
121         ArrayList JavaDoc dm = new ArrayList JavaDoc();
122         prepareListModel( dm, mask, pckg, err_mask );
123         return dm;
124     }
125
126     private void addElements( ClassElement classElement ) {
127         elements.add( new AutoCommenterClass( classElement ) );
128
129         FieldElement[] fe = classElement.getFields();
130         for( int i = 0; i < fe.length; i++ ) {
131             elements.add( new AutoCommenterField ( fe[i] ) );
132         }
133
134         ConstructorElement[] ce = classElement.getConstructors();
135         for( int i = 0; i < ce.length; i++ ) {
136             elements.add( new AutoCommenterConstructor ( ce[i] ) );
137         }
138
139         MethodElement[] me = classElement.getMethods();
140         for( int i = 0; i < me.length; i++ ) {
141             elements.add( new AutoCommenterMethod ( me[i] ) );
142         }
143
144         /* getAllClasses is already recursive
145         ClassElement[] ice = classElement.getClasses();
146         for( int i = 0; i < ice.length; i++ ) {
147           addElements( ice[i] );
148     }
149         */

150     }
151     
152     public interface AutoCommentChangeListener extends EventListener JavaDoc {
153         public void listChanged();
154     }
155     
156     /** Registers PropertyChangeListener to receive events.
157      * @param listener The listener to register.
158      */

159     public synchronized void addAutoCommentChangeListener(AutoCommentChangeListener listener) throws java.util.TooManyListenersException JavaDoc {
160         if (autoCommentChangeListener != null) {
161             throw new java.util.TooManyListenersException JavaDoc();
162         }
163         autoCommentChangeListener = listener;
164     }
165     
166     /** Removes PropertyChangeListener from the list of listeners.
167      * @param listener The listener to remove.
168      */

169     public synchronized void removeAutoCommentChangeListener(AutoCommentChangeListener listener) {
170         autoCommentChangeListener = null;
171     }
172     
173     public ArrayList JavaDoc findErrors() {
174         SourceCookie sc = (SourceCookie)dataObject.getCookie(SourceCookie.class);
175         
176         // We end up showing this panel for non-javadoc areas as well,
177
// such as Bundle.properties files. Make sure we only operate
178
// on files we have SourceCookies for!
179
if (sc == null)
180             return null;
181        
182         SourceElement se = sc.getSource();
183         if ( se != null ) {
184             ClassElement[] ces = se.getAllClasses();
185             for( int j = 0; j < ces.length; j++ ){
186                 addElements( ces[j] );
187             }
188         }
189        
190         return findErrors_();
191     }
192     
193     private ArrayList JavaDoc findErrors_() {
194         ArrayList JavaDoc tasks = new ArrayList JavaDoc(30);
195
196         assert false : "Reimplemented in tasklist.javadoc.ext package";
197 // // check for Javadoc for API (protected & public)
198
// int modifierMask = Modifier.PUBLIC | Modifier.PROTECTED;
199
// int errorMask = AutoCommenter.JDC_ERROR | AutoCommenter.JDC_MISSING;
200
// boolean bpackage = false; // Modifier.PACKAGE
201
//
202
// ArrayList model = prepareListModel(modifierMask, bpackage, errorMask);
203
//
204
// int n = model.size();
205
// for (int i = 0; i < n; i++) {
206
// Object value = model.get(i);
207
// final AutoCommenterElement element = (AutoCommenterElement)value;
208
// if (element.getErrorNumber() ==
209
// AutoCommenter.JDC_OK) {
210
// continue;
211
// }
212
//
213
// SuggestionPerformer action = new JavaDocSuggestionPerformer(
214
// element, env);
215
//
216
// ArrayList m2 = element.getErrorList();
217
// int m2n = m2.size();
218
// for (int j = 0; j < m2n; j++) {
219
// Object v2 = m2.get(j);
220
// String summary = element.getSrcElement().getName() +
221
// ": " + // NOI18N
222
// v2.toString();
223
// MemberElement el = element.getSrcElement();
224
// if ((el instanceof MethodElement) &&
225
// (inheritsJavadoc((MethodElement)el))) {
226
// continue;
227
// }
228
// SuggestionAgent s = man.createSuggestion(
229
// DocSuggester.TYPE, summary, null, this);
230
//
231
// //LineCookie ck = el.getCookie
232
// // TODO: get & add line position
233
// // Line l = null;
234
// //s.setLine(l);
235
//
236
// SourceCookie.Editor editor =
237
// (SourceCookie.Editor)dataObject.getCookie(SourceCookie.Editor.class);
238
// javax.swing.text.Element textElement = editor.sourceToText(el);
239
// if (textElement != null) {
240
// StyledDocument document = editor.getDocument();
241
// if (document != null) {
242
// int offset = textElement.getStartOffset();
243
// int lineNumber = NbDocument.findLineNumber(document, offset);
244
// Line line = editor.getLineSet().getCurrent(lineNumber);
245
// s.setLine(line);
246
// }
247
// }
248
// /* Error checking - do something similar above
249
// try {
250
// javax.swing.text.Element textElement =
251
// editor.sourceToText((Element)offendingObject);
252
//
253
// if (textElement != null)
254
// {
255
// StyledDocument document = findDocument(editor);
256
//
257
// if (document != null) {
258
// int offset = textElement.getStartOffset();
259
// line = NbDocument.findLineNumber(document,
260
// offset) + 1;
261
// column = NbDocument.findLineColumn(document,
262
// offset) + 1;
263
// }
264
// }
265
// } catch (IllegalArgumentException iae) {
266
// // found an element which doesn't have source
267
// // just don't set up a line and column
268
// }
269
// */
270
//
271
// if (element.isCorrectable()) {
272
// if (element.getErrorNumber() ==
273
// AutoCommenter.JDC_MISSING) {
274
// // isCorrectable() seems to lie. Missing javadocs
275
// // are never correctable.
276
// Image taskIcon = Utilities.loadImage("org/netbeans/modules/tasklist/javadoc/missing.gif"); // NOI18N
277
// s.setIcon(taskIcon);
278
// } else {
279
// Image taskIcon = Utilities.loadImage("org/netbeans/modules/tasklist/javadoc/fixable-error.gif"); // NOI18N
280
// s.setIcon(taskIcon);
281
// s.setAction(action);
282
// }
283
// } else if (element.getErrorNumber() ==
284
// AutoCommenter.JDC_MISSING) {
285
// Image taskIcon = Utilities.loadImage("org/netbeans/modules/tasklist/javadoc/missing.gif"); // NOI18N
286
// s.setIcon(taskIcon);
287
// }
288
//
289
// tasks.add(s.getSuggestion());
290
// }
291
// }
292
return tasks;
293     }
294
295     private boolean inheritsJavadoc(MethodElement method) {
296         ClassElement cl = method.getDeclaringClass();
297         if (cl == null) {
298             return false;
299         }
300         MethodParameter[] params = method.getParameters();
301         Type[] types = new Type[params.length];
302         for (int i = 0; i < params.length; i++) {
303             types[i] = params[i].getType();
304         }
305         boolean[] checkedObject = new boolean[] {false};
306         boolean found = inheritsJavadoc(cl, method, method.getName(), types, checkedObject);
307         return found;
308     }
309
310     private boolean inheritsJavadoc(ClassElement cl, MethodElement self,
311                                     Identifier method, Type[] arguments,
312                                     boolean[] checkedObject) {
313         checkedObject[0] |= cl.getName().getFullName().equals("java.lang.Object"); // NOI18N
314

315         // See if the class itself contains the given method
316
MethodElement mel = cl.getMethod(method, arguments);
317         if ((mel != null) && (mel != self)) {
318             // Check to see if the method has javadoc
319
JavaDoc javadoc = mel.getJavaDoc();
320             if ((javadoc != null) && !javadoc.isEmpty()) {
321                 return true;
322             }
323         }
324
325         // Check interfaces
326
Identifier[] interfaces = cl.getInterfaces();
327         for (int j = 0; j < interfaces.length; j++) {
328             ClassElement icl = ClassElement.forName(interfaces[j].getFullName());
329             if (icl == null) {
330                 continue;
331             }
332             boolean found = inheritsJavadoc(icl, self, method, arguments, checkedObject);
333             if (found) {
334                 return true;
335             }
336         }
337
338         // Check super class
339
Identifier scli = cl.getSuperclass();
340         if (scli == null && !checkedObject[0]) {
341             scli = Identifier.create("java.lang.Object"); // NOI18N
342
}
343         if (scli != null) {
344             ClassElement scl = ClassElement.forName(scli.getFullName());
345             if (scl != null) {
346                 boolean found = inheritsJavadoc(scl, self, method, arguments, checkedObject);
347                 if (found) {
348                     return true;
349                 }
350             }
351         }
352         return false;
353     }
354 }
355
Popular Tags