⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 project.java

📁 A static analysis tool to find bugs in Java programs
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * 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 *//* * Project.java * * Created on March 30, 2003, 2:22 PM */package edu.umd.cs.findbugs;import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.FileWriter;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.PrintWriter;import java.io.Reader;import java.net.MalformedURLException;import java.net.URL;import java.util.HashMap;import java.util.HashSet;import java.util.LinkedList;import java.util.List;import java.util.Map;import java.util.jar.Attributes;import java.util.jar.Manifest;import org.dom4j.DocumentException;import org.xml.sax.InputSource;import org.xml.sax.SAXException;import org.xml.sax.SAXParseException;import org.xml.sax.XMLReader;import org.xml.sax.helpers.XMLReaderFactory;import edu.umd.cs.findbugs.annotations.NonNull;import edu.umd.cs.findbugs.ba.AnalysisContext;import edu.umd.cs.findbugs.ba.URLClassPath;import edu.umd.cs.findbugs.filter.Filter;import edu.umd.cs.findbugs.util.Util;import edu.umd.cs.findbugs.xml.OutputStreamXMLOutput;import edu.umd.cs.findbugs.xml.XMLAttributeList;import edu.umd.cs.findbugs.xml.XMLOutput;import edu.umd.cs.findbugs.xml.XMLOutputUtil;import edu.umd.cs.findbugs.xml.XMLWriteable;/** * A project in the GUI. * This consists of some number of Jar files to analyze for bugs, and optionally * <p/> * <ul> * <li> some number of source directories, for locating the program's * source code * <li> some number of auxiliary classpath entries, for locating classes * referenced by the program which the user doesn't want to analyze * <li> some number of boolean options * </ul> * * @author David Hovemeyer */public class Project implements XMLWriteable {	private static final boolean DEBUG = SystemProperties.getBoolean("findbugs.project.debug");	private File currentWorkingDirectory;	/**	 * Project filename.	 */	@Deprecated	private String projectFileName;	private String projectName;	/**	 * Options.	 */	private Map<String, Boolean> optionsMap;	/**	 * The list of project files.	 */	private LinkedList<String> fileList;	/**	 * The list of source directories.	 */	private LinkedList<String> srcDirList;	/**	 * The list of auxiliary classpath entries.	 */	private LinkedList<String> auxClasspathEntryList;	/**	 * Flag to indicate that this Project has been modified.	 */	private boolean isModified;	/**	 * Constant used to name anonymous projects.	 */	public static final String UNNAMED_PROJECT = "<<unnamed project>>";	private long timestamp = 0L;		@NonNull private Filter suppressionFilter = new Filter();	/**	 * Create an anonymous project.	 */	public Project() {		this.projectFileName = UNNAMED_PROJECT;		optionsMap = new HashMap<String, Boolean>();		optionsMap.put(RELATIVE_PATHS, Boolean.FALSE);		fileList = new LinkedList<String>();		srcDirList = new LinkedList<String>();		auxClasspathEntryList = new LinkedList<String>();		isModified = false;	}	/**	 * Return an exact copy of this Project.	 */	public Project duplicate() {		Project dup = new Project();		dup.projectFileName = this.projectFileName;		dup.currentWorkingDirectory = this.currentWorkingDirectory;		dup.optionsMap.clear();		dup.optionsMap.putAll(this.optionsMap);		dup.fileList.addAll(this.fileList);		dup.srcDirList.addAll(this.srcDirList);		dup.auxClasspathEntryList.addAll(this.auxClasspathEntryList);		dup.timestamp = timestamp;		return dup;	}	public void setCurrentWorkingDirectory(File f) {		this.currentWorkingDirectory = f;	}	/**	 * Return whether or not this Project has unsaved modifications.	 */	public boolean isModified() {		return isModified;	}	/**	 * Set whether or not this Project has unsaved modifications.	 */	public void setModified(boolean isModified) {		this.isModified = isModified;	}	/**	 * Get the project filename.	 */	@Deprecated	public String getProjectFileName() {		return projectFileName;	}	/**	 * Set the project filename.	 *	 * @param projectFileName the new filename	 */	@Deprecated	public void setProjectFileName(String projectFileName) {		this.projectFileName = projectFileName;	}	/**	 * Add a file to the project.	 *	 * @param fileName the file to add	 * @return true if the file was added, or false if the	 *         file was already present	 */	public boolean addFile(String fileName) {		return addToListInternal(fileList, makeAbsoluteCWD(fileName));	}	/**	 * Add a source directory to the project.	 * @param dirName the directory to add	 * @return true if the source directory was added, or false if the	 *   source directory was already present	 */	public boolean addSourceDir(String dirName) {		return addToListInternal(srcDirList, makeAbsoluteCWD(dirName));	}	/**	 * Retrieve the Options value.	 *	 * @param option the name of option to get	 * @return the value of the option	 */	public boolean getOption(String option) {		Boolean value = optionsMap.get(option);		return value != null && value.booleanValue();	}	/**	 * Get the number of files in the project.	 *	 * @return the number of files in the project	 */	public int getFileCount() {		return fileList.size();	}	/**	 * Get the given file in the list of project files.	 *	 * @param num the number of the file in the list of project files	 * @return the name of the file	 */	public String getFile(int num) {		return fileList.get(num);	}	/**	 * Remove file at the given index in the list of project files	 *	 * @param num index of the file to remove in the list of project files	 */	public void removeFile(int num) {		fileList.remove(num);		isModified = true;	}	/**	 * Get the list of files, directories, and zip files in the project.	 */	public List<String> getFileList() {		return fileList;	}	/**	 * Get the number of source directories in the project.	 *	 * @return the number of source directories in the project	 */	public int getNumSourceDirs() {		return srcDirList.size();	}	/**	 * Get the given source directory.	 *	 * @param num the number of the source directory	 * @return the source directory	 */	public String getSourceDir(int num) {		return srcDirList.get(num);	}	/**	 * Remove source directory at given index.	 *	 * @param num index of the source directory to remove	 */	public void removeSourceDir(int num) {		srcDirList.remove(num);		isModified = true;	}	/**	 * Get project files as an array of Strings.	 */	public String[] getFileArray() {		return fileList.toArray(new String[fileList.size()]);	}	/**	 * Get source dirs as an array of Strings.	 */	public String[] getSourceDirArray() {		return srcDirList.toArray(new String[srcDirList.size()]);	}	/**	 * Get the source dir list.	 */	public List<String> getSourceDirList() {		return srcDirList;	}	/**	 * Add an auxiliary classpath entry	 *	 * @param auxClasspathEntry the entry	 * @return true if the entry was added successfully, or false	 *         if the given entry is already in the list	 */	public boolean addAuxClasspathEntry(String auxClasspathEntry) {		return addToListInternal(auxClasspathEntryList, makeAbsoluteCWD(auxClasspathEntry));	}	/**	 * Get the number of auxiliary classpath entries.	 */	public int getNumAuxClasspathEntries() {		return auxClasspathEntryList.size();	}	/**	 * Get the n'th auxiliary classpath entry.	 */	public String getAuxClasspathEntry(int n) {		return auxClasspathEntryList.get(n);	}	/**	 * Remove the n'th auxiliary classpath entry.	 */	public void removeAuxClasspathEntry(int n) {		auxClasspathEntryList.remove(n);		isModified = true;	}	/**	 * Return the list of aux classpath entries.	 */	public List<String> getAuxClasspathEntryList() {		return auxClasspathEntryList;	}	/**	 * Worklist item for finding implicit classpath entries.	 */	private static class WorkListItem {		private URL url;		/**		 * Constructor.		 *		 * @param url the URL of the Jar or Zip file		 */		public WorkListItem(URL url) {			this.url = url;		}		/**		 * Get URL of Jar/Zip file.		 */		public URL getURL() {			return this.url;		}	}	/**	 * Worklist for finding implicit classpath entries.	 */	private static class WorkList {		private LinkedList<WorkListItem> itemList;		private HashSet<String> addedSet;		/**		 * Constructor.		 * Creates an empty worklist.		 */		public WorkList() {			this.itemList = new LinkedList<WorkListItem>();			this.addedSet = new HashSet<String>();		}		/**		 * Create a URL from a filename specified in the project file.		 */		public URL createURL(String fileName) throws MalformedURLException {			String protocol = URLClassPath.getURLProtocol(fileName);			if (protocol == null) {				fileName = "file:" + fileName;			}			return new URL(fileName);		}		/**		 * Create a URL of a file relative to another URL.		 */		public URL createRelativeURL(URL base, String fileName) throws MalformedURLException {			return new URL(base, fileName);		}		/**		 * Add a worklist item.		 *		 * @param item the WorkListItem representing a zip/jar file to be examined		 * @return true if the item was added, false if not (because it was		 *         examined already)		 */		public boolean add(WorkListItem item) {			if (DEBUG) System.out.println("Adding " + item.getURL().toString());			if (!addedSet.add(item.getURL().toString())) {				if (DEBUG) System.out.println("\t==> Already processed");				return false;			}			itemList.add(item);			return true;		}		/**		 * Return whether or not the worklist is empty.		 */		public boolean isEmpty() {			return itemList.isEmpty();		}		/**		 * Get the next item in the worklist.		 */		public WorkListItem getNextItem() {			return itemList.removeFirst();		}	}	/**	 * Return the list of implicit classpath entries.  The implicit	 * classpath is computed from the closure of the set of jar files	 * that are referenced by the <code>"Class-Path"</code> attribute	 * of the manifest of the any jar file that is part of this project	 * or by the <code>"Class-Path"</code> attribute of any directly or	 * indirectly referenced jar.  The referenced jar files that exist	 * are the list of implicit classpath entries.	 * 	 * @deprecated FindBugs2 and ClassPathBuilder take care of this automatically	 */	public List<String> getImplicitClasspathEntryList() {		final LinkedList<String> implicitClasspath = new LinkedList<String>();		WorkList workList = new WorkList();		// Prime the worklist by adding the zip/jar files		// in the project.		for (String fileName : fileList) {			try {				URL url = workList.createURL(fileName);				WorkListItem item = new WorkListItem(url);				workList.add(item);			} catch (MalformedURLException ignore) {				// Ignore			}		}		// Scan recursively.		while (!workList.isEmpty()) {			WorkListItem item = workList.getNextItem();			processComponentJar(item.getURL(), workList, implicitClasspath);		}		return implicitClasspath;	}	/**	 * Examine the manifest of a single zip/jar file for implicit	 * classapth entries.	 *	 * @param jarFileURL        URL of the zip/jar file	 * @param workList          worklist of zip/jar files to examine	 * @param implicitClasspath list of implicit classpath entries found	 */	private void processComponentJar(URL jarFileURL, WorkList workList,		List<String> implicitClasspath) {		if (DEBUG) System.out.println("Processing " + jarFileURL.toString());		if (!jarFileURL.toString().endsWith(".zip") && !jarFileURL.toString().endsWith(".jar"))			return;		try {			URL manifestURL = new URL("jar:" + jarFileURL.toString() + "!/META-INF/MANIFEST.MF");			InputStream in = null;			try {				in = manifestURL.openStream();				Manifest manifest = new Manifest(in);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -