diff --git a/build.gradle b/build.gradle index ea703370..ab444c93 100644 --- a/build.gradle +++ b/build.gradle @@ -92,7 +92,7 @@ dependencies { implementation ('net.fabricmc:cfr:0.2.0') // source code remapping - implementation ('net.fabricmc:mercury:0.2.7') + implementation ('net.fabricmc:mercury:0.3.0') // Kotlin implementation('org.jetbrains.kotlinx:kotlinx-metadata-jvm:0.5.0') { diff --git a/src/main/java/net/fabricmc/loom/task/service/SourceRemapperService.java b/src/main/java/net/fabricmc/loom/task/service/SourceRemapperService.java index 80c55f90..b3f5456c 100644 --- a/src/main/java/net/fabricmc/loom/task/service/SourceRemapperService.java +++ b/src/main/java/net/fabricmc/loom/task/service/SourceRemapperService.java @@ -42,7 +42,6 @@ import org.slf4j.LoggerFactory; import net.fabricmc.loom.LoomGradleExtension; import net.fabricmc.loom.task.RemapSourcesJarTask; -import net.fabricmc.loom.util.Constants; import net.fabricmc.loom.util.DeletingFileVisitor; import net.fabricmc.loom.util.FileSystemUtil; import net.fabricmc.loom.util.SourceRemapper; @@ -58,22 +57,24 @@ public final class SourceRemapperService implements SharedService { final String from = task.getSourceNamespace().get(); final LoomGradleExtension extension = LoomGradleExtension.get(project); final String id = extension.getMappingConfiguration().getBuildServiceName("sourceremapper", from, to); + final int javaCompileRelease = SourceRemapper.getJavaCompileRelease(project); return serviceManager.getOrCreateService(id, () -> - new SourceRemapperService(MappingsService.createDefault(project, serviceManager, from, to), task.getClasspath() - )); + new SourceRemapperService(MappingsService.createDefault(project, serviceManager, from, to), task.getClasspath(), javaCompileRelease)); } private static final Logger LOGGER = LoggerFactory.getLogger(SourceRemapperService.class); private final MappingsService mappingsService; private final ConfigurableFileCollection classpath; + private final int javaCompileRelease; private final Supplier mercury = Suppliers.memoize(this::createMercury); - private SourceRemapperService(MappingsService mappingsService, ConfigurableFileCollection classpath) { + private SourceRemapperService(MappingsService mappingsService, ConfigurableFileCollection classpath, int javaCompileRelease) { this.mappingsService = mappingsService; this.classpath = classpath; + this.javaCompileRelease = javaCompileRelease; } public void remapSourcesJar(Path source, Path destination) throws IOException { @@ -122,7 +123,7 @@ public final class SourceRemapperService implements SharedService { private Mercury createMercury() { var mercury = new Mercury(); mercury.setGracefulClasspathChecks(true); - mercury.setSourceCompatibility(Constants.MERCURY_SOURCE_VERSION); + mercury.setSourceCompatibilityFromRelease(javaCompileRelease); try { mercury.getProcessors().add(MercuryRemapper.create(getMappings())); diff --git a/src/main/java/net/fabricmc/loom/util/Constants.java b/src/main/java/net/fabricmc/loom/util/Constants.java index 78eb818d..3eee8cc6 100644 --- a/src/main/java/net/fabricmc/loom/util/Constants.java +++ b/src/main/java/net/fabricmc/loom/util/Constants.java @@ -24,7 +24,6 @@ package net.fabricmc.loom.util; -import org.eclipse.jdt.core.JavaCore; import org.objectweb.asm.Opcodes; public class Constants { @@ -35,7 +34,6 @@ public class Constants { public static final String FABRIC_REPOSITORY = "https://maven.fabricmc.net/"; public static final int ASM_VERSION = Opcodes.ASM9; - public static final String MERCURY_SOURCE_VERSION = JavaCore.VERSION_17; private Constants() { } diff --git a/src/main/java/net/fabricmc/loom/util/SourceRemapper.java b/src/main/java/net/fabricmc/loom/util/SourceRemapper.java index ab2e7983..7812dd58 100644 --- a/src/main/java/net/fabricmc/loom/util/SourceRemapper.java +++ b/src/main/java/net/fabricmc/loom/util/SourceRemapper.java @@ -31,13 +31,17 @@ import java.nio.file.Path; import java.util.ArrayList; import java.util.List; import java.util.Set; +import java.util.concurrent.atomic.AtomicInteger; import java.util.function.Consumer; import org.cadixdev.lorenz.MappingSet; import org.cadixdev.mercury.Mercury; import org.cadixdev.mercury.remapper.MercuryRemapper; +import org.gradle.api.JavaVersion; import org.gradle.api.Project; import org.gradle.api.internal.project.ProjectInternal; +import org.gradle.api.provider.Property; +import org.gradle.api.tasks.compile.JavaCompile; import org.gradle.internal.logging.progress.ProgressLogger; import org.gradle.internal.logging.progress.ProgressLoggerFactory; import org.slf4j.Logger; @@ -175,6 +179,7 @@ public class SourceRemapper { Mercury mercury = extension.getOrCreateSrcMercuryCache(toNamed ? 1 : 0, () -> { Mercury m = createMercuryWithClassPath(project, toNamed); + m.setSourceCompatibilityFromRelease(getJavaCompileRelease(project)); for (File file : extension.getUnmappedModCollection()) { Path path = file.toPath(); @@ -209,6 +214,30 @@ public class SourceRemapper { return mercury; } + public static int getJavaCompileRelease(Project project) { + AtomicInteger release = new AtomicInteger(-1); + + project.getTasks().withType(JavaCompile.class, javaCompile -> { + Property releaseProperty = javaCompile.getOptions().getRelease(); + + if (!releaseProperty.isPresent()) { + return; + } + + int compileRelease = releaseProperty.get(); + release.set(Math.max(release.get(), compileRelease)); + }); + + final int i = release.get(); + + if (i < 0) { + // Unable to find the release used to compile with, default to the current version + return Integer.parseInt(JavaVersion.current().getMajorVersion()); + } + + return i; + } + public static void copyNonJavaFiles(Path from, Path to, Logger logger, Path source) throws IOException { Files.walk(from).forEach(path -> { Path targetPath = to.resolve(from.relativize(path).toString()); @@ -226,7 +255,6 @@ public class SourceRemapper { public static Mercury createMercuryWithClassPath(Project project, boolean toNamed) { Mercury m = new Mercury(); m.setGracefulClasspathChecks(true); - m.setSourceCompatibility(Constants.MERCURY_SOURCE_VERSION); final List classPath = new ArrayList<>(); diff --git a/src/test/resources/projects/simple/src/main/java/net/fabricmc/example/ExampleMod.java b/src/test/resources/projects/simple/src/main/java/net/fabricmc/example/ExampleMod.java index bd3cb846..5330d119 100644 --- a/src/test/resources/projects/simple/src/main/java/net/fabricmc/example/ExampleMod.java +++ b/src/test/resources/projects/simple/src/main/java/net/fabricmc/example/ExampleMod.java @@ -7,6 +7,9 @@ import org.apache.logging.log4j.Logger; public class ExampleMod implements ModInitializer { public static final Logger LOGGER = LogManager.getLogger("modid"); + /** + * @see net.fabricmc.example + */ @Override public void onInitialize() { LOGGER.info("Hello simple Fabric mod!");