mirror of
https://github.com/GTNewHorizons/ForgeMultipart.git
synced 2026-11-13 17:04:25 +00:00
refactor: remove the package objects instead of porting them
A Scala package object compiles to a class literally named 'package', and that is a Java keyword, so no Java source can declare or even name it. The characterization had to reach both through Class.forName, which is the argument in one line. There is no Java form to port to. Each held exactly one member, a logger alias for its proxy, so the indirection is inlined at its eight call sites: ConfigContent (7) and MultipartGenerator (1). The functional characterization asserted beforehand that each alias returned the very instance its proxy holds, so the substitution is an identity; both classes are descriptor-identical afterwards. Exactly four classes leave the jar and nothing else changes. No consumer referenced them, and no Java consumer could have. 120 plain-JVM tests and 22 Forge server tests pass. PackageObjectCharacterization went with its subject; ProxyLoggerFunctionalTest keeps the assertion worth having, that both proxies publish a logger during preInit.
This commit is contained in:
1 parent
d6bba7ff46
commit
d224ce116d
9 files changed
+71
-100
No files matched your search
@@ -1,55 +0,0 @@
|
||||
package codechicken.multipart;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Both package objects hold one member, a {@code logger} alias for their proxy's logger.
|
||||
* <p>
|
||||
* Everything here goes through reflection because {@code package} is a Java keyword:
|
||||
* {@code codechicken.multipart.package} cannot be written as a type name in Java source at all. That is not a style
|
||||
* choice, it is the reason these two classes have no Java form.
|
||||
*/
|
||||
class PackageObjectCharacterizationTest {
|
||||
|
||||
@Test
|
||||
void bothPackageObjectsExposeAStaticLoggerAccessor() throws Exception {
|
||||
for (String name : new String[] { "codechicken.multipart.package", "codechicken.microblock.package" }) {
|
||||
Method logger = Class.forName(name).getDeclaredMethod("logger");
|
||||
|
||||
assertSame(Logger.class, logger.getReturnType(), name);
|
||||
assertTrue(Modifier.isPublic(logger.getModifiers()), name);
|
||||
assertTrue(Modifier.isStatic(logger.getModifiers()), name);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void bothCarryACompanionSingleton() throws Exception {
|
||||
for (String name : new String[] { "codechicken.multipart.package$", "codechicken.microblock.package$" }) {
|
||||
Class<?> companion = Class.forName(name);
|
||||
|
||||
assertSame(companion, companion.getField("MODULE$").getType(), name);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The alias reads the proxy's var rather than holding anything, so before preInit has run it reports null instead
|
||||
* of failing. The Forge server suite asserts the other half, that after preInit it is the very same instance.
|
||||
*/
|
||||
@Test
|
||||
void theAliasReportsWhateverTheProxyHoldsAndIsNullBeforePreInit() throws Exception {
|
||||
assertNull(invokeLogger("codechicken.multipart.package"));
|
||||
assertNull(invokeLogger("codechicken.microblock.package"));
|
||||
}
|
||||
|
||||
private static Object invokeLogger(String name) throws Exception {
|
||||
return Class.forName(name).getDeclaredMethod("logger").invoke(null);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user