📄 pluginloader.java
字号:
/* * FindBugs - Find bugs in Java programs * Copyright (C) 2003-2005 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.net.URL;import java.net.URLClassLoader;import java.util.ArrayList;import java.util.HashSet;import java.util.List;import java.util.Locale;import java.util.Set;import org.dom4j.Document;import org.dom4j.DocumentException;import org.dom4j.Element;import org.dom4j.Node;import org.dom4j.io.SAXReader;import edu.umd.cs.findbugs.plan.ByInterfaceDetectorFactorySelector;import edu.umd.cs.findbugs.plan.DetectorFactorySelector;import edu.umd.cs.findbugs.plan.DetectorOrderingConstraint;import edu.umd.cs.findbugs.plan.ReportingDetectorFactorySelector;import edu.umd.cs.findbugs.plan.SingleDetectorFactorySelector;/** * Loader for a FindBugs plugin. * A plugin is a jar file containing two metadata files, * "findbugs.xml" and "messages.xml". Those files specify * <ul> * <li> the bug pattern Detector classes, * <li> the bug patterns detected (including all text for displaying * detected instances of those patterns), and * <li> the "bug codes" which group together related bug instances * </ul> * * <p> The PluginLoader creates a Plugin object to store * the Detector factories and metadata.</p> * * @author David Hovemeyer * @see Plugin * @see PluginException */public class PluginLoader extends URLClassLoader { private static final boolean DEBUG = SystemProperties.getBoolean("findbugs.debug.PluginLoader"); // Keep a count of how many plugins we've seen without a // "pluginid" attribute, so we can assign them all unique ids. private static int nextUnknownId; // The loaded Plugin private Plugin plugin; /** * Constructor. * * @param url the URL of the plugin Jar file * @throws PluginException if the plugin cannot be fully loaded */ public PluginLoader(URL url) throws PluginException { super(new URL[]{url}); init(); } /** * Constructor. * * @param url the URL of the plugin Jar file * @param parent the parent classloader */ public PluginLoader(URL url, ClassLoader parent) throws PluginException { super(new URL[]{url}, parent); init(); } /** * Get the Plugin. * @throws PluginException if the plugin cannot be fully loaded */ public Plugin getPlugin() throws PluginException { if (plugin == null) init(); return plugin; } private void init() throws PluginException { // Plugin descriptor (a.k.a, "findbugs.xml"). Defines // the bug detectors and bug patterns that the plugin provides. Document pluginDescriptor; // Unique plugin id String pluginId; // List of message translation files in decreasing order of precedence ArrayList<Document> messageCollectionList = new ArrayList<Document>(); // Read the plugin descriptor try { URL descriptorURL = findResource("findbugs.xml"); if (descriptorURL == null) throw new PluginException("Couldn't find \"findbugs.xml\" in plugin"); SAXReader reader = new SAXReader(); pluginDescriptor = reader.read(descriptorURL); } catch (DocumentException e) { throw new PluginException("Couldn't parse \"findbugs.xml\"", e); } // Get the unique plugin id (or generate one, if none is present) pluginId = pluginDescriptor.valueOf("/FindbugsPlugin/@pluginid"); if (pluginId.equals("")) { synchronized (PluginLoader.class) { pluginId = "plugin" + nextUnknownId++; } } // See if the plugin is enabled or disabled by default. // Note that if there is no "defaultenabled" attribute, // then we assume that the plugin IS enabled by default. String defaultEnabled = pluginDescriptor.valueOf("/FindbugsPlugin/@defaultenabled"); boolean pluginEnabled = defaultEnabled.equals("") || Boolean.valueOf(defaultEnabled).booleanValue(); // Load the message collections try { //Locale locale = Locale.getDefault(); Locale locale = I18N.defaultLocale; String language = locale.getLanguage(); String country = locale.getCountry(); if (country != null) addCollection(messageCollectionList, "messages_" + language + "_" + country + ".xml"); addCollection(messageCollectionList, "messages_" + language + ".xml"); addCollection(messageCollectionList, "messages.xml"); } catch (DocumentException e) { e.printStackTrace(); throw new PluginException("Couldn't parse \"messages.xml\"", e); } // Create the Plugin object (but don't assign to the plugin field yet, // since we're still not sure if everything will load correctly) Plugin plugin = new Plugin(pluginId); plugin.setEnabled(pluginEnabled); // Set provider and website, if specified String provider = pluginDescriptor.valueOf("/FindbugsPlugin/@provider"); if (!provider.equals("")) plugin.setProvider(provider); String website = pluginDescriptor.valueOf("/FindbugsPlugin/@website"); if (!website.equals("")) plugin.setWebsite(website); // Set short description, if specified Node pluginShortDesc = null; try { pluginShortDesc = findMessageNode( messageCollectionList, "/MessageCollection/Plugin/ShortDescription", "no plugin description"); } catch (PluginException e) { // Missing description is not fatal, so ignore } if (pluginShortDesc != null) { plugin.setShortDescription(pluginShortDesc.getText()); } // Create a DetectorFactory for all Detector nodes try { List<Node> detectorNodeList = pluginDescriptor.selectNodes("/FindbugsPlugin/Detector"); int detectorCount = 0; for (Node detectorNode : detectorNodeList) { String className = detectorNode.valueOf("@class"); String speed = detectorNode.valueOf("@speed"); String disabled = detectorNode.valueOf("@disabled"); String reports = detectorNode.valueOf("@reports"); String requireJRE = detectorNode.valueOf("@requirejre"); String hidden = detectorNode.valueOf("@hidden"); //System.out.println("Found detector: class="+className+", disabled="+disabled); // Create DetectorFactory for the detector Class<?> detectorClass = loadClass(className); if (!Detector.class.isAssignableFrom(detectorClass) && !Detector2.class.isAssignableFrom(detectorClass)) throw new PluginException("Class " + className + " does not implement Detector or Detector2"); DetectorFactory factory = new DetectorFactory( plugin, detectorClass, !disabled.equals("true"), speed, reports, requireJRE); if (Boolean.valueOf(hidden).booleanValue()) factory.setHidden(true); factory.setPositionSpecifiedInPluginDescriptor(detectorCount++); plugin.addDetectorFactory(factory); // Find Detector node in one of the messages files, // to get the detail HTML. Node node = findMessageNode(messageCollectionList, "/MessageCollection/Detector[@class='" + className + "']/Details", "Missing Detector description for detector " + className);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -