|
| 1 | +/* |
| 2 | + * Copyright (c) Forge Development LLC |
| 3 | + * SPDX-License-Identifier: LGPL-2.1-only |
| 4 | + */ |
| 5 | +package net.minecraftforge.eventbus.test.compiletime; |
| 6 | + |
| 7 | +import com.google.testing.compile.Compilation; |
| 8 | +import com.google.testing.compile.Compiler; |
| 9 | +import com.google.testing.compile.JavaFileObjects; |
| 10 | +import net.minecraftforge.eventbus.api.bus.EventBus; |
| 11 | +import net.minecraftforge.eventbus.testjar.events.EventWithData; |
| 12 | +import net.minecraftforge.eventbus.validator.EventBusValidator; |
| 13 | +import net.minecraftforge.eventbus.validator.EventTypeValidator; |
| 14 | +import net.minecraftforge.eventbus.validator.SubscribeEventValidator; |
| 15 | +import org.intellij.lang.annotations.Language; |
| 16 | +import org.jspecify.annotations.NullMarked; |
| 17 | + |
| 18 | +import java.io.File; |
| 19 | +import java.net.URISyntaxException; |
| 20 | +import java.nio.file.Path; |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +final class CompileTestHelper { |
| 24 | + private CompileTestHelper() {} |
| 25 | + |
| 26 | + private static final List<File> CLASSPATH; |
| 27 | + |
| 28 | + @Language("Java") |
| 29 | + static final String SOURCE_PREFIX = """ |
| 30 | + import net.minecraftforge.eventbus.api.bus.*; |
| 31 | + import net.minecraftforge.eventbus.api.event.*; |
| 32 | + import net.minecraftforge.eventbus.api.event.characteristic.*; |
| 33 | + import net.minecraftforge.eventbus.api.listener.*; |
| 34 | + |
| 35 | + import net.minecraftforge.eventbus.testjar.events.*; |
| 36 | + |
| 37 | + import org.jspecify.annotations.NullMarked; |
| 38 | + import org.jspecify.annotations.Nullable; |
| 39 | + |
| 40 | + @NullMarked |
| 41 | + """; |
| 42 | + |
| 43 | + static { |
| 44 | + try { |
| 45 | + CLASSPATH = List.of( |
| 46 | + getClassPath(EventBus.class).toFile(), |
| 47 | + getClassPath(EventWithData.class).toFile(), |
| 48 | + getClassPath(NullMarked.class).toFile() |
| 49 | + ); |
| 50 | + } catch (URISyntaxException e) { |
| 51 | + throw new RuntimeException(e); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + private static Path getClassPath(Class<?> clazz) throws URISyntaxException { |
| 56 | + return Path.of(clazz |
| 57 | + .getProtectionDomain() |
| 58 | + .getCodeSource() |
| 59 | + .getLocation() |
| 60 | + .toURI() |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + static Compilation compile(@Language(value = "Java", prefix = SOURCE_PREFIX) String sourceCode) { |
| 65 | + return compileWithoutDefaultPrefix(SOURCE_PREFIX + sourceCode); |
| 66 | + } |
| 67 | + |
| 68 | + static Compilation compileWithoutDefaultPrefix(@Language(value = "Java") String sourceCode) { |
| 69 | + return Compiler.javac() |
| 70 | + .withProcessors(new EventBusValidator(), new EventTypeValidator(), new SubscribeEventValidator()) |
| 71 | + .withClasspath(CLASSPATH) |
| 72 | + .compile(JavaFileObjects.forSourceString("", sourceCode)); |
| 73 | + } |
| 74 | +} |
0 commit comments