From 030798a11d2d21330cbdddd159cdc1824492157a Mon Sep 17 00:00:00 2001 From: Oliver Burn Date: Wed, 31 Oct 2001 12:42:19 +0000 Subject: [PATCH] incorporate performance patch --- ChangeLog | 16 ++- .../puppycrawl/tools/checkstyle/Checker.java | 14 ++- .../tools/checkstyle/StringArrayReader.java | 107 ++++++++++++++++++ 3 files changed, 126 insertions(+), 11 deletions(-) create mode 100644 src/checkstyle/com/puppycrawl/tools/checkstyle/StringArrayReader.java diff --git a/ChangeLog b/ChangeLog index e864e7635..c6f18018d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,10 +1,16 @@ -2001-08-17 Oliver Burn +2001-10-31 Oliver Burn + + * src/checkstyle/com/puppycrawl/tools/checkstyle/Checker.java, src/checkstyle/com/puppycrawl/tools/checkstyle/StringArrayReader.java: + Included performance patch from Lars Kuhne [lars _DOT_ kuehne _AT_ + planet-interkom _DOT_ de]. Really nice performance increase. + +2001-08-17 Oliver Burn * src/tests/com/puppycrawl/tools/checkstyle/CheckerTest.java: Automagically locate the files from the classpath. Neat patch from Stephane Bailliez [stephane DOT bailliez AT wanadoo DOT fr]. -2001-08-06 Oliver Burn +2001-08-06 Oliver Burn * src/checkstyle/com/puppycrawl/tools/checkstyle/Main.java: Now destroys the checker. @@ -15,7 +21,7 @@ * src/checkstyle/com/puppycrawl/tools/checkstyle/PropertyCacheFile.java: First implementation -2001-08-05 Oliver Burn +2001-08-05 Oliver Burn * src/checkstyle/com/puppycrawl/tools/checkstyle/Configuration.java: Now support boolean property values of "yes", "on", "true" and ignore @@ -24,7 +30,7 @@ * src/checkstyle/com/puppycrawl/tools/checkstyle/VerifierImpl.java, src/checkstyle/com/puppycrawl/tools/checkstyle/Defn.java, src/checkstyle/com/puppycrawl/tools/checkstyle/Configuration.java: Added support for public member patterns. -2001-07-02 Oliver Burn +2001-07-02 Oliver Burn * docs/index.html: Added that can ignore checking braces. @@ -83,7 +89,7 @@ * build.xml: Added checkstyle.import target. -2001-06-29 Oliver Burn +2001-06-29 Oliver Burn * src/tests/com/puppycrawl/tools/checkstyle/CheckerTest.java: Refactored to use the Configuration object. diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/Checker.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/Checker.java index 1f4b1d182..1f715c698 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/Checker.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/Checker.java @@ -28,6 +28,7 @@ import java.io.IOException; import java.io.LineNumberReader; import java.io.OutputStream; import java.io.PrintStream; +import java.io.Reader; import java.util.ArrayList; import java.util.Iterator; import java.util.Properties; @@ -135,9 +136,11 @@ class Checker LineText[] errors; try { fireFileStarted(aFileName); + final String[] lines = getLines(aFileName); VerifierSingleton.getInstance().clearMessages(); - VerifierSingleton.getInstance().setLines(getLines(aFileName)); - final AST ast = getAST(aFileName); + VerifierSingleton.getInstance().setLines(lines); + final Reader sar = new StringArrayReader(lines); + final AST ast = getAST(aFileName, sar); processAST(ast); errors = VerifierSingleton.getInstance().getMessages(); } @@ -196,17 +199,16 @@ class Checker /** * Parses and returns the AST for a file. * @return the AST - * @param aFileName the name of the file to generate the AST + * @param aReader the Reader to generate the AST * @throws FileNotFoundException error occurred * @throws RecognitionException error occurred * @throws TokenStreamException error occurred **/ - private AST getAST(String aFileName) + private AST getAST(String aFileName, Reader aReader) throws FileNotFoundException, RecognitionException, TokenStreamException { // Create the lexer/parser stuff - final GeneratedJavaLexer jl = - new GeneratedJavaLexer(new FileReader(aFileName)); + final GeneratedJavaLexer jl = new GeneratedJavaLexer(aReader); jl.setFilename(aFileName); final GeneratedJavaRecognizer jr = new GeneratedJavaRecognizer(jl); jr.setFilename(aFileName); diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/StringArrayReader.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/StringArrayReader.java new file mode 100644 index 000000000..77cc7e7cf --- /dev/null +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/StringArrayReader.java @@ -0,0 +1,107 @@ +//////////////////////////////////////////////////////////////////////////////// +// checkstyle: Checks Java source code for adherence to a set of rules. +// Copyright (C) 2001 Oliver Burn +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 2 +// of the License, or (at your option) any later version. +// +// This program 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 General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +//////////////////////////////////////////////////////////////////////////////// +package com.puppycrawl.tools.checkstyle; + +import java.io.Reader; +import java.io.IOException; + +/** + * A Reader that reads from an underlying String array, assuming each + * array element corresponds to one line of text. + * + * @author Lars Kühne */ +public class StringArrayReader extends Reader +{ + /** the underlying String array */ + private final String[] mUnderlyingArray; + + /** the index of the currently read String */ + private int mArrayIdx = 0; + + /** the index of the next character to be read */ + private int mStringIdx = 0; + + /** flag to tell whether an implicit newline has to be reported */ + private boolean mUnreportedNewline = false; + + /** flag to tell if the reader has been closed */ + private boolean mClosed = false; + + /** + * Creates a new StringArrayReader. + * + * @param aUnderlyingArray the underlying String array. + */ + StringArrayReader(String[] aUnderlyingArray) + { + final int length = aUnderlyingArray.length; + mUnderlyingArray = new String[length]; + System.arraycopy(aUnderlyingArray, 0, mUnderlyingArray, 0, length); + } + + /** @see Reader */ + public void close() + { + mClosed = true; + } + + /** @return whether data is available that has not yet been read. */ + private boolean dataAvailable() + { + return (mUnderlyingArray.length > mArrayIdx); + } + + /** @see Reader */ + public int read( char[] aCbuf, int aOff, int aLen ) throws IOException + { + if (mClosed) { + throw new IOException("already closed"); + } + + int retVal = 0; + + if (!mUnreportedNewline && (mUnderlyingArray.length <= mArrayIdx)) { + return -1; + } + + while ((retVal < aLen) && (mUnreportedNewline || dataAvailable())) { + if (mUnreportedNewline) { + aCbuf[aOff + retVal] = '\n'; + retVal++; + mUnreportedNewline = false; + } + + if ((retVal < aLen) && dataAvailable()) { + final String currentStr = mUnderlyingArray[mArrayIdx]; + final int srcEnd = Math.min(currentStr.length(), + mStringIdx + aLen - retVal); + currentStr.getChars(mStringIdx, srcEnd, aCbuf, aOff + retVal); + retVal += srcEnd - mStringIdx; + mStringIdx = srcEnd; + + if (mStringIdx >= currentStr.length()) { + mArrayIdx++; + mStringIdx = 0; + mUnreportedNewline = true; + } + } + } + return retVal; + } +}