1 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 46 51 52 53 public class RemovePerformer implements SuggestionPerformer { 54 private Line line; 55 private RuleViolation violation; 56 private boolean field; 57 58 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 desc = violation.getDescription(); 95 int idx = desc.indexOf("such as '"); 101 String 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; } 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 method, ClassElement cl, 141 DataObject dobj) { 142 MethodElement[] methods = cl.getMethods(); 143 for (int i = 0; i < methods.length; i++) { 144 if ((method == null) || 147 (methods[i].getName().getName().equals(method))) { 148 Line l = getLine(methods[i], dobj); 150 if ((l != null) && (l == line)) { 151 return methods[i]; 152 } } 154 } 155 return null; 156 } 157 158 private FieldElement findField() { 159 String desc = violation.getDescription(); 160 int idx = desc.indexOf("such as '"); 166 String 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; } 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 field, ClassElement cl, 206 DataObject dobj) { 207 FieldElement[] fields = cl.getFields(); 208 for (int i = 0; i < fields.length; i++) { 209 if ((field == null) || 212 (fields[i].getName().getName().equals(field))) { 213 Line l = getLine(fields[i], dobj); 215 if ((l != null) && (l == line)) { 216 return fields[i]; 217 } } 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 textElement = editor.sourceToText(el); 227 if (textElement != null) { 228 StyledDocument document = editor.getDocument(); 229 if (document != null) { 230 int offset = textElement.getStartOffset(); 231 232 int bias = 0; 237 JavaDoc.Method jdm = el.getJavaDoc(); 238 if (jdm != null) { 239 String 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 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 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 textElement = editor.sourceToText(el); 280 if (textElement != null) { 281 StyledDocument document = editor.getDocument(); 282 if (document != null) { 283 int offset = textElement.getStartOffset(); 284 285 int bias = 0; 290 JavaDoc.Field jdm = el.getJavaDoc(); 291 if (jdm != null) { 292 String 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 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 source = sw.toString(); 325 return source; 326 } 327 328 329 public boolean hasConfirmation() { 330 return true; 331 } 332 public Object getConfirmation(Suggestion s) { 333 DataObject dao = DataEditorSupport.findDataObject(line); 334 int linenumber = line.getLineNumber(); 335 String filename = dao.getPrimaryFile().getNameExt(); 336 String ruleDesc = violation.getRule().getDescription(); 337 String ruleExample = violation.getRule().getExample(); 338 StringBuffer sb = new StringBuffer (2000); 339 String beforeContents = null; 340 String afterContents = null; 341 String afterDesc = null; 342 String beforeDesc = null; 343 beforeDesc = NbBundle.getMessage(RemovePerformer.class, 347 "RemoveUnusedMethod"); sb.append("<html>"); 350 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>"); beforeContents = sb.toString(); 363 365 return new ConfPanel(beforeDesc, 366 beforeContents, afterDesc, 367 afterContents, 368 filename, linenumber); 369 373 } 374 } 375 | Popular Tags |