KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > anttask > FindBugsViewerTask


1 /*
2  * FindBugs - Find bugs in Java programs
3  * Copyright (C) 2006 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
20 package edu.umd.cs.findbugs.anttask;
21
22
23 import edu.umd.cs.findbugs.ExitCodes;
24 import java.io.File JavaDoc;
25 import org.apache.tools.ant.BuildException;
26 import org.apache.tools.ant.Task;
27 import org.apache.tools.ant.taskdefs.Ant.Reference;
28 import org.apache.tools.ant.taskdefs.Java;
29 import org.apache.tools.ant.types.Path;
30
31 /**
32  * FindBugsViewerTask.java -- Ant Task to launch the FindBugsFrame
33  *
34  * To use, create a new task that refrences the ant task
35  * (such as "findbugs-viewer"). Then call this task while
36  * passing in parameters to modify it's behaviour. It
37  * supports several options that are the same as the
38  * findbugs task:
39  *
40  * -projectFile
41  * -debug
42  * -jvmargs
43  * -home
44  * -classpath
45  * -pluginList
46  * -timeout
47  *
48  * It also adds some new options:
49  *
50  * -look: string name representing look and feel. Can be
51  * "native", "plastic" or "gtk"
52  * -loadbugs: file name of bug report to load
53  *
54  * The below is an example of how this could be done in an
55  * ant script:
56  *
57  * <taskdef name="findbugs"
58  * classname="edu.umd.cs.findbugs.anttask.FindBugsTask"
59  * classpath="C:\dev\cvs.sourceforge.net\findbugs\lib\findbugs-ant.jar" />
60  * <taskdef name="findbugs-viewer"
61  * classname="edu.umd.cs.findbugs.anttask.FindBugsViewerTask"
62  * classpath="C:\dev\cvs.sourceforge.net\findbugs\lib\findbugs-ant.jar" />
63  *
64  * <property name="findbugs.home"
65  * location="C:\dev\cvs.sourceforge.net\findbugs" />
66  * <property name="findbugs.bugReport"
67  * location="bcel-fb.xml" />
68  *
69  * <target name="findbugs-viewer" depends="jar">
70  * <findbugs-viewer home="${findbugs.home}"
71  * look="native" loadbugs="${findbugs.bugReport}"/>
72  * </target>
73  *
74  * Created on March 21, 2006, 12:57 PM
75  * @author Mark McKay, mark@kitfox.com
76  */

77 public class FindBugsViewerTask extends Task {
78
79     private static final String JavaDoc FINDBUGSGUI_JAR = "findbugsGUI.jar";
80     private static final long DEFAULT_TIMEOUT = -1; // ten minutes
81

82     //location to load bug report from
83
private boolean debug = false;
84     private File JavaDoc projectFile = null;
85     private File JavaDoc loadbugs = null;
86     private long timeout = DEFAULT_TIMEOUT;
87     private String JavaDoc jvmargs = "";
88     private String JavaDoc look = "native";
89     private File JavaDoc homeDir = null;
90     private Path classpath = null;
91     private Path pluginList = null;
92     
93     private Java findbugsEngine = null;
94     
95     /** Creates a new instance of FindBugsViewerTask */
96     public FindBugsViewerTask() {
97     }
98     
99     /**
100      * Sets the file that contains the XML output of a findbugs report.
101      *
102      * @param bugReport XML output from a findbugs session
103      */

104     public void setLoadbugs(File JavaDoc loadbugs) {
105         this.loadbugs = loadbugs;
106     }
107     
108     /**
109      * Set the project file
110      */

111     public void setProjectFile(File JavaDoc projectFile) {
112         this.projectFile = projectFile;
113     }
114     
115     /**
116      * Set the debug flag
117      */

118     public void setDebug(boolean flag) {
119         this.debug = flag;
120     }
121     
122     /**
123      * Set any specific jvm args
124      */

125     public void setJvmargs(String JavaDoc args) {
126         this.jvmargs = args;
127     }
128     
129     /**
130      * Set look. One of "native", "gtk" or "plastic"
131      */

132     public void setLook(String JavaDoc look) {
133         this.look = look;
134     }
135     
136     
137     /**
138      * Set the home directory into which findbugs was installed
139      */

140     public void setHome(File JavaDoc homeDir) {
141         this.homeDir = homeDir;
142     }
143     
144     
145     /**
146      * Path to use for classpath.
147      */

148     public Path createClasspath() {
149         if (classpath == null) {
150             classpath = new Path(getProject());
151         }
152         return classpath.createPath();
153     }
154     
155     /**
156      * Adds a reference to a classpath defined elsewhere.
157      */

158     public void setClasspathRef(Reference r) {
159         createClasspath().setRefid(r);
160     }
161     
162     
163     /**
164      * the plugin list to use.
165      */

166     public void setPluginList(Path src) {
167         if (pluginList == null) {
168             pluginList = src;
169         }
170         else {
171             pluginList.append(src);
172         }
173     }
174     
175     /**
176      * Path to use for plugin list.
177      */

178     public Path createPluginList() {
179         if (pluginList == null) {
180             pluginList = new Path(getProject());
181         }
182         return pluginList.createPath();
183     }
184     
185     /**
186      * Adds a reference to a plugin list defined elsewhere.
187      */

188     public void setPluginListRef(Reference r) {
189         createPluginList().setRefid(r);
190     }
191     
192     /**
193      * Set timeout in milliseconds.
194      *
195      * @param timeout the timeout
196      */

197     public void setTimeout(long timeout) {
198         this.timeout = timeout;
199     }
200     
201     /**
202      * Add an argument to the JVM used to execute FindBugs.
203      * @param arg the argument
204      */

205     private void addArg(String JavaDoc arg) {
206         findbugsEngine.createArg().setValue(arg);
207     }
208     
209     @Override JavaDoc
210     public void execute() throws BuildException {
211         findbugsEngine = (Java)getProject().createTask("java");
212         
213         findbugsEngine.setTaskName(getTaskName());
214         findbugsEngine.setFork(true);
215         
216         if (timeout > 0) {
217             findbugsEngine.setTimeout(timeout);
218         }
219         
220         
221         if (debug) {
222             jvmargs = jvmargs + " -Dfindbugs.debug=true";
223         }
224         findbugsEngine.createJvmarg().setLine(jvmargs);
225         
226         if (homeDir != null) {
227             // Use findbugs.home to locate findbugs.jar and the standard
228
// plugins. This is the usual means of initialization.
229

230             findbugsEngine.setJar( new File JavaDoc( homeDir + File.separator + "lib" +
231                     File.separator + FINDBUGSGUI_JAR ) );
232             
233             addArg("-home");
234             addArg(homeDir.getPath());
235         }
236         else {
237             // Use an explicitly specified classpath and list of plugin Jars
238
// to initialize. This is useful for other tools which may have
239
// FindBugs installed using a non-standard directory layout.
240

241             findbugsEngine.setClasspath(classpath);
242             findbugsEngine.setClassname("edu.umd.cs.findbugs.FindBugsFrame");
243             
244             addArg("-pluginList");
245             addArg(pluginList.toString());
246         }
247         
248         if (projectFile != null) {
249             addArg("-project");
250             addArg(projectFile.getPath());
251         }
252         
253         if (loadbugs != null) {
254             addArg("-loadbugs");
255             addArg(loadbugs.getPath());
256         }
257         
258         if (look != null) {
259             addArg("-look:" + look);
260             //addArg("-look");
261
//addArg(look);
262
}
263         
264         
265         // findbugsEngine.setClassname("edu.umd.cs.findbugs.gui.FindBugsFrame");
266

267         log("Launching FindBugs Viewer...");
268         
269         int rc = findbugsEngine.executeJava();
270         
271         if ((rc & ExitCodes.ERROR_FLAG) != 0) {
272             throw new BuildException("Execution of findbugs failed.");
273         }
274         if ((rc & ExitCodes.MISSING_CLASS_FLAG) != 0) {
275             log("Classes needed for analysis were missing");
276         }
277     }
278
279 }
280
Popular Tags