📄 project.java
字号:
/* * FindBugs - Find bugs in Java programs * Copyright (C) 2003,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 *//* * Project.java * * Created on March 30, 2003, 2:22 PM */package edu.umd.cs.findbugs;import edu.umd.cs.findbugs.xml.XMLOutput;import edu.umd.cs.findbugs.xml.XMLOutputUtil;import edu.umd.cs.findbugs.xml.XMLWriteable;import java.io.*;import java.net.URL;import java.net.MalformedURLException;import java.util.*;import java.util.jar.*;import org.dom4j.DocumentException;import org.dom4j.Element;/** * 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 = Boolean.getBoolean("findbugs.project.debug"); /** * Project filename. */ private String projectFileName; /** * 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>>"; /** * 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.optionsMap.putAll(this.optionsMap); dup.fileList.addAll(this.fileList); dup.srcDirList.addAll(this.srcDirList); dup.auxClasspathEntryList.addAll(this.auxClasspathEntryList); return dup; } /** * 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. */ public String getProjectFileName() { return projectFileName; } /** * Set the project filename. * * @param projectFileName the new filename */ 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 (String[]) fileList.toArray(new String[fileList.size()]); } /** * Get source dirs as an array of Strings. */ public String[] getSourceDirArray() { return (String[]) 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 = FindBugs.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. */ 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 (Iterator<String> i = fileList.iterator(); i.hasNext();) { String fileName = i.next(); try {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -