Skip to content

Commit

Permalink
Java 17 features in code. Fixed tests for windows (#882)
Browse files Browse the repository at this point in the history
  • Loading branch information
altro3 authored Nov 27, 2023
1 parent e8c6d59 commit 2d20a51
Show file tree
Hide file tree
Showing 21 changed files with 96 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
Expand All @@ -80,7 +79,7 @@ public abstract class MicronautAotPlugin implements Plugin<Project> {
public static final String OPTIMIZED_DIST_NAME = "optimized";
public static final String MAIN_BINARY_NAME = "main";

static final List<String> TYPES_TO_CHECK = Collections.unmodifiableList(Arrays.asList(
static final List<String> TYPES_TO_CHECK = List.of(
"io.reactivex.Observable",
"reactor.core.publisher.Flux",
"kotlinx.coroutines.flow.Flow",
Expand All @@ -96,9 +95,10 @@ public abstract class MicronautAotPlugin implements Plugin<Project> {
"io.methvin.watchservice.MacOSXListeningWatchService",
"io.micronaut.core.async.publisher.CompletableFuturePublisher",
"io.micronaut.core.async.publisher.Publishers.JustPublisher",
"io.micronaut.core.async.subscriber.Completable"));
"io.micronaut.core.async.subscriber.Completable"
);

public static final List<String> SERVICE_TYPES = Collections.unmodifiableList(Arrays.asList(
public static final List<String> SERVICE_TYPES = List.of(
"io.micronaut.context.env.PropertySourceLoader",
"io.micronaut.inject.BeanConfiguration",
"io.micronaut.inject.BeanDefinitionReference",
Expand All @@ -107,7 +107,7 @@ public abstract class MicronautAotPlugin implements Plugin<Project> {
"io.micronaut.core.beans.BeanIntrospectionReference",
"io.micronaut.core.convert.TypeConverterRegistrar",
"io.micronaut.context.env.PropertyExpressionResolver"
));
);
public static final String AOT_APPLICATION_CLASSPATH = "aotApplicationClasspath";
public static final String OPTIMIZED_RUNTIME_CLASSPATH_CONFIGURATION_NAME = "optimizedRuntimeClasspath";
public static final String DEFAULT_GENERATED_PACKAGE = "io.micronaut.aot.generated";
Expand Down Expand Up @@ -139,7 +139,7 @@ private void configureAotDefaults(Project project, AOTExtension aotExtension) {
JavaApplication javaApplication = project.getExtensions().findByType(JavaApplication.class);
if (javaApplication != null) {
String mainClass = javaApplication.getMainClass().get();
return mainClass.contains(".") ? mainClass.substring(0, mainClass.lastIndexOf(".")) : DEFAULT_GENERATED_PACKAGE;
return mainClass.contains(".") ? mainClass.substring(0, mainClass.lastIndexOf('.')) : DEFAULT_GENERATED_PACKAGE;
}
return DEFAULT_GENERATED_PACKAGE;
}));
Expand All @@ -160,9 +160,9 @@ 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 -> {
task.setDescription("Generates Micronaut AOT sample configuration files");
});
TaskProvider<Task> createAotSampleConfigurationFiles = tasks.register("createAotSampleConfigurationFiles", task ->
task.setDescription("Generates Micronaut AOT sample configuration files")
);
for (OptimizerIO.TargetRuntime targetRuntime : OptimizerIO.TargetRuntime.values()) {
TaskProvider<MicronautAotSampleConfTask> createSample = tasks.register("createAot" + targetRuntime.getCapitalizedName() + "Sample", MicronautAotSampleConfTask.class, task -> {
task.setDescription("Creates a sample " + targetRuntime.getCapitalizedName() + " AOT configuration file");
Expand Down Expand Up @@ -320,13 +320,13 @@ private void registerJavaExecOptimizedRun(Project project,
// https://github.com/micronaut-projects/micronaut-gradle-plugin/issues/385
task.getOutputs().upToDateWhen(t -> false);
task.setClasspath(project.files(jarTask, optimizedRuntimeClasspath));
task.doFirst(new Action<Task>() {
task.doFirst(new Action<>() {
@Override
public void execute(Task t) {
if (task.getLogger().isDebugEnabled()) {
task.getLogger().debug(
"Running optimized entry point: " + task.getMainClass().get() +
"\nClasspath:\n " + task.getClasspath().getFiles()
"\nClasspath:\n " + 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 @@ -46,13 +46,10 @@ public String getSimpleName() {
}

public String getCapitalizedName() {
switch (this) {
case JIT:
return "Jit";
case NATIVE:
return "Native";
}
return name();
return switch (this) {
case JIT -> "Jit";
case NATIVE -> "Native";
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ public void process() throws IOException {
if (Files.isDirectory(path)) {
Files.createDirectories(target);
} else if (Files.isRegularFile(path)) {
String contents = new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
String contents = Files.readString(path);
for (Map.Entry<String, String> entry : replacements.entrySet()) {
contents = contents.replace(entry.getKey(), entry.getValue());
}
Files.write(target, contents.getBytes(StandardCharsets.UTF_8));
Files.writeString(target, contents);
}
} catch (IOException e) {
throw new GradleException("Unable to create target directory " + target, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ private Optional<TaskProvider<CRaCFinalDockerfile>> configureFinalDockerBuild(Pr
}
task.getImages().set(Collections.singletonList(project.getName()));
task.getInputDir().set(dockerFileTask.flatMap(Dockerfile::getDestDir));
task.doLast(new Action<Task>() {
task.doLast(new Action<>() {
@Override
public void execute(Task t) {
t.getLogger().warn("**********************************************************");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private static String adaptTaskName(String baseName, String context) {
private void createDockerImage(Project project, MicronautDockerImage imageSpec) {
TaskContainer tasks = project.getTasks();
String imageName = imageSpec.getName();
project.getLogger().info("Creating docker tasks for image " + imageName);
project.getLogger().info("Creating docker tasks for image {}", imageName);
TaskProvider<BuildLayersTask> buildLayersTask = tasks.register(adaptTaskName("buildLayers", imageName), BuildLayersTask.class, task -> {
task.setGroup(BasePlugin.BUILD_GROUP);
task.setDescription("Builds application layers for use in a Docker container (" + imageName + " image)");
Expand Down Expand Up @@ -307,7 +307,7 @@ private TaskProvider<NativeImageDockerfile> configureNativeDockerBuild(Project p
);
task.getRemotePath().set("/function/function.zip");
task.getHostPath().set(lambdaZip);
task.doLast(new Action<Task>() {
task.doLast(new Action<>() {
@Override
public void execute(Task task1) {
System.out.println("AWS Lambda ZIP built: " + lambdaZip.get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import javax.inject.Inject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand All @@ -56,9 +55,9 @@
*/
public abstract class NativeImageDockerfile extends Dockerfile implements DockerBuildOptions {

private static final List<Integer> SUPPORTED_JAVA_VERSIONS = Collections.unmodifiableList(
private static final List<Integer> SUPPORTED_JAVA_VERSIONS = List.of(
// keep those in descending order
Arrays.asList(17)
17
);
private static final String ARM_ARCH = "aarch64";
private static final String X86_64_ARCH = "x64";
Expand Down Expand Up @@ -373,10 +372,10 @@ public DirectoryProperty getPgoProfilesDirectory() {
);

//noinspection Convert2Lambda
doLast(new Action<Task>() {
doLast(new Action<>() {
@Override
public void execute(Task task) {
java.io.File f = NativeImageDockerfile.this.getDestFile().get().getAsFile();
java.io.File f = getDestFile().get().getAsFile();
System.out.println("Dockerfile written to: " + f.getAbsolutePath());
}
});
Expand Down Expand Up @@ -593,7 +592,7 @@ private List<String> buildNativeImageCommandLineArgs(Provider<String> executable

private static Integer toMajorVersion(String version) {
if (version.contains(".")) {
return Integer.parseInt(version.substring(0, version.indexOf(".")));
return Integer.parseInt(version.substring(0, version.indexOf('.')));
}
return Integer.parseInt(version);
}
Expand Down Expand Up @@ -696,7 +695,7 @@ public NativeImageDockerfile baseImage(String imageName) {

@Override
public DockerBuildOptions exportPorts(Integer... ports) {
this.getExposedPorts().set(Arrays.asList(ports));
this.getExposedPorts().set(List.of(ports));
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,11 @@ private static Provider<Directory> layerDirectoryOf(Layer layer,
Provider<Directory> libsDir,
Provider<Directory> resourcesDir,
Provider<Directory> classesDir) {
Provider<Directory> layerDir = libsDir;
switch (layer.getLayerKind().get()) {
case APP:
layerDir = appDir;
break;
case LIBS:
layerDir = libsDir;
break;
case EXPANDED_CLASSES:
layerDir = classesDir;
break;
case EXPANDED_RESOURCES:
layerDir = resourcesDir;
break;
}
return layerDir;
return switch (layer.getLayerKind().get()) {
case APP -> appDir;
case LIBS -> libsDir;
case EXPANDED_CLASSES -> classesDir;
case EXPANDED_RESOURCES -> resourcesDir;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ class BasicMicronautAOTSpec extends AbstractAOTPluginSpec {

private String subpath(File fullpath, File basePath) {
def path = fullpath.getAbsolutePath()
path.substring(basePath.getAbsolutePath().length() + 1, path.size())
path.substring(basePath.getAbsolutePath().length() + 1, path.size()).replaceAll("\\\\", "/")
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import org.gradle.language.jvm.tasks.ProcessResources;

import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
Expand All @@ -44,13 +42,13 @@ public class MicronautGraalPlugin implements Plugin<Project> {

public static final String RICH_OUTPUT_PROPERTY = "io.micronaut.graalvm.rich.output";

private static final Set<String> SOURCE_SETS = Collections.unmodifiableSet(new HashSet<>(Arrays.asList("main", "test")));
private static final List<String> GRAALVM_MODULE_EXPORTS = Collections.unmodifiableList(Arrays.asList(
private static final Set<String> SOURCE_SETS = Set.of("main", "test");
private static final List<String> GRAALVM_MODULE_EXPORTS = List.of(
"org.graalvm.nativeimage.builder/com.oracle.svm.core.configure",
"org.graalvm.nativeimage.builder/com.oracle.svm.core.jdk",
"org.graalvm.nativeimage.builder/com.oracle.svm.core.jni",
"org.graalvm.sdk/org.graalvm.nativeimage.impl"
));
);

@Override
public void apply(Project project) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public AnnotationProcessingConfigBuilder group(String name) {

@Override
public AnnotationProcessingConfigBuilder annotations(String... annotations) {
if (annotations != null && annotations.length > 0) {
if (annotations != null) {
for (String annotation : annotations) {
if (annotation != null) {
this.annotations.add(annotation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ private static void configureSourceSet(Project project, SourceSet sourceSet, Con
.getAnnotationProcessorConfigurationName();
String implementationConfigurationName = sourceSet
.getImplementationConfigurationName();
List<String> both = Arrays.asList(
List<String> both = List.of(
implementationConfigurationName,
annotationProcessorConfigurationName
);
Expand Down Expand Up @@ -214,13 +214,13 @@ private void configureJava(Project project, TaskContainer tasks) {
if (!annotations.isEmpty()) {
compilerArgs.add("-Amicronaut.processing.annotations=" + String.join(",", annotations));
} else {
if (group.length() > 0) {
if (!group.isEmpty()) {
compilerArgs.add("-Amicronaut.processing.annotations=" + group + ".*");
}
}
}

if (group.length() > 0) {
if (!group.isEmpty()) {
compilerArgs.add("-Amicronaut.processing.group=" + group);
compilerArgs.add("-Amicronaut.processing.module=" + module);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptions;
import org.jetbrains.kotlin.gradle.plugin.KaptExtension;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
Expand All @@ -34,17 +33,17 @@
* @since 1.0.0
*/
public class MicronautKotlinSupport {
private static final String[] KAPT_CONFIGURATIONS = new String[]{
private static final String[] KAPT_CONFIGURATIONS = {
"kapt",
"kaptTest"
};
private static final String[] KSP_CONFIGURATIONS = new String[]{
private static final String[] KSP_CONFIGURATIONS = {
"ksp",
"kspTest"
};
public static final String KOTLIN_PROCESSORS = "kotlinProcessors";

private static final List<String> KSP_ANNOTATION_PROCESSOR_MODULES = Arrays.asList("inject-kotlin");
private static final List<String> KSP_ANNOTATION_PROCESSOR_MODULES = List.of("inject-kotlin");

public static void whenKotlinSupportPresent(Project p, Consumer<? super Project> action) {
p.getPluginManager().withPlugin("org.jetbrains.kotlin.jvm", unused -> action.accept(p));
Expand Down Expand Up @@ -97,7 +96,7 @@ private static void configureKsp(Project project) {
final String module = processingConfig.getModule().getOrElse(project.getName());
if (isIncremental) {
kspExtension.arg("micronaut.processing.incremental", "true");
if (group.length() > 0) {
if (!group.isEmpty()) {
kspExtension.arg("micronaut.processing.group", group);
}
kspExtension.arg("micronaut.processing.module", module);
Expand All @@ -124,12 +123,12 @@ private static void configureKapt(Project project) {
if (!annotations.isEmpty()) {
options.arg("micronaut.processing.annotations", String.join(",", annotations));
} else {
if (group.length() > 0) {
if (!group.isEmpty()) {
options.arg("micronaut.processing.annotations", group + ".*");
}
}

if (group.length() > 0) {
if (!group.isEmpty()) {
options.arg("micronaut.processing.group", group);
}
options.arg("micronaut.processing.module", module);
Expand Down Expand Up @@ -208,7 +207,7 @@ private static void configureAdditionalSourceSet(String compilerType,
String annotationProcessorConfigurationName = compilerType + Strings.capitalize(sourceSet.getName());
String implementationConfigurationName = sourceSet
.getImplementationConfigurationName();
List<String> both = Arrays.asList(
List<String> both = List.of(
implementationConfigurationName,
annotationProcessorConfigurationName
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,8 @@
import org.gradle.api.tasks.TaskContainer;

import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
Expand All @@ -56,12 +54,10 @@ public class MicronautMinimalApplicationPlugin implements Plugin<Project> {
// This flag is used for testing purposes only
public static final String INTERNAL_CONTINUOUS_FLAG = "io.micronaut.internal.gradle.continuous";

private static final Map<String, String> LOGGER_CONFIG_FILE_TO_DEPENDENCY = Collections.unmodifiableMap(new HashMap<String, String>() {
{
put("logback.xml", "ch.qos.logback:logback-classic");
put("simplelogger.properties", "org.slf4j:slf4j-simple");
}
});
private static final Map<String, String> LOGGER_CONFIG_FILE_TO_DEPENDENCY = Map.of(
"logback.xml", "ch.qos.logback:logback-classic",
"simplelogger.properties", "org.slf4j:slf4j-simple"
);

@Override
public void apply(Project project) {
Expand Down Expand Up @@ -119,7 +115,7 @@ private void configureJavaExecTasks(Project project, Configuration developmentOn
sysProps.put("micronaut.io.watch.restart", true);
sysProps.put("micronaut.io.watch.enabled", true);
//noinspection Convert2Lambda
javaExec.doFirst(new Action<Task>() {
javaExec.doFirst(new Action<>() {
@Override
public void execute(Task workaroundEagerSystemProps) {
String watchPaths = sourceSet
Expand Down Expand Up @@ -205,7 +201,7 @@ private void configureGoogleCloudFunctionRuntime(Project project, Project p, Dep
run.dependsOn(taskContainer.findByName("processResources"), taskContainer.findByName("classes"));
run.getMainClass().set("com.google.cloud.functions.invoker.runner.Invoker");
run.setClasspath(ic);
run.setArgs(Arrays.asList(
run.setArgs(List.of(
"--target", "io.micronaut.gcp.function.http.HttpFunction",
"--port", 8080
));
Expand Down
Loading

0 comments on commit 2d20a51

Please sign in to comment.