diff --git a/log-utils/build.gradle b/log-utils/build.gradle index b3e55be..17b8dfe 100644 --- a/log-utils/build.gradle +++ b/log-utils/build.gradle @@ -14,9 +14,22 @@ version = gitversion.tagOffset println "Version: $version" +final java11 = sourceSets.create('java11') + java { toolchain.languageVersion = JavaLanguageVersion.of 8 withSourcesJar() + + registerFeature(java11.name) { + usingSourceSet java11 + capability project.group.toString(), project.name, project.version.toString() + disablePublication() + } +} + +configurations { + named(java11.implementationConfigurationName) { extendsFrom implementation } + named(java11.compileOnlyConfigurationName) { extendsFrom compileOnly } } dependencies { @@ -25,7 +38,10 @@ dependencies { tasks.named('jar', Jar) { manifest { - attributes('Automatic-Module-Name': 'net.minecraftforge.utils.logging') + attributes([ + 'Automatic-Module-Name': 'net.minecraftforge.utils.logging', + 'Multi-Release' : 'true' + ]) attributes([ 'Specification-Title' : projectDisplayName, 'Specification-Vendor' : projectVendor, @@ -34,6 +50,16 @@ tasks.named('jar', Jar) { 'Implementation-Vendor' : projectVendor, 'Implementation-Version': project.version ], 'net/minecraftforge/util/logging/') + + into('META-INF/versions/11') { + from java11.output + } + } +} + +tasks.named(java11.compileJavaTaskName, JavaCompile) { + javaCompiler = javaToolchains.compilerFor { + languageVersion = JavaLanguageVersion.of 11 } } diff --git a/log-utils/src/java11/java/net/minecraftforge/util/logging/MultiReleaseMethods.java b/log-utils/src/java11/java/net/minecraftforge/util/logging/MultiReleaseMethods.java new file mode 100644 index 0000000..604c7fd --- /dev/null +++ b/log-utils/src/java11/java/net/minecraftforge/util/logging/MultiReleaseMethods.java @@ -0,0 +1,24 @@ +/* + * Copyright (c) Forge Development LLC + * SPDX-License-Identifier: LGPL-2.1-only + */ +package net.minecraftforge.util.logging; + +import java.util.Map; + +/** + * Utility class for methods that are exclusively used in multi-release form. (Java 11+) + */ +final class MultiReleaseMethods { + static final String INDENT_STRING = " "; + + static Map.Entry mapEntry(K key, V value) { + return Map.entry(key, value); + } + + static String getIndentation(byte indent) { + return INDENT_STRING.repeat(indent); + } + + private MultiReleaseMethods() { } +} diff --git a/log-utils/src/main/java/net/minecraftforge/util/logging/Log.java b/log-utils/src/main/java/net/minecraftforge/util/logging/Log.java index 6393732..e0d50a7 100644 --- a/log-utils/src/main/java/net/minecraftforge/util/logging/Log.java +++ b/log-utils/src/main/java/net/minecraftforge/util/logging/Log.java @@ -11,6 +11,7 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import java.util.Map; import java.util.function.BiConsumer; import java.util.function.Consumer; @@ -88,30 +89,13 @@ public static void pop(byte indent) { static String getIndentation() { String ret = INDENT_CACHE[indentLevel]; //noinspection ConstantValue -- IntelliJ skill issue - return ret == null ? INDENT_CACHE[indentLevel] = getIndentation(indentLevel) : ret; - } - - private static String getIndentation(byte indent) { - StringBuilder builder = new StringBuilder(INDENT_STRING.length() * indent); - for (int i = 0; i < indent; i++) - builder.append(INDENT_STRING); - return builder.toString(); + return ret == null ? INDENT_CACHE[indentLevel] = MultiReleaseMethods.getIndentation(indentLevel) : ret; } /* CAPTURING */ - private static @UnknownNullability List CAPTURED; - - private static final class CapturedMessage { - private final Level level; - private final String message; - - private CapturedMessage(Level level, String message) { - this.level = level; - this.message = message; - } - } + private static @UnknownNullability List> CAPTURED; /** * If the log is capturing log messages. @@ -136,7 +120,7 @@ public static void capture() { static void tryCapture(Consumer logger, Level level, String message) { if (CAPTURED != null) - CAPTURED.add(new CapturedMessage(level, message)); + CAPTURED.add(MultiReleaseMethods.mapEntry(level, message)); else logger.accept(message); } @@ -163,17 +147,18 @@ public static void release() { /** * Releases all captured log messages into the given consumer, then stops capturing. * - * @param consumer The consumer to release the captured log messages to + * @param consumer The consumer to release the captured log messages to. The first input is the {@link Level} of the + * message, the second input is the message itself. * @see #capture() */ public static void release(BiConsumer consumer) { if (CAPTURED == null) return; - Iterator itor = CAPTURED.iterator(); + Iterator> itor = CAPTURED.iterator(); CAPTURED = null; while (itor.hasNext()) { - CapturedMessage capture = itor.next(); - consumer.accept(capture.level, capture.message); + Map.Entry capture = itor.next(); + consumer.accept(capture.getKey(), capture.getValue()); } } diff --git a/log-utils/src/main/java/net/minecraftforge/util/logging/MultiReleaseMethods.java b/log-utils/src/main/java/net/minecraftforge/util/logging/MultiReleaseMethods.java new file mode 100644 index 0000000..e5f4143 --- /dev/null +++ b/log-utils/src/main/java/net/minecraftforge/util/logging/MultiReleaseMethods.java @@ -0,0 +1,29 @@ +/* + * Copyright (c) Forge Development LLC + * SPDX-License-Identifier: LGPL-2.1-only + */ +package net.minecraftforge.util.logging; + +import java.util.AbstractMap; +import java.util.Map; + +/** + * Utility class for methods that are exclusively used in multi-release form. (Java 8) + */ +final class MultiReleaseMethods { + static final String INDENT_STRING = " "; + + static Map.Entry mapEntry(K key, V value) { + return new AbstractMap.SimpleImmutableEntry<>(key, value); + } + + static String getIndentation(byte indent) { + StringBuilder builder = new StringBuilder(INDENT_STRING.length() * indent); + for (int i = 0; i < indent; i++) { + builder.append(INDENT_STRING); + } + return builder.toString(); + } + + private MultiReleaseMethods() { } +}