forked from spotbugs/spotbugs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Plugin.java
709 lines (605 loc) · 20.7 KB
/
Plugin.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
/*
* FindBugs - Find bugs in Java programs
* Copyright (C) 2004, University of Maryland
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package edu.umd.cs.findbugs;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import edu.umd.cs.findbugs.ba.AnalysisContext;
import edu.umd.cs.findbugs.classfile.IAnalysisEngineRegistrar;
import edu.umd.cs.findbugs.plan.DetectorOrderingConstraint;
import edu.umd.cs.findbugs.util.DualKeyHashMap;
/**
* A FindBugs plugin. A plugin contains executable Detector classes, as well as
* meta information describing those detectors (such as human-readable detector
* and bug descriptions).
*
* @see PluginLoader
* @author David Hovemeyer
*/
public class Plugin implements AutoCloseable {
private static final String USE_FINDBUGS_VERSION = "USE_FINDBUGS_VERSION";
static Map<URI, Plugin> allPlugins = new LinkedHashMap<>();
private final String pluginId;
private final String version;
private final Date releaseDate;
private String provider;
private URI website;
private @CheckForNull URI updateUrl;
private String shortDescription;
private String detailedDescription;
private final ArrayList<DetectorFactory> detectorFactoryList;
private final Map<String, FindBugsMain> mainPlugins;
private final LinkedHashSet<BugPattern> bugPatterns;
private final LinkedHashSet<BugCode> bugCodeList;
private final LinkedHashMap<String, BugCategory> bugCategories;
private final HashMap<String, String> myGlobalOptions;
private final DualKeyHashMap<Class<?>, String, ComponentPlugin<?>> componentPlugins;
private BugRanker bugRanker;
// Ordering constraints
private final ArrayList<DetectorOrderingConstraint> interPassConstraintList;
private final ArrayList<DetectorOrderingConstraint> intraPassConstraintList;
// Optional: engine registrar class
private Class<? extends IAnalysisEngineRegistrar> engineRegistrarClass;
// PluginLoader that loaded this plugin
private final PluginLoader pluginLoader;
private final boolean enabledByDefault;
private final boolean cannotDisable;
static enum EnabledState {
PLUGIN_DEFAULT, ENABLED, DISABLED
}
private EnabledState enabled;
/**
* Constructor. Creates an empty plugin object.
*
* @param pluginId
* the plugin's unique identifier
*/
public Plugin(String pluginId, String version, Date releaseDate, @Nonnull PluginLoader pluginLoader, boolean enabled, boolean cannotDisable) {
this.pluginId = pluginId;
if (version == null) {
version = "";
} else if (USE_FINDBUGS_VERSION.equals(version)) {
version = Version.VERSION_STRING;
}
assert enabled || !cannotDisable;
myGlobalOptions = new HashMap<>();
componentPlugins = new DualKeyHashMap<>();
this.version = version;
this.releaseDate = releaseDate;
this.detectorFactoryList = new ArrayList<>();
this.bugPatterns = new LinkedHashSet<>();
this.bugCodeList = new LinkedHashSet<>();
this.bugCategories = new LinkedHashMap<>();
this.interPassConstraintList = new ArrayList<>();
this.intraPassConstraintList = new ArrayList<>();
this.mainPlugins = new HashMap<>();
this.pluginLoader = pluginLoader;
this.enabledByDefault = enabled;
this.cannotDisable = cannotDisable;
this.enabled = EnabledState.PLUGIN_DEFAULT;
}
@Override
public String toString() {
return this.getClass().getSimpleName() + ":" + pluginId;
}
/**
* Return whether or not the Plugin is enabled.
*
* @return true if the Plugin is enabled, false if not
*/
public boolean isEnabledByDefault() {
return enabledByDefault;
}
/**
* Set plugin provider.
*
* @param provider
* the plugin provider
*/
public void setProvider(String provider) {
this.provider = provider;
}
/**
* Get the plugin provider.
*
* @return the provider, or null if the provider was not specified
*/
public @CheckForNull String getProvider() {
return provider;
}
public void setUpdateUrl(String url) throws URISyntaxException {
this.updateUrl = new URI(url);
}
public @CheckForNull URI getUpdateUrl() {
return updateUrl;
}
public void setMyGlobalOption(String key, String value) {
myGlobalOptions.put(key, value);
}
Map<String, String> getMyGlobalOptions() {
return Collections.unmodifiableMap(myGlobalOptions);
}
/**
* Set plugin website.
*
* @param website
* the plugin website
* @throws URISyntaxException
*/
public void setWebsite(String website) throws URISyntaxException {
this.website = new URI(website);
}
/**
* Get the plugin website.
*
* @return the website, or null if the was not specified
*/
public @CheckForNull String getWebsite() {
if (website == null) {
return null;
}
return website.toASCIIString();
}
public @CheckForNull URI getWebsiteURI() {
return website;
}
public String getVersion() {
return version;
}
public Date getReleaseDate() {
return releaseDate;
}
public void setShortDescription(String shortDescription) {
this.shortDescription = shortDescription;
}
public String getShortDescription() {
return shortDescription;
}
public String getDetailedDescription() {
return detailedDescription;
}
public void setDetailedDescription(String detailedDescription) {
this.detailedDescription = detailedDescription;
}
/**
* Add a DetectorFactory for a Detector implemented by the Plugin.
*
* @param factory
* the DetectorFactory
*/
public void addDetectorFactory(DetectorFactory factory) {
detectorFactoryList.add(factory);
}
/**
* Add a BugPattern reported by the Plugin.
*
* @param bugPattern
*/
public void addBugPattern(BugPattern bugPattern) {
bugPatterns.add(bugPattern);
}
/**
* Add a BugCode reported by the Plugin.
*
* @param bugCode
*/
public void addBugCode(BugCode bugCode) {
bugCodeList.add(bugCode);
}
/**
* Add a BugCategory reported by the Plugin.
*
* @param bugCategory
*/
public void addBugCategory(BugCategory bugCategory) {
BugCategory old = bugCategories.get(bugCategory.getCategory());
if (old != null) {
throw new IllegalArgumentException("Category already exists");
}
bugCategories.put(bugCategory.getCategory(), bugCategory);
}
public BugCategory addOrCreateBugCategory(String id) {
BugCategory c = bugCategories.get(id);
if (c != null) {
return c;
}
c = new BugCategory(id);
bugCategories.put(id, c);
return c;
}
/**
* Add an inter-pass Detector ordering constraint.
*
* @param constraint
* the inter-pass Detector ordering constraint
*/
public void addInterPassOrderingConstraint(DetectorOrderingConstraint constraint) {
interPassConstraintList.add(constraint);
}
/**
* Add an intra-pass Detector ordering constraint.
*
* @param constraint
* the intra-pass Detector ordering constraint
*/
public void addIntraPassOrderingConstraint(DetectorOrderingConstraint constraint) {
intraPassConstraintList.add(constraint);
}
/**
* Look up a DetectorFactory by short name.
*
* @param shortName
* the short name
* @return the DetectorFactory
*/
public DetectorFactory getFactoryByShortName(final String shortName) {
return findFirstMatchingFactory(factory -> factory.getShortName().equals(shortName));
}
/**
* Look up a DetectorFactory by full name.
*
* @param fullName
* the full name
* @return the DetectorFactory
*/
public DetectorFactory getFactoryByFullName(final String fullName) {
return findFirstMatchingFactory(factory -> factory.getFullName().equals(fullName));
}
/**
* Get Iterator over DetectorFactory objects in the Plugin.
*
* @return Iterator over DetectorFactory objects
*/
public Collection<DetectorFactory> getDetectorFactories() {
return detectorFactoryList;
}
/**
* Get the set of BugPatterns
*
*/
public Set<BugPattern> getBugPatterns() {
return bugPatterns;
}
/**
* Get Iterator over BugCode objects in the Plugin.
*
* @return Iterator over BugCode objects
*/
public Set<BugCode> getBugCodes() {
return bugCodeList;
}
/**
* Get Iterator over BugCategories objects in the Plugin.
*
* @return Iterator over BugCategory objects
*/
public Collection<BugCategory> getBugCategories() {
return bugCategories.values();
}
/**
* @param id may be null
* @return return bug category with given id, may return null if the bug category is unknown
*/
@CheckForNull
public BugCategory getBugCategory(String id) {
return bugCategories.get(id);
}
/**
* Return an Iterator over the inter-pass Detector ordering constraints.
*/
public Iterator<DetectorOrderingConstraint> interPassConstraintIterator() {
return interPassConstraintList.iterator();
}
/**
* Return an Iterator over the intra-pass Detector ordering constraints.
*/
public Iterator<DetectorOrderingConstraint> intraPassConstraintIterator() {
return intraPassConstraintList.iterator();
}
public String getPluginId() {
return pluginId;
}
public String getShortPluginId() {
int i = pluginId.lastIndexOf('.');
return pluginId.substring(i + 1);
}
/**
* Set the analysis engine registrar class that, when instantiated, can be
* used to register the plugin's analysis engines with the analysis cache.
*
* @param engineRegistrarClass
* The engine registrar class to set.
*/
public void setEngineRegistrarClass(Class<? extends IAnalysisEngineRegistrar> engineRegistrarClass) {
this.engineRegistrarClass = engineRegistrarClass;
}
/**
* Get the analysis engine registrar class that, when instantiated, can be
* used to register the plugin's analysis engines with the analysis cache.
*
* @return Returns the engine registrar class.
*/
public Class<? extends IAnalysisEngineRegistrar> getEngineRegistrarClass() {
return engineRegistrarClass;
}
public PluginLoader getPluginLoader() {
return pluginLoader;
}
private interface FactoryChooser {
public boolean choose(DetectorFactory factory);
}
private @CheckForNull DetectorFactory findFirstMatchingFactory(FactoryChooser chooser) {
for (DetectorFactory factory : getDetectorFactories()) {
if (chooser.choose(factory)) {
return factory;
}
}
return null;
}
public void setBugRanker(BugRanker ranker) {
this.bugRanker = ranker;
}
public BugRanker getBugRanker() {
return bugRanker;
}
<T> void addFindBugsMain(Class<?> mainClass, String cmd, String description, String kind, boolean analysis)
throws SecurityException, NoSuchMethodException {
FindBugsMain main = new FindBugsMain(mainClass, cmd, description, kind, analysis);
mainPlugins.put(cmd, main);
}
public @CheckForNull FindBugsMain getFindBugsMain(String cmd) {
return mainPlugins.get(cmd);
}
public Collection<FindBugsMain> getAllFindBugsMain() {
return mainPlugins.values();
}
<T> void addComponentPlugin(Class<T> componentKind, ComponentPlugin<T> plugin) {
Class<? extends T> componentClass = plugin.getComponentClass();
if (componentClass != null && !componentKind.isAssignableFrom(componentClass)) {
throw new IllegalArgumentException();
}
componentPlugins.put(componentKind, plugin.getId(), plugin);
}
@SuppressWarnings("unchecked")
public <T> Iterable<ComponentPlugin<T>> getComponentPlugins(Class<T> componentClass) {
@SuppressWarnings("rawtypes")
Collection values = componentPlugins.get(componentClass).values();
return values;
}
@SuppressWarnings("unchecked")
public <T> ComponentPlugin<T> getComponentPlugin(Class<T> componentClass, String name) {
return (ComponentPlugin<T>) componentPlugins.get(componentClass, name);
}
public static synchronized @CheckForNull Plugin getByPluginId(String name) {
if (name == null) {
return null;
}
for (Plugin plugin : allPlugins.values()) {
// the second part is questionable, as this may lead to id collisions
if (name.equals(plugin.getPluginId()) /*|| name.equals(plugin.getShortPluginId())*/) {
return plugin;
}
}
return null;
}
public static synchronized void removePlugin(URI uri) {
allPlugins.remove(uri);
}
/**
* @return a copy of the internal plugins collection
*/
public static synchronized Collection<Plugin> getAllPlugins() {
return new ArrayList<>(allPlugins.values());
}
public static synchronized Collection<String> getAllPluginIds() {
ArrayList<String> result = new ArrayList<>();
for (Plugin p : allPlugins.values()) {
result.add(p.getPluginId());
}
return result;
}
/**
* @return a copy of the internal plugins collection
*/
public static synchronized Map<URI, Plugin> getAllPluginsMap() {
return new LinkedHashMap<>(allPlugins);
}
public static synchronized Set<URI> getAllPluginsURIs() {
Collection<Plugin> plugins = getAllPlugins();
Set<URI> uris = new HashSet<>();
for (Plugin plugin : plugins) {
try {
URI uri = plugin.getPluginLoader().getURL().toURI();
uris.add(uri);
} catch (URISyntaxException e) {
AnalysisContext.logError("Unable to get URI", e);
}
}
return uris;
}
/**
* @return may return null
*/
@CheckForNull
static synchronized Plugin getPlugin(URI uri) {
return allPlugins.get(uri);
}
/**
* @return may return null
*/
@CheckForNull
static synchronized Plugin putPlugin(URI uri, Plugin plugin) {
return allPlugins.put(uri, plugin);
}
public boolean isCorePlugin() {
return pluginLoader.isCorePlugin();
}
public boolean cannotDisable() {
return cannotDisable;
}
public boolean isGloballyEnabled() {
if (isCorePlugin()) {
return true;
}
switch (enabled) {
case ENABLED:
return true;
case DISABLED:
return false;
case PLUGIN_DEFAULT:
return isEnabledByDefault();
default:
throw new IllegalStateException("Unknown state : " + enabled);
}
}
public void setGloballyEnabled(boolean enabled) {
if (isCorePlugin()) {
if (!enabled) {
throw new IllegalArgumentException("Can't disable core plugin");
}
return;
}
if (cannotDisable) {
if (enabled) {
return;
}
throw new IllegalArgumentException("Cannot disable " + pluginId);
}
// EnabledState oldState = this.enabled;
if (enabled) {
if (isEnabledByDefault()) {
this.enabled = EnabledState.PLUGIN_DEFAULT;
} else {
this.enabled = EnabledState.ENABLED;
}
} else {
if (isEnabledByDefault()) {
this.enabled = EnabledState.DISABLED;
} else {
this.enabled = EnabledState.PLUGIN_DEFAULT;
}
}
// if(oldState != this.enabled) {
// TODO update detector factory collection?
// }
}
public boolean isInitialPlugin() {
return getPluginLoader().initialPlugin;
}
public URL getResource(String name) {
return getPluginLoader().getResource(name);
}
public ClassLoader getClassLoader() {
return getPluginLoader().getClassLoader();
}
public @CheckForNull Plugin getParentPlugin() {
if (getPluginLoader().hasParent()) {
return Plugin.getByPluginId(getPluginLoader().parentId);
}
return null;
}
/**
* Loads the given plugin and enables it for the given project.
*
* @param f
* A non-null jar file of custom plugin.
* @param project
* A nullable target project
*/
public static Plugin loadCustomPlugin(File f, @CheckForNull Project project)
throws PluginException {
URL urlString;
try {
urlString = f.toURI().toURL();
} catch (MalformedURLException e) {
throw new IllegalArgumentException(e);
}
return loadCustomPlugin(urlString, project);
}
/**
* Loads the given plugin and enables it for the given project.
*/
public static Plugin loadCustomPlugin(URL urlString, @CheckForNull Project project) throws PluginException {
Plugin plugin = addCustomPlugin(urlString);
if (project != null) {
project.setPluginStatusTrinary(plugin.getPluginId(), true);
}
return plugin;
}
public static @CheckForNull Plugin addCustomPlugin(URL u) throws PluginException {
return addCustomPlugin(u, PluginLoader.class.getClassLoader());
}
public static @CheckForNull Plugin addCustomPlugin(URI u) throws PluginException {
return addCustomPlugin(u, PluginLoader.class.getClassLoader());
}
public static @CheckForNull Plugin addCustomPlugin(URL u, ClassLoader parent) throws PluginException {
PluginLoader pluginLoader = PluginLoader.getPluginLoader(u, parent, false, true);
Plugin plugin = pluginLoader.loadPlugin();
if (plugin != null) {
DetectorFactoryCollection.instance().loadPlugin(plugin);
}
return plugin;
}
public static @CheckForNull Plugin addCustomPlugin(URI u, ClassLoader parent) throws PluginException {
URL url;
try {
url = u.toURL();
} catch (MalformedURLException e) {
throw new PluginException("Unable to convert uri to url:" + u, e);
}
return addCustomPlugin(url, parent);
}
public static synchronized void removeCustomPlugin(Plugin plugin) {
Set<Entry<URI, Plugin>> entrySet = Plugin.allPlugins.entrySet();
for (Entry<URI, Plugin> entry : entrySet) {
if (entry.getValue() == plugin) {
Plugin.allPlugins.remove(entry.getKey());
PluginLoader.loadedPluginIds.remove(plugin.getPluginId());
break;
}
}
DetectorFactoryCollection.instance().unLoadPlugin(plugin);
}
/**
* Closes the underlying {@link PluginLoader}, in turn this closes the {@link URLClassLoader}.
* When loading a custom plugin from a .jar file this method needs to be called to release the reference on that file.
*/
@Override
public void close() throws IOException {
pluginLoader.close();
}
}