KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > pmd > RemovePerformer


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.pmd;
21
22 import net.sourceforge.pmd.RuleViolation;
23 import pmd.*;
24 import java.io.*;
25 import java.awt.*;
26 import java.awt.event.*;
27 import javax.swing.*;
28 import javax.swing.text.*;
29 import org.openide.text.NbDocument;
30 import org.openide.cookies.SourceCookie;
31 import org.openide.explorer.view.*;
32 import org.openide.nodes.*;
33 import org.openide.ErrorManager;
34 import org.openide.loaders.DataObject;
35 import org.openide.text.Line;
36 import org.openide.text.DataEditorSupport;
37 import org.openide.util.NbBundle;
38 import org.openide.src.*;
39
40
41 import org.netbeans.modules.tasklist.core.TLUtils;
42 import org.netbeans.modules.tasklist.client.Suggestion;
43 import org.netbeans.modules.tasklist.client.SuggestionPerformer;
44 //import org.netbeans.modules.tasklist.core.ConfPanel;
45

46 /**
47  * Perform method removal confirmation and execution
48  * <p>
49  * @author Tor Norbye
50  */

51
52
53 public class RemovePerformer implements SuggestionPerformer {
54     private Line line;
55     private RuleViolation violation;
56     private boolean field;
57
58     /** "comment" parameter not yet implemented (this will allow
59         you to comment out rather than just delete some code
60         @param field when true, remove a field instead of a method
61     */

62     RemovePerformer(boolean field, Line line, RuleViolation violation,
63                     boolean comment) {
64         this.line = line;
65         this.violation = violation;
66         this.field = field;
67     }
68
69     public void perform(Suggestion s) {
70         if (field) {
71             FieldElement el = findField();
72             if (el != null) {
73                 ClassElement cl = el.getDeclaringClass();
74                 try {
75                     cl.removeField(el);
76                 } catch (SourceException ex) {
77                     ErrorManager.getDefault().notify(ErrorManager.WARNING, ex);
78                 }
79             }
80         } else {
81             MethodElement el = findMethod();
82             if (el != null) {
83                 ClassElement cl = el.getDeclaringClass();
84                 try {
85                     cl.removeMethod(el);
86                 } catch (SourceException ex) {
87                     ErrorManager.getDefault().notify(ErrorManager.WARNING, ex);
88                 }
89             }
90         }
91     }
92
93     private MethodElement findMethod() {
94         String JavaDoc desc = violation.getDescription();
95         // HACK - use the name of the method as reported by PMD
96
// to limit the class search. If we fail to find it,
97
// the search will only be line number based, which may not
98
// be as accurate (in case there are multiple elements on the
99
// same line.
100
int idx = desc.indexOf("such as '");
101         String JavaDoc method = null;
102         if (idx != -1) {
103             int edix = desc.indexOf('\'', idx+9);
104             if (edix != -1) {
105                 method = desc.substring(idx+9, edix);
106             } else {
107                 method = desc.substring(idx+9);
108             }
109         }
110
111         DataObject dobj = DataEditorSupport.findDataObject(line);
112         SourceCookie sc = (SourceCookie)dobj.getCookie(SourceCookie.class);
113         if (sc == null) {
114             return null; // shouldn't happen
115
}
116         MethodElement m = null;
117         SourceElement se = sc.getSource();
118         if ( se != null ) {
119             ClassElement[] ces = se.getAllClasses();
120             for( int j = 0; j < ces.length; j++ ) {
121                 m = findMethod(method, ces[j], dobj);
122                 if (m != null) {
123                     break;
124                 }
125                 ClassElement[] inner = ces[j].getClasses();
126                 for( int k = 0; k < inner.length; k++ ) {
127                     m = findMethod(method, inner[k], dobj);
128                     if (m != null) {
129                         break;
130                     }
131                 }
132                 if (m != null) {
133                     break;
134                 }
135             }
136         }
137         return m;
138     }
139     
140     private MethodElement findMethod(String JavaDoc method, ClassElement cl,
141                                      DataObject dobj) {
142         MethodElement[] methods = cl.getMethods();
143         for (int i = 0; i < methods.length; i++) {
144             // If I've got a method name extracted from the rule,
145
// try to match it, otherwise just look for source position
146
if ((method == null) ||
147                 (methods[i].getName().getName().equals(method))) {
148                 // Possible candidate. Check for source position
149
Line l = getLine(methods[i], dobj);
150                 if ((l != null) && (l == line)) {
151                     return methods[i];
152                 } // else : Found the method, but not the right line.
153
}
154         }
155         return null;
156     }
157     
158     private FieldElement findField() {
159         String JavaDoc desc = violation.getDescription();
160         // HACK - use the name of the field as reported by PMD
161
// to limit the class search. If we fail to find it,
162
// the search will only be line number based, which may not
163
// be as accurate (in case there are multiple elements on the
164
// same line.
165
int idx = desc.indexOf("such as '");
166         String JavaDoc field = null;
167         if (idx != -1) {
168             int edix = desc.indexOf('\'', idx+9);
169             if (edix != -1) {
170                 field = desc.substring(idx+9, edix);
171             } else {
172                 field = desc.substring(idx+9);
173             }
174         }
175
176         DataObject dobj = DataEditorSupport.findDataObject(line);
177         SourceCookie sc = (SourceCookie)dobj.getCookie(SourceCookie.class);
178         if (sc == null) {
179             return null; // shouldn't happen
180
}
181         FieldElement m = null;
182         SourceElement se = sc.getSource();
183         if ( se != null ) {
184             ClassElement[] ces = se.getAllClasses();
185             for( int j = 0; j < ces.length; j++ ) {
186                 m = findField(field, ces[j], dobj);
187                 if (m != null) {
188                     break;
189                 }
190                 ClassElement[] inner = ces[j].getClasses();
191                 for( int k = 0; k < inner.length; k++ ) {
192                     m = findField(field, inner[k], dobj);
193                     if (m != null) {
194                         break;
195                     }
196                 }
197                 if (m != null) {
198                     break;
199                 }
200             }
201         }
202         return m;
203     }
204     
205     private FieldElement findField(String JavaDoc field, ClassElement cl,
206                                      DataObject dobj) {
207         FieldElement[] fields = cl.getFields();
208         for (int i = 0; i < fields.length; i++) {
209             // If I've got a field name extracted from the rule,
210
// try to match it, otherwise just look for source position
211
if ((field == null) ||
212                 (fields[i].getName().getName().equals(field))) {
213                 // Possible candidate. Check for source position
214
Line l = getLine(fields[i], dobj);
215                 if ((l != null) && (l == line)) {
216                     return fields[i];
217                 } // else : Found the field, but not the right line.
218
}
219         }
220         return null;
221     }
222     
223     private Line getLine(MethodElement el, DataObject dobj) {
224         SourceCookie.Editor editor =
225             (SourceCookie.Editor)dobj.getCookie(SourceCookie.Editor.class);
226         javax.swing.text.Element JavaDoc textElement = editor.sourceToText(el);
227         if (textElement != null) {
228             StyledDocument document = editor.getDocument();
229             if (document != null) {
230                 int offset = textElement.getStartOffset();
231                 
232                 // If a method has javadoc, we get the position of the
233
// javadoc, not the beginning of the method declaration -
234
// and it's this second number that PMD is reporting to
235
// us. So we have to compute where the method really begins.
236
int bias = 0;
237                 JavaDoc.Method jdm = el.getJavaDoc();
238                 if (jdm != null) {
239                     String JavaDoc raw = jdm.getRawText();
240                     if (raw != null) {
241                         bias++;
242                         for (int i = 0; i < raw.length(); i++) {
243                             if (raw.charAt(i) == '\n') {
244                                 bias++;
245                             }
246                         }
247                     }
248                 }
249                 
250                 int lineNumber = NbDocument.findLineNumber(document, offset)+bias;
251                 Line line = editor.getLineSet().getCurrent(lineNumber);
252                 return line;
253             }
254         }
255         return null;
256     }
257
258
259     private String JavaDoc getMethodText() {
260         MethodElement el = findMethod();
261         if (el == null) {
262             return "";
263         }
264         StringWriter sw = new StringWriter();
265         ElementPrinter p = new DefaultElementPrinter(new PrintWriter(sw));
266         try {
267             el.print(p);
268         } catch (ElementPrinterInterruptException e) {
269             ErrorManager.getDefault().notify(ErrorManager.WARNING, e);
270         }
271         String JavaDoc source = sw.toString();
272         return source;
273     }
274
275
276     private Line getLine(FieldElement el, DataObject dobj) {
277         SourceCookie.Editor editor =
278             (SourceCookie.Editor)dobj.getCookie(SourceCookie.Editor.class);
279         javax.swing.text.Element JavaDoc textElement = editor.sourceToText(el);
280         if (textElement != null) {
281             StyledDocument document = editor.getDocument();
282             if (document != null) {
283                 int offset = textElement.getStartOffset();
284                 
285                 // If a field has javadoc, we get the position of the
286
// javadoc, not the beginning of the field declaration -
287
// and it's this second number that PMD is reporting to
288
// us. So we have to compute where the field really begins.
289
int bias = 0;
290                 JavaDoc.Field jdm = el.getJavaDoc();
291                 if (jdm != null) {
292                     String JavaDoc raw = jdm.getRawText();
293                     if (raw != null) {
294                         bias++;
295                         for (int i = 0; i < raw.length(); i++) {
296                             if (raw.charAt(i) == '\n') {
297                                 bias++;
298                             }
299                         }
300                     }
301                 }
302                 
303                 int lineNumber = NbDocument.findLineNumber(document, offset)+bias;
304                 Line line = editor.getLineSet().getCurrent(lineNumber);
305                 return line;
306             }
307         }
308         return null;
309     }
310
311
312     private String JavaDoc getFieldText() {
313         FieldElement el = findField();
314         if (el == null) {
315             return "";
316         }
317         StringWriter sw = new StringWriter();
318         ElementPrinter p = new DefaultElementPrinter(new PrintWriter(sw));
319         try {
320             el.print(p);
321         } catch (ElementPrinterInterruptException e) {
322             ErrorManager.getDefault().notify(ErrorManager.WARNING, e);
323         }
324         String JavaDoc source = sw.toString();
325         return source;
326     }
327
328
329     public boolean hasConfirmation() {
330         return true;
331     }
332     public Object JavaDoc getConfirmation(Suggestion s) {
333         DataObject dao = DataEditorSupport.findDataObject(line);
334         int linenumber = line.getLineNumber();
335         String JavaDoc filename = dao.getPrimaryFile().getNameExt();
336         String JavaDoc ruleDesc = violation.getRule().getDescription();
337         String JavaDoc ruleExample = violation.getRule().getExample();
338         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(2000);
339         String JavaDoc beforeContents = null;
340         String JavaDoc afterContents = null;
341         String JavaDoc afterDesc = null;
342         String JavaDoc beforeDesc = null;
343         //if (comment) {
344
// Not yet implemented
345
//} else {
346
beforeDesc = NbBundle.getMessage(RemovePerformer.class,
347                                 "RemoveUnusedMethod"); // NOI18N
348
sb.append("<html>"); // NOI18N
349

350             // HACK: I also noticed that "/** Javadoc here"
351
// wouldn't correctly draw the first line, so
352
// hack around it by putting some useless
353
// attributes in there.
354
sb.append("<b></b>");
355
356             if (field) {
357                 TLUtils.appendHTMLString(sb, getFieldText());
358             } else {
359                 TLUtils.appendHTMLString(sb, getMethodText());
360             }
361             sb.append("</html>"); // NOI18N
362
beforeContents = sb.toString();
363         //}
364

365         return new ConfPanel(beforeDesc,
366                              beforeContents, afterDesc,
367                              afterContents,
368                              filename, linenumber);
369         /*
370                              ViolationProvider.getBottomPanel(ruleDesc,
371                                                               ruleExample));
372         */

373     }
374 }
375
Popular Tags