New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue #3573: Resolve design problem of FileContentsHolder (step 1) #4468
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,6 +45,7 @@ | |
import com.puppycrawl.tools.checkstyle.api.Configuration; | ||
import com.puppycrawl.tools.checkstyle.api.Context; | ||
import com.puppycrawl.tools.checkstyle.api.ExternalResourceHolder; | ||
import com.puppycrawl.tools.checkstyle.api.FileProcessingResult; | ||
import com.puppycrawl.tools.checkstyle.api.FileSetCheck; | ||
import com.puppycrawl.tools.checkstyle.api.FileText; | ||
import com.puppycrawl.tools.checkstyle.api.Filter; | ||
|
@@ -63,6 +64,8 @@ | |
* @author lkuehne | ||
* @author Andrei Selkin | ||
*/ | ||
// -@cs[ClassFanOutComplexity] Added to suppress error, now number of classes exceeds 25. | ||
// See https://github.com/checkstyle/checkstyle/issues/4478 | ||
public class Checker extends AutomaticBean implements MessageDispatcher, RootModule { | ||
/** Message to use when an exception occurs and should be printed as a violation. */ | ||
public static final String EXCEPTION_MSG = "general.exception"; | ||
|
@@ -283,8 +286,8 @@ private void processFiles(List<File> files) throws CheckstyleException { | |
cache.put(fileName, timestamp); | ||
} | ||
fireFileStarted(fileName); | ||
final SortedSet<LocalizedMessage> fileMessages = processFile(file); | ||
fireErrors(fileName, fileMessages); | ||
final FileProcessingResult fileProcessingResult = processFile(file); | ||
fireErrors(fileProcessingResult); | ||
fireFileFinished(fileName); | ||
} | ||
// -@cs[IllegalCatch] There is no other way to deliver filename that was under | ||
|
@@ -304,16 +307,18 @@ private void processFiles(List<File> files) throws CheckstyleException { | |
/** | ||
* Processes a file with all FileSetChecks. | ||
* @param file a file to process. | ||
* @return a sorted set of messages to be logged. | ||
* @return file processing results object. | ||
* @throws CheckstyleException if error condition within Checkstyle occurs. | ||
* @noinspection ProhibitedExceptionThrown | ||
*/ | ||
private SortedSet<LocalizedMessage> processFile(File file) throws CheckstyleException { | ||
private FileProcessingResult processFile(File file) throws CheckstyleException { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. method javadoc should be corrected There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
final SortedSet<LocalizedMessage> fileMessages = new TreeSet<>(); | ||
FileText fileText = null; | ||
|
||
try { | ||
final FileText theText = new FileText(file.getAbsoluteFile(), charset); | ||
fileText = new FileText(file.getAbsoluteFile(), charset); | ||
for (final FileSetCheck fsc : fileSetChecks) { | ||
fileMessages.addAll(fsc.process(file, theText)); | ||
fileMessages.addAll(fsc.process(file, fileText)); | ||
} | ||
} | ||
catch (final IOException ioe) { | ||
|
@@ -340,7 +345,7 @@ private SortedSet<LocalizedMessage> processFile(File file) throws CheckstyleExce | |
new String[] {sw.getBuffer().toString()}, | ||
null, getClass(), null)); | ||
} | ||
return fileMessages; | ||
return new FileProcessingResult(file.getAbsolutePath(), fileText, fileMessages); | ||
} | ||
|
||
/** | ||
|
@@ -373,15 +378,16 @@ public void fireFileStarted(String fileName) { | |
/** | ||
* Notify all listeners about the errors in a file. | ||
* | ||
* @param fileName the audited file | ||
* @param errors the audit errors from the file | ||
* @param fileProcessingResult file processing results object | ||
*/ | ||
@Override | ||
public void fireErrors(String fileName, SortedSet<LocalizedMessage> errors) { | ||
public void fireErrors(FileProcessingResult fileProcessingResult) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
please see my comment at #3573 (comment) |
||
final String fileName = fileProcessingResult.getFilePath(); | ||
final String stripped = CommonUtils.relativizeAndNormalizePath(basedir, fileName); | ||
boolean hasNonFilteredViolations = false; | ||
for (final LocalizedMessage element : errors) { | ||
final AuditEvent event = new AuditEvent(this, stripped, element); | ||
final SortedSet<LocalizedMessage> errorMessages = fileProcessingResult.getMessages(); | ||
for (LocalizedMessage errorMessage : errorMessages) { | ||
final AuditEvent event = new AuditEvent(this, stripped, errorMessage); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
@MEZk Shouldn't the event eventually accept There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @rnveach There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @rnveach There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok. |
||
if (filters.accept(event)) { | ||
hasNonFilteredViolations = true; | ||
for (final AuditListener listener : listeners) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// checkstyle: Checks Java source code for adherence to a set of rules. | ||
// Copyright (C) 2001-2017 the original author or authors. | ||
// | ||
// This library 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. | ||
// | ||
// This library 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 | ||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
package com.puppycrawl.tools.checkstyle.api; | ||
|
||
import java.util.Collections; | ||
import java.util.SortedSet; | ||
import java.util.TreeSet; | ||
|
||
/** | ||
* Represents the file processing results. | ||
* | ||
* @author Timur Tibeyev | ||
*/ | ||
public class FileProcessingResult { | ||
/** The contents of the file. */ | ||
private final FileText fileText; | ||
|
||
/** The sorted set of messages. */ | ||
private final SortedSet<LocalizedMessage> messages; | ||
|
||
/** The file path. */ | ||
private final String filePath; | ||
|
||
/** | ||
* Creates a new {@code FileProcessingResult} instance. | ||
* | ||
* @param filePath absolute path to the file | ||
* @param fileText the contents of the file | ||
* @param messages the sorted set of messages | ||
*/ | ||
public FileProcessingResult(String filePath, FileText fileText, | ||
SortedSet<LocalizedMessage> messages) { | ||
this.filePath = filePath; | ||
if (fileText == null) { | ||
this.fileText = null; | ||
} | ||
else { | ||
this.fileText = new FileText(fileText); | ||
} | ||
final SortedSet<LocalizedMessage> validCopy = new TreeSet<>(messages); | ||
this.messages = Collections.unmodifiableSortedSet(validCopy); | ||
} | ||
|
||
/** | ||
* Returns the contents of the file. | ||
* @return an object containing the full text of the file | ||
*/ | ||
public FileText getFileText() { | ||
return fileText; | ||
} | ||
|
||
/** | ||
* Returns the sorted set of messages. | ||
* @return the sorted set of messages | ||
*/ | ||
public SortedSet<LocalizedMessage> getMessages() { | ||
final SortedSet<LocalizedMessage> validCopy = new TreeSet<>(messages); | ||
return Collections.unmodifiableSortedSet(validCopy); | ||
} | ||
|
||
/** | ||
* Returns the file path. | ||
* @return the file path | ||
*/ | ||
public String getFilePath() { | ||
return filePath; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// checkstyle: Checks Java source code for adherence to a set of rules. | ||
// Copyright (C) 2001-2017 the original author or authors. | ||
// | ||
// This library 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. | ||
// | ||
// This library 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 | ||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
package com.puppycrawl.tools.checkstyle.api; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertNull; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.SortedSet; | ||
import java.util.TreeSet; | ||
|
||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.TemporaryFolder; | ||
|
||
public class FileProcessingResultTest { | ||
@Rule | ||
public final TemporaryFolder temporaryFolder = new TemporaryFolder(); | ||
|
||
@Test | ||
public void testNonNullFileText() throws IOException { | ||
final String charsetName = "ISO-8859-1"; | ||
final File file = temporaryFolder.newFile("test.java"); | ||
final FileText fileText = new FileText(file, charsetName); | ||
final SortedSet<LocalizedMessage> messages = new TreeSet<>(); | ||
messages.add(new LocalizedMessage(0, 0, "a Bundle", "message.key", | ||
new Object[] {"arg"}, null, getClass(), null)); | ||
final FileProcessingResult processingResult = new FileProcessingResult( | ||
file.getAbsolutePath(), fileText, messages); | ||
assertNotNull(processingResult.getFileText()); | ||
} | ||
|
||
@Test | ||
public void testNullFileText() { | ||
final SortedSet<LocalizedMessage> messages = new TreeSet<>(); | ||
messages.add(new LocalizedMessage(0, 0, "a Bundle", "message.key", | ||
new Object[] {"arg"}, null, getClass(), null)); | ||
final FileProcessingResult processingResult = new FileProcessingResult("Some file name", | ||
null, messages); | ||
assertEquals(1, processingResult.getMessages().size()); | ||
assertNull(processingResult.getFileText()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if there is problem in one file for whole rule, we do not change a rule, we do suppression in certain class.
please use javadoc tag suppression for CheckerTest.