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 #12542: new TreeWalker property to skip exceptions #14779
base: master
Are you sure you want to change the base?
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 |
---|---|---|
|
@@ -42,6 +42,7 @@ | |
import com.puppycrawl.tools.checkstyle.api.ExternalResourceHolder; | ||
import com.puppycrawl.tools.checkstyle.api.FileContents; | ||
import com.puppycrawl.tools.checkstyle.api.FileText; | ||
import com.puppycrawl.tools.checkstyle.api.SeverityLevel; | ||
import com.puppycrawl.tools.checkstyle.api.Violation; | ||
import com.puppycrawl.tools.checkstyle.utils.TokenUtil; | ||
|
||
|
@@ -53,6 +54,9 @@ | |
@FileStatefulCheck | ||
public final class TreeWalker extends AbstractFileSetCheck implements ExternalResourceHolder { | ||
|
||
/** Message to use when an exception occurs and should be printed as a violation. */ | ||
public static final String EXCEPTION_MSG = "general.exception"; | ||
|
||
/** Maps from token name to ordinary checks. */ | ||
private final Map<Integer, Set<AbstractCheck>> tokenToOrdinaryChecks = | ||
new HashMap<>(); | ||
|
@@ -79,6 +83,12 @@ public final class TreeWalker extends AbstractFileSetCheck implements ExternalRe | |
/** A factory for creating submodules (i.e. the Checks) */ | ||
private ModuleFactory moduleFactory; | ||
|
||
/** Control whether to skip files with Java parsing errors. */ | ||
private boolean skipFileOnJavaParseException; | ||
|
||
/** Severity Level to log Java parsing exceptions when they are skipped. */ | ||
private SeverityLevel javaParseExceptionSeverity = SeverityLevel.ERROR; | ||
|
||
/** | ||
* Creates a new {@code TreeWalker} instance. | ||
*/ | ||
|
@@ -95,6 +105,27 @@ public void setModuleFactory(ModuleFactory moduleFactory) { | |
this.moduleFactory = moduleFactory; | ||
} | ||
|
||
/** | ||
* Setter to control whether to skip files with Java parsing errors. | ||
* | ||
* @param skipFileOnJavaParseException whether to always check for a trailing comma. | ||
* @since 10.17.0 | ||
*/ | ||
public void setSkipFileOnJavaParseException(boolean skipFileOnJavaParseException) { | ||
this.skipFileOnJavaParseException = skipFileOnJavaParseException; | ||
} | ||
|
||
/** | ||
* Sets severity level to log Java parsing exceptions when they are skipped. | ||
* | ||
* @param javaParseExceptionSeverity severity level to log parsing exceptions | ||
* when they are skipped. | ||
* @since 10.17.0 | ||
*/ | ||
public void setJavaParseExceptionSeverity(SeverityLevel javaParseExceptionSeverity) { | ||
this.javaParseExceptionSeverity = javaParseExceptionSeverity; | ||
} | ||
|
||
@Override | ||
public void finishLocalSetup() { | ||
final DefaultContext checkContext = new DefaultContext(); | ||
|
@@ -149,21 +180,41 @@ protected void processFiltered(File file, FileText fileText) throws CheckstyleEx | |
// check if already checked and passed the file | ||
if (!ordinaryChecks.isEmpty() || !commentChecks.isEmpty()) { | ||
final FileContents contents = getFileContents(); | ||
final DetailAST rootAST = JavaParser.parse(contents); | ||
if (!ordinaryChecks.isEmpty()) { | ||
walk(rootAST, contents, AstState.ORDINARY); | ||
} | ||
if (!commentChecks.isEmpty()) { | ||
final DetailAST astWithComments = JavaParser.appendHiddenCommentNodes(rootAST); | ||
walk(astWithComments, contents, AstState.WITH_COMMENTS); | ||
DetailAST rootAST = null; | ||
// whether skip the procedure after parsing Java files. | ||
boolean skip = false; | ||
try { | ||
rootAST = JavaParser.parse(contents); | ||
rnveach marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
if (filters.isEmpty()) { | ||
addViolations(violations); | ||
catch (CheckstyleException ex) { | ||
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. This report shows we can get parse errors from more than just CheckstyleExceptions. It seems to me we need to expand it to all throwables for this to work properly. Edit: We will also need a test case to show this. Let me know what the new test name will be. 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. @Lmh-java , please add this file to our Inputs https://github.com/openjdk/jdk/blob/master/test/langtools/tools/javac/diags/examples/VarargsAndReceiver.java without header comment. |
||
if (skipFileOnJavaParseException) { | ||
skip = true; | ||
violations.add(new Violation(1, Definitions.CHECKSTYLE_BUNDLE, EXCEPTION_MSG, | ||
new Object[] {ex.getMessage()}, javaParseExceptionSeverity, null, | ||
getClass(), null)); | ||
addViolations(violations); | ||
} | ||
else { | ||
throw ex; | ||
} | ||
rnveach marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
else { | ||
final SortedSet<Violation> filteredViolations = | ||
getFilteredViolations(file.getAbsolutePath(), contents, rootAST); | ||
addViolations(filteredViolations); | ||
|
||
if (!skip) { | ||
if (!ordinaryChecks.isEmpty()) { | ||
walk(rootAST, contents, AstState.ORDINARY); | ||
} | ||
if (!commentChecks.isEmpty()) { | ||
final DetailAST astWithComments = JavaParser.appendHiddenCommentNodes(rootAST); | ||
walk(astWithComments, contents, AstState.WITH_COMMENTS); | ||
} | ||
if (filters.isEmpty()) { | ||
addViolations(violations); | ||
} | ||
else { | ||
final SortedSet<Violation> filteredViolations = | ||
getFilteredViolations(file.getAbsolutePath(), contents, rootAST); | ||
addViolations(filteredViolations); | ||
} | ||
} | ||
violations.clear(); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
//non-compiled syntax: bad file for testing | ||
|
||
public clazz InputTreeWalkerSkipParsingException {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/*xml | ||
<module name="Checker"> | ||
<module name="TreeWalker"> | ||
<property name="skipFileOnJavaParseException" value="true"/> | ||
<property name="javaParseExceptionSeverity" value="ignore"/> | ||
<module name="ConstantName"/> | ||
romani marked this conversation as resolved.
Show resolved
Hide resolved
|
||
</module> | ||
</module> | ||
*/ | ||
|
||
//non-compiled syntax: bad file for testing | ||
public clazz InputTreeWalkerSkipParsingExceptionConfig {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -539,6 +539,20 @@ | |
<td><code>.java</code></td> | ||
<td>3.0</td> | ||
</tr> | ||
<tr> | ||
<td>javaParseExceptionSeverity</td> | ||
<td>Severity level to log Java parsing exceptions when they are skipped.</td> | ||
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. => |
||
<td><a href="property_types.html#SeverityLevel">SeverityLevel</a></td> | ||
<td><code>error</code></td> | ||
<td>10.17.0</td> | ||
</tr> | ||
<tr> | ||
<td>skipFileOnJavaParseException</td> | ||
<td>Control whether to skip files with Java parsing errors.</td> | ||
<td><a href="property_types.html#boolean">boolean</a></td> | ||
<td><code>false</code></td> | ||
<td>10.17.0</td> | ||
</tr> | ||
</table> | ||
</div> | ||
</subsection> | ||
|
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.
=>
Setter to specify the severity level...