1 19 20 package edu.umd.cs.findbugs; 21 22 import java.lang.reflect.Constructor ; 23 import java.lang.reflect.Method ; 24 import java.util.Collection ; 25 import java.util.LinkedList ; 26 import java.util.List ; 27 import java.util.StringTokenizer ; 28 29 import edu.umd.cs.findbugs.ba.AnalysisContext; 30 31 38 public class DetectorFactory { 39 private static final boolean DEBUG_JAVA_VERSION = SystemProperties.getBoolean("findbugs.debug.javaversion"); 40 41 private static final Class [] constructorArgTypes = new Class []{BugReporter.class}; 42 43 static class ReflectionDetectorCreator { 44 private Class <?> detectorClass; 45 private Method setAnalysisContext; 46 47 ReflectionDetectorCreator(Class <?> detectorClass) { 48 this.detectorClass = detectorClass; 49 50 try { 51 setAnalysisContext = detectorClass.getDeclaredMethod( 52 "setAnalysisContext", new Class []{AnalysisContext.class}); 53 } catch (NoSuchMethodException e) { 54 } 56 } 57 58 public Detector createDetector(BugReporter bugReporter) { 59 try { 60 Constructor <?> constructor = 61 detectorClass.getConstructor(constructorArgTypes); 62 Detector detector = (Detector) constructor.newInstance(new Object []{bugReporter}); 63 if (setAnalysisContext != null) { 64 setAnalysisContext.invoke( 65 detector, 66 new Object []{AnalysisContext.currentAnalysisContext()}); 67 } 68 return detector; 69 } catch (Exception e) { 70 throw new RuntimeException ("Could not instantiate " + detectorClass.getName() + 71 " as Detector", e); 72 } 73 } 74 75 public Detector2 createDetector2(BugReporter bugReporter) { 76 if (Detector2.class.isAssignableFrom(detectorClass)) { 77 try { 78 Constructor <?> constructor = 79 detectorClass.getConstructor(constructorArgTypes); 80 return (Detector2) constructor.newInstance(new Object []{bugReporter}); 81 } catch (Exception e) { 82 throw new RuntimeException ("Could not instantiate " + detectorClass.getName() + 83 " as Detector2", e); 84 } 85 } 86 87 if (Detector.class.isAssignableFrom(detectorClass)) { 88 DetectorToDetector2Adapter adapter = new DetectorToDetector2Adapter( 89 createDetector(bugReporter)); 90 return adapter; 91 } 92 93 throw new RuntimeException ("Class " + detectorClass.getName() + " is not a detector class"); 94 } 95 96 public Class <?> getDetectorClass() { 97 return detectorClass; 98 } 99 } 100 101 private Plugin plugin; 102 private final ReflectionDetectorCreator detectorCreator; 103 private int positionSpecifiedInPluginDescriptor; 104 private boolean defEnabled; 105 private final String speed; 106 private final String reports; 107 private final String requireJRE; 108 private String detailHTML; 109 private int priorityAdjustment; 110 private boolean hidden; 111 112 125 public DetectorFactory(Plugin plugin, 126 Class <?> detectorClass, boolean enabled, String speed, String reports, 127 String requireJRE) { 128 this.plugin = plugin; 129 this.detectorCreator = new ReflectionDetectorCreator(detectorClass); 130 this.defEnabled = enabled; 131 this.speed = speed; 132 this.reports = reports; 133 this.requireJRE = requireJRE; 134 this.priorityAdjustment = 0; 135 this.hidden = false; 136 } 137 166 172 public void setPositionSpecifiedInPluginDescriptor( 173 int positionSpecifiedInPluginDescriptor) { 174 this.positionSpecifiedInPluginDescriptor = positionSpecifiedInPluginDescriptor; 175 } 176 177 183 public int getPositionSpecifiedInPluginDescriptor() { 184 return positionSpecifiedInPluginDescriptor; 185 } 186 187 192 public Plugin getPlugin() { 193 return plugin; 194 } 195 196 202 public boolean isDetectorClassSubtypeOf(Class <?> otherClass) { 203 return otherClass.isAssignableFrom(detectorCreator.getDetectorClass()); 204 } 205 206 212 public boolean isReportingDetector() { 213 return !isDetectorClassSubtypeOf(TrainingDetector.class) 214 && !isDetectorClassSubtypeOf(NonReportingDetector.class); 215 216 } 217 218 225 public boolean isEnabledForCurrentJRE() { 226 if (requireJRE.equals("")) 227 return true; 228 try { 229 JavaVersion requiredVersion = new JavaVersion(requireJRE); 230 JavaVersion runtimeVersion = JavaVersion.getRuntimeVersion(); 231 232 if (DEBUG_JAVA_VERSION) { 233 System.out.println( 234 "Checking JRE version for " + getShortName() + 235 " (requires " + requiredVersion + 236 ", running on " + runtimeVersion + ")"); 237 } 238 239 240 boolean enabledForCurrentJRE = runtimeVersion.isSameOrNewerThan(requiredVersion); 241 if (DEBUG_JAVA_VERSION) { 242 System.out.println("\t==> " + enabledForCurrentJRE); 243 } 244 return enabledForCurrentJRE; 245 } catch (JavaVersionException e) { 246 if (DEBUG_JAVA_VERSION) { 247 System.out.println("Couldn't check Java version: " + e.toString()); 248 e.printStackTrace(System.out); 249 } 250 return false; 251 } 252 } 253 254 261 public void setHidden(boolean hidden) { 262 this.hidden = hidden; 263 } 264 265 268 public boolean isHidden() { 269 return hidden; 270 } 271 272 275 public boolean isDefaultEnabled() { 276 return defEnabled; 277 } 278 279 284 public void setPriorityAdjustment(int priorityAdjustment) { 285 this.priorityAdjustment = priorityAdjustment; 286 } 287 288 293 public int getPriorityAdjustment() { 294 return priorityAdjustment; 295 } 296 297 300 public String getSpeed() { 301 return speed; 302 } 303 304 307 public String getReportedBugPatternCodes() { 308 return reports; 309 } 310 311 316 public Collection <BugPattern> getReportedBugPatterns() { 317 List <BugPattern> result = new LinkedList <BugPattern>(); 318 StringTokenizer tok = new StringTokenizer (reports, ","); 319 while (tok.hasMoreTokens()) { 320 String type = tok.nextToken(); 321 BugPattern bugPattern = I18N.instance().lookupBugPattern(type); 322 if (bugPattern != null) 323 result.add(bugPattern); 324 } 325 return result; 326 } 327 328 331 public String getDetailHTML() { 332 return detailHTML; 333 } 334 335 338 public void setDetailHTML(String detailHTML) { 339 this.detailHTML = detailHTML; 340 } 341 342 351 public Detector create(BugReporter bugReporter) { 352 return detectorCreator.createDetector(bugReporter); 353 } 354 355 361 public Detector2 createDetector2(BugReporter bugReporter) { 362 return detectorCreator.createDetector2(bugReporter); 363 } 364 365 369 public String getShortName() { 370 String className = detectorCreator.getDetectorClass().getName(); 371 int endOfPkg = className.lastIndexOf('.'); 372 if (endOfPkg >= 0) 373 className = className.substring(endOfPkg + 1); 374 return className; 375 } 376 377 381 public String getFullName() { 382 return detectorCreator.getDetectorClass().getName(); 383 } 384 } 385 386 | Popular Tags |