From 7e570084e62eda58f3c4fa7e75e26a1a41d255cf Mon Sep 17 00:00:00 2001 From: Oliver Burn Date: Sat, 21 Sep 2002 13:49:01 +0000 Subject: [PATCH] Nearly for the first test to run. Having problems with loading resource bundles. --- .../tools/checkstyle/TreeWalker.java | 6 +- .../checkstyle/api/LocalizedMessage.java | 2 + .../checkstyle/checks/AvoidStarImport.java | 4 +- .../checkstyle/checks/messages.properties | 61 ++++++++++++ .../puppycrawl/tools/checkstyle/java_new.g | 2 +- .../tools/checkstyle/AvoidStarImportTest.java | 94 +++++++++++++++++++ .../tools/checkstyle/TreeWalkerTest.java | 2 +- 7 files changed, 164 insertions(+), 7 deletions(-) create mode 100644 src/checkstyle/com/puppycrawl/tools/checkstyle/checks/messages.properties create mode 100644 src/tests/com/puppycrawl/tools/checkstyle/AvoidStarImportTest.java diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/TreeWalker.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/TreeWalker.java index 31cab7779..058802b60 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/TreeWalker.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/TreeWalker.java @@ -104,6 +104,7 @@ class TreeWalker */ void registerCheck(Check aCheck, CheckConfiguration aConfig) { + aCheck.setMessages(mMessages); if (!aConfig.getTokens().isEmpty()) { final Iterator it = aConfig.getTokens().iterator(); while (it.hasNext()) { @@ -151,14 +152,13 @@ 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 */ - LocalizedMessages walk(DetailAST aAST, String[] aLines, String aFilename) + void walk(DetailAST aAST, String[] aLines, String aFilename) { mMessages.reset(); notifyBegin(aLines, aFilename); aAST.setParent(null); process(aAST); notifyEnd(); - return mMessages; } /** @@ -173,8 +173,8 @@ class TreeWalker while (it.hasNext()) { final Check check = (Check) it.next(); final HashMap treeContext = new HashMap(); - check.setFilename(aFilename); check.setTreeContext(treeContext); + check.setFilename(aFilename); check.setLines(aLines); check.beginTree(); } diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/api/LocalizedMessage.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/api/LocalizedMessage.java index 9bcae5f28..6ce55977c 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/api/LocalizedMessage.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/api/LocalizedMessage.java @@ -96,6 +96,7 @@ public class LocalizedMessage /** @return the translated message **/ public String getMessage() { + System.err.println("LocalizedMessage.getMessage"); try { // PERF: Very simple approach - wait for performance problems. // Important to use the default class loader, and not the one in the @@ -108,6 +109,7 @@ public class LocalizedMessage return MessageFormat.format(pattern, mArgs); } catch (MissingResourceException ex) { + ex.printStackTrace(); // If the Check author didn't provide i18n resource bundles // and logs error messages directly, this will return // the author's original message diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/AvoidStarImport.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/AvoidStarImport.java index 72f7a881b..a7d20a481 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/AvoidStarImport.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/AvoidStarImport.java @@ -30,7 +30,7 @@ import com.puppycrawl.tools.checkstyle.api.DetailAST; * introduces name clashes. * * @author Oliver Burn - * @version $Id: AvoidStarImport.java,v 1.3 2002-09-17 12:33:29 oburn Exp $ + * @version 1.0 */ public class AvoidStarImport extends ImportCheck { @@ -43,7 +43,7 @@ public class AvoidStarImport extends ImportCheck { final String name = getImportText(aAST); if ((name != null) && name.endsWith(".*")) { - log(aAST.getLineNo(), "Avoid STAR import - " + name); + log(aAST.getLineNo(), "import.avoidStar"); } } } diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/messages.properties b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/messages.properties new file mode 100644 index 000000000..924124ef2 --- /dev/null +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/messages.properties @@ -0,0 +1,61 @@ +maxLineLen=Line is longer than {0,number,integer} characters. +containsTab=Line contains a tab character. + +maxLen.file=File length is {0,number,integer} lines (max allowed is {1,number,integer}). +maxLen.method=Method length is {0,number,integer} lines (max allowed is {1,number,integer}). +maxLen.constructor=Constructor length is {0,number,integer} lines (max allowed is {1,number,integer}). + +redundantModifier=Redundant ''{0}'' modifier. + +name.invalidPattern=Name ''{0}'' must match pattern ''{1}''. + +javadoc.missing=Missing a Javadoc comment. +javadoc.unusedTagGeneral=Unused Javadoc tag. +javadoc.unusedTag=Unused {0} tag for ''{1}''. +javadoc.expectedTag=Expected {0} tag for ''{1}''. +javadoc.return.duplicate=Duplicate @return tag. +javadoc.return.expected=Expected an @return tag. +javadoc.classInfo=Unable to get class information for {0} tag ''{1}''. +javadoc.packageHtml=Missing package documentation file. + +type.missingTag=Type Javadoc comment is missing an {0} tag. +variable.notPrivate=Variable ''{0}'' must be private and have accessor methods. + +needBraces=''{0}'' construct must use '''{}'''s. + +ws.notPreceeded=''{0}'' is not preceeded with whitespace. +ws.notFollowed=''{0}'' is not followed by whitespace. +ws.preceeded=''{0}'' is preceeded with whitespace. +ws.followed=''{0}'' is followed by whitespace. + +line.same=''{0}'' should be on the same line. +line.alone=''{0}'' should be alone on a line. +line.new=''{0}'' should be on a new line. +line.previous=''{0}'' should be on the previous line. + +todo.match=Comment matches to-do format ''{0}''. + +import.duplicate=Duplicate import to line {0,number,integer}. +import.avoidStar=Avoid using the ''.*'' form of import. +import.lang=Redundant import from the java.lang package. +import.same=Redundant import from the same package. +import.unused=Unused import - {0}. +import.illegal=Import from illegal package - {0}. + +instantiation.avoid=Avoid instantiation of {0}. + +upperEll=Should use uppercase ''L''. + +header.missing=Missing a header - not enough lines in file. +header.mismatch=Line does not match expected header line of ''{0}''. + +regexp.parseError=Unable to parse regular expression ''{0}''. + +mod.order=''{0}'' modifier out of order with the JLS suggestions. + +block.noStmt=Must have at least one statement. +block.empty=Empty {0} block. + +maxParam=More than {0,number,integer} parameters. + +field.unused=''{0}'' is an unused field. \ No newline at end of file diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/java_new.g b/src/checkstyle/com/puppycrawl/tools/checkstyle/java_new.g index e0b9bb67a..ad020d911 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/java_new.g +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/java_new.g @@ -942,7 +942,7 @@ options { void setCommentManager(CommentManager aCommentManager) { - mCommentManager = aCommentManager; + mCommentManager = aCommentManager; } } diff --git a/src/tests/com/puppycrawl/tools/checkstyle/AvoidStarImportTest.java b/src/tests/com/puppycrawl/tools/checkstyle/AvoidStarImportTest.java new file mode 100644 index 000000000..8cf94b6fc --- /dev/null +++ b/src/tests/com/puppycrawl/tools/checkstyle/AvoidStarImportTest.java @@ -0,0 +1,94 @@ +/* + * Created by IntelliJ IDEA. + * User: oliver.burn + * Date: 20/09/2002 + * Time: 17:18:38 + * To change template for new class use + * Code Style | Class Templates options (Tools | IDE Options). + */ +package com.puppycrawl.tools.checkstyle; + +import junit.framework.TestCase; +import com.puppycrawl.tools.checkstyle.api.LocalizedMessages; +import com.puppycrawl.tools.checkstyle.api.Check; +import com.puppycrawl.tools.checkstyle.api.DetailAST; +import com.puppycrawl.tools.checkstyle.TreeWalker; +import com.puppycrawl.tools.checkstyle.checks.AvoidStarImport; + +import java.io.IOException; +import java.io.LineNumberReader; +import java.io.FileReader; +import java.io.BufferedReader; +import java.io.Reader; +import java.util.ArrayList; + +public class AvoidStarImportTest +extends TestCase +{ + public AvoidStarImportTest(String aName) + { + super(aName); + } + + public void testIt() + throws Exception + { + final LocalizedMessages msgs = new LocalizedMessages(8); + final TreeWalker walker = new TreeWalker(msgs); + final CheckConfiguration config = new CheckConfiguration(); + config.setClassname(AvoidStarImport.class.getName()); + final Check c = config.createInstance( + Thread.currentThread().getContextClassLoader()); + walker.registerCheck(c, config); + final String fname = CheckerTest.getPath("InputImport.java"); + final String[] lines = getLines(fname); + walker.walk(getAST(fname, lines), lines, fname); + assertEquals(msgs.getMessages().length, 3); + assertEquals(msgs.getMessages()[0].getLineNo(), 7); + assertEquals(msgs.getMessages()[1].getLineNo(), 9); + assertEquals(msgs.getMessages()[2].getLineNo(), 10); + assertEquals(msgs.getMessages()[0].getMessage(), "help me Ronda"); + } + + /** + * Loads the contents of a file in a String array. + * @return the lines in the file + * @param aFileName the name of the file to load + * @throws java.io.IOException error occurred + **/ + private String[] getLines(String aFileName) + throws IOException + { + final LineNumberReader lnr = + new LineNumberReader(new FileReader(aFileName)); + final ArrayList lines = new ArrayList(); + while (true) { + final String l = lnr.readLine(); + if (l == null) { + break; + } + lines.add(l); + } + + return (String[]) lines.toArray(new String[0]); + } + + private static DetailAST getAST(String aFilename, String[] aLines) + throws Exception + { + final Reader r = new BufferedReader(new FileReader(aFilename)); + // Create a scanner that reads from the input stream passed to us + JavaLexer lexer = new JavaLexer(r); + lexer.setFilename(aFilename); + lexer.setCommentManager(new CommentManager(aLines)); + + // Create a parser that reads from the scanner + JavaRecognizer parser = new JavaRecognizer(lexer); + parser.setFilename(aFilename); + parser.setASTNodeClass(DetailAST.class.getName()); + + // start parsing at the compilationUnit rule + parser.compilationUnit(); + return (DetailAST) parser.getAST(); + } +} diff --git a/src/tests/com/puppycrawl/tools/checkstyle/TreeWalkerTest.java b/src/tests/com/puppycrawl/tools/checkstyle/TreeWalkerTest.java index 765c541e4..01e82b8f7 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/TreeWalkerTest.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/TreeWalkerTest.java @@ -21,7 +21,7 @@ public class TreeWalkerTest public void testCreate() { - new TreeWalker(new LocalizedMessages(8)); + new TreeWalker(new LocalizedMessages(0)); assertTrue(true); } }