Added a lot more checks for name format. Also endeavouring to get Checkstyle

passing Javadoc again before it gets out of control.
This commit is contained in:
Oliver Burn 2002-09-27 06:41:28 +00:00
parent d7142fd15f
commit d077a402da
30 changed files with 590 additions and 149 deletions

View File

@ -95,10 +95,13 @@ class CheckConfiguration
/**
* Create an instance of the check that is properly initialised.
*
* @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
*/
Check createInstance(ClassLoader aLoader)
throws ClassNotFoundException, InstantiationException,

View File

@ -148,11 +148,23 @@ public class Checker
/** vector of listeners */
private final ArrayList mListeners = new ArrayList();
// TODO: delete me
/** used to collect messages TODO: delete */
private final LocalizedMessages mMessages;
/** used to walk an AST and notify the checks */
private final TreeWalker mWalker;
/**
* Creates a new <code>Checker</code> instance.
*
* @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
*/
public Checker(Configuration aConfig, CheckConfiguration[] aConfigs)
throws ClassNotFoundException, InstantiationException,
IllegalAccessException, InvocationTargetException,
@ -177,6 +189,13 @@ public class Checker
* @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
*/
public Checker(Configuration aConfig)
throws RESyntaxException, IOException,
@ -441,9 +460,10 @@ public class Checker
"general.fileNotFound", null));
}
catch (IOException ioe) {
mMessages.add(new LocalizedMessage(0, Defn.CHECKSTYLE_BUNDLE,
"general.exception",
new String[] {ioe.getMessage()}));
mMessages.add(new LocalizedMessage(
0, Defn.CHECKSTYLE_BUNDLE,
"general.exception",
new String[] {ioe.getMessage()}));
}
catch (RecognitionException re) {
mMessages.add(new LocalizedMessage(0, Defn.CHECKSTYLE_BUNDLE,

View File

@ -68,12 +68,10 @@ public class Configuration
PATTERN_DEFAULTS.put(Defn.MEMBER_PATTERN_PROP, "^[a-z][a-zA-Z0-9]*$");
PATTERN_DEFAULTS.put(Defn.PUBLIC_MEMBER_PATTERN_PROP,
"^f[A-Z][a-zA-Z0-9]*$");
PATTERN_DEFAULTS.put(Defn.TYPE_PATTERN_PROP, "^[A-Z][a-zA-Z0-9]*$");
PATTERN_DEFAULTS.put(Defn.LOCAL_VAR_PATTERN_PROP,
"^[a-z][a-zA-Z0-9]*$");
PATTERN_DEFAULTS.put(Defn.LOCAL_FINAL_VAR_PATTERN_PROP,
"^[a-z][a-zA-Z0-9]*$");
PATTERN_DEFAULTS.put(Defn.METHOD_PATTERN_PROP, "^[a-z][a-zA-Z0-9]*$");
PATTERN_DEFAULTS.put(Defn.IGNORE_LINE_LENGTH_PATTERN_PROP, "^$");
// Uppercase letters seem rather uncommon, but they're allowed in
@ -456,18 +454,6 @@ public class Configuration
return getRegexpProperty(Defn.PUBLIC_MEMBER_PATTERN_PROP);
}
/** @return pattern to match type names **/
String getTypePat()
{
return getPatternProperty(Defn.TYPE_PATTERN_PROP);
}
/** @return regexp to match type names **/
RE getTypeRegexp()
{
return getRegexpProperty(Defn.TYPE_PATTERN_PROP);
}
/** @return pattern to match local variables **/
String getLocalVarPat()
{
@ -492,18 +478,6 @@ public class Configuration
return getRegexpProperty(Defn.LOCAL_FINAL_VAR_PATTERN_PROP);
}
/** @return pattern to match method names **/
String getMethodPat()
{
return getPatternProperty(Defn.METHOD_PATTERN_PROP);
}
/** @return regexp to match method names **/
RE getMethodRegexp()
{
return getRegexpProperty(Defn.METHOD_PATTERN_PROP);
}
/** @return the maximum line length **/
int getMaxLineLength()
{

View File

@ -39,14 +39,10 @@ public interface Defn
String MEMBER_PATTERN_PROP = "checkstyle.pattern.member";
/** property name for the public member variable pattern **/
String PUBLIC_MEMBER_PATTERN_PROP = "checkstyle.pattern.publicmember";
/** property name for the type pattern **/
String TYPE_PATTERN_PROP = "checkstyle.pattern.type";
/** property name for the method local variable pattern **/
String LOCAL_VAR_PATTERN_PROP = "checkstyle.pattern.localvar";
/** property name for the method local final variable pattern **/
String LOCAL_FINAL_VAR_PATTERN_PROP = "checkstyle.pattern.localfinalvar";
/** property name for the method local variable pattern **/
String METHOD_PATTERN_PROP = "checkstyle.pattern.method";
/** property name for the package name pattern **/
String PACKAGE_PATTERN_PROP = "checkstyle.pattern.package";
/** property name for the maximum line length **/
@ -161,10 +157,8 @@ public interface Defn
MEMBER_PATTERN_PROP,
PUBLIC_MEMBER_PATTERN_PROP,
PACKAGE_PATTERN_PROP,
TYPE_PATTERN_PROP,
LOCAL_VAR_PATTERN_PROP,
LOCAL_FINAL_VAR_PATTERN_PROP,
METHOD_PATTERN_PROP,
IGNORE_LINE_LENGTH_PATTERN_PROP,
};

View File

@ -27,7 +27,6 @@ import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import org.apache.regexp.RESyntaxException;
/**
* Wrapper command line program for the Checker.

View File

@ -77,6 +77,11 @@ class TreeWalker
}
/**
* Creates a new <code>TreeWalker</code> instance.
*
* @param aMessages used to collect messages
*/
public TreeWalker(LocalizedMessages aMessages)
{
mMessages = aMessages;

View File

@ -248,17 +248,6 @@ class Verifier
**/
void verifyMethod(MethodSignature aSig)
{
// no need to check constructor names
if (!aSig.isConstructor()
&& !mConfig.getMethodRegexp().match(aSig.getName().getText()))
{
mMessages.add(aSig.getName().getLineNo(),
aSig.getName().getColumnNo(),
"name.invalidPattern",
aSig.getName().getText(),
mConfig.getMethodPat());
}
// Always check that the order of modifiers follows the JLS suggestion
checkModOrder(aSig.getModSet());
@ -331,12 +320,6 @@ class Verifier
**/
void verifyType(MyModifierSet aMods, MyCommonAST aType)
{
if (!mConfig.getTypeRegexp().match(aType.getText())) {
mMessages.add(aType.getLineNo(), aType.getColumnNo(),
"name.invalidPattern",
aType.getText(), mConfig.getTypePat());
}
// Always check that the order of modifiers follows the JLS suggestion
checkModOrder(aMods);

View File

@ -290,7 +290,8 @@ public abstract class Check
* @param aLineNo line number to associate with the message
* @param aColNo column number to associate with the message
* @param aKey key to locale message format
* @param aArgs arguments for message
* @param aArg0 an <code>Object</code> value
* @param aArg1 an <code>Object</code> value
*/
public void log(int aLineNo, int aColNo, String aKey,
Object aArg0, Object aArg1)

View File

@ -38,6 +38,9 @@ public class LocalizedMessages
private final int mTabWidth;
/** the lines of the file being checked **/
private String[] mLines;
/** Name of the exising resource bundle
* TODO: remove this
*/
private static final String OLD_BUNDLE =
"com.puppycrawl.tools.checkstyle.messages";
@ -239,6 +242,7 @@ public class LocalizedMessages
add(aLineNo, aColNo, aKey, new Object[] {aArg0, aArg1, aArg2});
}
/** @return the number of messages */
public int size()
{
return mMessages.size();

View File

@ -0,0 +1,79 @@
////////////////////////////////////////////////////////////////////////////////
// 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.checks;
import com.puppycrawl.tools.checkstyle.api.Check;
import com.puppycrawl.tools.checkstyle.api.Utils;
import org.apache.regexp.RESyntaxException;
import org.apache.regexp.RE;
import org.apache.commons.beanutils.ConversionException;
/**
* Abstract class for checks that verify a name matches a specified regular
* expression. Provides support for setting the format of the name.
*
* @author <a href="mailto:checkstyle@puppycrawl.com">Oliver Burn</a>
* @version 1.0
*/
public abstract class AbstractFormatCheck
extends Check
{
/** the regexp to match against */
private RE mRegexp;
/** the format string of the regexp */
private String mFormat;
/**
* Creates a new <code>AbstractFormatCheck</code> instance.
* @param aDefaultFormat default format
* @throws ConversionException unable to parse aDefaultFormat
*/
public AbstractFormatCheck(String aDefaultFormat)
{
setFormat(aDefaultFormat);
}
/**
* Set the format.
* @param aFormat a <code>String</code> value
* @throws ConversionException unable to parse aFormat
*/
public void setFormat(String aFormat)
{
try {
mRegexp = Utils.getRE(aFormat);
mFormat = aFormat;
}
catch (RESyntaxException e) {
throw new ConversionException("unable to parse " + aFormat, e);
}
}
/** @return the regexp to match against */
public RE getRegexp()
{
return mRegexp;
}
/** @return the regexp format */
public String getFormat()
{
return mFormat;
}
}

View File

@ -35,12 +35,14 @@ import com.puppycrawl.tools.checkstyle.api.DetailAST;
public class AvoidStarImport
extends ImportCheck
{
/** @see com.puppycrawl.tools.checkstyle.api.Check */
public int[] getDefaultTokens()
{
return new int[] {JavaTokenTypes.IMPORT};
}
public void visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST aAST)
/** @see com.puppycrawl.tools.checkstyle.api.Check */
public void visitToken(DetailAST aAST)
{
final String name = getImportText(aAST);
if ((name != null) && name.endsWith(".*")) {

View File

@ -49,11 +49,13 @@ public class EmptyBlockCheck extends Check
// TODO: overwrite mCheckFor based on user settings in config file
/** @see com.puppycrawl.tools.checkstyle.api.Check */
public int[] getDefaultTokens()
{
return new int[] {JavaTokenTypes.SLIST};
}
/** @see com.puppycrawl.tools.checkstyle.api.Check */
public void visitToken(DetailAST aAST)
{
// defend against users that change the token set in the config file.

View File

@ -1,3 +1,21 @@
////////////////////////////////////////////////////////////////////////////////
// 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.checks;
import com.puppycrawl.tools.checkstyle.api.Check;
@ -14,16 +32,16 @@ import com.puppycrawl.tools.checkstyle.api.Check;
*/
public class HeaderCheck extends Check
{
/** @see Check */
/** @see com.puppycrawl.tools.checkstyle.api.Check */
public int[] getDefaultTokens()
{
return new int[0];
}
/** @see Check */
/** @see com.puppycrawl.tools.checkstyle.api.Check */
public void beginTree()
{
String[] lines = getLines();
log(0,"file has " + lines.length + " lines");
log(0, "file has " + lines.length + " lines");
}
}

View File

@ -32,8 +32,15 @@ import com.puppycrawl.tools.checkstyle.JavaTokenTypes;
public abstract class ImportCheck
extends Check
{
/** key to store name of import as */
private static final String TEXT_KEY = "name";
/**
* Return the name of the import associated with a specifed DetailAST.
*
* @param aAST the node containing the import
* @return a <code>String</code> value
*/
protected String getImportText(DetailAST aAST)
{
String text = (String) getTokenContext().get(TEXT_KEY);
@ -48,6 +55,12 @@ public abstract class ImportCheck
return text;
}
/**
* Fills in the name of an import.
*
* @param aBuf the StringBuffer to add the name to
* @param aAST the node to operate on
*/
private static void extractIdent(StringBuffer aBuf, DetailAST aAST)
{
if (aAST == null) {
@ -62,7 +75,8 @@ public abstract class ImportCheck
(DetailAST) aAST.getFirstChild().getNextSibling());
}
else if ((aAST.getType() == JavaTokenTypes.IDENT)
|| (aAST.getType() == JavaTokenTypes.STAR)) {
|| (aAST.getType() == JavaTokenTypes.STAR))
{
aBuf.append(aAST.getText());
}
else {

View File

@ -0,0 +1,59 @@
////////////////////////////////////////////////////////////////////////////////
// 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.checks;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.JavaTokenTypes;
/**
* Checks that method names conform to a specified format.
*
* @author <a href="mailto:checkstyle@puppycrawl.com">Oliver Burn</a>
* @version 1.0
*/
public class MethodNameCheck
extends AbstractFormatCheck
{
/** Creates a new <code>MethodNameCheck</code> instance. */
public MethodNameCheck()
{
super("^[a-z][a-zA-Z0-9]*$");
}
/** @see com.puppycrawl.tools.checkstyle.api.Check */
public int[] getDefaultTokens()
{
return new int[] {JavaTokenTypes.METHOD_DEF};
}
/** @see com.puppycrawl.tools.checkstyle.api.Check */
public void visitToken(DetailAST aAST)
{
final DetailAST nameAST = (DetailAST)
aAST.getFirstChild().getNextSibling().getNextSibling();
if (!getRegexp().match(nameAST.getText())) {
log(nameAST.getLineNo(),
nameAST.getColumnNo(),
"name.invalidPattern",
nameAST.getText(),
getFormat());
}
}
}

View File

@ -29,7 +29,8 @@ import com.puppycrawl.tools.checkstyle.api.Check;
import com.puppycrawl.tools.checkstyle.JavaTokenTypes;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
public class ModifierCheck extends Check
public class ModifierCheck
extends Check
{
/**
* The order of modifiers as suggested in sections 8.1.1,
@ -41,11 +42,13 @@ public class ModifierCheck extends Check
"transient", "volatile", "synchronized", "native", "strictfp"
};
/** @see com.puppycrawl.tools.checkstyle.api.Check */
public int[] getDefaultTokens()
{
return new int[] {JavaTokenTypes.MODIFIERS};
}
/** @see com.puppycrawl.tools.checkstyle.api.Check */
public void visitToken(DetailAST aAST)
{
final List mods = new ArrayList();

View File

@ -0,0 +1,79 @@
////////////////////////////////////////////////////////////////////////////////
// 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.checks;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.JavaTokenTypes;
public class PackageNameCheck
extends AbstractFormatCheck
{
/**
* Creates a new <code>PackageNameCheck</code> instance.
*/
public PackageNameCheck()
{
// Uppercase letters seem rather uncommon, but they're allowed in
// http://java.sun.com/docs/books/jls/
// second_edition/html/packages.doc.html#40169
super("^[a-z]+(\\.[a-zA-Z_][a-zA-Z_0-9]*)*$");
}
/** @see com.puppycrawl.tools.checkstyle.api.Check */
public int[] getDefaultTokens()
{
return new int[] {JavaTokenTypes.PACKAGE_DEF};
}
/** @see com.puppycrawl.tools.checkstyle.api.Check */
public void visitToken(DetailAST aAST)
{
final DetailAST nameAST = (DetailAST) aAST.getFirstChild();
final StringBuffer buf = new StringBuffer();
extractIdent(buf, nameAST);
final String text = buf.toString();
if (!getRegexp().match(text)) {
log(nameAST.getLineNo(),
"name.invalidPattern",
text,
getFormat());
}
}
// TODO: refactor to Utils. It should return the text, plus the starting
// line and column
private static void extractIdent(StringBuffer aBuf, DetailAST aAST)
{
if (aAST == null) {
System.out.println("CALLED WITH NULL");
return;
}
if (aAST.getType() == JavaTokenTypes.DOT) {
extractIdent(aBuf, (DetailAST) aAST.getFirstChild());
aBuf.append(".");
extractIdent(aBuf,
(DetailAST) aAST.getFirstChild().getNextSibling());
}
else {
aBuf.append(aAST.getText());
}
}
}

View File

@ -1,12 +1,25 @@
////////////////////////////////////////////////////////////////////////////////
// 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.checks;
import com.puppycrawl.tools.checkstyle.api.Check;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.Utils;
import com.puppycrawl.tools.checkstyle.JavaTokenTypes;
import org.apache.regexp.RE;
import org.apache.regexp.RESyntaxException;
import org.apache.commons.beanutils.ConversionException;
/**
* Checks that the header of the source file is correct.
@ -19,44 +32,35 @@ import org.apache.commons.beanutils.ConversionException;
* @author Oliver Burn
*/
public class ParameterFormatCheck
extends Check
extends AbstractFormatCheck
{
private RE mRegexp;
private String mFormat;
/**
* Creates a new <code>ParameterFormatCheck</code> instance.
*/
public ParameterFormatCheck()
{
setFormat("^[a-z][a-zA-Z0-9]*$");
super("^[a-z][a-zA-Z0-9]*$");
}
/** @see Check */
/** @see com.puppycrawl.tools.checkstyle.api.Check */
public int[] getDefaultTokens()
{
return new int[] {JavaTokenTypes.PARAMETER_DEF};
}
/** @see com.puppycrawl.tools.checkstyle.api.Check */
public void visitToken(DetailAST aAST)
{
final DetailAST nameAST =
(DetailAST) aAST.getFirstChild().getNextSibling().getNextSibling();
if (!mRegexp.match(nameAST.getText())) {
if (!getRegexp().match(nameAST.getText())) {
log(nameAST.getLineNo(),
nameAST.getColumnNo(),
"name.invalidPattern",
nameAST.getText(),
mFormat);
getFormat());
}
}
public void setFormat(String aFormat)
{
try {
mRegexp = Utils.getRE(aFormat);
mFormat = aFormat;
}
catch (RESyntaxException e) {
throw new ConversionException("unable to parse " + aFormat, e);
}
}
}

View File

@ -1,3 +1,21 @@
////////////////////////////////////////////////////////////////////////////////
// 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.checks;
import java.util.Stack;
@ -12,58 +30,60 @@ public class RedundantModifierCheck extends Check implements JavaTokenTypes
{
private final Stack mInInterface = new Stack();
/** @see com.puppycrawl.tools.checkstyle.api.Check */
public void beginTree()
{
super.beginTree();
mInInterface.clear();
}
/** @see com.puppycrawl.tools.checkstyle.api.Check */
public int[] getDefaultTokens()
{
return new int[] {MODIFIERS, INTERFACE_DEF, CLASS_DEF};
}
/** @see com.puppycrawl.tools.checkstyle.api.Check */
public void visitToken(DetailAST aAST)
{
switch (aAST.getType())
{
case INTERFACE_DEF:
mInInterface.push(Boolean.TRUE);
break;
case CLASS_DEF:
mInInterface.push(Boolean.FALSE);
break;
case MODIFIERS:
switch (aAST.getType()) {
case INTERFACE_DEF:
mInInterface.push(Boolean.TRUE);
break;
case CLASS_DEF:
mInInterface.push(Boolean.FALSE);
break;
case MODIFIERS:
// modifiers of the interface itself (public interface X)
// will be below the INTERFACE_DEF node. Example:
// modifiers of the interface itself (public interface X)
// will be below the INTERFACE_DEF node. Example:
// public interface X {void y();}
// public interface X {void y();}
// INTERFACE_DEF
// + MODUFIERS
// + public
// + OBJ_BLOCK
// + ...
// INTERFACE_DEF
// + MODUFIERS
// + public
// + OBJ_BLOCK
// + ...
if (inInterfaceBlock(aAST)) {
DetailAST ast = (DetailAST) aAST.getFirstChild();
while (ast != null) {
String modifier = ast.getText();
if ("public".equals(modifier)
|| "abstract".equals(modifier))
{
log(ast.getLineNo(),
ast.getColumnNo(),
"redundantModifier",
new String[] {modifier});
}
ast = (DetailAST) ast.getNextSibling();
if (inInterfaceBlock(aAST)) {
DetailAST ast = (DetailAST) aAST.getFirstChild();
while (ast != null) {
String modifier = ast.getText();
if ("public".equals(modifier)
|| "abstract".equals(modifier))
{
log(ast.getLineNo(),
ast.getColumnNo(),
"redundantModifier",
new String[] {modifier});
}
ast = (DetailAST) ast.getNextSibling();
}
break;
default:
return;
}
break;
default:
return;
}
}

View File

@ -32,11 +32,13 @@ import com.puppycrawl.tools.checkstyle.api.DetailAST;
*/
public class SimplifyBooleanReturnCheck extends Check
{
/** @see com.puppycrawl.tools.checkstyle.api.Check */
public int[] getDefaultTokens()
{
return new int[] {JavaTokenTypes.LITERAL_if};
}
/** @see com.puppycrawl.tools.checkstyle.api.Check */
public void visitToken(DetailAST aAST)
{
// paranoia - what an untrusting sole :-)
@ -72,14 +74,15 @@ public class SimplifyBooleanReturnCheck extends Check
private boolean isBooleanLiteralReturnStatement(AST aAST)
{
if (aAST.getType() != JavaTokenTypes.LITERAL_return)
if (aAST.getType() != JavaTokenTypes.LITERAL_return) {
return false;
}
AST expr = aAST.getFirstChild();
AST value = expr.getFirstChild();
final AST expr = aAST.getFirstChild();
final AST value = expr.getFirstChild();
int valueType = value.getType();
return ( valueType == JavaTokenTypes.LITERAL_true
|| valueType == JavaTokenTypes.LITERAL_false );
final int valueType = value.getType();
return ((valueType == JavaTokenTypes.LITERAL_true)
|| (valueType == JavaTokenTypes.LITERAL_false));
}
}

View File

@ -0,0 +1,66 @@
////////////////////////////////////////////////////////////////////////////////
// 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.checks;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.JavaTokenTypes;
/**
* Checks that the header of the source file is correct.
*
* <p>
* Rationale: In most projects each file must have a fixed header,
* usually the header contains copyright information.
* </p>
*
* @author Oliver Burn
*/
public class TypeNameCheck
extends AbstractFormatCheck
{
/**
* Creates a new <code>TypeNameCheck</code> instance.
*/
public TypeNameCheck()
{
super("^[A-Z][a-zA-Z0-9]*$");
}
/** @see com.puppycrawl.tools.checkstyle.api.Check */
public int[] getDefaultTokens()
{
return new int[] {JavaTokenTypes.CLASS_DEF,
JavaTokenTypes.INTERFACE_DEF};
}
/** @see com.puppycrawl.tools.checkstyle.api.Check */
public void visitToken(DetailAST aAST)
{
final DetailAST nameAST =
(DetailAST) aAST.getFirstChild().getNextSibling();
if (!getRegexp().match(nameAST.getText())) {
log(nameAST.getLineNo(),
nameAST.getColumnNo(),
"name.invalidPattern",
nameAST.getText(),
getFormat());
}
}
}

View File

@ -33,10 +33,14 @@ import java.util.Iterator;
*/
public class UnusedImportsCheck extends ImportCheck
{
/** flag to indicate when time to start collecting references */
private boolean mCollect;
/** set of the imports */
private final Set mImports = new HashSet();
/** set of references - possibly to imports or other things */
private final Set mReferenced = new HashSet();
/** @see com.puppycrawl.tools.checkstyle.api.Check */
public void beginTree()
{
mCollect = false;
@ -44,6 +48,7 @@ public class UnusedImportsCheck extends ImportCheck
mReferenced.clear();
}
/** @see com.puppycrawl.tools.checkstyle.api.Check */
public void finishTree()
{
// loop over all the imports to see if referenced.
@ -56,6 +61,7 @@ public class UnusedImportsCheck extends ImportCheck
}
}
/** @see com.puppycrawl.tools.checkstyle.api.Check */
public int[] getDefaultTokens()
{
return new int[] {JavaTokenTypes.IMPORT,
@ -64,6 +70,7 @@ public class UnusedImportsCheck extends ImportCheck
JavaTokenTypes.IDENT};
}
/** @see com.puppycrawl.tools.checkstyle.api.Check */
public void visitToken(DetailAST aAST)
{
if (aAST.getType() == JavaTokenTypes.IDENT) {
@ -81,6 +88,10 @@ public class UnusedImportsCheck extends ImportCheck
}
}
/**
* Collects references made by IDENT.
* @param aAST the IDENT node to process
*/
private void processIdent(DetailAST aAST)
{
// TODO: should be a lot smarter in selection. Currently use
@ -96,6 +107,10 @@ public class UnusedImportsCheck extends ImportCheck
}
}
/**
* Collects the details of imports.
* @param aAST node containing the import details
*/
private void processImport(DetailAST aAST)
{
final String name = getImportText(aAST);

View File

@ -35,11 +35,13 @@ import com.puppycrawl.tools.checkstyle.api.Check;
public class UpperEllCheck
extends Check
{
/** @see com.puppycrawl.tools.checkstyle.api.Check */
public int[] getDefaultTokens()
{
return new int[] {JavaTokenTypes.NUM_LONG};
}
/** @see com.puppycrawl.tools.checkstyle.api.Check */
public void visitToken(DetailAST aAST)
{
if (aAST.getText().endsWith("l")) {

View File

@ -23,8 +23,11 @@ import com.puppycrawl.tools.checkstyle.api.Check;
import com.puppycrawl.tools.checkstyle.Java14TokenTypes;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
public class WhitespaceAroundCheck extends Check implements Java14TokenTypes
public class WhitespaceAroundCheck
extends Check
implements Java14TokenTypes
{
/** @see com.puppycrawl.tools.checkstyle.api.Check */
public int[] getDefaultTokens()
{
return new int[] {
@ -75,6 +78,7 @@ public class WhitespaceAroundCheck extends Check implements Java14TokenTypes
};
}
/** @see com.puppycrawl.tools.checkstyle.api.Check */
public void visitToken(DetailAST aAST)
{
final String[] lines = getLines();
@ -89,7 +93,8 @@ public class WhitespaceAroundCheck extends Check implements Java14TokenTypes
if ((after < line.length())
&& !Character.isWhitespace(line.charAt(after))
&& !(aAST.getType() == LITERAL_return && aAST.getFirstChild() == null))
&& !((aAST.getType() == LITERAL_return)
&& (aAST.getFirstChild() == null)))
{
log(aAST.getLineNo(), aAST.getColumnNo() + aAST.getText().length(),
"ws.notFollowed", new Object[]{aAST.getText()});

View File

@ -502,7 +502,6 @@ public class CheckerTest
filepath + ":130:18: Name 'I' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
filepath + ":131:9: '{' should be on the previous line.",
filepath + ":132:20: Name 'InnerBlockVariable' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
filepath + ":137:10: Name 'ALL_UPPERCASE_METHOD' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
filepath + ":142:30: Name 'BAD__NAME' must match pattern '^[A-Z](_?[A-Z0-9]+)*$'.",
filepath + ":145: Line is longer than 80 characters.",
filepath + ":145:35: Line contains a tab character.",
@ -727,7 +726,6 @@ public class CheckerTest
final String[] expected = {
filepath + ":1: Missing a header - not enough lines in file.",
filepath + ":1: Missing a Javadoc comment.",
filepath + ":1:48: Name 'inputHeader' must match pattern '^[A-Z][a-zA-Z0-9]*$'.",
};
verify(c, filepath, expected);
}

View File

@ -6,7 +6,6 @@ import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Properties;
import junit.framework.TestCase;
import org.apache.regexp.RE;
@ -40,26 +39,6 @@ public class ConfigSerializationTest
return configCopy;
}
/**
* Test that the RE deserialization mechanism works for one example.
*/
public void testRegexpDeserialization()
throws Exception
{
final Properties props = new Properties();
props.setProperty(Defn.TYPE_PATTERN_PROP, "xyz");
Configuration configOrig = new Configuration(props, System.out);
Configuration configCopy = copyBySerialization(configOrig);
assertNotNull(configCopy);
assertNotNull(configCopy.getClassLoader());
// test that the general deserialization mechanism for RE fields works
RE typeRegexp = configCopy.getTypeRegexp();
assertTrue(typeRegexp.match("xyz"));
assertTrue(!typeRegexp.match("DefaultCompatibleTypeFormat"));
}
/**
* Tests that all RE fields are restored during deserialization.
* This test is designed to prevent addition of transient RE

View File

@ -0,0 +1,28 @@
package com.puppycrawl.tools.checkstyle;
import com.puppycrawl.tools.checkstyle.checks.AvoidStarImport;
import com.puppycrawl.tools.checkstyle.checks.ParameterFormatCheck;
import com.puppycrawl.tools.checkstyle.checks.TypeNameCheck;
import com.puppycrawl.tools.checkstyle.checks.MethodNameCheck;
public class MethodNameCheckTest
extends BaseCheckTestCase
{
public MethodNameCheckTest(String aName)
{
super(aName);
}
public void testDefault()
throws Exception
{
final CheckConfiguration checkConfig = new CheckConfiguration();
checkConfig.setClassname(MethodNameCheck.class.getName());
final Checker c = createChecker(checkConfig);
final String fname = getPath("InputSimple.java");
final String[] expected = {
"137:10: Name 'ALL_UPPERCASE_METHOD' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
};
verify(c, fname, expected);
}
}

View File

@ -0,0 +1,42 @@
package com.puppycrawl.tools.checkstyle;
import com.puppycrawl.tools.checkstyle.checks.AvoidStarImport;
import com.puppycrawl.tools.checkstyle.checks.ParameterFormatCheck;
import com.puppycrawl.tools.checkstyle.checks.TypeNameCheck;
import com.puppycrawl.tools.checkstyle.checks.MethodNameCheck;
import com.puppycrawl.tools.checkstyle.checks.PackageNameCheck;
public class PackageNameCheckTest
extends BaseCheckTestCase
{
public PackageNameCheckTest(String aName)
{
super(aName);
}
public void testSpecified()
throws Exception
{
final CheckConfiguration checkConfig = new CheckConfiguration();
checkConfig.setClassname(PackageNameCheck.class.getName());
checkConfig.addProperty("format", "[A-Z]+");
final Checker c = createChecker(checkConfig);
final String fname = getPath("InputSimple.java");
final String[] expected = {
"6:1: Name 'com.puppycrawl.tools.checkstyle' must match pattern '[A-Z]+'.",
};
verify(c, fname, expected);
}
public void testDefault()
throws Exception
{
final CheckConfiguration checkConfig = new CheckConfiguration();
checkConfig.setClassname(PackageNameCheck.class.getName());
final Checker c = createChecker(checkConfig);
final String fname = getPath("InputSimple.java");
final String[] expected = {
};
verify(c, fname, expected);
}
}

View File

@ -4,7 +4,7 @@ import com.puppycrawl.tools.checkstyle.checks.AvoidStarImport;
import com.puppycrawl.tools.checkstyle.checks.ParameterFormatCheck;
public class ParameterFormatCheckTest
extends BaseCheckTestCase
extends BaseCheckTestCase
{
public ParameterFormatCheckTest(String aName)
{

View File

@ -0,0 +1,40 @@
package com.puppycrawl.tools.checkstyle;
import com.puppycrawl.tools.checkstyle.checks.AvoidStarImport;
import com.puppycrawl.tools.checkstyle.checks.ParameterFormatCheck;
import com.puppycrawl.tools.checkstyle.checks.TypeNameCheck;
public class TypeNameCheckTest
extends BaseCheckTestCase
{
public TypeNameCheckTest(String aName)
{
super(aName);
}
public void testSpecified()
throws Exception
{
final CheckConfiguration checkConfig = new CheckConfiguration();
checkConfig.setClassname(TypeNameCheck.class.getName());
checkConfig.addProperty("format", "^inputHe");
final Checker c = createChecker(checkConfig);
final String fname = getPath("inputHeader.java");
final String[] expected = {
};
verify(c, fname, expected);
}
public void testDefault()
throws Exception
{
final CheckConfiguration checkConfig = new CheckConfiguration();
checkConfig.setClassname(TypeNameCheck.class.getName());
final Checker c = createChecker(checkConfig);
final String fname = getPath("inputHeader.java");
final String[] expected = {
"1:48: Name 'inputHeader' must match pattern '^[A-Z][a-zA-Z0-9]*$'."
};
verify(c, fname, expected);
}
}