Codebase list findbugs / 77e952f src / antTask / edu / umd / cs / findbugs / anttask / AbstractFindBugsTask.java
77e952f

Tree @77e952f (Download .tar.gz)

AbstractFindBugsTask.java @77e952fraw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*
 * FindBugs - Find Bugs in Java programs
 * Copyright (C) 2003-2007 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.anttask;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.taskdefs.Java;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.types.Reference;

/**
 * Abstract base class for Ant tasks that run programs (main() methods) in
 * findbugs.jar or findbugsGUI.jar.
 *
 * @author David Hovemeyer
 */
public abstract class AbstractFindBugsTask extends Task {
    public static final String FINDBUGS_JAR = "findbugs.jar";

    public static final long DEFAULT_TIMEOUT = 1200000; // twenty minutes

    public static final String RESULT_PROPERTY_SUFFIX = "executeReturnCode";

    // A System property to set when FindBugs is run
    public static class SystemProperty {
        private String name;

        private String value;

        public SystemProperty() {
        }

        public void setName(String name) {
            this.name = name;
        }

        public void setValue(String value) {
            this.value = value;
        }

        public String getName() {
            return name;
        }

        public String getValue() {
            return value;
        }
    }

    private final String mainClass;

    private boolean debug = false;

    private File homeDir = null;

    private String jvm = "";

    private String jvmargs = "";

    private long timeout = DEFAULT_TIMEOUT;

    private boolean failOnError = false;

    protected String errorProperty = null;

    private final List<SystemProperty> systemPropertyList = new ArrayList<SystemProperty>();

    private Path classpath = null;

    private Path pluginList = null;

    private Java findbugsEngine = null;

    public String execResultProperty = "edu.umd.cs.findbugs.anttask.AbstractFindBugsTask" + "." + RESULT_PROPERTY_SUFFIX;

    /**
     * Constructor.
     */
    protected AbstractFindBugsTask(String mainClass) {
        this.mainClass = mainClass;
        this.execResultProperty = mainClass + "." + RESULT_PROPERTY_SUFFIX;
    }

    /**
     * Set the home directory into which findbugs was installed
     */
    public void setHome(File homeDir) {
        this.homeDir = homeDir;
    }

    /**
     * Set the debug flag
     */
    public void setDebug(boolean flag) {
        this.debug = flag;
    }

    /**
     * Get the debug flag.
     */
    protected boolean getDebug() {
        return debug;
    }

    /**
     * Set any specific jvm args
     */
    public void setJvmargs(String args) {
        this.jvmargs = args;
    }

    /**
     * Set the command used to start the VM
     */
    public void setJvm(String jvm) {
        this.jvm = jvm;
    }

    /**
     * Set timeout in milliseconds.
     *
     * @param timeout
     *            the timeout
     */
    public void setTimeout(long timeout) {
        this.timeout = timeout;
    }

    /**
     * Set the failOnError flag
     */
    public void setFailOnError(boolean flag) {
        this.failOnError = flag;
    }

    /**
     * Tells this task to set the property with the given name to "true" when
     * there were errors.
     */
    public void setErrorProperty(String name) {
        this.errorProperty = name;
    }

    /**
     * Create a SystemProperty (to handle &lt;systemProperty&gt; elements).
     */
    public SystemProperty createSystemProperty() {
        SystemProperty systemProperty = new SystemProperty();
        systemPropertyList.add(systemProperty);
        return systemProperty;
    }

    /**
     * Set the classpath to use.
     */
    public void setClasspath(Path src) {
        if (classpath == null) {
            classpath = src;
        } else {
            classpath.append(src);
        }
    }

    /**
     * Path to use for classpath.
     */
    public Path createClasspath() {
        if (classpath == null) {
            classpath = new Path(getProject());
        }
        return classpath.createPath();
    }

    /**
     * Adds a reference to a classpath defined elsewhere.
     */
    public void setClasspathRef(Reference r) {
        Path path = createClasspath();
        path.setRefid(r);
        path.toString(); // Evaluated for its side-effects (throwing a
        // BuildException)
    }

    /**
     * the plugin list to use.
     */
    public void setPluginList(Path src) {
        if (pluginList == null) {
            pluginList = src;
        } else {
            pluginList.append(src);
        }
    }

    /**
     * Path to use for plugin list.
     */
    public Path createPluginList() {
        if (pluginList == null) {
            pluginList = new Path(getProject());
        }
        return pluginList.createPath();
    }

    /**
     * Adds a reference to a plugin list defined elsewhere.
     */
    public void setPluginListRef(Reference r) {
        createPluginList().setRefid(r);
    }

    @Override
    public void execute() throws BuildException {
        checkParameters();
        try {
            execFindbugs();
        } catch (BuildException e) {
            // log("Oops: " + e.getMessage());
            if (errorProperty != null) {
                getProject().setProperty(errorProperty, "true");
            }
            if (failOnError) {
                throw e;
            }
        }
    }

    /**
     * Check that all required attributes have been set.
     */
    protected void checkParameters() {
        if (homeDir == null && classpath == null) {
            throw new BuildException("either home attribute or " + "classpath attributes "
                    + " must be defined for task <" + getTaskName() + "/>", getLocation());
        }

        if (pluginList != null) {
            // Make sure that all plugins are actually Jar files.
            String[] pluginFileList = pluginList.list();
            for (String pluginFile : pluginFileList) {
                if (!pluginFile.endsWith(".jar")) {
                    throw new BuildException("plugin file " + pluginFile + " is not a Jar file " + "in task <" + getTaskName()
                            + "/>", getLocation());
                }
            }
        }

        for (SystemProperty systemProperty : systemPropertyList) {
            if (systemProperty.getName() == null || systemProperty.getValue() == null) {
                throw new BuildException("systemProperty elements must have name and value attributes");
            }
        }
    }

    /**
     * Create the FindBugs engine (the Java process that will run whatever
     * FindBugs-related program this task is going to execute).
     */
    protected void createFindbugsEngine() {
        findbugsEngine = new Java();
        findbugsEngine.setProject(getProject());
        findbugsEngine.setTaskName(getTaskName());
        findbugsEngine.setFork(true);
        if (jvm.length() > 0) {
            findbugsEngine.setJvm(jvm);
        }
        findbugsEngine.setTimeout(timeout);

        if (debug) {
            jvmargs = jvmargs + " -Dfindbugs.debug=true";
        }
        jvmargs = jvmargs + " -Dfindbugs.hostApp=FBAntTask";
        findbugsEngine.createJvmarg().setLine(jvmargs);

        // Add JVM arguments for system properties
        for (SystemProperty systemProperty : systemPropertyList) {
            String jvmArg = "-D" + systemProperty.getName() + "=" + systemProperty.getValue();
            findbugsEngine.createJvmarg().setValue(jvmArg);
        }

        if (homeDir != null) {
            // Use findbugs.home to locate findbugs.jar and the standard
            // plugins. This is the usual means of initialization.
            File findbugsLib = new File(homeDir, "lib");
            if (!findbugsLib.exists() && "lib".equals(homeDir.getName())) {
                findbugsLib = homeDir;
                homeDir = homeDir.getParentFile();
            }
            File findbugsLibFindBugs = new File(findbugsLib, "findbugs.jar");
            // log("executing using home dir [" + homeDir + "]");
            if (findbugsLibFindBugs.exists()) {
                findbugsEngine.setClasspath(new Path(getProject(), findbugsLibFindBugs.getPath()));
            } else {
                throw new IllegalArgumentException("Can't find findbugs.jar in " + findbugsLib);
            }
            findbugsEngine.createJvmarg().setValue("-Dfindbugs.home=" + homeDir.getPath());
        } else {
            // Use an explicitly specified classpath and list of plugin Jars
            // to initialize. This is useful for other tools which may have
            // FindBugs installed using a non-standard directory layout.

            findbugsEngine.setClasspath(classpath);
        }
        if (pluginList != null) {
            addArg("-pluginList");
            addArg(pluginList.toString());
        }
        // Set the main class to be whatever the subclass's constructor
        // specified.
        findbugsEngine.setClassname(mainClass);
    }

    /**
     * Get the Findbugs engine.
     */
    protected Java getFindbugsEngine() {
        return findbugsEngine;
    }

    /**
     * Add an argument to the JVM used to execute FindBugs.
     *
     * @param arg
     *            the argument
     */
    protected void addArg(String arg) {
        findbugsEngine.createArg().setValue(arg.trim());
    }

    /**
     * Sets the given string to be piped to standard input of the FindBugs JVM
     * upon launching.
     */
    protected void setInputString(String input) {
        findbugsEngine.setInputString(input);
    }

    /**
     * Create a new JVM to do the work.
     *
     * @since Ant 1.5
     */
    private void execFindbugs() throws BuildException {

        System.out.println("Executing findbugs " + this.getClass().getSimpleName() + " from ant task");
        createFindbugsEngine();
        configureFindbugsEngine();

        beforeExecuteJavaProcess();

        if (getDebug()) {
            log(getFindbugsEngine().getCommandLine().describeCommand());
        }

        /*
         * set property containing return code of child process using a task
         * identifier and a UUID to ensure exit code corresponds to this
         * execution (the base Ant Task won't overwrite return code once it's
         * been set, so unique identifiers must be used for each execution if we
         * want to get the exit code)
         */
        String execReturnCodeIdentifier = execResultProperty + "." + UUID.randomUUID().toString();
        getFindbugsEngine().setResultProperty(execReturnCodeIdentifier);

        /*
         * if the execution fails, we'll report it ourself -- prevent the
         * underlying Ant Java object from throwing an exception
         */
        getFindbugsEngine().setFailonerror(false);
        try {
            getFindbugsEngine().execute();
        } catch (BuildException be) {
            // setFailonerror(false) should ensure that this doesn't happen,
            // but...
            log(be.toString());
        }
        String returnProperty = getFindbugsEngine().getProject().getProperty(execReturnCodeIdentifier);
        int rc = returnProperty == null ? 0 : Integer.parseInt(returnProperty);

        afterExecuteJavaProcess(rc);
    }

    protected abstract void configureFindbugsEngine();

    protected abstract void beforeExecuteJavaProcess();

    protected void afterExecuteJavaProcess(int rc) {
        if (rc != 0) {
            throw new BuildException("execution of " + getTaskName() + " failed");
        }

    }

}