Skip to content

Commit

Permalink
Fix ErrorProne warnings.
Browse files Browse the repository at this point in the history
- Add english locale to format strings.
- Regenerate equals
  • Loading branch information
uhafner committed Sep 23, 2024
1 parent 5cc4f6d commit dc5c058
Show file tree
Hide file tree
Showing 20 changed files with 225 additions and 150 deletions.
48 changes: 16 additions & 32 deletions src/main/java/edu/hm/hafner/grading/AggregatedScore.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
Expand All @@ -23,6 +24,7 @@
import edu.hm.hafner.grading.CoverageScore.CoverageScoreBuilder;
import edu.hm.hafner.grading.TestScore.TestScoreBuilder;
import edu.hm.hafner.util.FilteredLog;
import edu.hm.hafner.util.Generated;

/**
* Stores the scores of an autograding run. Persists the configuration and the scores for each metric.
Expand Down Expand Up @@ -343,53 +345,35 @@ public List<AnalysisScore> getAnalysisScores() {
return List.copyOf(analysisScores);
}

@Override @SuppressWarnings("PMD.NPathComplexity")
@Override
@Generated
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

AggregatedScore that = (AggregatedScore) o;

if (!Objects.equals(log, that.log)) {
return false;
}
if (!testScores.equals(that.testScores)) {
return false;
}
if (!coverageScores.equals(that.coverageScores)) {
return false;
}
if (!analysisScores.equals(that.analysisScores)) {
return false;
}
if (!Objects.equals(testConfigurations, that.testConfigurations)) {
return false;
}
if (!Objects.equals(coverageConfigurations, that.coverageConfigurations)) {
return false;
}
return Objects.equals(analysisConfigurations, that.analysisConfigurations);
var that = (AggregatedScore) o;
return Objects.equals(log, that.log)
&& Objects.equals(testScores, that.testScores)
&& Objects.equals(coverageScores, that.coverageScores)
&& Objects.equals(analysisScores, that.analysisScores)
&& Objects.equals(testConfigurations, that.testConfigurations)
&& Objects.equals(coverageConfigurations, that.coverageConfigurations)
&& Objects.equals(analysisConfigurations, that.analysisConfigurations);
}

@Override
@Generated
public int hashCode() {
int result = log != null ? log.hashCode() : 0;
result = 31 * result + testScores.hashCode();
result = 31 * result + coverageScores.hashCode();
result = 31 * result + analysisScores.hashCode();
result = 31 * result + (testConfigurations != null ? testConfigurations.hashCode() : 0);
result = 31 * result + (coverageConfigurations != null ? coverageConfigurations.hashCode() : 0);
result = 31 * result + (analysisConfigurations != null ? analysisConfigurations.hashCode() : 0);
return result;
return Objects.hash(log, testScores, coverageScores, analysisScores, testConfigurations, coverageConfigurations,
analysisConfigurations);
}

@Override
public String toString() {
return String.format("Score: %d / %d", getAchievedScore(), getMaxScore());
return String.format(Locale.ENGLISH, "Score: %d / %d", getAchievedScore(), getMaxScore());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import com.fasterxml.jackson.annotation.JsonIgnore;

import edu.hm.hafner.util.Generated;

/**
* Configuration to grade static analysis results. The configuration specifies the impact of the static analysis results
* on the score. This class is intended to be deserialized from JSON, there is no public constructor available.
Expand Down Expand Up @@ -72,6 +74,7 @@ public int getLowImpact() {
}

@Override
@Generated
public boolean equals(final Object o) {
if (this == o) {
return true;
Expand All @@ -82,14 +85,15 @@ public boolean equals(final Object o) {
if (!super.equals(o)) {
return false;
}
AnalysisConfiguration that = (AnalysisConfiguration) o;
var that = (AnalysisConfiguration) o;
return errorImpact == that.errorImpact
&& highImpact == that.highImpact
&& normalImpact == that.normalImpact
&& lowImpact == that.lowImpact;
}

@Override
@Generated
public int hashCode() {
return Objects.hash(super.hashCode(), errorImpact, highImpact, normalImpact, lowImpact);
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/edu/hm/hafner/grading/AnalysisMarkdown.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ protected String extractSeverities(final AnalysisScore score) {
return "No warnings";
}
else {
return String.format("%d warning%s (%d error%s, %d high, %d normal, %d low)",
return format("%d warning%s (%d error%s, %d high, %d normal, %d low)",
score.getTotalSize(), AnalysisScore.plural(score.getTotalSize()),
score.getErrorSize(), AnalysisScore.plural(score.getErrorSize()),
score.getHighSeveritySize(),
Expand All @@ -114,7 +114,7 @@ protected String createSummary(final AnalysisScore score) {
}

private String getIconAndName(final AnalysisScore analysisScore) {
return " %s &nbsp; %s".formatted(extractParserIcon(analysisScore), analysisScore.getName())
return format(" %s &nbsp; %s", extractParserIcon(analysisScore), analysisScore.getName())
+ createScoreTitle(analysisScore);
}

Expand All @@ -124,8 +124,8 @@ private String extractParserIcon(final AnalysisScore analysisScore) {
return getIcon(analysisScore);
}
else {
return "<img src=\"%s\" alt=\"%s\" height=\"%d\" width=\"%d\">"
.formatted(descriptor.getIconUrl(), analysisScore.getName(), ICON_SIZE, ICON_SIZE);
return format("<img src=\"%s\" alt=\"%s\" height=\"%d\" width=\"%d\">",
descriptor.getIconUrl(), analysisScore.getName(), ICON_SIZE, ICON_SIZE);
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/edu/hm/hafner/grading/AnalysisScore.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ protected String createSummary() {
return "No warnings";
}
else {
return String.format("%d warning%s (%d error%s, %d high, %d normal, %d low)",
return format("%d warning%s (%d error%s, %d high, %d normal, %d low)",
getTotalSize(), plural(getTotalSize()),
getErrorSize(), plural(getErrorSize()),
getHighSeveritySize(), getNormalSeveritySize(), getLowSeveritySize());
Expand Down
54 changes: 47 additions & 7 deletions src/main/java/edu/hm/hafner/grading/CommentBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Map.Entry;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;

import com.google.errorprone.annotations.FormatMethod;

import edu.hm.hafner.analysis.Issue;
import edu.hm.hafner.analysis.registry.ParserRegistry;
import edu.hm.hafner.coverage.FileNode;
Expand Down Expand Up @@ -229,9 +232,9 @@ private String getMissedLinesMessage(final LineRange range) {

private String getMissedLinesDescription(final LineRange range) {
if (range.getStart() == range.getEnd()) {
return String.format("Line %d is not covered by tests", range.getStart());
return format("Line %d is not covered by tests", range.getStart());
}
return String.format("Lines %d-%d are not covered by tests", range.getStart(), range.getEnd());
return format("Lines %d-%d are not covered by tests", range.getStart(), range.getEnd());
}

private void createAnnotationsForPartiallyCoveredLines(final AggregatedScore score,
Expand All @@ -257,9 +260,9 @@ private void createAnnotationForMissedBranches(final FileNode file,

private String createBranchMessage(final int line, final int missed) {
if (missed == 1) {
return String.format("Line %d is only partially covered, one branch is missing", line);
return format("Line %d is only partially covered, one branch is missing", line);
}
return String.format("Line %d is only partially covered, %d branches are missing", line, missed);
return format("Line %d is only partially covered, %d branches are missing", line, missed);
}

private String createRelativeRepositoryPath(final String fileName, final Set<String> sourcePaths) {
Expand Down Expand Up @@ -303,9 +306,9 @@ private void createAnnotationForSurvivedMutation(final FileNode file,

private String createMutationMessage(final int line, final List<Mutation> survived) {
if (survived.size() == 1) {
return String.format("One mutation survived in line %d (%s)", line, formatMutator(survived));
return format("One mutation survived in line %d (%s)", line, formatMutator(survived));
}
return String.format("%d mutations survived in line %d", survived.size(), line);
return format("%d mutations survived in line %d", survived.size(), line);
}

private String formatMutator(final List<Mutation> survived) {
Expand All @@ -314,7 +317,44 @@ private String formatMutator(final List<Mutation> survived) {

private String createMutationDetails(final List<Mutation> mutations) {
return mutations.stream()
.map(mutation -> String.format("- %s (%s)", mutation.getDescription(), mutation.getMutator()))
.map(mutation -> format("- %s (%s)", mutation.getDescription(), mutation.getMutator()))
.collect(Collectors.joining("\n", "Survived mutations:\n", ""));
}

/**
* Returns a formatted string using the specified format string and
* arguments. The English locale is always used to format the string.
*
* @param format
* A <a href="../util/Formatter.html#syntax">format string</a>
*
* @param args
* Arguments referenced by the format specifiers in the format
* string. If there are more arguments than format specifiers, the
* extra arguments are ignored. The number of arguments is
* variable and may be zero. The maximum number of arguments is
* limited by the maximum dimension of a Java array as defined by
* <cite>The Java Virtual Machine Specification</cite>.
* The behaviour on a
* {@code null} argument depends on the <a
* href="../util/Formatter.html#syntax">conversion</a>.
*
* @throws java.util.IllegalFormatException
* If a format string contains an illegal syntax, a format
* specifier that is incompatible with the given arguments,
* insufficient arguments given the format string, or other
* illegal conditions. For specification of all possible
* formatting errors, see the <a
* href="../util/Formatter.html#detail">Details</a> section of the
* formatter class specification.
*
* @return A formatted string
*
* @see java.util.Formatter
* @since 1.5
*/
@FormatMethod
protected String format(final String format, final Object... args) {
return String.format(Locale.ENGLISH, format, args);
}
}
32 changes: 10 additions & 22 deletions src/main/java/edu/hm/hafner/grading/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.JsonNode;

import edu.hm.hafner.util.Generated;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

Expand Down Expand Up @@ -130,39 +131,26 @@ private void validateDefaults() {
// protected abstract void validate();

@Override
@Generated
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

Configuration that = (Configuration) o;

if (maxScore != that.maxScore) {
return false;
}
if (!Objects.equals(id, that.id)) {
return false;
}
if (!Objects.equals(name, that.name)) {
return false;
}
if (!Objects.equals(icon, that.icon)) {
return false;
}
return Objects.equals(tools, that.tools);
var that = (Configuration) o;
return maxScore == that.maxScore
&& Objects.equals(id, that.id)
&& Objects.equals(name, that.name)
&& Objects.equals(icon, that.icon)
&& Objects.equals(tools, that.tools);
}

@Override
@Generated
public int hashCode() {
int result = id != null ? id.hashCode() : 0;
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + (icon != null ? icon.hashCode() : 0);
result = 31 * result + maxScore;
result = 31 * result + (tools != null ? tools.hashCode() : 0);
return result;
return Objects.hash(id, name, icon, maxScore, tools);
}

@Override
Expand Down
20 changes: 9 additions & 11 deletions src/main/java/edu/hm/hafner/grading/CoverageConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

import java.io.Serial;
import java.util.List;
import java.util.Objects;

import org.apache.commons.lang3.StringUtils;

import com.fasterxml.jackson.annotation.JsonIgnore;

import edu.hm.hafner.util.Generated;

/**
* Configuration to grade code coverage results. The configuration specifies the impact of the coverage results on the
* score. This class is intended to be deserialized from JSON, there is no public constructor available.
Expand Down Expand Up @@ -80,6 +83,7 @@ public boolean isMutationCoverage() {
}

@Override
@Generated
public boolean equals(final Object o) {
if (this == o) {
return true;
Expand All @@ -90,20 +94,14 @@ public boolean equals(final Object o) {
if (!super.equals(o)) {
return false;
}

CoverageConfiguration that = (CoverageConfiguration) o;

if (coveredPercentageImpact != that.coveredPercentageImpact) {
return false;
}
return missedPercentageImpact == that.missedPercentageImpact;
var that = (CoverageConfiguration) o;
return coveredPercentageImpact == that.coveredPercentageImpact
&& missedPercentageImpact == that.missedPercentageImpact;
}

@Override
@Generated
public int hashCode() {
int result = super.hashCode();
result = 31 * result + coveredPercentageImpact;
result = 31 * result + missedPercentageImpact;
return result;
return Objects.hash(super.hashCode(), coveredPercentageImpact, missedPercentageImpact);
}
}
2 changes: 1 addition & 1 deletion src/main/java/edu/hm/hafner/grading/CoverageScore.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public int getMissedPercentage() {

@Override
protected String createSummary() {
return String.format("%d%% (%d %s)", getCoveredPercentage(), getMissedItems(), getItemName());
return format("%d%% (%d %s)", getCoveredPercentage(), getMissedItems(), getItemName());
}

private String getItemName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public Node create(final ToolConfiguration tool, final FilteredLog log) {
var parser = new ParserRegistry().get(StringUtils.upperCase(tool.getId()), ProcessingMode.IGNORE_ERRORS);

var nodes = new ArrayList<Node>();
for (Path file : REPORT_FINDER.find(tool, log)) {
for (Path file : REPORT_FINDER.find(log, tool)) {
var node = parser.parse(new FileReaderFactory(file).create(), file.toString(), log);
log.logInfo("- %s: %s", PATH_UTIL.getRelativePath(file), extractMetric(tool, node));
nodes.add(node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ protected List<CoverageScore> createScores(final AggregatedScore aggregation) {
@Override
protected String getIcon(final CoverageScore score) {
if (score.getId().equals("pit")) {
return "<img src=\"https://pitest.org/images/pit-black-150x152.png\" alt=\"PIT\" height=\"%d\" width=\"%d\">"
.formatted(ICON_SIZE, ICON_SIZE);
return format("<img src=\"https://pitest.org/images/pit-black-150x152.png\" alt=\"PIT\" height=\"%d\" width=\"%d\">",
ICON_SIZE, ICON_SIZE);
}
else {
return super.getIcon(score);
Expand Down
Loading

0 comments on commit dc5c058

Please sign in to comment.