Skip to content

Commit

Permalink
Issue #12101: swap all non-violation messages to localized message
Browse files Browse the repository at this point in the history
  • Loading branch information
rnveach authored and nrmancuso committed Oct 10, 2022
1 parent 65655da commit a8f0657
Show file tree
Hide file tree
Showing 15 changed files with 113 additions and 281 deletions.
Expand Up @@ -662,24 +662,6 @@
</details>
</checkerFrameworkError>

<checkerFrameworkError unstable="false">
<fileName>checkstyle/src/main/java/com/puppycrawl/tools/checkstyle/checks/TranslationCheck.java</fileName>
<specifier>array.initializer</specifier>
<message>incompatible types in array initializer.</message>
<lineContent>WRONG_LANGUAGE_CODE_KEY, new Object[] {code}, getId(), getClass(), null);</lineContent>
<details>
found : @SameLen({&quot;code&quot;, &quot;iter#num0.next()&quot;}) String
required: @SameLen({&quot;code&quot;, &quot;[error for expression: iter#num0.next(); error: Invalid &apos;iter#num0.next()&apos; because the expression did not parse. Error message: Lexical error at line 1, column 5. Encountered: &quot;#&quot; (35), after : &quot;&quot;]&quot;}) Object
</details>
</checkerFrameworkError>

<checkerFrameworkError unstable="false">
<fileName>checkstyle/src/main/java/com/puppycrawl/tools/checkstyle/checks/TranslationCheck.java</fileName>
<specifier>expression.unparsable</specifier>
<message>Expression invalid in dependent type annotation: [error for expression: iter#num0.next(); error: Invalid &apos;iter#num0.next()&apos; because the expression did not parse. Error message: Lexical error at line 1, column 5. Encountered: &quot;#&quot; (35), after : &quot;&quot;]</message>
<lineContent>WRONG_LANGUAGE_CODE_KEY, new Object[] {code}, getId(), getClass(), null);</lineContent>
</checkerFrameworkError>

<checkerFrameworkError unstable="false">
<fileName>checkstyle/src/main/java/com/puppycrawl/tools/checkstyle/checks/annotation/MissingDeprecatedCheck.java</fileName>
<specifier>argument</specifier>
Expand Down
Expand Up @@ -288,17 +288,6 @@
</details>
</checkerFrameworkError>

<checkerFrameworkError unstable="false">
<fileName>checkstyle/src/main/java/com/puppycrawl/tools/checkstyle/DetailNodeTreeStringPrinter.java</fileName>
<specifier>argument</specifier>
<message>incompatible argument for parameter customMessage of Violation.</message>
<lineContent>null);</lineContent>
<details>
found : null (NullType)
required: @Initialized @NonNull String
</details>
</checkerFrameworkError>

<checkerFrameworkError unstable="false">
<fileName>checkstyle/src/main/java/com/puppycrawl/tools/checkstyle/JavaAstVisitor.java</fileName>
<specifier>argument</specifier>
Expand Down Expand Up @@ -489,28 +478,6 @@
</details>
</checkerFrameworkError>

<checkerFrameworkError unstable="false">
<fileName>checkstyle/src/main/java/com/puppycrawl/tools/checkstyle/Main.java</fileName>
<specifier>argument</specifier>
<message>incompatible argument for parameter customMessage of Violation.</message>
<lineContent>new String[] {String.valueOf(errorCounter)}, null, Main.class, null);</lineContent>
<details>
found : null (NullType)
required: @Initialized @NonNull String
</details>
</checkerFrameworkError>

<checkerFrameworkError unstable="false">
<fileName>checkstyle/src/main/java/com/puppycrawl/tools/checkstyle/Main.java</fileName>
<specifier>argument</specifier>
<message>incompatible argument for parameter customMessage of Violation.</message>
<lineContent>new String[] {file.getAbsolutePath()}, null, Main.class, null);</lineContent>
<details>
found : null (NullType)
required: @Initialized @NonNull String
</details>
</checkerFrameworkError>

<checkerFrameworkError unstable="false">
<fileName>checkstyle/src/main/java/com/puppycrawl/tools/checkstyle/Main.java</fileName>
<specifier>argument</specifier>
Expand All @@ -522,28 +489,6 @@
</details>
</checkerFrameworkError>

<checkerFrameworkError unstable="false">
<fileName>checkstyle/src/main/java/com/puppycrawl/tools/checkstyle/Main.java</fileName>
<specifier>argument</specifier>
<message>incompatible argument for parameter moduleId of Violation.</message>
<lineContent>new String[] {String.valueOf(errorCounter)}, null, Main.class, null);</lineContent>
<details>
found : null (NullType)
required: @Initialized @NonNull String
</details>
</checkerFrameworkError>

<checkerFrameworkError unstable="false">
<fileName>checkstyle/src/main/java/com/puppycrawl/tools/checkstyle/Main.java</fileName>
<specifier>argument</specifier>
<message>incompatible argument for parameter moduleId of Violation.</message>
<lineContent>new String[] {file.getAbsolutePath()}, null, Main.class, null);</lineContent>
<details>
found : null (NullType)
required: @Initialized @NonNull String
</details>
</checkerFrameworkError>

<checkerFrameworkError unstable="false">
<fileName>checkstyle/src/main/java/com/puppycrawl/tools/checkstyle/Main.java</fileName>
<specifier>dereference.of.nullable</specifier>
Expand Down Expand Up @@ -653,55 +598,11 @@
<checkerFrameworkError unstable="false">
<fileName>checkstyle/src/main/java/com/puppycrawl/tools/checkstyle/PackageObjectFactory.java</fileName>
<specifier>argument</specifier>
<message>incompatible argument for parameter args of Violation.</message>
<lineContent>new String[] {name, attemptedNames}, null, getClass(), null);</lineContent>
<details>
found : @Initialized @Nullable String @Initialized @NonNull []
required: @Initialized @NonNull Object @Initialized @NonNull []
</details>
</checkerFrameworkError>

<checkerFrameworkError unstable="false">
<fileName>checkstyle/src/main/java/com/puppycrawl/tools/checkstyle/PackageObjectFactory.java</fileName>
<specifier>argument</specifier>
<message>incompatible argument for parameter customMessage of Violation.</message>
<lineContent>new String[] {name, attemptedNames}, null, getClass(), null);</lineContent>
<details>
found : null (NullType)
required: @Initialized @NonNull String
</details>
</checkerFrameworkError>

<checkerFrameworkError unstable="false">
<fileName>checkstyle/src/main/java/com/puppycrawl/tools/checkstyle/PackageObjectFactory.java</fileName>
<specifier>argument</specifier>
<message>incompatible argument for parameter customMessage of Violation.</message>
<lineContent>new String[] {name, optionalNames}, null, getClass(), null);</lineContent>
<message>incompatible argument for parameter args of LocalizedMessage.</message>
<lineContent>UNABLE_TO_INSTANTIATE_EXCEPTION_MESSAGE, name, attemptedNames);</lineContent>
<details>
found : null (NullType)
required: @Initialized @NonNull String
</details>
</checkerFrameworkError>

<checkerFrameworkError unstable="false">
<fileName>checkstyle/src/main/java/com/puppycrawl/tools/checkstyle/PackageObjectFactory.java</fileName>
<specifier>argument</specifier>
<message>incompatible argument for parameter moduleId of Violation.</message>
<lineContent>new String[] {name, attemptedNames}, null, getClass(), null);</lineContent>
<details>
found : null (NullType)
required: @Initialized @NonNull String
</details>
</checkerFrameworkError>

<checkerFrameworkError unstable="false">
<fileName>checkstyle/src/main/java/com/puppycrawl/tools/checkstyle/PackageObjectFactory.java</fileName>
<specifier>argument</specifier>
<message>incompatible argument for parameter moduleId of Violation.</message>
<lineContent>new String[] {name, optionalNames}, null, getClass(), null);</lineContent>
<details>
found : null (NullType)
required: @Initialized @NonNull String
found : @Initialized @Nullable String
required: @Initialized @NonNull Object
</details>
</checkerFrameworkError>

Expand Down Expand Up @@ -1442,17 +1343,6 @@
</details>
</checkerFrameworkError>

<checkerFrameworkError unstable="false">
<fileName>checkstyle/src/main/java/com/puppycrawl/tools/checkstyle/checks/TranslationCheck.java</fileName>
<specifier>argument</specifier>
<message>incompatible argument for parameter customMessage of Violation.</message>
<lineContent>WRONG_LANGUAGE_CODE_KEY, new Object[] {code}, getId(), getClass(), null);</lineContent>
<details>
found : null (NullType)
required: @Initialized @NonNull String
</details>
</checkerFrameworkError>

<checkerFrameworkError unstable="false">
<fileName>checkstyle/src/main/java/com/puppycrawl/tools/checkstyle/checks/TranslationCheck.java</fileName>
<specifier>argument</specifier>
Expand Down
9 changes: 0 additions & 9 deletions .ci/pitest-suppressions/pitest-misc-suppressions.xml
Expand Up @@ -207,15 +207,6 @@
<lineContent>getId(),</lineContent>
</mutation>

<mutation unstable="false">
<sourceFile>TranslationCheck.java</sourceFile>
<mutatedClass>com.puppycrawl.tools.checkstyle.checks.TranslationCheck</mutatedClass>
<mutatedMethod>validateUserSpecifiedLanguageCodes</mutatedMethod>
<mutator>org.pitest.mutationtest.engine.gregor.mutators.NonVoidMethodCallMutator</mutator>
<description>removed call to com/puppycrawl/tools/checkstyle/checks/TranslationCheck::getId</description>
<lineContent>WRONG_LANGUAGE_CODE_KEY, new Object[] {code}, getId(), getClass(), null);</lineContent>
</mutation>

<mutation unstable="false">
<sourceFile>TranslationCheck.java</sourceFile>
<mutatedClass>com.puppycrawl.tools.checkstyle.checks.TranslationCheck$ResourceBundle</mutatedClass>
Expand Down
9 changes: 0 additions & 9 deletions .ci/pitest-suppressions/pitest-tree-walker-suppressions.xml
@@ -1,14 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<suppressedMutations>
<mutation unstable="false">
<sourceFile>DetailNodeTreeStringPrinter.java</sourceFile>
<mutatedClass>com.puppycrawl.tools.checkstyle.DetailNodeTreeStringPrinter</mutatedClass>
<mutatedMethod>getParseErrorMessage</mutatedMethod>
<mutator>org.pitest.mutationtest.engine.gregor.mutators.NonVoidMethodCallMutator</mutator>
<description>removed call to com/puppycrawl/tools/checkstyle/JavadocDetailNodeParser$ParseErrorMessage::getLineNumber</description>
<lineContent>parseErrorMessage.getLineNumber(),</lineContent>
</mutation>

<mutation unstable="false">
<sourceFile>DetailNodeTreeStringPrinter.java</sourceFile>
<mutatedClass>com.puppycrawl.tools.checkstyle.DetailNodeTreeStringPrinter</mutatedClass>
Expand Down
3 changes: 3 additions & 0 deletions config/import-control.xml
Expand Up @@ -136,6 +136,9 @@
<allow class="com.puppycrawl.tools.checkstyle.FileStatefulCheck"/>
<allow class="com.puppycrawl.tools.checkstyle.GlobalStatefulCheck"/>

<file name="TranslationCheck">
<allow class="com.puppycrawl.tools.checkstyle.LocalizedMessage"/>
</file>
<subpackage name="imports">
<allow class="com.puppycrawl.tools.checkstyle.XmlLoader" local-only="true"/>
</subpackage>
Expand Down
4 changes: 3 additions & 1 deletion config/pmd.xml
Expand Up @@ -336,10 +336,12 @@
<!-- Main actively uses Checkstyle, logging and command line parsing API.
Checker collects external resource locations and sets up the configuration.
CheckstyleAntTask integrates Checkstyle with Ant.
TranslationCheck uses a lot of imports for it's logic and custom violations.
-->
<property name="violationSuppressXPath"
value="//ClassOrInterfaceDeclaration[@SimpleName='Main'
or @SimpleName='Checker' or @SimpleName='CheckstyleAntTask']"/>
or @SimpleName='Checker' or @SimpleName='CheckstyleAntTask'
or @SimpleName='TranslationCheck']"/>
</properties>
</rule>
<rule ref="category/java/design.xml/CognitiveComplexity">
Expand Down
Expand Up @@ -29,7 +29,6 @@
import com.puppycrawl.tools.checkstyle.api.DetailNode;
import com.puppycrawl.tools.checkstyle.api.FileText;
import com.puppycrawl.tools.checkstyle.api.JavadocTokenTypes;
import com.puppycrawl.tools.checkstyle.api.Violation;
import com.puppycrawl.tools.checkstyle.utils.JavadocUtil;
import com.puppycrawl.tools.checkstyle.utils.ParserUtil;

Expand Down Expand Up @@ -91,15 +90,12 @@ private static DetailNode parseJavadocAsDetailNode(String javadocComment) {
* @return error violation
*/
private static String getParseErrorMessage(ParseErrorMessage parseErrorMessage) {
final Violation lmessage = new Violation(
parseErrorMessage.getLineNumber(),
final LocalizedMessage message = new LocalizedMessage(
"com.puppycrawl.tools.checkstyle.checks.javadoc.messages",
parseErrorMessage.getMessageKey(),
parseErrorMessage.getMessageArguments(),
"",
DetailNodeTreeStringPrinter.class,
null);
return "[ERROR:" + parseErrorMessage.getLineNumber() + "] " + lmessage.getViolation();
parseErrorMessage.getMessageKey(),
parseErrorMessage.getMessageArguments());
return "[ERROR:" + parseErrorMessage.getLineNumber() + "] " + message.getMessage();
}

/**
Expand Down
17 changes: 8 additions & 9 deletions src/main/java/com/puppycrawl/tools/checkstyle/Main.java
Expand Up @@ -48,7 +48,6 @@
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
import com.puppycrawl.tools.checkstyle.api.Configuration;
import com.puppycrawl.tools.checkstyle.api.RootModule;
import com.puppycrawl.tools.checkstyle.api.Violation;
import com.puppycrawl.tools.checkstyle.utils.ChainedPropertyUtil;
import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
import com.puppycrawl.tools.checkstyle.utils.XpathUtil;
Expand Down Expand Up @@ -146,12 +145,12 @@ else if (parseResult.isUsageHelpRequested()) {
finally {
// return exit code base on validation of Checker
if (errorCounter > 0) {
final Violation errorCounterViolation = new Violation(1,
Definitions.CHECKSTYLE_BUNDLE, ERROR_COUNTER,
new String[] {String.valueOf(errorCounter)}, null, Main.class, null);
final LocalizedMessage errorCounterViolation = new LocalizedMessage(
Definitions.CHECKSTYLE_BUNDLE, Main.class,
ERROR_COUNTER, String.valueOf(errorCounter));
// print error count statistic to error output stream,
// output stream might be used by validation report content
System.err.println(errorCounterViolation.getViolation());
System.err.println(errorCounterViolation.getMessage());
}
}
Runtime.getRuntime().exit(exitStatus);
Expand Down Expand Up @@ -439,10 +438,10 @@ private static Properties loadProperties(File file)
properties.load(stream);
}
catch (final IOException ex) {
final Violation loadPropertiesExceptionMessage = new Violation(1,
Definitions.CHECKSTYLE_BUNDLE, LOAD_PROPERTIES_EXCEPTION,
new String[] {file.getAbsolutePath()}, null, Main.class, null);
throw new CheckstyleException(loadPropertiesExceptionMessage.getViolation(), ex);
final LocalizedMessage loadPropertiesExceptionMessage = new LocalizedMessage(
Definitions.CHECKSTYLE_BUNDLE, Main.class,
LOAD_PROPERTIES_EXCEPTION, file.getAbsolutePath());
throw new CheckstyleException(loadPropertiesExceptionMessage.getMessage(), ex);
}

return ChainedPropertyUtil.getResolvedProperties(properties);
Expand Down
Expand Up @@ -31,7 +31,6 @@
import java.util.stream.Stream;

import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
import com.puppycrawl.tools.checkstyle.api.Violation;
import com.puppycrawl.tools.checkstyle.utils.ModuleReflectionUtil;

/**
Expand Down Expand Up @@ -209,10 +208,10 @@ public Object createModule(String name) throws CheckstyleException {
+ STRING_SEPARATOR + nameCheck + STRING_SEPARATOR
+ joinPackageNamesWithClassName(nameCheck, packages);
}
final Violation exceptionMessage = new Violation(1,
Definitions.CHECKSTYLE_BUNDLE, UNABLE_TO_INSTANTIATE_EXCEPTION_MESSAGE,
new String[] {name, attemptedNames}, null, getClass(), null);
throw new CheckstyleException(exceptionMessage.getViolation());
final LocalizedMessage exceptionMessage = new LocalizedMessage(
Definitions.CHECKSTYLE_BUNDLE, getClass(),
UNABLE_TO_INSTANTIATE_EXCEPTION_MESSAGE, name, attemptedNames);
throw new CheckstyleException(exceptionMessage.getMessage());
}
return instance;
}
Expand Down Expand Up @@ -285,10 +284,10 @@ private Object createObjectFromFullModuleNames(String name, Set<String> fullModu
final String optionalNames = fullModuleNames.stream()
.sorted()
.collect(Collectors.joining(STRING_SEPARATOR));
final Violation exceptionMessage = new Violation(1,
Definitions.CHECKSTYLE_BUNDLE, AMBIGUOUS_MODULE_NAME_EXCEPTION_MESSAGE,
new String[] {name, optionalNames}, null, getClass(), null);
throw new CheckstyleException(exceptionMessage.getViolation());
final LocalizedMessage exceptionMessage = new LocalizedMessage(
Definitions.CHECKSTYLE_BUNDLE, getClass(),
AMBIGUOUS_MODULE_NAME_EXCEPTION_MESSAGE, name, optionalNames);
throw new CheckstyleException(exceptionMessage.getMessage());
}
return returnValue;
}
Expand Down
21 changes: 5 additions & 16 deletions src/main/java/com/puppycrawl/tools/checkstyle/api/Violation.java
Expand Up @@ -431,26 +431,15 @@ else if (other.moduleId == null) {
* @return the translated violation
*/
public String getViolation() {
String violation = getCustomViolation();
final String violation;

if (violation == null) {
if (customMessage != null) {
violation = new MessageFormat(customMessage, Locale.ROOT).format(args);
}
else {
violation = new LocalizedMessage(bundle, sourceClass, key, args).getMessage();
}
return violation;
}

/**
* Returns the formatted custom violation if one is configured.
*
* @return the formatted custom violation or {@code null}
* if there is no custom violation
*/
private String getCustomViolation() {
String violation = null;
if (customMessage != null) {
final MessageFormat formatter = new MessageFormat(customMessage, Locale.ROOT);
violation = formatter.format(args);
}
return violation;
}

Expand Down
Expand Up @@ -45,6 +45,7 @@

import com.puppycrawl.tools.checkstyle.Definitions;
import com.puppycrawl.tools.checkstyle.GlobalStatefulCheck;
import com.puppycrawl.tools.checkstyle.LocalizedMessage;
import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck;
import com.puppycrawl.tools.checkstyle.api.FileText;
import com.puppycrawl.tools.checkstyle.api.MessageDispatcher;
Expand Down Expand Up @@ -318,11 +319,9 @@ public void setRequiredTranslations(String... translationCodes) {
private void validateUserSpecifiedLanguageCodes(Set<String> languageCodes) {
for (String code : languageCodes) {
if (!isValidLanguageCode(code)) {
final Violation msg = new Violation(1, TRANSLATION_BUNDLE,
WRONG_LANGUAGE_CODE_KEY, new Object[] {code}, getId(), getClass(), null);
final String exceptionMessage = String.format(Locale.ROOT,
"%s [%s]", msg.getViolation(), TranslationCheck.class.getSimpleName());
throw new IllegalArgumentException(exceptionMessage);
final LocalizedMessage msg = new LocalizedMessage(TRANSLATION_BUNDLE,
getClass(), WRONG_LANGUAGE_CODE_KEY, code);
throw new IllegalArgumentException(msg.getMessage());
}
}
}
Expand Down

0 comments on commit a8f0657

Please sign in to comment.