KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > FindBugsCommandLine


1 /*
2  * FindBugs - Find bugs in Java programs
3  * Copyright (C) 2003-2005 University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package edu.umd.cs.findbugs;
20
21 import java.io.File JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.StringTokenizer JavaDoc;
26
27 import edu.umd.cs.findbugs.config.AnalysisFeatureSetting;
28 import edu.umd.cs.findbugs.config.CommandLine;
29
30 /**
31  * Base class for FindBugs command line classes.
32  * Handles all shared switches/options.
33  *
34  * @author David Hovemeyer
35  */

36 public abstract class FindBugsCommandLine extends CommandLine {
37     
38     /**
39      * Analysis settings to configure the analysis effort.
40      */

41     protected AnalysisFeatureSetting[] settingList = FindBugs.DEFAULT_EFFORT;
42
43     /**
44      * Project to analyze.
45      */

46     protected Project project = new Project();
47
48     /**
49      * Constructor.
50      * Adds shared options/switches.
51      */

52     public FindBugsCommandLine() {
53         addOption("-project", "project", "analyze given project");
54         addOption("-home", "home directory", "specify FindBugs home directory");
55         addOption("-pluginList", "jar1[" + File.pathSeparator + "jar2...]",
56                 "specify list of plugin Jar files to load");
57         addSwitchWithOptionalExtraPart("-effort", "min|default|max", "set analysis effort level");
58         addSwitch("-adjustExperimental", "lower priority of experimental Bug Patterns");
59         addSwitch("-workHard", "ensure analysis effort is at least 'default'");
60         addSwitch("-conserveSpace", "same as -effort:min (for backward compatibility)");
61     }
62     
63     public AnalysisFeatureSetting[] getSettingList() {
64         return settingList;
65     }
66     
67     public Project getProject() {
68         return project;
69     }
70
71     //@Override
72
@Override JavaDoc
73     protected void handleOption(String JavaDoc option, String JavaDoc optionExtraPart) {
74         if (option.equals("-effort")) {
75             if (optionExtraPart.equals("min")) {
76                 settingList = FindBugs.MIN_EFFORT;
77             } else if (optionExtraPart.equals("less")) {
78                 settingList = FindBugs.LESS_EFFORT;
79             } else if (optionExtraPart.equals("default")) {
80                 settingList = FindBugs.DEFAULT_EFFORT;
81             } else if (optionExtraPart.equals("more")) {
82                 settingList = FindBugs.MORE_EFFORT;
83             } else if (optionExtraPart.equals("max")) {
84                 settingList = FindBugs.MAX_EFFORT;
85             } else {
86                 throw new IllegalArgumentException JavaDoc("-effort:<value> must be one of min,default,more,max");
87             }
88         } else if (option.equals("-workHard")) {
89             if (settingList != FindBugs.MAX_EFFORT)
90                 settingList = FindBugs.MORE_EFFORT;
91             
92         } else if (option.equals("-conserveSpace")) {
93             settingList = FindBugs.MIN_EFFORT;
94         } else if (option.equals("-adjustExperimental")) {
95             BugInstance.setAdjustExperimental(true);
96         } else {
97             throw new IllegalStateException JavaDoc();
98         }
99     }
100
101     @Override JavaDoc
102     protected void handleOptionWithArgument(String JavaDoc option, String JavaDoc argument) throws IOException JavaDoc {
103         if (option.equals("-home")) {
104             FindBugs.setHome(argument);
105         } else if (option.equals("-pluginList")) {
106             String JavaDoc pluginListStr = argument;
107             ArrayList JavaDoc<URL JavaDoc> pluginList = new ArrayList JavaDoc<URL JavaDoc>();
108             StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(pluginListStr, File.pathSeparator);
109             while (tok.hasMoreTokens()) {
110                 pluginList.add(new File JavaDoc(tok.nextToken()).toURL());
111             }
112
113             DetectorFactoryCollection.rawInstance().setPluginList(pluginList.toArray(new URL JavaDoc[pluginList.size()]));
114         } else if (option.equals("-project")) {
115             String JavaDoc projectFile = argument;
116
117             // Convert project file to be an absolute path
118
projectFile = new File JavaDoc(projectFile).getAbsolutePath();
119
120             try {
121                 project = new Project();
122                 project.read(projectFile);
123             } catch (IOException JavaDoc e) {
124                 System.err.println("Error opening " + projectFile);
125                 e.printStackTrace(System.err);
126                 throw e;
127             }
128         } else {
129             throw new IllegalStateException JavaDoc();
130         }
131     }
132 }
133
Popular Tags