diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/AuditEvent.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/AuditEvent.java index af8cf2f05..3a22a7198 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/AuditEvent.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/AuditEvent.java @@ -18,6 +18,8 @@ //////////////////////////////////////////////////////////////////////////////// package com.puppycrawl.tools.checkstyle; +import com.puppycrawl.tools.checkstyle.api.LocalizedMessage; + import java.util.EventObject; /** diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/Checker.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/Checker.java index d09877570..3252ea5f4 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/Checker.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/Checker.java @@ -31,6 +31,7 @@ import java.util.HashSet; import java.util.Iterator; import java.util.Locale; import org.apache.regexp.RESyntaxException; +import com.puppycrawl.tools.checkstyle.api.LocalizedMessage; /** * This class provides the functionality to check a set of files. diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/CommentManager.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/CommentManager.java index 6d0a79aa3..a772fa1e4 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/CommentManager.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/CommentManager.java @@ -25,18 +25,18 @@ import java.util.TreeMap; /** * Checks can query the CommentManager if they want to analyse comments. - *
- * The AST tree does not contain comments as they can occur at arbitrary places. - * Adding them to the AST tree would make it very hard to write checks, as you would - * always have to beware of comment nodes. - *
- *- * Instead, during Java parsing comments are registered with the CommentManager, and - * Checks that need to analyse comments can use the CommentManager to access that data. - *
- *- * Sample Checks that use the CommentManager are the to-do check and the javadoc check. - *
+ * + *The AST tree does not contain comments as they can occur at arbitrary + * places. Adding them to the AST tree would make it very hard to write + * checks, as you would always have to beware of comment nodes.
+ * + *Instead, during Java parsing comments are registered with the + * CommentManager, and Checks that need to analyse comments can use the + * CommentManager to access that data.
+ * + *Sample Checks that use the CommentManager are the to-do check and the + * javadoc check.
+ * * @author Lars Kühne */ public class CommentManager @@ -150,8 +150,8 @@ public class CommentManager /** * An iterator over all comments. - * @return an iterator that contains entries of type String (CPP style comments) - * or String[] (C style comments) + * @return an iterator that contains entries of type String (CPP style + * comments) or String[] (C style comments) */ public Iterator iterator() { diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/Configuration.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/Configuration.java index c7f73eda1..c762f0973 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/Configuration.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/Configuration.java @@ -39,6 +39,7 @@ import java.util.TreeSet; import org.apache.regexp.RE; import org.apache.regexp.RESyntaxException; +import com.puppycrawl.tools.checkstyle.api.Utils; /** * Represents the configuration that checkstyle uses when checking. The diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/TreeWalker.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/TreeWalker.java index a57db6357..31cab7779 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/TreeWalker.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/TreeWalker.java @@ -20,6 +20,7 @@ package com.puppycrawl.tools.checkstyle; import com.puppycrawl.tools.checkstyle.api.Check; import com.puppycrawl.tools.checkstyle.api.DetailAST; +import com.puppycrawl.tools.checkstyle.api.LocalizedMessages; import java.util.Map; import java.util.HashMap; @@ -46,9 +47,11 @@ class TreeWalker private static final Map TOKEN_VALUE_TO_NAME = new HashMap(); /** maps from token name to checks */ - private Map mTokenToChecks = new HashMap(); + private final Map mTokenToChecks = new HashMap(); /** all the registered checks */ - private Set mAllChecks = new HashSet(); + private final Set mAllChecks = new HashSet(); + /** collects the error messages */ + private final LocalizedMessages mMessages; // initialise the constants static { @@ -75,6 +78,11 @@ class TreeWalker } + public TreeWalker(LocalizedMessages aMessages) + { + mMessages = aMessages; + } + /** * Returns the name of a token for a given ID. * @param aID the ID of the token name to get @@ -143,12 +151,14 @@ class TreeWalker * @param aLines the lines of the file the AST was generated from * @param aFilename the file name of the file the AST was generated from */ - void walk(DetailAST aAST, String[] aLines, String aFilename) + LocalizedMessages walk(DetailAST aAST, String[] aLines, String aFilename) { + mMessages.reset(); notifyBegin(aLines, aFilename); aAST.setParent(null); process(aAST); notifyEnd(); + return mMessages; } /** diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/Verifier.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/Verifier.java index fbf5581d9..4fe01210f 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/Verifier.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/Verifier.java @@ -31,6 +31,9 @@ import java.util.Stack; import org.apache.regexp.RE; import org.apache.regexp.RESyntaxException; +import com.puppycrawl.tools.checkstyle.api.LocalizedMessages; +import com.puppycrawl.tools.checkstyle.api.Utils; +import com.puppycrawl.tools.checkstyle.api.LocalizedMessage; /** * Verifier of Java rules. Each rule verifier takes the form of diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/api/Check.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/api/Check.java index 7b5e54a33..8fcbbd6d7 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/api/Check.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/api/Check.java @@ -35,6 +35,11 @@ public abstract class Check /** the global context for the check */ private Map mGlobalContext; + /** + * the object for collecting messages. decided to not put in the global + * context for performance and ease of use. + */ + private LocalizedMessages mMessages; /** the context for the check across an AST */ private Map mTreeContext; /** the context for a check across a token. */ @@ -59,11 +64,20 @@ public abstract class Check /** * Set the global context for the check. - * @param aGlobalContext the global context + * @param aContext the context */ - public void setGlobalContext(Map aGlobalContext) + public void setGlobalContext(Map aContext) { - mGlobalContext = aGlobalContext; + mGlobalContext = aContext; + } + + /** + * Set the global object used to collect messages. + * @param aMessages the messages to log with + */ + public void setMessages(LocalizedMessages aMessages) + { + mMessages = aMessages; } /** diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/LocalizedMessage.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/api/LocalizedMessage.java similarity index 92% rename from src/checkstyle/com/puppycrawl/tools/checkstyle/LocalizedMessage.java rename to src/checkstyle/com/puppycrawl/tools/checkstyle/api/LocalizedMessage.java index 77a870720..24225b530 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/LocalizedMessage.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/api/LocalizedMessage.java @@ -16,7 +16,7 @@ // 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; +package com.puppycrawl.tools.checkstyle.api; import java.util.ResourceBundle; import java.util.Locale; @@ -59,7 +59,10 @@ public class LocalizedMessage * @param aKey the key to locate the translation * @param aArgs arguments for the translation */ - LocalizedMessage(int aLineNo, int aColNo, String aKey, Object[] aArgs) + public LocalizedMessage(int aLineNo, + int aColNo, + String aKey, + Object[] aArgs) { mLineNo = aLineNo; mColNo = aColNo; @@ -75,7 +78,7 @@ public class LocalizedMessage * @param aKey the key to locate the translation * @param aArgs arguments for the translation */ - LocalizedMessage(int aLineNo, String aKey, Object[] aArgs) + public LocalizedMessage(int aLineNo, String aKey, Object[] aArgs) { this(aLineNo, 0, aKey, aArgs); } @@ -106,7 +109,7 @@ public class LocalizedMessage } /** @param aLocale the locale to use for localization **/ - static void setLocale(Locale aLocale) + public static void setLocale(Locale aLocale) { sLocale = aLocale; } diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/LocalizedMessages.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/api/LocalizedMessages.java similarity index 87% rename from src/checkstyle/com/puppycrawl/tools/checkstyle/LocalizedMessages.java rename to src/checkstyle/com/puppycrawl/tools/checkstyle/api/LocalizedMessages.java index be562b46e..de0209a32 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/LocalizedMessages.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/api/LocalizedMessages.java @@ -16,7 +16,7 @@ // 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; +package com.puppycrawl.tools.checkstyle.api; import java.util.Collections; import java.util.ArrayList; @@ -26,7 +26,7 @@ import java.util.ArrayList; * @author Oliver Burn * @version 1.0 */ -class LocalizedMessages +public class LocalizedMessages { /** contains the messages logged **/ private final ArrayList mMessages = new ArrayList(); @@ -40,19 +40,19 @@ class LocalizedMessages * * @param aTabWidth the tab width to calculate columns with */ - LocalizedMessages(int aTabWidth) + public LocalizedMessages(int aTabWidth) { mTabWidth = aTabWidth; } /** @param aLines the lines to record messages against **/ - void setLines(String[] aLines) + public void setLines(String[] aLines) { mLines = aLines; } /** @return the logged messages **/ - LocalizedMessage[] getMessages() + public LocalizedMessage[] getMessages() { Collections.sort(mMessages); return (LocalizedMessage[]) @@ -60,7 +60,7 @@ class LocalizedMessages } /** Reset the object **/ - void reset() + public void reset() { mMessages.clear(); mLines = null; @@ -70,7 +70,7 @@ class LocalizedMessages * Logs a message to be reported * @param aMsg the message to log **/ - void add(LocalizedMessage aMsg) + public void add(LocalizedMessage aMsg) { mMessages.add(aMsg); } @@ -82,7 +82,7 @@ class LocalizedMessages * @param aKey key to locale message format * @param aArgs arguments for message */ - void add(int aLineNo, String aKey, Object[] aArgs) + public void add(int aLineNo, String aKey, Object[] aArgs) { add(new LocalizedMessage(aLineNo, 0, aKey, aArgs)); } @@ -93,7 +93,7 @@ class LocalizedMessages * @param aLineNo line number to associate with the message * @param aKey key to locale message format */ - void add(int aLineNo, String aKey) + public void add(int aLineNo, String aKey) { add(aLineNo, aKey, new Object[0]); } @@ -105,7 +105,7 @@ class LocalizedMessages * @param aKey key to locale message format * @param aArg0 first argument */ - void add(int aLineNo, String aKey, Object aArg0) + public void add(int aLineNo, String aKey, Object aArg0) { add(aLineNo, aKey, new Object[] {aArg0}); } @@ -118,7 +118,7 @@ class LocalizedMessages * @param aArg0 first argument * @param aArg1 second argument */ - void add(int aLineNo, String aKey, Object aArg0, Object aArg1) + public void add(int aLineNo, String aKey, Object aArg0, Object aArg1) { add(aLineNo, aKey, new Object[] {aArg0, aArg1}); } @@ -132,7 +132,7 @@ class LocalizedMessages * @param aArg1 second argument * @param aArg2 third argument */ - void add(int aLineNo, String aKey, + public void add(int aLineNo, String aKey, Object aArg0, Object aArg1, Object aArg2) { add(aLineNo, aKey, new Object[] {aArg0, aArg1, aArg2}); @@ -146,7 +146,7 @@ class LocalizedMessages * @param aKey key to locale message format * @param aArgs arguments for message */ - void add(int aLineNo, int aColNo, String aKey, Object[] aArgs) + public void add(int aLineNo, int aColNo, String aKey, Object[] aArgs) { final int col = 1 + Utils.lengthExpandedTabs( mLines[aLineNo - 1], aColNo, mTabWidth); @@ -160,7 +160,7 @@ class LocalizedMessages * @param aColNo column number to associate with the message * @param aKey key to locale message format */ - void add(int aLineNo, int aColNo, String aKey) + public void add(int aLineNo, int aColNo, String aKey) { add(aLineNo, aColNo, aKey, new Object[0]); } @@ -173,7 +173,7 @@ class LocalizedMessages * @param aKey key to locale message format * @param aArg0 first argument */ - void add(int aLineNo, int aColNo, String aKey, Object aArg0) + public void add(int aLineNo, int aColNo, String aKey, Object aArg0) { add(aLineNo, aColNo, aKey, new Object[] {aArg0}); } @@ -187,7 +187,7 @@ class LocalizedMessages * @param aArg0 first argument * @param aArg1 second argument */ - void add(int aLineNo, int aColNo, String aKey, + public void add(int aLineNo, int aColNo, String aKey, Object aArg0, Object aArg1) { add(aLineNo, aColNo, aKey, new Object[] {aArg0, aArg1}); diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/Utils.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/api/Utils.java similarity index 87% rename from src/checkstyle/com/puppycrawl/tools/checkstyle/Utils.java rename to src/checkstyle/com/puppycrawl/tools/checkstyle/api/Utils.java index a9805785a..18ff17e04 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/Utils.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/api/Utils.java @@ -16,7 +16,7 @@ // 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; +package com.puppycrawl.tools.checkstyle.api; import java.util.HashMap; import java.util.Iterator; @@ -33,7 +33,7 @@ import org.apache.regexp.RESyntaxException; * @author Oliver Burn * @version 1.0 */ -final class Utils +public final class Utils { /** Map of all created regular expressions **/ private static final Map CREATED_RES = new HashMap(); @@ -51,7 +51,7 @@ final class Utils * @param aLine the line to check * @return whether there is only whitespace */ - static boolean whitespaceBefore(int aIndex, String aLine) + public static boolean whitespaceBefore(int aIndex, String aLine) { for (int i = 0; i < aIndex; i++) { if (!Character.isWhitespace(aLine.charAt(i))) { @@ -68,7 +68,7 @@ final class Utils * @param aLine the string to process * @return the length of the string ignoring all trailing whitespace **/ - static int lengthMinusTrailingWhitespace(String aLine) + public static int lengthMinusTrailingWhitespace(String aLine) { int len = aLine.length(); for (int i = len - 1; i >= 0; i--) { @@ -89,7 +89,9 @@ final class Utils * @param aTabWidth the distance betweeen tab stop position. * @return the length of aString.substring(0, aToIdx) with tabs expanded. */ - static int lengthExpandedTabs(String aString, int aToIdx, int aTabWidth) + public static int lengthExpandedTabs(String aString, + int aToIdx, + int aTabWidth) { int len = 0; for (int idx = 0; idx < aToIdx; idx++) { @@ -111,7 +113,7 @@ final class Utils * @param aPattern the regular expression pattern * @throws RESyntaxException an invalid pattern was supplied **/ - static RE getRE(String aPattern) + public static RE getRE(String aPattern) throws RESyntaxException { RE retVal = (RE) CREATED_RES.get(aPattern); @@ -128,7 +130,9 @@ final class Utils * @param aKey the key to add the property under * @param aValue if not null, then the value to add the property with */ - static void addObjectString(Properties aProps, String aKey, Object aValue) + public static void addObjectString(Properties aProps, + String aKey, + Object aValue) { if (aValue != null) { aProps.put(aKey, aValue.toString()); @@ -142,7 +146,7 @@ final class Utils * @param aKey the key to add the property under * @param aSet the Set to encode */ - static void addSetString(Properties aProps, String aKey, Set aSet) + public static void addSetString(Properties aProps, String aKey, Set aSet) { final StringBuffer buf = new StringBuffer(); final Iterator it = aSet.iterator(); diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/AvoidStarImport.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/AvoidStarImport.java index 9e846a2a9..72f7a881b 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/AvoidStarImport.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/AvoidStarImport.java @@ -24,13 +24,13 @@ import com.puppycrawl.tools.checkstyle.api.DetailAST; /** * Check that finds import statement that use the * notation. - *- * Rationale: Importing all classes from a package leads to tight coupling between - * packages and might lead to problems when a new version of a library introduces - * name clashes. + * + *
Rationale: Importing all classes from a package leads to tight coupling + * between packages and might lead to problems when a new version of a library + * introduces name clashes. * * @author Oliver Burn - * @version $Id: AvoidStarImport.java,v 1.2 2002-09-15 16:55:59 lkuehne Exp $ + * @version $Id: AvoidStarImport.java,v 1.3 2002-09-17 12:33:29 oburn Exp $ */ public class AvoidStarImport extends ImportCheck { diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/ImportCheck.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/ImportCheck.java index f342df625..f873c8f11 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/ImportCheck.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/ImportCheck.java @@ -24,7 +24,10 @@ import com.puppycrawl.tools.checkstyle.api.DetailAST; import com.puppycrawl.tools.checkstyle.JavaTokenTypes; /** - * Abstract base class that provides functionality that is used in import checks. + * Abstract base class that provides functionality that is used in import + * checks. + * @author Oliver Burn + * @version 1.0 */ public abstract class ImportCheck extends Check diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/UnusedImportsCheck.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/UnusedImportsCheck.java index 068eb47fc..e6e0e65f4 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/UnusedImportsCheck.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/UnusedImportsCheck.java @@ -19,7 +19,6 @@ package com.puppycrawl.tools.checkstyle.checks; -import com.puppycrawl.tools.checkstyle.checks.ImportCheck; import com.puppycrawl.tools.checkstyle.JavaTokenTypes; import com.puppycrawl.tools.checkstyle.api.DetailAST; @@ -29,6 +28,8 @@ import java.util.Iterator; /** * Checks for unused import statements. + * @author Oliver Burn + * @version 1.0 */ public class UnusedImportsCheck extends ImportCheck { diff --git a/src/tests/com/puppycrawl/tools/checkstyle/UtilsTest.java b/src/tests/com/puppycrawl/tools/checkstyle/UtilsTest.java index 1e29694c5..ab866d460 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/UtilsTest.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/UtilsTest.java @@ -2,6 +2,7 @@ package com.puppycrawl.tools.checkstyle; import junit.framework.TestCase; import org.apache.regexp.RE; +import com.puppycrawl.tools.checkstyle.api.Utils; public class UtilsTest extends TestCase