Start of conversion to only supporting the new framework. Also refactored

error handling.
This commit is contained in:
Oliver Burn 2002-10-31 12:28:47 +00:00
parent cd32cd1ab0
commit 2effcfd0bc
8 changed files with 227 additions and 83 deletions

View File

@ -205,6 +205,19 @@
</java>
</target>
<!-- runs the command line version on a file -->
<target name="run.simple" depends="compile.tests"
description="Runs the command line version on a file">
<java classname="com.puppycrawl.tools.checkstyle.Main"
fork="yes"
dir="."
classpathref="run.classpath">
<sysproperty key="checkstyle.allow.tabs" value="yes"/>
<arg value="docs/checkstyle_checks.xml" />
<arg value="src/testinputs/com/puppycrawl/tools/checkstyle/InputSimple.java" />
</java>
</target>
<!-- runs the command line version on a file -->
<target name="run.checkstyle" depends="compile.tests"
description="Runs the command line version on a file">

View File

@ -0,0 +1,62 @@
<?xml version="1.0"?>
<configuration>
<!--
<check classname="visitor.checks.ModifierCheck">
<set-property name="order" value="public,private,minus,negative"/>
<set-property name="level" value="doggy"/>
<tokens>MODIFIERS</tokens>
</check>
-->
<check classname="com.puppycrawl.tools.checkstyle.checks.AvoidStarImport"/>
<check classname="com.puppycrawl.tools.checkstyle.checks.ConstantNameCheck"/>
<check classname="com.puppycrawl.tools.checkstyle.checks.ConstructorLengthCheck"/>
<check classname="com.puppycrawl.tools.checkstyle.checks.EmptyBlockCheck"/>
<check classname="com.puppycrawl.tools.checkstyle.checks.EqualsHashCodeCheck"/>
<check classname="com.puppycrawl.tools.checkstyle.checks.FileLengthCheck"/>
<check classname="com.puppycrawl.tools.checkstyle.checks.GenericIllegalRegexpCheck"/>
<check classname="com.puppycrawl.tools.checkstyle.checks.HeaderCheck"/>
<check classname="com.puppycrawl.tools.checkstyle.checks.IllegalImportCheck"/>
<check classname="com.puppycrawl.tools.checkstyle.checks.IllegalInstantiationCheck"/>
<check classname="com.puppycrawl.tools.checkstyle.checks.InnerAssignmentCheck"/>
<check classname="com.puppycrawl.tools.checkstyle.checks.JavadocTypeCheck"/>
<check classname="com.puppycrawl.tools.checkstyle.checks.JavadocVariableCheck"/>
<check classname="com.puppycrawl.tools.checkstyle.checks.LineLengthCheck"/>
<check classname="com.puppycrawl.tools.checkstyle.checks.LocalFinalVariableNameCheck"/>
<check classname="com.puppycrawl.tools.checkstyle.checks.LocalVariableNameCheck"/>
<check classname="com.puppycrawl.tools.checkstyle.checks.MemberNameCheck"/>
<check classname="com.puppycrawl.tools.checkstyle.checks.MethodLeftCurlyCheck"/>
<check classname="com.puppycrawl.tools.checkstyle.checks.MethodLengthCheck"/>
<check classname="com.puppycrawl.tools.checkstyle.checks.MethodNameCheck"/>
<check classname="com.puppycrawl.tools.checkstyle.checks.ModifierCheck"/>
<check classname="com.puppycrawl.tools.checkstyle.checks.NoWhiteSpaceAroundCheck"/>
<check classname="com.puppycrawl.tools.checkstyle.checks.OtherLeftCurlyCheck"/>
<check classname="com.puppycrawl.tools.checkstyle.checks.PackageNameCheck"/>
<check classname="com.puppycrawl.tools.checkstyle.checks.ParameterNameCheck"/>
<check classname="com.puppycrawl.tools.checkstyle.checks.ParameterNumberCheck"/>
<check classname="com.puppycrawl.tools.checkstyle.checks.ParenPadCheck"/>
<check classname="com.puppycrawl.tools.checkstyle.checks.PublicMemberNameCheck"/>
<check classname="com.puppycrawl.tools.checkstyle.checks.RedundantImportCheck"/>
<check classname="com.puppycrawl.tools.checkstyle.checks.RedundantModifierCheck"/>
<check classname="com.puppycrawl.tools.checkstyle.checks.RegexpHeaderCheck"/>
<check classname="com.puppycrawl.tools.checkstyle.checks.RightCurlyCheck"/>
<check classname="com.puppycrawl.tools.checkstyle.checks.SimplifyBooleanExpressionCheck"/>
<check classname="com.puppycrawl.tools.checkstyle.checks.SimplifyBooleanReturnCheck"/>
<check classname="com.puppycrawl.tools.checkstyle.checks.StaticVariableNameCheck"/>
<check classname="com.puppycrawl.tools.checkstyle.checks.TabCharacterCheck"/>
<check classname="com.puppycrawl.tools.checkstyle.checks.TypeLeftCurlyCheck"/>
<check classname="com.puppycrawl.tools.checkstyle.checks.TypeNameCheck"/>
<check classname="com.puppycrawl.tools.checkstyle.checks.UnusedImportsCheck"/>
<check classname="com.puppycrawl.tools.checkstyle.checks.UpperEllCheck"/>
<check classname="com.puppycrawl.tools.checkstyle.checks.VisibilityModifierCheck"/>
<check classname="com.puppycrawl.tools.checkstyle.checks.WhitespaceAroundCheck"/>
</configuration>

View File

@ -176,27 +176,42 @@ class CheckConfiguration
*
* @param aLoader the <code>ClassLoader</code> 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;
}
}

View File

@ -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 <a href="mailto:oliver@puppycrawl.com">Oliver Burn</a>
@ -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]);

View File

@ -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 <a href="mailto:checkstyle@puppycrawl.com">Oliver Burn</a>
* @version 1.0
*/
class CheckstyleException extends Exception
{
/**
* Creates a new <code>CheckstyleException</code> instance.
*
* @param aMessage a <code>String</code> value
*/
CheckstyleException(String aMessage)
{
super(aMessage);
}
}

View File

@ -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 <a href="mailto:oliver@puppycrawl.com">Oliver Burn</a>
@ -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() + " <options> <file>......");
"Usage: java " + Main.class.getName()
+ " <options> config <file>......");
System.out.println("Options");
System.out.println(
"\t-f <format>\tsets output format. (plain|xml). "

View File

@ -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});

View File

@ -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);