diff --git a/build.xml b/build.xml index 38ce1f8e6..6d3ce56f8 100644 --- a/build.xml +++ b/build.xml @@ -205,6 +205,19 @@ + + + + + + + + + diff --git a/docs/checkstyle_checks.xml b/docs/checkstyle_checks.xml new file mode 100644 index 000000000..f2781aa1a --- /dev/null +++ b/docs/checkstyle_checks.xml @@ -0,0 +1,62 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/CheckConfiguration.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/CheckConfiguration.java index 559a25f43..d7b48d083 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/CheckConfiguration.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/CheckConfiguration.java @@ -176,27 +176,42 @@ class CheckConfiguration * * @param aLoader the ClassLoader to create the instance with * @return the created check - * @throws ClassNotFoundException if an error occurs - * @throws InstantiationException if an error occurs - * @throws IllegalAccessException if an error occurs - * @throws InvocationTargetException if an error occurs - * @throws NoSuchMethodException if an error occurs + * @throws CheckstyleException if an error occurs */ Check createInstance(ClassLoader aLoader) - throws ClassNotFoundException, InstantiationException, - IllegalAccessException, InvocationTargetException, - NoSuchMethodException + throws CheckstyleException { - final Class clazz = Class.forName(mClassname, true, aLoader); - final Check check = (Check) clazz.newInstance(); - // TODO: need to set the properties - // Loop setting the properties - final Iterator keyIt = mProperties.keySet().iterator(); - while (keyIt.hasNext()) { - final String key = (String) keyIt.next(); - final String value = (String) mProperties.get(key); - BeanUtils.copyProperty(check, key, value); + try { + final Class clazz = Class.forName(mClassname, true, aLoader); + final Check check = (Check) clazz.newInstance(); + // TODO: need to set the properties + // Loop setting the properties + final Iterator keyIt = mProperties.keySet().iterator(); + while (keyIt.hasNext()) { + final String key = (String) keyIt.next(); + final String value = (String) mProperties.get(key); + try { + BeanUtils.copyProperty(check, key, value); + } + catch (InvocationTargetException e) { + throw new CheckstyleException( + "for check " + mClassname + " unable to set " + key + + " with " + value); + } + } + return check; + } + catch (ClassNotFoundException e) { + throw new CheckstyleException( + "Unable to find class for " + mClassname); + } + catch (InstantiationException e) { + throw new CheckstyleException( + "Unable to instantiate " + mClassname); + } + catch (IllegalAccessException e) { + throw new CheckstyleException( + "Unable to instantiate " + mClassname); } - return check; } } diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/Checker.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/Checker.java index c591acc58..9f8729fea 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/Checker.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/Checker.java @@ -18,27 +18,23 @@ //////////////////////////////////////////////////////////////////////////////// package com.puppycrawl.tools.checkstyle; -import antlr.RecognitionException; -import antlr.TokenStreamException; -import com.puppycrawl.tools.checkstyle.api.DetailAST; -import com.puppycrawl.tools.checkstyle.api.LocalizedMessage; -import com.puppycrawl.tools.checkstyle.api.LocalizedMessages; -import com.puppycrawl.tools.checkstyle.api.Utils; -import com.puppycrawl.tools.checkstyle.api.FileContents; -import org.apache.regexp.RESyntaxException; -import org.xml.sax.SAXException; - -import javax.xml.parsers.ParserConfigurationException; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.Reader; -import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; import java.util.Locale; +import antlr.RecognitionException; +import antlr.TokenStreamException; +import com.puppycrawl.tools.checkstyle.api.DetailAST; +import com.puppycrawl.tools.checkstyle.api.FileContents; +import com.puppycrawl.tools.checkstyle.api.LocalizedMessage; +import com.puppycrawl.tools.checkstyle.api.LocalizedMessages; +import com.puppycrawl.tools.checkstyle.api.Utils; + /** * This class provides the functionality to check a set of files. * @author Oliver Burn @@ -158,16 +154,10 @@ public class Checker * * @param aConfig the configuration to use * @param aConfigs the configuation of the checks to use - * @throws ClassNotFoundException if an error occurs - * @throws InstantiationException if an error occurs - * @throws IllegalAccessException if an error occurs - * @throws InvocationTargetException if an error occurs - * @throws NoSuchMethodException if an error occurs + * @throws CheckstyleException if an error occurs */ public Checker(Configuration aConfig, CheckConfiguration[] aConfigs) - throws ClassNotFoundException, InstantiationException, - IllegalAccessException, InvocationTargetException, - NoSuchMethodException + throws CheckstyleException { mConfig = aConfig; mCache = new PropertyCacheFile(aConfig); @@ -186,22 +176,10 @@ public class Checker /** * Constructs the object. * @param aConfig contains the configuration to check with - * @throws RESyntaxException unable to create a regexp object - * @throws IOException if an error occurs - * @throws ParserConfigurationException if an error occurs - * @throws SAXException if an error occurs - * @throws ClassNotFoundException if an error occurs - * @throws InstantiationException if an error occurs - * @throws IllegalAccessException if an error occurs - * @throws InvocationTargetException if an error occurs - * @throws NoSuchMethodException if an error occurs + * @throws CheckstyleException if an error occurs */ public Checker(Configuration aConfig) - throws RESyntaxException, IOException, - ParserConfigurationException, SAXException, - ClassNotFoundException, InstantiationException, - IllegalAccessException, InvocationTargetException, - NoSuchMethodException + throws CheckstyleException { // TODO: delete this method eventually this(aConfig, new CheckConfiguration[0]); diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/CheckstyleException.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/CheckstyleException.java new file mode 100644 index 000000000..fdaa9b808 --- /dev/null +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/CheckstyleException.java @@ -0,0 +1,38 @@ +//////////////////////////////////////////////////////////////////////////////// +// checkstyle: Checks Java source code for adherence to a set of rules. +// Copyright (C) 2001-2002 Oliver Burn +// +// 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 com.puppycrawl.tools.checkstyle; + +/** + * Represents an error condition within Checkstyle. + * + * @author Oliver Burn + * @version 1.0 + */ +class CheckstyleException extends Exception +{ + /** + * Creates a new CheckstyleException instance. + * + * @param aMessage a String value + */ + CheckstyleException(String aMessage) + { + super(aMessage); + } +} diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/Main.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/Main.java index 276a490ab..299ee66cb 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/Main.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/Main.java @@ -28,6 +28,10 @@ import java.util.ArrayList; import java.util.List; import java.util.Properties; +import javax.xml.parsers.ParserConfigurationException; + +import org.xml.sax.SAXException; + /** * Wrapper command line program for the Checker. * @author Oliver Burn @@ -48,7 +52,8 @@ public final class Main // be brain dead about arguments parsing String format = "plain"; String output = null; - Properties properties = System.getProperties(); + Properties props = System.getProperties(); + final List foundFiles = new ArrayList(); final ArrayList files = new ArrayList(); for (int i = 0; i < aArgs.length; i++) { if ("-f".equals(aArgs[i])) { @@ -58,10 +63,10 @@ public final class Main output = aArgs[++i]; } else if ("-r".equals(aArgs[i])) { - traverse(new File(aArgs[++i]), files); + traverse(new File(aArgs[++i]), foundFiles); } else if ("-p".equals(aArgs[i])) { - properties = loadProperties(new File(aArgs[++i])); + props = loadProperties(new File(aArgs[++i])); } else { files.add(aArgs[i]); @@ -94,9 +99,27 @@ public final class Main usage(); } + // Check that I have a config file + if (files.isEmpty()) { + System.out.println("Need to specify a config file"); + usage(); + } + + // Load the config file + final String configFname = (String) files.remove(0); + CheckConfiguration[] checkConfigs = null; + try { + checkConfigs = loadConfigs(configFname); + } + catch (CheckstyleException e) { + System.out.println("Error loading configuration file"); + e.printStackTrace(System.out); + System.exit(1); + } + Checker c = null; try { - c = new Checker(new Configuration(properties, System.out)); + c = new Checker(new Configuration(props, System.out), checkConfigs); c.addListener(listener); } catch (Exception e) { @@ -106,17 +129,48 @@ public final class Main System.exit(1); } + files.addAll(foundFiles); final int numErrs = - c.process((String[]) files.toArray(new String[files.size()])); + c.processNEW((String[]) files.toArray(new String[files.size()])); c.destroy(); System.exit(numErrs); } + /** + * Returns the check configurations in a specified file. + * @param aConfigFname name of config file + * @return the check configurations + * @throws CheckstyleException if an error occurs + */ + private static CheckConfiguration[] loadConfigs(String aConfigFname) + throws CheckstyleException + { + System.out.println("Loading from " + aConfigFname); + try { + final ConfigurationLoader loader = new ConfigurationLoader(); + loader.parseFile(aConfigFname); + return loader.getConfigs(); + } + catch (FileNotFoundException e) { + throw new CheckstyleException("unable to find " + aConfigFname); + } + catch (ParserConfigurationException e) { + throw new CheckstyleException("unable to parse " + aConfigFname); + } + catch (SAXException e) { + throw new CheckstyleException("unable to parse " + aConfigFname); + } + catch (IOException e) { + throw new CheckstyleException("unable to read " + aConfigFname); + } + } + /** Prints the usage information. **/ private static void usage() { System.out.println( - "Usage: java " + Main.class.getName() + " ......"); + "Usage: java " + Main.class.getName() + + " config ......"); System.out.println("Options"); System.out.println( "\t-f \tsets output format. (plain|xml). " diff --git a/src/tests/com/puppycrawl/tools/checkstyle/BaseCheckTestCase.java b/src/tests/com/puppycrawl/tools/checkstyle/BaseCheckTestCase.java index c535eef14..98615fec6 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/BaseCheckTestCase.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/BaseCheckTestCase.java @@ -3,19 +3,14 @@ package com.puppycrawl.tools.checkstyle; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; -import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.io.LineNumberReader; import java.io.OutputStream; import java.io.PrintStream; -import java.lang.reflect.InvocationTargetException; import java.util.Properties; -import javax.xml.parsers.ParserConfigurationException; import junit.framework.TestCase; -import org.apache.regexp.RESyntaxException; -import org.xml.sax.SAXException; public abstract class BaseCheckTestCase extends TestCase @@ -43,9 +38,7 @@ public abstract class BaseCheckTestCase } protected Checker createChecker(CheckConfiguration aCheckConfig) - throws RESyntaxException, FileNotFoundException, IOException, - ParserConfigurationException, SAXException, ClassNotFoundException, - InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException + throws Exception { final Checker c = new Checker(new Configuration(mProps, mStream), new CheckConfiguration[] {aCheckConfig}); diff --git a/src/tests/com/puppycrawl/tools/checkstyle/CheckerTest.java b/src/tests/com/puppycrawl/tools/checkstyle/CheckerTest.java index 9f1cfc562..147b307a0 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/CheckerTest.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/CheckerTest.java @@ -1,22 +1,17 @@ package com.puppycrawl.tools.checkstyle; -import junit.framework.TestCase; -import org.apache.regexp.RESyntaxException; -import org.xml.sax.SAXException; - -import javax.xml.parsers.ParserConfigurationException; +import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.LineNumberReader; import java.io.OutputStream; import java.io.PrintStream; -import java.io.IOException; -import java.io.FileNotFoundException; -import java.io.File; -import java.io.ByteArrayInputStream; -import java.io.LineNumberReader; -import java.io.InputStreamReader; import java.util.Locale; import java.util.Properties; -import java.lang.reflect.InvocationTargetException; + +import junit.framework.TestCase; public class CheckerTest extends TestCase @@ -61,11 +56,7 @@ public class CheckerTest } protected Checker createChecker() - throws RESyntaxException, FileNotFoundException, IOException, - ParserConfigurationException, SAXException, - ClassNotFoundException, InstantiationException, - IllegalAccessException, InvocationTargetException, - NoSuchMethodException + throws Exception { final Configuration config = new Configuration(mProps, mStream); final Checker c = new Checker(config);