Skip to content

Commit

Permalink
Issue checkstyle#3034: FileText should not extends AbstractList
Browse files Browse the repository at this point in the history
  • Loading branch information
timurt committed Jun 27, 2017
1 parent c090fd8 commit 156d871
Show file tree
Hide file tree
Showing 27 changed files with 96 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,6 @@ private static String getIndentation(DetailNode node) {
* @throws IOException if the file could not be read.
*/
private static DetailNode parseFile(File file) throws IOException {
// Details: https://github.com/checkstyle/checkstyle/issues/3034
//noinspection MismatchedQueryAndUpdateOfCollection
final FileText text = new FileText(file.getAbsoluteFile(),
System.getProperty("file.encoding", "UTF-8"));
return parseJavadocAsDetailNode(text.getFullText().toString());
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/com/puppycrawl/tools/checkstyle/TreeWalker.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map.Entry;
import java.util.Set;
Expand Down Expand Up @@ -166,16 +165,15 @@ public void setupChild(Configuration childConf)
}

@Override
protected void processFiltered(File file, List<String> lines) throws CheckstyleException {
protected void processFiltered(File file, FileText fileText) throws CheckstyleException {
// check if already checked and passed the file
if (CommonUtils.matchesFileExtension(file, getFileExtensions())) {
final String msg = "%s occurred during the analysis of file %s.";
final String fileName = file.getPath();
try {
if (!ordinaryChecks.isEmpty()
|| !commentChecks.isEmpty()) {
final FileText text = FileText.fromLines(file, lines);
final FileContents contents = new FileContents(text);
final FileContents contents = new FileContents(fileText);
final DetailAST rootAST = parse(contents);

if (!ordinaryChecks.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.SortedSet;

import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
Expand All @@ -48,10 +47,10 @@ public abstract class AbstractFileSetCheck
/**
* Called to process a file that matches the specified file extensions.
* @param file the file to be processed
* @param lines an immutable list of the contents of the file.
* @param fileText the contents of the file.
* @throws CheckstyleException if error condition within Checkstyle occurs.
*/
protected abstract void processFiltered(File file, List<String> lines)
protected abstract void processFiltered(File file, FileText fileText)
throws CheckstyleException;

@Override
Expand All @@ -70,12 +69,12 @@ public void beginProcessing(String charset) {
}

@Override
public final SortedSet<LocalizedMessage> process(File file, List<String> lines)
public final SortedSet<LocalizedMessage> process(File file, FileText fileText)
throws CheckstyleException {
messageCollector.reset();
// Process only what interested in
if (CommonUtils.matchesFileExtension(file, fileExtensions)) {
processFiltered(file, lines);
processFiltered(file, fileText);
}
return messageCollector.getMessages();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public final class FileContents implements CommentListener {
@Deprecated
public FileContents(String filename, String... lines) {
fileName = filename;
text = FileText.fromLines(new File(filename), Arrays.asList(lines));
text = new FileText(new File(filename), Arrays.asList(lines));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package com.puppycrawl.tools.checkstyle.api;

import java.io.File;
import java.util.List;
import java.util.SortedSet;

/**
Expand Down Expand Up @@ -68,11 +67,11 @@ public interface FileSetCheck
* </p>
*
* @param file the file to be processed
* @param lines an immutable list of the contents of the file.
* @param fileText the contents of the file.
* @return the sorted set of messages to be logged.
* @throws CheckstyleException if error condition within Checkstyle occurs
*/
SortedSet<LocalizedMessage> process(File file, List<String> lines) throws CheckstyleException;
SortedSet<LocalizedMessage> process(File file, FileText fileText) throws CheckstyleException;

/**
* Called when all the files have been processed. This is the time to
Expand Down
44 changes: 11 additions & 33 deletions src/main/java/com/puppycrawl/tools/checkstyle/api/FileText.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CodingErrorAction;
import java.nio.charset.UnsupportedCharsetException;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand All @@ -44,15 +43,12 @@
* Represents the text contents of a file of arbitrary plain text type.
* <p>
* This class will be passed to instances of class FileSetCheck by
* Checker. It implements a string list to ensure backwards
* compatibility, but can be extended in the future to allow more
* flexible, more powerful or more efficient handling of certain
* situations.
* Checker.
* </p>
*
* @author Martin von Gagern
*/
public final class FileText extends AbstractList<String> {
public final class FileText {

/**
* The number of characters to read in one go.
Expand Down Expand Up @@ -171,7 +167,7 @@ public FileText(FileText fileText) {
* @param lines the lines of the text, without terminators
* @throws NullPointerException if the lines array is null
*/
private FileText(File file, List<String> lines) {
public FileText(File file, List<String> lines) {
final StringBuilder buf = new StringBuilder();
for (final String line : lines) {
buf.append(line).append('\n');
Expand Down Expand Up @@ -215,30 +211,6 @@ private static String readFile(final File inputFile, final CharsetDecoder decode
return buf.toString();
}

/**
* Compatibility conversion.
*
* <p>This method can be used to convert the arguments passed to
* {@link FileSetCheck#process(File,List)} to a FileText
* object. If the list of lines already is a FileText, it is
* returned as is. Otherwise, a new FileText is constructed by
* joining the lines using line feed characters.
*
* @param file the name of the file
* @param lines the lines of the text, without terminators
* @return an object representing the denoted text file
*/
public static FileText fromLines(File file, List<String> lines) {
final FileText fileText;
if (lines instanceof FileText) {
fileText = (FileText) lines;
}
else {
fileText = new FileText(file, lines);
}
return fileText;
}

/**
* Get the name of the file.
* @return an object containing the name of the file
Expand All @@ -264,6 +236,14 @@ public CharSequence getFullText() {
return fullText;
}

/**
* Returns a list of lines.
* @return a list of all lines of the file
*/
public List<String> getLines() {
return new ArrayList<>(Arrays.asList(lines));
}

/**
* Returns an array of all lines.
* {@code text.toLinesArray()} is equivalent to
Expand Down Expand Up @@ -321,7 +301,6 @@ public LineColumn lineColumn(int pos) {
* @param lineNo the number of the line to get, starting at zero
* @return the line with the given number
*/
@Override
public String get(final int lineNo) {
return lines[lineNo];
}
Expand All @@ -330,7 +309,6 @@ public String get(final int lineNo) {
* Counts the lines of the text.
* @return the number of lines in the text
*/
@Override
public int size() {
return lines.length;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.List;
import java.util.Locale;

import com.google.common.io.Closeables;
import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck;
import com.puppycrawl.tools.checkstyle.api.FileText;

/**
* <p>
Expand Down Expand Up @@ -75,7 +75,7 @@ public class NewlineAtEndOfFileCheck
private LineSeparatorOption lineSeparator = LineSeparatorOption.SYSTEM;

@Override
protected void processFiltered(File file, List<String> lines) {
protected void processFiltered(File file, FileText fileText) {
// Cannot use lines as the line separators have been removed!
try {
final RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.Properties;
Expand All @@ -46,6 +45,7 @@
import com.google.common.io.Closeables;
import com.puppycrawl.tools.checkstyle.Definitions;
import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck;
import com.puppycrawl.tools.checkstyle.api.FileText;
import com.puppycrawl.tools.checkstyle.api.LocalizedMessage;
import com.puppycrawl.tools.checkstyle.api.MessageDispatcher;
import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
Expand Down Expand Up @@ -241,7 +241,7 @@ public void beginProcessing(String charset) {
}

@Override
protected void processFiltered(File file, List<String> lines) {
protected void processFiltered(File file, FileText fileText) {
// We just collecting files for processing at finishProcessing()
filesToProcess.add(file);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.google.common.collect.Multiset;
import com.google.common.collect.Multiset.Entry;
import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck;
import com.puppycrawl.tools.checkstyle.api.FileText;

/**
* Checks the uniqueness of property keys (left from equal sign) in the
Expand Down Expand Up @@ -63,7 +64,7 @@ public UniquePropertiesCheck() {
}

@Override
protected void processFiltered(File file, List<String> lines) {
protected void processFiltered(File file, FileText fileText) {
final UniqueProperties properties = new UniqueProperties();

try {
Expand All @@ -84,6 +85,7 @@ protected void processFiltered(File file, List<String> lines) {
for (Entry<String> duplication : properties
.getDuplicatedKeys().entrySet()) {
final String keyName = duplication.getElement();
final List<String> lines = fileText.getLines();
final int lineNumber = getLineNumber(lines, keyName);
// Number of occurrences is number of duplications + 1
log(lineNumber, MSG_KEY, keyName, duplication.getCount() + 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.util.Arrays;
import java.util.List;

import com.puppycrawl.tools.checkstyle.api.FileText;

/**
* Checks the header of the source against a fixed header file.
* In default configuration,if header is not specified,
Expand Down Expand Up @@ -88,7 +90,8 @@ public void setIgnoreLines(int... list) {
}

@Override
protected void processFiltered(File file, List<String> lines) {
protected void processFiltered(File file, FileText fileText) {
final List<String> lines = fileText.getLines();
if (getHeaderLines().size() > lines.size()) {
log(1, MSG_MISSING);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import com.puppycrawl.tools.checkstyle.api.FileText;
import com.puppycrawl.tools.checkstyle.utils.CommonUtils;

/**
Expand Down Expand Up @@ -77,8 +78,9 @@ public void setMultiLines(int... list) {
}

@Override
protected void processFiltered(File file, List<String> lines) {
protected void processFiltered(File file, FileText fileText) {
final int headerSize = getHeaderLines().size();
final List<String> lines = fileText.getLines();
final int fileSize = lines.size();

if (headerSize - multiLines.length > fileSize) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@

import java.io.File;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck;
import com.puppycrawl.tools.checkstyle.api.FileText;

/**
* Checks that all packages have a package documentation. See the documentation
Expand Down Expand Up @@ -67,7 +67,7 @@ public void beginProcessing(String charset) {
}

@Override
protected void processFiltered(File file, List<String> lines) {
protected void processFiltered(File file, FileText fileText) {
// Check if already processed directory
final File dir = file.getParentFile();
if (!directoriesChecked.contains(dir)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package com.puppycrawl.tools.checkstyle.checks.regexp;

import java.io.File;
import java.util.List;
import java.util.regex.Pattern;

import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck;
Expand Down Expand Up @@ -63,8 +62,8 @@ public void beginProcessing(String charset) {
}

@Override
protected void processFiltered(File file, List<String> lines) {
detector.processLines(FileText.fromLines(file, lines));
protected void processFiltered(File file, FileText fileText) {
detector.processLines(fileText);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.regex.Pattern;

import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck;
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
import com.puppycrawl.tools.checkstyle.api.FileText;
import com.puppycrawl.tools.checkstyle.utils.CommonUtils;

/**
Expand Down Expand Up @@ -248,7 +248,7 @@ public void init() {
}

@Override
protected void processFiltered(File file, List<String> lines) throws CheckstyleException {
protected void processFiltered(File file, FileText fileText) throws CheckstyleException {
final String fileName = getFileName(file);
final String folderPath = getFolderPath(file);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
package com.puppycrawl.tools.checkstyle.checks.regexp;

import java.io.File;
import java.util.List;

import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck;
import com.puppycrawl.tools.checkstyle.api.FileText;

/**
* Implementation of a check that looks for a single line in any file type.
Expand Down Expand Up @@ -60,8 +60,8 @@ public void beginProcessing(String charset) {
}

@Override
protected void processFiltered(File file, List<String> lines) {
detector.processLines(lines);
protected void processFiltered(File file, FileText fileText) {
detector.processLines(fileText.getLines());
}

/**
Expand Down
Loading

0 comments on commit 156d871

Please sign in to comment.