finally managed to tell beanutils what I want, checks now have their 'natural' api

This commit is contained in:
Lars Kühne 2002-10-06 13:14:16 +00:00
parent 1efd41aa92
commit e20d45697c
4 changed files with 107 additions and 40 deletions

View File

@ -18,17 +18,34 @@
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle;
import com.puppycrawl.tools.checkstyle.api.Check;
import java.util.Set;
import java.util.HashSet;
import java.util.Map;
import java.util.HashMap;
import java.util.StringTokenizer;
import java.util.Iterator;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import org.apache.commons.beanutils.PropertyUtils;
import com.puppycrawl.tools.checkstyle.api.Check;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.BooleanConverter;
import org.apache.commons.beanutils.converters.BooleanArrayConverter;
import org.apache.commons.beanutils.converters.ByteConverter;
import org.apache.commons.beanutils.converters.ByteArrayConverter;
import org.apache.commons.beanutils.converters.CharacterConverter;
import org.apache.commons.beanutils.converters.CharacterArrayConverter;
import org.apache.commons.beanutils.converters.DoubleConverter;
import org.apache.commons.beanutils.converters.DoubleArrayConverter;
import org.apache.commons.beanutils.converters.FloatConverter;
import org.apache.commons.beanutils.converters.FloatArrayConverter;
import org.apache.commons.beanutils.converters.IntegerConverter;
import org.apache.commons.beanutils.converters.IntegerArrayConverter;
import org.apache.commons.beanutils.converters.LongConverter;
import org.apache.commons.beanutils.converters.LongArrayConverter;
import org.apache.commons.beanutils.converters.ShortConverter;
import org.apache.commons.beanutils.converters.ShortArrayConverter;
import org.apache.commons.beanutils.converters.StringArrayConverter;
/**
* Represents the configuration for a check.
@ -38,6 +55,69 @@ import org.apache.commons.beanutils.PropertyUtils;
*/
class CheckConfiguration
{
static
{
initConverters();
}
/**
* Setup the jakarta-commons-beanutils type converters so they throw
* a ConversionException instead of using the default value.
*/
private static void initConverters()
{
// TODO: is there a smarter way to tell beanutils not to use defaults?
boolean booleanArray[] = new boolean[0];
byte byteArray[] = new byte[0];
char charArray[] = new char[0];
double doubleArray[] = new double[0];
float floatArray[] = new float[0];
int intArray[] = new int[0];
long longArray[] = new long[0];
short shortArray[] = new short[0];
ConvertUtils.register(new BooleanConverter(), Boolean.TYPE);
ConvertUtils.register(new BooleanConverter(), Boolean.class);
ConvertUtils.register(new BooleanArrayConverter(),
booleanArray.getClass());
ConvertUtils.register(new ByteConverter(), Byte.TYPE);
ConvertUtils.register(new ByteConverter(), Byte.class);
ConvertUtils.register(new ByteArrayConverter(byteArray),
byteArray.getClass());
ConvertUtils.register(new CharacterConverter(), Character.TYPE);
ConvertUtils.register(new CharacterConverter(), Character.class);
ConvertUtils.register(new CharacterArrayConverter(),
charArray.getClass());
ConvertUtils.register(new DoubleConverter(), Double.TYPE);
ConvertUtils.register(new DoubleConverter(), Double.class);
ConvertUtils.register(new DoubleArrayConverter(doubleArray),
doubleArray.getClass());
ConvertUtils.register(new FloatConverter(), Float.TYPE);
ConvertUtils.register(new FloatConverter(), Float.class);
ConvertUtils.register(new FloatArrayConverter(),
floatArray.getClass());
ConvertUtils.register(new IntegerConverter(), Integer.TYPE);
ConvertUtils.register(new IntegerConverter(), Integer.class);
ConvertUtils.register(new IntegerArrayConverter(),
intArray.getClass());
ConvertUtils.register(new LongConverter(), Long.TYPE);
ConvertUtils.register(new LongConverter(), Long.class);
ConvertUtils.register(new LongArrayConverter(), longArray.getClass());
ConvertUtils.register(new ShortConverter(), Short.TYPE);
ConvertUtils.register(new ShortConverter(), Short.class);
ConvertUtils.register(new ShortArrayConverter(),
shortArray.getClass());
ConvertUtils.register(new StringArrayConverter(),
String[].class );
ConvertUtils.register(new IntegerArrayConverter(),
Integer[].class );
// BigDecimal, BigInteger, Class, Date, String, Time, TimeStamp
// do not use defaults in the default configuration of ConvertUtils
}
/** the classname for the check */
private String mClassname;
/** the tokens the check is interested in */
@ -116,7 +196,7 @@ class CheckConfiguration
while (keyIt.hasNext()) {
final String key = (String) keyIt.next();
final String value = (String) mProperties.get(key);
PropertyUtils.setSimpleProperty(check, key, value);
BeanUtils.copyProperty(check, key, value);
}
return check;
}

View File

@ -24,6 +24,7 @@ import java.io.IOException;
import java.util.ArrayList;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

View File

@ -20,9 +20,6 @@
package com.puppycrawl.tools.checkstyle.checks;
import com.puppycrawl.tools.checkstyle.api.Check;
import org.apache.commons.beanutils.ConversionException;
// TODO: tests!
/**
* Checks for long source files.
@ -60,18 +57,9 @@ public class FileLengthCheck extends Check
/**
* @param aLength the maximum length of a Java source file
*/
// TODO: Should be an int argument.
// It seems like I don't know how to use beanutils, I thought
// that parsing this stuff is the core job of beanutils
public void setMax(String aLength)
public void setMax(int aLength)
{
try {
mMaxFileLength = Integer.parseInt(aLength);
}
catch (NumberFormatException ex) {
throw new ConversionException("Can't parse '"
+ aLength + "' as maximal file length", ex);
}
mMaxFileLength = aLength;
}
}

View File

@ -23,8 +23,7 @@ import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.TreeSet;
import java.util.Arrays;
import com.puppycrawl.tools.checkstyle.api.Check;
import org.apache.commons.beanutils.ConversionException;
@ -41,11 +40,13 @@ import org.apache.commons.beanutils.ConversionException;
*/
public class HeaderCheck extends Check
{
private static int[] EMPTY_INT_ARRAY = new int[0];
/** the lines of the header file */
private String[] mHeaderLines = null;
/** the header lines to ignore in the check */
private TreeSet mIgnoreLines = new TreeSet();
/** the header lines to ignore in the check, sorted */
private int[] mIgnoreLines = EMPTY_INT_ARRAY;
/** @see com.puppycrawl.tools.checkstyle.api.Check */
public int[] getDefaultTokens()
@ -85,14 +86,13 @@ public class HeaderCheck extends Check
*/
private boolean isIgnoreLine(int aLineNo)
{
return mIgnoreLines.contains(new Integer(aLineNo));
return (Arrays.binarySearch(mIgnoreLines, aLineNo) >= 0);
}
/**
* Checks if a code line matches the required header line.
* @param lineNumber the linenumber to check against the header
* @return true if and only if the line matches the required header line
* TODO: override this in RegexpHeaderCheck
*/
protected boolean isMatch(int lineNumber)
{
@ -136,19 +136,17 @@ public class HeaderCheck extends Check
/**
* Set the lines numbers to ignore in the header check.
* @param aList comma separated list of line numbers to ignore in header.
* TODO: This should really be of type int[]
* and beanutils should do the parsing for us!
*/
public void setIgnoreLines(String aList)
public void setIgnoreLines(int[] aList)
{
mIgnoreLines.clear();
if (aList != null) {
final StringTokenizer tokens = new StringTokenizer(aList, ",");
while (tokens.hasMoreTokens()) {
final String ignoreLine = tokens.nextToken();
mIgnoreLines.add(new Integer(ignoreLine));
}
if (aList == null || aList.length == 0) {
mIgnoreLines = EMPTY_INT_ARRAY;
return;
}
mIgnoreLines = new int[aList.length];
System.arraycopy(aList, 0, mIgnoreLines, 0, aList.length);
Arrays.sort(mIgnoreLines);
}
protected String[] getHeaderLines()