Skip to content

Commit

Permalink
Sonar code smells fixes (#894)
Browse files Browse the repository at this point in the history
  • Loading branch information
altro3 authored Dec 3, 2023
1 parent 98526ab commit b4e2b3e
Show file tree
Hide file tree
Showing 27 changed files with 62 additions and 44 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ Documentation for the development version of the plugin can be found [here](http

Documentation for the 3.x version of the plugin can be found [here](https://github.com/micronaut-projects/micronaut-gradle-plugin/tree/3.7.x#readme).

Documentation for the 2.x version of the plugin can be found [here](https://github.com/micronaut-projects/micronaut-gradle-plugin/tree/2.0.x#readme).
Documentation for the 2.x version of the plugin can be found [here](https://github.com/micronaut-projects/micronaut-gradle-plugin/tree/2.0.x#readme).
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public final void execute() throws IOException {
}
} finally {
onSuccess(outputDir);
argFile.delete();
Files.delete(argFile.toPath());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
*/
class AotShadowSupport {

private AotShadowSupport() {
}

static void registerShadowJar(Project project,
ArchiveOperations archiveOperations,
TaskContainer tasks,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,23 +77,21 @@ public abstract class MicronautAOTConfigWriterTask extends DefaultTask {
private static void booleanOptimization(Properties props, String optimizationId, Provider<Boolean> provider) {
if (provider.isPresent()) {
String key = optimizationId + ".enabled";
if (!props.containsKey(key)) {
props.put(key, String.valueOf(provider.get()));
}
props.computeIfAbsent(key, (k) -> String.valueOf(provider.get()));
}
}

private static void stringListParameter(Properties props, String parameter, ListProperty<String> provider) {
if (provider.isPresent()) {
List<String> elements = provider.get();
if (!props.containsKey(parameter) && !elements.isEmpty()) {
props.put(parameter, String.join(",", elements));
if (!elements.isEmpty()) {
props.computeIfAbsent(parameter, (k) -> String.join(",", elements));
}
}
}
private static void stringParameter(Properties props, String parameter, Property<String> provider) {
if (provider.isPresent()) {
props.put(parameter, provider.get());
props.setProperty(parameter, provider.get());
}
}

Expand All @@ -112,10 +110,10 @@ void writeConfigFile() {
props.putAll(optimizations.getConfigurationProperties().get());
}
if (!props.containsKey(KnownMissingTypesSourceGenerator.OPTION.key())) {
props.put(KnownMissingTypesSourceGenerator.OPTION.key(), String.join(",", MicronautAotPlugin.TYPES_TO_CHECK));
props.setProperty(KnownMissingTypesSourceGenerator.OPTION.key(), String.join(",", MicronautAotPlugin.TYPES_TO_CHECK));
}
if (!props.containsKey(AbstractStaticServiceLoaderSourceGenerator.SERVICE_TYPES)) {
props.put(AbstractStaticServiceLoaderSourceGenerator.SERVICE_TYPES, String.join(",", MicronautAotPlugin.SERVICE_TYPES));
props.setProperty(AbstractStaticServiceLoaderSourceGenerator.SERVICE_TYPES, String.join(",", MicronautAotPlugin.SERVICE_TYPES));
}
booleanOptimization(props, GraalVMOptimizationFeatureSourceGenerator.ID, getForNative());
booleanOptimization(props, LogbackConfigurationSourceGenerator.ID, optimizations.getReplaceLogbackXml());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ private void registerPrepareOptimizationsTasks(Project project, Configurations c
}

private void registerCreateSamplesTasks(Project project, Configuration optimizerRuntimeClasspath, Configuration applicationClasspath, TaskContainer tasks, AOTExtension aotExtension) {
TaskProvider<Task> createAotSampleConfigurationFiles = tasks.register("createAotSampleConfigurationFiles", task ->
TaskProvider<Task> createAotSampleConfigurationFiles = tasks.register("createAotSampleConfigurationFiles", task ->
task.setDescription("Generates Micronaut AOT sample configuration files")
);
for (OptimizerIO.TargetRuntime targetRuntime : OptimizerIO.TargetRuntime.values()) {
Expand Down Expand Up @@ -325,8 +325,8 @@ private void registerJavaExecOptimizedRun(Project project,
public void execute(Task t) {
if (task.getLogger().isDebugEnabled()) {
task.getLogger().debug(
"Running optimized entry point: " + task.getMainClass().get() +
"\nClasspath:\n " + task.getClasspath().getFiles()
"Running optimized entry point: {}\nClasspath:\n {}",
task.getMainClass().get(), task.getClasspath().getFiles()
.stream()
.map(File::getName)
.collect(Collectors.joining("\n "))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected void configureExtraArguments(List<String> args) {
protected void onSuccess(File outputDir) {
File sampleFile = new File(outputDir, getTargetRuntime().map(runtime -> runtime.getSimpleName() + ".properties").orElse("sample.properties").get());
if (sampleFile.exists()) {
System.out.println("Sample configuration file written to " + Strings.clickableUrl(sampleFile));
getLogger().lifecycle("Sample configuration file written to {}", Strings.clickableUrl(sampleFile));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ extensions.create("functionalTesting", FunctionalTestingExtension)

def prepareRepository = tasks.register("prepareRepository", Sync) {
from configurations.pluginsUnderTest
into file("${buildDir}/aggregatedRepo")
into layout.buildDirectory.dir("aggregatedRepo").get().asFile
}

tasks.withType(Test).configureEach { Test test ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.gradle.api.tasks.TaskAction;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public interface CRaCConfiguration {

/**
* The platform to specify in the FROM instruction defaults to {@value MicronautCRaCPlugin#CRAC_DEFAULT_BASE_IMAGE_PLATFORM}.
* @return the platform (can be removed with {@code platform.convention(null)} in the {@link CRaCConfiguration} extension)
* @return the platform (can be removed with {@code platform.convention(null)} in the CRaCConfiguration extension)
* @deprecated use {@link #getArch()} instead
*/
@SuppressWarnings("java:S6355") // We need Java 8... Java 8 doesn't have forRemoval and since
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ private TaskProvider<NativeImageDockerfile> configureNativeDockerBuild(Project p
task.doLast(new Action<>() {
@Override
public void execute(Task task1) {
System.out.println("AWS Lambda ZIP built: " + lambdaZip.get());
project.getLogger().lifecycle("AWS Lambda ZIP built: {}", lambdaZip.get());
}
});
task.finalizedBy(removeContainer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void create() throws IOException {
if (getDockerfileTweaks().isPresent()) {
DockerfileEditor.apply(getObjects(), this, getDockerfileTweaks().get());
}
System.out.println("Dockerfile written to: " + getDestFile().get().getAsFile().getAbsolutePath());
getLogger().lifecycle("Dockerfile written to: {}", getDestFile().get().getAsFile().getAbsolutePath());
}

protected void setupInstructions(List<Instruction> additionalInstructions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ public DirectoryProperty getPgoProfilesDirectory() {
@Override
public void execute(Task task) {
java.io.File f = getDestFile().get().getAsFile();
System.out.println("Dockerfile written to: " + f.getAbsolutePath());
getLogger().lifecycle("Dockerfile written to: {}", f.getAbsolutePath());
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
*/
public final class GraalUtil {

private GraalUtil() {
}

/**
* @return Return whether the JVM in use a GraalVM JVM.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void apply(Project project) {
}
);
TaskContainer tasks = project.getTasks();
project.getPluginManager().withPlugin("application", plugin -> {
project.getPluginManager().withPlugin("application", plugin ->
tasks.withType(BuildNativeImageTask.class).named("nativeCompile", nativeImageTask -> {
MicronautRuntime mr = PluginsHelper.resolveRuntime(project);
if (mr.isLambdaProvided()) {
Expand All @@ -98,8 +98,7 @@ public void apply(Project project) {
nativeImageTask.getOptions().get().getMainClass().set(nativeLambdaExtension.getLambdaRuntimeClassName());
}
}
});
});
}));
}

private void workaroundForResourcesDirectoryMissing(Project project) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
public class LenientGradle {
private static final Map<String, Optional<Class<?>>> CLASSES = new ConcurrentHashMap<>();

private LenientGradle() {
}

private static Optional<Class<?>> findClass(String name) {
return CLASSES.computeIfAbsent(name, LenientGradle::loadClass);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.gradle.api.tasks.testing.Test;

import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ public abstract class PluginsHelper {
VALIDATION_VERSION_PROPERTY
);

private PluginsHelper() {
}

public static void maybeAddMicronautPlaformBom(Project p, Configuration configuration) {
MicronautExtension micronautExtension = p.getExtensions().findByType(MicronautExtension.class);
configuration.getDependencies().addAllLater(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
public class ShadowPluginSupport {
public static final String SHADOW_PLUGIN = "com.github.johnrengelman.shadow";

private ShadowPluginSupport() {
}

public static void withShadowPlugin(Project p, Runnable action) {
p.getPluginManager().withPlugin(SHADOW_PLUGIN, unused -> action.run());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@
import static java.util.Locale.ENGLISH;

public abstract class Strings {

private Strings() {
}

public static String capitalize(String name) {
if (name == null || name.length() == 0) {
if (name == null || name.isEmpty()) {
return name;
}
return name.substring(0, 1).toUpperCase(ENGLISH) + name.substring(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
*/
public final class GraalUtil {

private GraalUtil() {
}

/**
* @return Return whether the JVM in use a GraalVM JVM.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.util.List;

import io.micronaut.openapi.generator.MicronautCodeGeneratorBuilder;
import io.micronaut.openapi.generator.MicronautCodeGeneratorOptionsBuilder.GeneratorLanguage;

import org.gradle.api.provider.ListProperty;
import org.gradle.api.provider.Property;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package io.micronaut.gradle.openapi.tasks;

import io.micronaut.openapi.generator.MicronautCodeGeneratorBuilder;
import io.micronaut.openapi.generator.MicronautCodeGeneratorOptionsBuilder.GeneratorLanguage;

import org.gradle.api.provider.Property;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ private static Boolean expectBoolean(TomlTable table, String element) {
private void parseLibrary(String alias, TomlTable librariesTable, RichVersionParser strictVersionParser) {
Object gav = librariesTable.get(alias);
TomlPosition position = librariesTable.inputPositionOf(alias);
if (gav instanceof String) {
List<String> splitted = splitToList((String) gav);
if (gav instanceof String gavStr) {
List<String> splitted = splitToList(gavStr);
if (splitted.size() == 3) {
String group = splitted.get(0);
String name = splitted.get(1);
Expand Down Expand Up @@ -147,9 +147,8 @@ private void parseLibrary(String alias, TomlTable librariesTable, RichVersionPar
}
}
VersionModel versionModel = null;
if (version instanceof String) {
String require = (String) version;
RichVersion richVersion = strictVersionParser.parse(require);
if (version instanceof String requireVersion) {
RichVersion richVersion = strictVersionParser.parse(requireVersion);
versionModel = new VersionModel(null, richVersion, position);
} else if (version instanceof TomlTable versionTable) {
String versionRef = versionTable.getString("ref");
Expand Down Expand Up @@ -182,8 +181,8 @@ private void parseVersion(String alias, TomlTable versionsTable, RichVersionPars
Boolean rejectAll = null;
Object version = versionsTable.get(alias);
TomlPosition position = versionsTable.inputPositionOf(alias);
if (version instanceof String) {
require = (String) version;
if (version instanceof String versionStr) {
require = versionStr;
RichVersion richVersion = strictVersionParser.parse(require);
model.addVersion(new VersionModel(alias, richVersion, position));
} else if (version instanceof TomlTable versionTable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ private Provider<String> readFromVersionCatalog(Settings settings) {
TomlTable versions = toml.getTable("versions");
if (versions != null) {
Object micronaut = versions.get("micronaut");
if (micronaut instanceof String) {
return (String) micronaut;
if (micronaut instanceof String micronautVer) {
return micronautVer;
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ public void addLibrary(Library library) {
}

public void addVersion(VersionModel version) {
assert version.getReference() != null && version.getVersion() != null;
if (version.getReference() == null || version.getVersion() == null) {
throw new IllegalArgumentException("version.getReference() is null or version.getVersion() is null");
}
versions.add(version);
versionAliasToVersion.put(version.getReference(), version);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ static ArrayList<Integer> parseVersion(String testedVersion) {
}
return Arrays.stream(version.split("\\."))
.map(String::trim)
.map(s -> s.replaceAll("[^0-9]", ""))
.map(s -> s.replaceAll("\\D", ""))
.map(Integer::parseInt)
.collect(Collectors.toCollection(ArrayList::new));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,20 @@
* issues.
*/
public final class TestResourcesGraalVM {

public static final String ENABLED_PROPERTY_NAME = "testresources.native";

private TestResourcesGraalVM() {
}

public static void configure(Project project,
Configuration client,
TaskProvider<StartTestResourcesService> internalStart) {
GraalVMExtension graalVMExtension = project.getExtensions().findByType(GraalVMExtension.class);
graalVMExtension.getBinaries().all(b -> {
b.getRuntimeArgs().addAll(internalStart.map(task -> {
ServerConnectionParametersProvider provider = new ServerConnectionParametersProvider(task.getSettingsDirectory());
return provider.asArguments();
}));
});
graalVMExtension.getBinaries().all(b -> b.getRuntimeArgs().addAll(internalStart.map(task -> {
ServerConnectionParametersProvider provider = new ServerConnectionParametersProvider(task.getSettingsDirectory());
return provider.asArguments();
})));
ProviderFactory providers = project.getProviders();
boolean includeClient = Boolean.TRUE.equals(providers
.systemProperty(ENABLED_PROPERTY_NAME)
Expand Down

0 comments on commit b4e2b3e

Please sign in to comment.