Prefixes, metrics, #512
This commit is contained in:
parent
3fec4a92e3
commit
1d6bf23ccd
|
|
@ -61,24 +61,24 @@ public abstract class AbstractClassCouplingCheck extends Check
|
|||
.add("Map", "HashMap", "SortedMap", "TreeMap")
|
||||
.build();
|
||||
/** User-configured class names to ignore. */
|
||||
private Set<String> mExcludedClasses = DEFAULT_EXCLUDED_CLASSES;
|
||||
private Set<String> excludedClasses = DEFAULT_EXCLUDED_CLASSES;
|
||||
/** Allowed complexity. */
|
||||
private int mMax;
|
||||
private int max;
|
||||
/** package of the file we check. */
|
||||
private String mPackageName;
|
||||
private String packageName;
|
||||
|
||||
/** Stack of contexts. */
|
||||
private final FastStack<Context> mContextStack = FastStack.newInstance();
|
||||
private final FastStack<Context> contextStack = FastStack.newInstance();
|
||||
/** Current context. */
|
||||
private Context mContext;
|
||||
private Context context;
|
||||
|
||||
/**
|
||||
* Creates new instance of the check.
|
||||
* @param aDefaultMax default value for allowed complexity.
|
||||
* @param defaultMax default value for allowed complexity.
|
||||
*/
|
||||
protected AbstractClassCouplingCheck(int aDefaultMax)
|
||||
protected AbstractClassCouplingCheck(int defaultMax)
|
||||
{
|
||||
setMax(aDefaultMax);
|
||||
setMax(defaultMax);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -90,67 +90,67 @@ public abstract class AbstractClassCouplingCheck extends Check
|
|||
/** @return allowed complexity. */
|
||||
public final int getMax()
|
||||
{
|
||||
return mMax;
|
||||
return max;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets maximum allowed complexity.
|
||||
* @param aMax allowed complexity.
|
||||
* @param max allowed complexity.
|
||||
*/
|
||||
public final void setMax(int aMax)
|
||||
public final void setMax(int max)
|
||||
{
|
||||
mMax = aMax;
|
||||
this.max = max;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets user-excluded classes to ignore.
|
||||
* @param aExcludedClasses the list of classes to ignore.
|
||||
* @param excludedClasses the list of classes to ignore.
|
||||
*/
|
||||
public final void setExcludedClasses(String[] aExcludedClasses)
|
||||
public final void setExcludedClasses(String[] excludedClasses)
|
||||
{
|
||||
mExcludedClasses = ImmutableSet.copyOf(aExcludedClasses);
|
||||
this.excludedClasses = ImmutableSet.copyOf(excludedClasses);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void beginTree(DetailAST aAST)
|
||||
public final void beginTree(DetailAST ast)
|
||||
{
|
||||
mPackageName = "";
|
||||
packageName = "";
|
||||
}
|
||||
|
||||
/** @return message key we use for log violations. */
|
||||
protected abstract String getLogMessageId();
|
||||
|
||||
@Override
|
||||
public void visitToken(DetailAST aAST)
|
||||
public void visitToken(DetailAST ast)
|
||||
{
|
||||
switch (aAST.getType()) {
|
||||
switch (ast.getType()) {
|
||||
case TokenTypes.PACKAGE_DEF:
|
||||
visitPackageDef(aAST);
|
||||
visitPackageDef(ast);
|
||||
break;
|
||||
case TokenTypes.CLASS_DEF:
|
||||
case TokenTypes.INTERFACE_DEF:
|
||||
case TokenTypes.ANNOTATION_DEF:
|
||||
case TokenTypes.ENUM_DEF:
|
||||
visitClassDef(aAST);
|
||||
visitClassDef(ast);
|
||||
break;
|
||||
case TokenTypes.TYPE:
|
||||
mContext.visitType(aAST);
|
||||
context.visitType(ast);
|
||||
break;
|
||||
case TokenTypes.LITERAL_NEW:
|
||||
mContext.visitLiteralNew(aAST);
|
||||
context.visitLiteralNew(ast);
|
||||
break;
|
||||
case TokenTypes.LITERAL_THROWS:
|
||||
mContext.visitLiteralThrows(aAST);
|
||||
context.visitLiteralThrows(ast);
|
||||
break;
|
||||
default:
|
||||
throw new IllegalStateException(aAST.toString());
|
||||
throw new IllegalStateException(ast.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void leaveToken(DetailAST aAST)
|
||||
public void leaveToken(DetailAST ast)
|
||||
{
|
||||
switch (aAST.getType()) {
|
||||
switch (ast.getType()) {
|
||||
case TokenTypes.CLASS_DEF:
|
||||
case TokenTypes.INTERFACE_DEF:
|
||||
case TokenTypes.ANNOTATION_DEF:
|
||||
|
|
@ -164,34 +164,34 @@ public abstract class AbstractClassCouplingCheck extends Check
|
|||
|
||||
/**
|
||||
* Stores package of current class we check.
|
||||
* @param aPkg package definition.
|
||||
* @param pkg package definition.
|
||||
*/
|
||||
private void visitPackageDef(DetailAST aPkg)
|
||||
private void visitPackageDef(DetailAST pkg)
|
||||
{
|
||||
final FullIdent ident = FullIdent.createFullIdent(aPkg.getLastChild()
|
||||
final FullIdent ident = FullIdent.createFullIdent(pkg.getLastChild()
|
||||
.getPreviousSibling());
|
||||
mPackageName = ident.getText();
|
||||
packageName = ident.getText();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates new context for a given class.
|
||||
* @param aClassDef class definition node.
|
||||
* @param classDef class definition node.
|
||||
*/
|
||||
private void visitClassDef(DetailAST aClassDef)
|
||||
private void visitClassDef(DetailAST classDef)
|
||||
{
|
||||
mContextStack.push(mContext);
|
||||
contextStack.push(context);
|
||||
final String className =
|
||||
aClassDef.findFirstToken(TokenTypes.IDENT).getText();
|
||||
mContext = new Context(className,
|
||||
aClassDef.getLineNo(),
|
||||
aClassDef.getColumnNo());
|
||||
classDef.findFirstToken(TokenTypes.IDENT).getText();
|
||||
context = new Context(className,
|
||||
classDef.getLineNo(),
|
||||
classDef.getColumnNo());
|
||||
}
|
||||
|
||||
/** Restores previous context. */
|
||||
private void leaveClassDef()
|
||||
{
|
||||
mContext.checkCoupling();
|
||||
mContext = mContextStack.pop();
|
||||
context.checkCoupling();
|
||||
context = contextStack.pop();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -206,35 +206,35 @@ public abstract class AbstractClassCouplingCheck extends Check
|
|||
* Set of referenced classes.
|
||||
* Sorted by name for predictable error messages in unit tests.
|
||||
*/
|
||||
private final Set<String> mReferencedClassNames = Sets.newTreeSet();
|
||||
private final Set<String> referencedClassNames = Sets.newTreeSet();
|
||||
/** Own class name. */
|
||||
private final String mClassName;
|
||||
private final String className;
|
||||
/* Location of own class. (Used to log violations) */
|
||||
/** Line number of class definition. */
|
||||
private final int mLineNo;
|
||||
private final int lineNo;
|
||||
/** Column number of class definition. */
|
||||
private final int mColumnNo;
|
||||
private final int columnNo;
|
||||
|
||||
/**
|
||||
* Create new context associated with given class.
|
||||
* @param aClassName name of the given class.
|
||||
* @param aLineNo line of class definition.
|
||||
* @param aColumnNo column of class definition.
|
||||
* @param className name of the given class.
|
||||
* @param lineNo line of class definition.
|
||||
* @param columnNo column of class definition.
|
||||
*/
|
||||
public Context(String aClassName, int aLineNo, int aColumnNo)
|
||||
public Context(String className, int lineNo, int columnNo)
|
||||
{
|
||||
mClassName = aClassName;
|
||||
mLineNo = aLineNo;
|
||||
mColumnNo = aColumnNo;
|
||||
this.className = className;
|
||||
this.lineNo = lineNo;
|
||||
this.columnNo = columnNo;
|
||||
}
|
||||
|
||||
/**
|
||||
* Visits throws clause and collects all exceptions we throw.
|
||||
* @param aThrows throws to process.
|
||||
* @param throws throws to process.
|
||||
*/
|
||||
public void visitLiteralThrows(DetailAST aThrows)
|
||||
public void visitLiteralThrows(DetailAST literalThrows)
|
||||
{
|
||||
for (DetailAST childAST = aThrows.getFirstChild();
|
||||
for (DetailAST childAST = literalThrows.getFirstChild();
|
||||
childAST != null;
|
||||
childAST = childAST.getNextSibling())
|
||||
{
|
||||
|
|
@ -246,67 +246,67 @@ public abstract class AbstractClassCouplingCheck extends Check
|
|||
|
||||
/**
|
||||
* Visits type.
|
||||
* @param aAST type to process.
|
||||
* @param ast type to process.
|
||||
*/
|
||||
public void visitType(DetailAST aAST)
|
||||
public void visitType(DetailAST ast)
|
||||
{
|
||||
final String className = CheckUtils.createFullType(aAST).getText();
|
||||
mContext.addReferencedClassName(className);
|
||||
final String className = CheckUtils.createFullType(ast).getText();
|
||||
context.addReferencedClassName(className);
|
||||
}
|
||||
|
||||
/**
|
||||
* Visits NEW.
|
||||
* @param aAST NEW to process.
|
||||
* @param ast NEW to process.
|
||||
*/
|
||||
public void visitLiteralNew(DetailAST aAST)
|
||||
public void visitLiteralNew(DetailAST ast)
|
||||
{
|
||||
mContext.addReferencedClassName(aAST.getFirstChild());
|
||||
context.addReferencedClassName(ast.getFirstChild());
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds new referenced class.
|
||||
* @param aAST a node which represents referenced class.
|
||||
* @param ast a node which represents referenced class.
|
||||
*/
|
||||
private void addReferencedClassName(DetailAST aAST)
|
||||
private void addReferencedClassName(DetailAST ast)
|
||||
{
|
||||
final String className = FullIdent.createFullIdent(aAST).getText();
|
||||
final String className = FullIdent.createFullIdent(ast).getText();
|
||||
addReferencedClassName(className);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds new referenced class.
|
||||
* @param aClassName class name of the referenced class.
|
||||
* @param className class name of the referenced class.
|
||||
*/
|
||||
private void addReferencedClassName(String aClassName)
|
||||
private void addReferencedClassName(String className)
|
||||
{
|
||||
if (isSignificant(aClassName)) {
|
||||
mReferencedClassNames.add(aClassName);
|
||||
if (isSignificant(className)) {
|
||||
referencedClassNames.add(className);
|
||||
}
|
||||
}
|
||||
|
||||
/** Checks if coupling less than allowed or not. */
|
||||
public void checkCoupling()
|
||||
{
|
||||
mReferencedClassNames.remove(mClassName);
|
||||
mReferencedClassNames.remove(mPackageName + "." + mClassName);
|
||||
referencedClassNames.remove(className);
|
||||
referencedClassNames.remove(packageName + "." + className);
|
||||
|
||||
if (mReferencedClassNames.size() > mMax) {
|
||||
log(mLineNo, mColumnNo, getLogMessageId(),
|
||||
mReferencedClassNames.size(), getMax(),
|
||||
mReferencedClassNames.toString());
|
||||
if (referencedClassNames.size() > max) {
|
||||
log(lineNo, columnNo, getLogMessageId(),
|
||||
referencedClassNames.size(), getMax(),
|
||||
referencedClassNames.toString());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if given class shouldn't be ignored and not from java.lang.
|
||||
* @param aClassName class to check.
|
||||
* @param className class to check.
|
||||
* @return true if we should count this class.
|
||||
*/
|
||||
private boolean isSignificant(String aClassName)
|
||||
private boolean isSignificant(String className)
|
||||
{
|
||||
return (aClassName.length() > 0)
|
||||
&& !mExcludedClasses.contains(aClassName)
|
||||
&& !aClassName.startsWith("java.lang.");
|
||||
return (className.length() > 0)
|
||||
&& !excludedClasses.contains(className)
|
||||
&& !className.startsWith("java.lang.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,18 +2,18 @@
|
|||
// checkstyle: Checks Java source code for adherence to a set of rules.
|
||||
// Copyright (C) 2001-2014 Oliver Burn
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// This librby 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.
|
||||
// version 2.1 of the License, or (at your option) by later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// This librby 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
|
||||
// License along with this librby; 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.metrics;
|
||||
|
|
@ -37,21 +37,21 @@ public abstract class AbstractComplexityCheck
|
|||
private static final BigInteger INITIAL_VALUE = BigInteger.ONE;
|
||||
|
||||
/** stack of values - all but the current value */
|
||||
private final FastStack<BigInteger> mValueStack = FastStack.newInstance();
|
||||
private final FastStack<BigInteger> valueStack = FastStack.newInstance();
|
||||
|
||||
/** the current value */
|
||||
private BigInteger mCurrentValue = BigInteger.ZERO;
|
||||
private BigInteger currentValue = BigInteger.ZERO;
|
||||
|
||||
/** threshold to report error for */
|
||||
private int mMax;
|
||||
private int max;
|
||||
|
||||
/**
|
||||
* Creates an instance.
|
||||
* @param aMax the threshold of when to report an error
|
||||
* @param max the threshold of when to report an error
|
||||
*/
|
||||
public AbstractComplexityCheck(int aMax)
|
||||
public AbstractComplexityCheck(int max)
|
||||
{
|
||||
mMax = aMax;
|
||||
this.max = max;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -63,9 +63,9 @@ public abstract class AbstractComplexityCheck
|
|||
* Hook called when visiting a token. Will not be called the method
|
||||
* definition tokens.
|
||||
*
|
||||
* @param aAST the token being visited
|
||||
* @param ast the token being visited
|
||||
*/
|
||||
protected void visitTokenHook(DetailAST aAST)
|
||||
protected void visitTokenHook(DetailAST ast)
|
||||
{
|
||||
}
|
||||
|
||||
|
|
@ -73,9 +73,9 @@ public abstract class AbstractComplexityCheck
|
|||
* Hook called when leaving a token. Will not be called the method
|
||||
* definition tokens.
|
||||
*
|
||||
* @param aAST the token being left
|
||||
* @param ast the token being left
|
||||
*/
|
||||
protected void leaveTokenHook(DetailAST aAST)
|
||||
protected void leaveTokenHook(DetailAST ast)
|
||||
{
|
||||
}
|
||||
|
||||
|
|
@ -93,23 +93,23 @@ public abstract class AbstractComplexityCheck
|
|||
/** @return the maximum threshold allowed */
|
||||
public final int getMax()
|
||||
{
|
||||
return mMax;
|
||||
return max;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the maximum threshold allowed.
|
||||
*
|
||||
* @param aMax the maximum threshold
|
||||
* @param max the maximum threshold
|
||||
*/
|
||||
public final void setMax(int aMax)
|
||||
public final void setMax(int max)
|
||||
{
|
||||
mMax = aMax;
|
||||
this.max = max;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitToken(DetailAST aAST)
|
||||
public void visitToken(DetailAST ast)
|
||||
{
|
||||
switch (aAST.getType()) {
|
||||
switch (ast.getType()) {
|
||||
case TokenTypes.CTOR_DEF:
|
||||
case TokenTypes.METHOD_DEF:
|
||||
case TokenTypes.INSTANCE_INIT:
|
||||
|
|
@ -117,22 +117,22 @@ public abstract class AbstractComplexityCheck
|
|||
visitMethodDef();
|
||||
break;
|
||||
default:
|
||||
visitTokenHook(aAST);
|
||||
visitTokenHook(ast);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void leaveToken(DetailAST aAST)
|
||||
public void leaveToken(DetailAST ast)
|
||||
{
|
||||
switch (aAST.getType()) {
|
||||
switch (ast.getType()) {
|
||||
case TokenTypes.CTOR_DEF:
|
||||
case TokenTypes.METHOD_DEF:
|
||||
case TokenTypes.INSTANCE_INIT:
|
||||
case TokenTypes.STATIC_INIT:
|
||||
leaveMethodDef(aAST);
|
||||
leaveMethodDef(ast);
|
||||
break;
|
||||
default:
|
||||
leaveTokenHook(aAST);
|
||||
leaveTokenHook(ast);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -141,33 +141,33 @@ public abstract class AbstractComplexityCheck
|
|||
*/
|
||||
protected final BigInteger getCurrentValue()
|
||||
{
|
||||
return mCurrentValue;
|
||||
return currentValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the current value
|
||||
* @param aValue the new value
|
||||
* @param value the new value
|
||||
*/
|
||||
protected final void setCurrentValue(BigInteger aValue)
|
||||
protected final void setCurrentValue(BigInteger value)
|
||||
{
|
||||
mCurrentValue = aValue;
|
||||
currentValue = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Increments the current value by a specified amount.
|
||||
*
|
||||
* @param aBy the amount to increment by
|
||||
* @param by the amount to increment by
|
||||
*/
|
||||
protected final void incrementCurrentValue(BigInteger aBy)
|
||||
protected final void incrementCurrentValue(BigInteger by)
|
||||
{
|
||||
setCurrentValue(getCurrentValue().add(aBy));
|
||||
setCurrentValue(getCurrentValue().add(by));
|
||||
}
|
||||
|
||||
/** Push the current value on the stack */
|
||||
protected final void pushValue()
|
||||
{
|
||||
mValueStack.push(mCurrentValue);
|
||||
mCurrentValue = INITIAL_VALUE;
|
||||
valueStack.push(currentValue);
|
||||
currentValue = INITIAL_VALUE;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -175,8 +175,8 @@ public abstract class AbstractComplexityCheck
|
|||
*/
|
||||
protected final BigInteger popValue()
|
||||
{
|
||||
mCurrentValue = mValueStack.pop();
|
||||
return mCurrentValue;
|
||||
currentValue = valueStack.pop();
|
||||
return currentValue;
|
||||
}
|
||||
|
||||
/** Process the start of the method definition */
|
||||
|
|
@ -188,13 +188,13 @@ public abstract class AbstractComplexityCheck
|
|||
/**
|
||||
* Process the end of a method definition.
|
||||
*
|
||||
* @param aAST the token representing the method definition
|
||||
* @param ast the token representing the method definition
|
||||
*/
|
||||
private void leaveMethodDef(DetailAST aAST)
|
||||
private void leaveMethodDef(DetailAST ast)
|
||||
{
|
||||
final BigInteger max = BigInteger.valueOf(mMax);
|
||||
if (mCurrentValue.compareTo(max) > 0) {
|
||||
log(aAST, getMessageID(), mCurrentValue, max);
|
||||
final BigInteger bigIntegerMax = BigInteger.valueOf(max);
|
||||
if (currentValue.compareTo(bigIntegerMax) > 0) {
|
||||
log(ast, getMessageID(), currentValue, bigIntegerMax);
|
||||
}
|
||||
popValue();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -37,11 +37,11 @@ public final class BooleanExpressionComplexityCheck extends Check
|
|||
private static final int DEFAULT_MAX = 3;
|
||||
|
||||
/** Stack of contexts. */
|
||||
private final FastStack<Context> mContextStack = FastStack.newInstance();
|
||||
private final FastStack<Context> contextStack = FastStack.newInstance();
|
||||
/** Maximum allowed complexity. */
|
||||
private int mMax;
|
||||
private int max;
|
||||
/** Current context. */
|
||||
private Context mContext;
|
||||
private Context context;
|
||||
|
||||
/** Creates new instance of the check. */
|
||||
public BooleanExpressionComplexityCheck()
|
||||
|
|
@ -80,25 +80,25 @@ public final class BooleanExpressionComplexityCheck extends Check
|
|||
*/
|
||||
public int getMax()
|
||||
{
|
||||
return mMax;
|
||||
return max;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter for maximum allowed complexity.
|
||||
* @param aMax new maximum allowed complexity.
|
||||
* @param max new maximum allowed complexity.
|
||||
*/
|
||||
public void setMax(int aMax)
|
||||
public void setMax(int max)
|
||||
{
|
||||
mMax = aMax;
|
||||
this.max = max;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitToken(DetailAST aAST)
|
||||
public void visitToken(DetailAST ast)
|
||||
{
|
||||
switch (aAST.getType()) {
|
||||
switch (ast.getType()) {
|
||||
case TokenTypes.CTOR_DEF:
|
||||
case TokenTypes.METHOD_DEF:
|
||||
visitMethodDef(aAST);
|
||||
visitMethodDef(ast);
|
||||
break;
|
||||
case TokenTypes.EXPR:
|
||||
visitExpr();
|
||||
|
|
@ -108,23 +108,23 @@ public final class BooleanExpressionComplexityCheck extends Check
|
|||
case TokenTypes.LOR:
|
||||
case TokenTypes.BOR:
|
||||
case TokenTypes.BXOR:
|
||||
mContext.visitBooleanOperator();
|
||||
context.visitBooleanOperator();
|
||||
break;
|
||||
default:
|
||||
throw new IllegalStateException(aAST.toString());
|
||||
throw new IllegalStateException(ast.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void leaveToken(DetailAST aAST)
|
||||
public void leaveToken(DetailAST ast)
|
||||
{
|
||||
switch (aAST.getType()) {
|
||||
switch (ast.getType()) {
|
||||
case TokenTypes.CTOR_DEF:
|
||||
case TokenTypes.METHOD_DEF:
|
||||
leaveMethodDef();
|
||||
break;
|
||||
case TokenTypes.EXPR:
|
||||
leaveExpr(aAST);
|
||||
leaveExpr(ast);
|
||||
break;
|
||||
default:
|
||||
// Do nothing
|
||||
|
|
@ -133,35 +133,35 @@ public final class BooleanExpressionComplexityCheck extends Check
|
|||
|
||||
/**
|
||||
* Creates new context for a given method.
|
||||
* @param aAST a method we start to check.
|
||||
* @param ast a method we start to check.
|
||||
*/
|
||||
private void visitMethodDef(DetailAST aAST)
|
||||
private void visitMethodDef(DetailAST ast)
|
||||
{
|
||||
mContextStack.push(mContext);
|
||||
mContext = new Context(!CheckUtils.isEqualsMethod(aAST));
|
||||
contextStack.push(context);
|
||||
context = new Context(!CheckUtils.isEqualsMethod(ast));
|
||||
}
|
||||
|
||||
/** Removes old context. */
|
||||
private void leaveMethodDef()
|
||||
{
|
||||
mContext = mContextStack.pop();
|
||||
context = contextStack.pop();
|
||||
}
|
||||
|
||||
/** Creates and pushes new context. */
|
||||
private void visitExpr()
|
||||
{
|
||||
mContextStack.push(mContext);
|
||||
mContext = new Context((mContext == null) || mContext.isChecking());
|
||||
contextStack.push(context);
|
||||
context = new Context((context == null) || context.isChecking());
|
||||
}
|
||||
|
||||
/**
|
||||
* Restores previous context.
|
||||
* @param aAST expression we leave.
|
||||
* @param ast expression we leave.
|
||||
*/
|
||||
private void leaveExpr(DetailAST aAST)
|
||||
private void leaveExpr(DetailAST ast)
|
||||
{
|
||||
mContext.checkCount(aAST);
|
||||
mContext = mContextStack.pop();
|
||||
context.checkCount(ast);
|
||||
context = contextStack.pop();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -176,18 +176,18 @@ public final class BooleanExpressionComplexityCheck extends Check
|
|||
* Should we perform check in current context or not.
|
||||
* Usually false if we are inside equals() method.
|
||||
*/
|
||||
private final boolean mChecking;
|
||||
private final boolean checking;
|
||||
/** Count of boolean operators. */
|
||||
private int mCount;
|
||||
private int count;
|
||||
|
||||
/**
|
||||
* Creates new instance.
|
||||
* @param aChecking should we check in current context or not.
|
||||
* @param checking should we check in current context or not.
|
||||
*/
|
||||
public Context(boolean aChecking)
|
||||
public Context(boolean checking)
|
||||
{
|
||||
mChecking = aChecking;
|
||||
mCount = 0;
|
||||
this.checking = checking;
|
||||
count = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -196,26 +196,26 @@ public final class BooleanExpressionComplexityCheck extends Check
|
|||
*/
|
||||
public boolean isChecking()
|
||||
{
|
||||
return mChecking;
|
||||
return checking;
|
||||
}
|
||||
|
||||
/** Increases operator counter. */
|
||||
public void visitBooleanOperator()
|
||||
{
|
||||
++mCount;
|
||||
++count;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if we violates maximum allowed complexity.
|
||||
* @param aAST a node we check now.
|
||||
* @param ast a node we check now.
|
||||
*/
|
||||
public void checkCount(DetailAST aAST)
|
||||
public void checkCount(DetailAST ast)
|
||||
{
|
||||
if (mChecking && (mCount > getMax())) {
|
||||
final DetailAST parentAST = aAST.getParent();
|
||||
if (checking && (count > getMax())) {
|
||||
final DetailAST parentAST = ast.getParent();
|
||||
|
||||
log(parentAST.getLineNo(), parentAST.getColumnNo(),
|
||||
"booleanExpressionComplexity", mCount, getMax());
|
||||
"booleanExpressionComplexity", count, getMax());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -67,7 +67,7 @@ public class CyclomaticComplexityCheck
|
|||
}
|
||||
|
||||
@Override
|
||||
protected final void visitTokenHook(DetailAST aAST)
|
||||
protected final void visitTokenHook(DetailAST ast)
|
||||
{
|
||||
incrementCurrentValue(BigInteger.ONE);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,8 +26,8 @@ import com.puppycrawl.tools.checkstyle.api.TokenTypes;
|
|||
/**
|
||||
* This check calculates the Non Commenting Source Statements (NCSS) metric for
|
||||
* java source files and methods. The check adheres to the <a
|
||||
* href="http://www.kclee.com/clemens/java/javancss/">JavaNCSS specification
|
||||
* </a> and gives the same results as the JavaNCSS tool.
|
||||
* href="http://www.kclee.com/clemens/java/javancss/">JavnCSS specification
|
||||
* </a> and gives the same results as the JavnCSS tool.
|
||||
*
|
||||
* The NCSS-metric tries to determine complexity of methods, classes and files
|
||||
* by counting the non commenting lines. Roughly said this is (nearly)
|
||||
|
|
@ -47,16 +47,16 @@ public class JavaNCSSCheck extends Check
|
|||
private static final int METHOD_MAX_NCSS = 50;
|
||||
|
||||
/** maximum ncss for a complete source file */
|
||||
private int mFileMax = FILE_MAX_NCSS;
|
||||
private int fileMax = FILE_MAX_NCSS;
|
||||
|
||||
/** maximum ncss for a class */
|
||||
private int mClassMax = CLASS_MAX_NCSS;
|
||||
private int classMax = CLASS_MAX_NCSS;
|
||||
|
||||
/** maximum ncss for a method */
|
||||
private int mMethodMax = METHOD_MAX_NCSS;
|
||||
private int methodMax = METHOD_MAX_NCSS;
|
||||
|
||||
/** list containing the stacked counters */
|
||||
private FastStack<Counter> mCounters;
|
||||
private FastStack<Counter> counters;
|
||||
|
||||
@Override
|
||||
public int[] getDefaultTokens()
|
||||
|
|
@ -129,18 +129,18 @@ public class JavaNCSSCheck extends Check
|
|||
}
|
||||
|
||||
@Override
|
||||
public void beginTree(DetailAST aRootAST)
|
||||
public void beginTree(DetailAST rootAST)
|
||||
{
|
||||
mCounters = new FastStack<Counter>();
|
||||
counters = new FastStack<Counter>();
|
||||
|
||||
//add a counter for the file
|
||||
mCounters.push(new Counter());
|
||||
counters.push(new Counter());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitToken(DetailAST aAST)
|
||||
public void visitToken(DetailAST ast)
|
||||
{
|
||||
final int tokenType = aAST.getType();
|
||||
final int tokenType = ast.getType();
|
||||
|
||||
if ((TokenTypes.CLASS_DEF == tokenType)
|
||||
|| (TokenTypes.METHOD_DEF == tokenType)
|
||||
|
|
@ -149,114 +149,114 @@ public class JavaNCSSCheck extends Check
|
|||
|| (TokenTypes.INSTANCE_INIT == tokenType))
|
||||
{
|
||||
//add a counter for this class/method
|
||||
mCounters.push(new Counter());
|
||||
counters.push(new Counter());
|
||||
}
|
||||
|
||||
//check if token is countable
|
||||
if (isCountable(aAST)) {
|
||||
if (isCountable(ast)) {
|
||||
//increment the stacked counters
|
||||
for (final Counter c : mCounters) {
|
||||
for (final Counter c : counters) {
|
||||
c.increment();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void leaveToken(DetailAST aAST)
|
||||
public void leaveToken(DetailAST ast)
|
||||
{
|
||||
final int tokenType = aAST.getType();
|
||||
final int tokenType = ast.getType();
|
||||
if ((TokenTypes.METHOD_DEF == tokenType)
|
||||
|| (TokenTypes.CTOR_DEF == tokenType)
|
||||
|| (TokenTypes.STATIC_INIT == tokenType)
|
||||
|| (TokenTypes.INSTANCE_INIT == tokenType))
|
||||
{
|
||||
//pop counter from the stack
|
||||
final Counter counter = mCounters.pop();
|
||||
final Counter counter = counters.pop();
|
||||
|
||||
final int count = counter.getCount();
|
||||
if (count > mMethodMax) {
|
||||
log(aAST.getLineNo(), aAST.getColumnNo(), "ncss.method",
|
||||
count, mMethodMax);
|
||||
if (count > methodMax) {
|
||||
log(ast.getLineNo(), ast.getColumnNo(), "ncss.method",
|
||||
count, methodMax);
|
||||
}
|
||||
}
|
||||
else if (TokenTypes.CLASS_DEF == tokenType) {
|
||||
//pop counter from the stack
|
||||
final Counter counter = mCounters.pop();
|
||||
final Counter counter = counters.pop();
|
||||
|
||||
final int count = counter.getCount();
|
||||
if (count > mClassMax) {
|
||||
log(aAST.getLineNo(), aAST.getColumnNo(), "ncss.class",
|
||||
count, mClassMax);
|
||||
if (count > classMax) {
|
||||
log(ast.getLineNo(), ast.getColumnNo(), "ncss.class",
|
||||
count, classMax);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void finishTree(DetailAST aRootAST)
|
||||
public void finishTree(DetailAST rootAST)
|
||||
{
|
||||
//pop counter from the stack
|
||||
final Counter counter = mCounters.pop();
|
||||
final Counter counter = counters.pop();
|
||||
|
||||
final int count = counter.getCount();
|
||||
if (count > mFileMax) {
|
||||
log(aRootAST.getLineNo(), aRootAST.getColumnNo(), "ncss.file",
|
||||
count, mFileMax);
|
||||
if (count > fileMax) {
|
||||
log(rootAST.getLineNo(), rootAST.getColumnNo(), "ncss.file",
|
||||
count, fileMax);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the maximum ncss for a file.
|
||||
*
|
||||
* @param aFileMax
|
||||
* @param fileMax
|
||||
* the maximum ncss
|
||||
*/
|
||||
public void setFileMaximum(int aFileMax)
|
||||
public void setFileMaximum(int fileMax)
|
||||
{
|
||||
mFileMax = aFileMax;
|
||||
this.fileMax = fileMax;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the maximum ncss for a class.
|
||||
*
|
||||
* @param aClassMax
|
||||
* @param classMax
|
||||
* the maximum ncss
|
||||
*/
|
||||
public void setClassMaximum(int aClassMax)
|
||||
public void setClassMaximum(int classMax)
|
||||
{
|
||||
mClassMax = aClassMax;
|
||||
this.classMax = classMax;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the maximum ncss for a method.
|
||||
*
|
||||
* @param aMethodMax
|
||||
* @param methodMax
|
||||
* the maximum ncss
|
||||
*/
|
||||
public void setMethodMaximum(int aMethodMax)
|
||||
public void setMethodMaximum(int methodMax)
|
||||
{
|
||||
mMethodMax = aMethodMax;
|
||||
this.methodMax = methodMax;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a token is countable for the ncss metric
|
||||
*
|
||||
* @param aAST
|
||||
* @param ast
|
||||
* the AST
|
||||
* @return true if the token is countable
|
||||
*/
|
||||
private boolean isCountable(DetailAST aAST)
|
||||
private boolean isCountable(DetailAST ast)
|
||||
{
|
||||
boolean countable = true;
|
||||
|
||||
final int tokenType = aAST.getType();
|
||||
final int tokenType = ast.getType();
|
||||
|
||||
//check if an expression is countable
|
||||
if (TokenTypes.EXPR == tokenType) {
|
||||
countable = isExpressionCountable(aAST);
|
||||
countable = isExpressionCountable(ast);
|
||||
}
|
||||
//check if an variable definition is countable
|
||||
else if (TokenTypes.VARIABLE_DEF == tokenType) {
|
||||
countable = isVariableDefCountable(aAST);
|
||||
countable = isVariableDefCountable(ast);
|
||||
}
|
||||
return countable;
|
||||
}
|
||||
|
|
@ -264,21 +264,21 @@ public class JavaNCSSCheck extends Check
|
|||
/**
|
||||
* Checks if a variable definition is countable.
|
||||
*
|
||||
* @param aAST the AST
|
||||
* @param ast the AST
|
||||
* @return true if the variable definition is countable, false otherwise
|
||||
*/
|
||||
private boolean isVariableDefCountable(DetailAST aAST)
|
||||
private boolean isVariableDefCountable(DetailAST ast)
|
||||
{
|
||||
boolean countable = false;
|
||||
|
||||
//count variable defs only if they are direct child to a slist or
|
||||
// object block
|
||||
final int parentType = aAST.getParent().getType();
|
||||
final int parentType = ast.getParent().getType();
|
||||
|
||||
if ((TokenTypes.SLIST == parentType)
|
||||
|| (TokenTypes.OBJBLOCK == parentType))
|
||||
{
|
||||
final DetailAST prevSibling = aAST.getPreviousSibling();
|
||||
final DetailAST prevSibling = ast.getPreviousSibling();
|
||||
|
||||
//is countable if no previous sibling is found or
|
||||
//the sibling is no COMMA.
|
||||
|
|
@ -294,17 +294,17 @@ public class JavaNCSSCheck extends Check
|
|||
/**
|
||||
* Checks if an expression is countable for the ncss metric.
|
||||
*
|
||||
* @param aAST the AST
|
||||
* @param ast the AST
|
||||
* @return true if the expression is countable, false otherwise
|
||||
*/
|
||||
private boolean isExpressionCountable(DetailAST aAST)
|
||||
private boolean isExpressionCountable(DetailAST ast)
|
||||
{
|
||||
boolean countable = true;
|
||||
|
||||
//count expressions only if they are direct child to a slist (method
|
||||
// body, for loop...)
|
||||
//or direct child of label,if,else,do,while,for
|
||||
final int parentType = aAST.getParent().getType();
|
||||
final int parentType = ast.getParent().getType();
|
||||
switch (parentType) {
|
||||
case TokenTypes.SLIST :
|
||||
case TokenTypes.LABELED_STAT :
|
||||
|
|
@ -314,7 +314,7 @@ public class JavaNCSSCheck extends Check
|
|||
case TokenTypes.LITERAL_IF :
|
||||
case TokenTypes.LITERAL_ELSE :
|
||||
//don't count if or loop conditions
|
||||
final DetailAST prevSibling = aAST.getPreviousSibling();
|
||||
final DetailAST prevSibling = ast.getPreviousSibling();
|
||||
countable = (prevSibling == null)
|
||||
|| (TokenTypes.LPAREN != prevSibling.getType());
|
||||
break;
|
||||
|
|
@ -326,21 +326,25 @@ public class JavaNCSSCheck extends Check
|
|||
}
|
||||
|
||||
/**
|
||||
<<<<<<< HEAD
|
||||
* @author Lars Ködderitzsch
|
||||
=======
|
||||
* @author Lars K<EFBFBD>dderitzsch
|
||||
>>>>>>> ca5a59d... Prefixes, Metrics, #512
|
||||
*
|
||||
* Class representing a counter,
|
||||
*/
|
||||
private static class Counter
|
||||
{
|
||||
/** the counters internal integer */
|
||||
private int mIvCount;
|
||||
private int ivCount;
|
||||
|
||||
/**
|
||||
* Increments the counter.
|
||||
*/
|
||||
public void increment()
|
||||
{
|
||||
mIvCount++;
|
||||
ivCount++;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -350,7 +354,7 @@ public class JavaNCSSCheck extends Check
|
|||
*/
|
||||
public int getCount()
|
||||
{
|
||||
return mIvCount;
|
||||
return ivCount;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -68,9 +68,9 @@ public final class NPathComplexityCheck extends AbstractComplexityCheck
|
|||
}
|
||||
|
||||
@Override
|
||||
public void visitToken(DetailAST aAST)
|
||||
public void visitToken(DetailAST ast)
|
||||
{
|
||||
switch (aAST.getType()) {
|
||||
switch (ast.getType()) {
|
||||
case TokenTypes.LITERAL_WHILE:
|
||||
case TokenTypes.LITERAL_DO:
|
||||
case TokenTypes.LITERAL_FOR:
|
||||
|
|
@ -86,14 +86,14 @@ public final class NPathComplexityCheck extends AbstractComplexityCheck
|
|||
visitAddingConditional();
|
||||
break;
|
||||
default:
|
||||
super.visitToken(aAST);
|
||||
super.visitToken(ast);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void leaveToken(DetailAST aAST)
|
||||
public void leaveToken(DetailAST ast)
|
||||
{
|
||||
switch (aAST.getType()) {
|
||||
switch (ast.getType()) {
|
||||
case TokenTypes.LITERAL_WHILE:
|
||||
case TokenTypes.LITERAL_DO:
|
||||
case TokenTypes.LITERAL_FOR:
|
||||
|
|
@ -109,7 +109,7 @@ public final class NPathComplexityCheck extends AbstractComplexityCheck
|
|||
leaveAddingConditional();
|
||||
break;
|
||||
default:
|
||||
super.leaveToken(aAST);
|
||||
super.leaveToken(ast);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue