Skip to content

Commit

Permalink
chore: fix bug patterns identified by Error Prone
Browse files Browse the repository at this point in the history
  • Loading branch information
murdos committed Dec 18, 2024
1 parent 5fd1968 commit d155afb
Show file tree
Hide file tree
Showing 27 changed files with 134 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import static tech.jhipster.lite.module.domain.JHipsterModule.to;
import static tech.jhipster.lite.module.domain.JHipsterModule.versionSlug;

import java.util.Locale;
import tech.jhipster.lite.module.domain.JHipsterModule;
import tech.jhipster.lite.module.domain.docker.DockerImages;
import tech.jhipster.lite.module.domain.file.JHipsterSource;
Expand Down Expand Up @@ -37,7 +38,7 @@ public JHipsterModule buildModule(JHipsterModuleProperties properties) {

String jwtBase64secret = properties.getOrDefaultString(JWT_BASE_64_SECRET, Base64Utils.getBase64Secret());
String baseName = properties.projectBaseName().get();
String lowerCaseBaseName = baseName.toLowerCase();
String lowerCaseBaseName = baseName.toLowerCase(Locale.ROOT);

//@formatter:off
return moduleBuilder(properties)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static tech.jhipster.lite.module.domain.JHipsterModule.to;

import java.time.Year;
import java.time.ZoneId;
import tech.jhipster.lite.module.domain.JHipsterModule;
import tech.jhipster.lite.module.domain.file.JHipsterSource;
import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties;
Expand All @@ -17,7 +18,7 @@ public JHipsterModule buildMitModule(JHipsterModuleProperties properties) {
//@formatter:off
return moduleBuilder(properties)
.context()
.put("currentYear", Year.now().getValue())
.put("currentYear", Year.now(ZoneId.systemDefault()).getValue())
.and()
.files()
.add(SOURCE.template("MIT.txt"), to("LICENSE.txt"))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tech.jhipster.lite.module.domain;

import java.util.Locale;
import org.apache.commons.lang3.StringUtils;
import tech.jhipster.lite.shared.error.domain.Assert;

Expand All @@ -11,7 +12,7 @@ public record DocumentationTitle(String title) {
}

public String filename() {
return StringUtils.stripAccents(title.toLowerCase()).replaceAll("\\W", SEPARATOR).replaceAll("-+", SEPARATOR);
return StringUtils.stripAccents(title.toLowerCase(Locale.ROOT)).replaceAll("\\W", SEPARATOR).replaceAll("-+", SEPARATOR);
}

public String get() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,10 @@ public boolean equals(Object obj) {
return true;
}

if (obj == null || getClass() != obj.getClass()) {
if (!(obj instanceof JHipsterSlug other)) {
return false;
}

JHipsterSlug other = (JHipsterSlug) obj;
return new EqualsBuilder().append(slug, other.slug).isEquals();
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package tech.jhipster.lite.module.domain.docker;

import java.util.Locale;
import tech.jhipster.lite.shared.error.domain.Assert;

public record DockerImageName(String imageName) {
public DockerImageName(String imageName) {
Assert.notBlank("imageName", imageName);

this.imageName = imageName.toLowerCase();
this.imageName = imageName.toLowerCase(Locale.ROOT);
}

public String get() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public Path pathInProject(JHipsterProjectFolder project) {
@Override
@ExcludeFromGeneratedCodeCoverage
public int hashCode() {
return new HashCodeBuilder().append(destination).hashCode();
return new HashCodeBuilder().append(destination).toHashCode();
}

@Override
Expand All @@ -65,11 +65,10 @@ public boolean equals(Object obj) {
return true;
}

if (obj == null || getClass() != obj.getClass()) {
if (!(obj instanceof JHipsterDestination other)) {
return false;
}

JHipsterDestination other = (JHipsterDestination) obj;
return new EqualsBuilder().append(destination, other.destination).isEquals();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ record GitIgnorePattern(String value) implements GitIgnoreEntry {
Assert.notBlank("value", value);
}

@Override
public String get() {
return value;
}
Expand All @@ -28,6 +29,7 @@ public GitIgnoreComment(String value) {
this.value = value.startsWith(COMMENT_PREFIX) ? value : COMMENT_PREFIX + " " + value;
}

@Override
public String get() {
return value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ private GradleCorePlugin(GradleCorePluginBuilder builder) {
toolVersionSlug = Optional.ofNullable(builder.toolVersionSlug);
}

@Override
public GradlePluginId id() {
return id;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,16 @@ public Collection<Object> values() {

@Override
@ExcludeFromGeneratedCodeCoverage
@SuppressWarnings("UndefinedEquals")
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj == null || obj.getClass() != this.getClass()) {

if (!(obj instanceof PropertyValue that)) {
return false;
}
var that = (PropertyValue) obj;

return Objects.equals(this.values, that.values);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public String get() {
}

public String majorVersion() {
return version().split("\\.")[0];
return version().split("\\.", -1)[0];
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ default NpmPackageVersion get(String packageName, NpmVersionSourceFactory source
}

/**
* @return The version of Node.js.
* The version of Node.js.
*/
default NpmPackageVersion nodeVersion() {
return get("node", JHLiteNpmVersionSource.COMMON.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public boolean equals(Object obj) {
return false;
}
var that = (PackageJsonDependency) obj;
return Objects.equals(this.packageName, that.packageName) && this.versionSource == that.versionSource;
return Objects.equals(this.packageName, that.packageName) && Objects.equals(this.versionSource, that.versionSource);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tech.jhipster.lite.module.domain.properties;

import java.util.Locale;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
import tech.jhipster.lite.shared.error.domain.Assert;
Expand Down Expand Up @@ -37,10 +38,10 @@ public String capitalized() {
}

public String upperCased() {
return StringUtils.uncapitalize(name()).replaceAll("([A-Z])", "_$1").toUpperCase();
return StringUtils.uncapitalize(name()).replaceAll("([A-Z])", "_$1").toUpperCase(Locale.ROOT);
}

public String kebabCase() {
return StringUtils.uncapitalize(name()).replaceAll("([A-Z])", "-$1").toLowerCase();
return StringUtils.uncapitalize(name()).replaceAll("([A-Z])", "-$1").toLowerCase(Locale.ROOT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

/**
* {@link ElementReplacer} that inserts content at the end of the file if the provided condition is met
* @param condition
* @param condition that must be met to insert content at the end of the file
*/
public record EndOfFileReplacer(ReplacementCondition condition) implements ElementReplacer {
private static final Pattern EOF_PATTERN = Pattern.compile("\\z", Pattern.MULTILINE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,10 @@ public boolean equals(Object obj) {
return true;
}

if (obj == null || getClass() != obj.getClass()) {
if (!(obj instanceof JHipsterHiddenModules other)) {
return false;
}

JHipsterHiddenModules other = (JHipsterHiddenModules) obj;
return new EqualsBuilder().append(slugs, other.slugs).append(tags, other.tags).isEquals();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
import static tech.jhipster.lite.module.domain.JHipsterModule.LINE_BREAK;

import java.io.IOException;
import java.nio.file.*;
import java.util.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -14,7 +19,13 @@
import tech.jhipster.lite.module.domain.JHipsterModuleContext;
import tech.jhipster.lite.module.domain.file.TemplateRenderer;
import tech.jhipster.lite.module.domain.npm.NpmVersions;
import tech.jhipster.lite.module.domain.packagejson.*;
import tech.jhipster.lite.module.domain.packagejson.JHipsterModulePackageJson;
import tech.jhipster.lite.module.domain.packagejson.NodeModuleFormat;
import tech.jhipster.lite.module.domain.packagejson.PackageJsonDependencies;
import tech.jhipster.lite.module.domain.packagejson.PackageJsonDependency;
import tech.jhipster.lite.module.domain.packagejson.PackageName;
import tech.jhipster.lite.module.domain.packagejson.PackageNames;
import tech.jhipster.lite.module.domain.packagejson.Scripts;
import tech.jhipster.lite.module.domain.properties.JHipsterProjectFolder;
import tech.jhipster.lite.shared.collection.domain.JHipsterCollections;
import tech.jhipster.lite.shared.error.domain.Assert;
Expand Down Expand Up @@ -140,7 +151,7 @@ private String replaceType(Indentation indentation, Optional<NodeModuleFormat> n
.blockName("type")
.jsonContent(content)
.indentation(indentation)
.blockValue(nodeModuleFormat.orElseThrow().name().toLowerCase())
.blockValue(nodeModuleFormat.orElseThrow().name().toLowerCase(Locale.ROOT))
.apply();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,40 @@
package tech.jhipster.lite.module.infrastructure.secondary;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.yaml.snakeyaml.comments.CommentType.BLOCK;

import java.io.*;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;
import org.yaml.snakeyaml.*;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.DumperOptions.FlowStyle;
import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.comments.CommentLine;
import org.yaml.snakeyaml.constructor.Constructor;
import org.yaml.snakeyaml.nodes.*;
import org.yaml.snakeyaml.nodes.MappingNode;
import org.yaml.snakeyaml.nodes.Node;
import org.yaml.snakeyaml.nodes.NodeTuple;
import org.yaml.snakeyaml.nodes.ScalarNode;
import org.yaml.snakeyaml.nodes.SequenceNode;
import org.yaml.snakeyaml.nodes.Tag;
import org.yaml.snakeyaml.representer.Representer;
import tech.jhipster.lite.module.domain.Indentation;
import tech.jhipster.lite.module.domain.javaproperties.*;
import tech.jhipster.lite.module.domain.javaproperties.Comment;
import tech.jhipster.lite.module.domain.javaproperties.PropertyKey;
import tech.jhipster.lite.module.domain.javaproperties.PropertyValue;
import tech.jhipster.lite.shared.error.domain.Assert;
import tech.jhipster.lite.shared.error.domain.GeneratorException;
import tech.jhipster.lite.shared.generation.domain.ExcludeFromGeneratedCodeCoverage;
Expand Down Expand Up @@ -187,14 +206,14 @@ private MappingNode loadConfiguration(File yamlFile) throws IOException {
return new MappingNode(Tag.MAP, new ArrayList<>(), FlowStyle.AUTO);
}

try (FileReader reader = new FileReader(yamlFile)) {
try (FileReader reader = new FileReader(yamlFile, UTF_8)) {
return (MappingNode) yaml.compose(reader);
}
}

private void saveConfiguration(Node actualConfiguration) throws IOException {
Files.createDirectories(file.getParent());
Writer writer = new FileWriter(file.toFile());
Writer writer = new FileWriter(file.toFile(), UTF_8);
yaml.serialize(actualConfiguration, writer);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package tech.jhipster.lite.module.infrastructure.secondary.javadependency.gradle;

import static tech.jhipster.lite.module.domain.JHipsterModule.*;
import static tech.jhipster.lite.module.domain.JHipsterModule.LINE_BREAK;
import static tech.jhipster.lite.module.domain.JHipsterModule.from;
import static tech.jhipster.lite.module.domain.JHipsterModule.regex;
import static tech.jhipster.lite.module.domain.JHipsterModule.to;
import static tech.jhipster.lite.module.domain.replacement.ReplacementCondition.always;
import static tech.jhipster.lite.module.domain.replacement.ReplacementCondition.notMatchingRegex;
import static tech.jhipster.lite.module.infrastructure.secondary.javadependency.gradle.VersionsCatalog.libraryAlias;
Expand All @@ -10,24 +13,54 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import tech.jhipster.lite.module.domain.*;
import tech.jhipster.lite.module.domain.Indentation;
import tech.jhipster.lite.module.domain.JHipsterModuleContext;
import tech.jhipster.lite.module.domain.JHipsterProjectFilePath;
import tech.jhipster.lite.module.domain.buildproperties.BuildProperty;
import tech.jhipster.lite.module.domain.buildproperties.PropertyKey;
import tech.jhipster.lite.module.domain.file.*;
import tech.jhipster.lite.module.domain.gradleplugin.*;
import tech.jhipster.lite.module.domain.file.JHipsterDestination;
import tech.jhipster.lite.module.domain.file.JHipsterFileContent;
import tech.jhipster.lite.module.domain.file.JHipsterModuleFile;
import tech.jhipster.lite.module.domain.file.JHipsterSource;
import tech.jhipster.lite.module.domain.file.JHipsterTemplatedFile;
import tech.jhipster.lite.module.domain.file.JHipsterTemplatedFiles;
import tech.jhipster.lite.module.domain.gradleplugin.BuildGradleImport;
import tech.jhipster.lite.module.domain.gradleplugin.GradleCommunityPlugin;
import tech.jhipster.lite.module.domain.gradleplugin.GradleCommunityProfilePlugin;
import tech.jhipster.lite.module.domain.gradleplugin.GradleCorePlugin;
import tech.jhipster.lite.module.domain.gradleplugin.GradlePluginConfiguration;
import tech.jhipster.lite.module.domain.javabuild.DependencySlug;
import tech.jhipster.lite.module.domain.javabuild.command.*;
import tech.jhipster.lite.module.domain.javabuild.command.AddDirectJavaDependency;
import tech.jhipster.lite.module.domain.javabuild.command.AddDirectMavenPlugin;
import tech.jhipster.lite.module.domain.javabuild.command.AddGradleConfiguration;
import tech.jhipster.lite.module.domain.javabuild.command.AddGradlePlugin;
import tech.jhipster.lite.module.domain.javabuild.command.AddGradleTasksTestInstruction;
import tech.jhipster.lite.module.domain.javabuild.command.AddJavaBuildProfile;
import tech.jhipster.lite.module.domain.javabuild.command.AddJavaDependencyManagement;
import tech.jhipster.lite.module.domain.javabuild.command.AddMavenBuildExtension;
import tech.jhipster.lite.module.domain.javabuild.command.AddMavenPluginManagement;
import tech.jhipster.lite.module.domain.javabuild.command.RemoveDirectJavaDependency;
import tech.jhipster.lite.module.domain.javabuild.command.RemoveJavaDependencyManagement;
import tech.jhipster.lite.module.domain.javabuild.command.SetBuildProperty;
import tech.jhipster.lite.module.domain.javabuild.command.SetVersion;
import tech.jhipster.lite.module.domain.javabuildprofile.BuildProfileActivation;
import tech.jhipster.lite.module.domain.javabuildprofile.BuildProfileId;
import tech.jhipster.lite.module.domain.javadependency.JavaDependency;
import tech.jhipster.lite.module.domain.javadependency.JavaDependencyScope;
import tech.jhipster.lite.module.domain.properties.JHipsterProjectFolder;
import tech.jhipster.lite.module.domain.replacement.*;
import tech.jhipster.lite.module.domain.replacement.ContentReplacers;
import tech.jhipster.lite.module.domain.replacement.MandatoryFileReplacer;
import tech.jhipster.lite.module.domain.replacement.MandatoryReplacer;
import tech.jhipster.lite.module.domain.replacement.RegexNeedleBeforeReplacer;
import tech.jhipster.lite.module.domain.replacement.RegexReplacer;
import tech.jhipster.lite.module.infrastructure.secondary.FileSystemJHipsterModuleFiles;
import tech.jhipster.lite.module.infrastructure.secondary.FileSystemReplacer;
import tech.jhipster.lite.module.infrastructure.secondary.javadependency.JavaDependenciesCommandHandler;
Expand Down Expand Up @@ -345,8 +378,8 @@ private static String convertToKotlinFormat(BuildProperty property) {

private static String toCamelCasedKotlinVariable(PropertyKey key) {
return Arrays.stream(key.get().split("[.-]"))
.map(s -> s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase())
.collect(Collectors.collectingAndThen(Collectors.joining(), str -> str.substring(0, 1).toLowerCase() + str.substring(1)));
.map(s -> s.substring(0, 1).toUpperCase(Locale.ROOT) + s.substring(1).toLowerCase(Locale.ROOT))
.collect(Collectors.collectingAndThen(Collectors.joining(), str -> str.substring(0, 1).toLowerCase(Locale.ROOT) + str.substring(1)));
}

private boolean propertyExistsFrom(PropertyKey key, Path buildGradleFile) {
Expand Down
Loading

0 comments on commit d155afb

Please sign in to comment.