From 1f0c932aa61e2fdad184aaae72a1226c2526df2e Mon Sep 17 00:00:00 2001 From: Lauriichan Date: Thu, 20 Jan 2022 19:00:39 +0100 Subject: [PATCH 1/4] Fix reflections --- .../minecraft/vcompat/VersionCompatProvider.java | 4 ++-- .../vcompat/util/java/tools/ReflectionTools.java | 8 ++++++++ .../sourcewriters/minecraft/vcompat/VersionCompat.java | 6 ++---- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/VersionCompatProvider.java b/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/VersionCompatProvider.java index 40d6a04..3cd2acc 100644 --- a/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/VersionCompatProvider.java +++ b/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/VersionCompatProvider.java @@ -4,7 +4,7 @@ import net.sourcewriters.minecraft.vcompat.provider.VersionControl; import net.sourcewriters.minecraft.vcompat.provider.lookup.ClassLookupProvider; -import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.ClassLookup; +import net.sourcewriters.minecraft.vcompat.util.java.tools.ReflectionTools; public abstract class VersionCompatProvider { @@ -14,7 +14,7 @@ public static VersionCompatProvider get() { if (PROVIDER.isPresent()) { return PROVIDER.get(); } - Object object = ClassLookup.of(VersionCompatProvider.class.getPackageName() + ".VersionCompat").init(); + Object object = ReflectionTools.createInstance(ReflectionTools.getClass(VersionCompatProvider.class.getPackageName() + ".VersionCompat")); if (object == null || !(object instanceof VersionCompatProvider)) { throw new IllegalStateException("Can't initialize VersionCompatProvider!"); } diff --git a/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/util/java/tools/ReflectionTools.java b/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/util/java/tools/ReflectionTools.java index a86ac9b..951a991 100644 --- a/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/util/java/tools/ReflectionTools.java +++ b/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/util/java/tools/ReflectionTools.java @@ -49,6 +49,14 @@ public static Class getClass(String classPath) { } } + public static Object createInstance(Class clazz) { + try { + return clazz.getConstructor().newInstance(); + } catch (Exception ignored) { + return null; + } + } + public static Object getValue(Field field, Object source) { if (field != null) { boolean access = field.canAccess(source); diff --git a/vcompat-core/src/main/java/net/sourcewriters/minecraft/vcompat/VersionCompat.java b/vcompat-core/src/main/java/net/sourcewriters/minecraft/vcompat/VersionCompat.java index 3dc33df..7cd068a 100644 --- a/vcompat-core/src/main/java/net/sourcewriters/minecraft/vcompat/VersionCompat.java +++ b/vcompat-core/src/main/java/net/sourcewriters/minecraft/vcompat/VersionCompat.java @@ -1,8 +1,8 @@ package net.sourcewriters.minecraft.vcompat; import net.sourcewriters.minecraft.vcompat.provider.VersionControl; -import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.ClassLookup; import net.sourcewriters.minecraft.vcompat.version.Versions; +import net.sourcewriters.minecraft.vcompat.util.java.tools.ReflectionTools; final class VersionCompat extends VersionCompatProvider { @@ -16,9 +16,7 @@ public VersionCompat() { } private final VersionControl initControl() { - Object object = ClassLookup - .of(String.format(CLASSPATH, VersionCompat.class.getPackageName(), Versions.getServerAsString(), Versions.getServerAsString())) - .init(); + Object object = ReflectionTools.createInstance(ReflectionTools.getClass(String.format(CLASSPATH, VersionCompat.class.getPackageName(), Versions.getServerAsString(), Versions.getServerAsString()))); if (object == null || !(object instanceof VersionControl)) { throw new IllegalStateException("Can't initialize VersionControl"); } From 8870670003c97378f0c4e7387e7e4881167174be Mon Sep 17 00:00:00 2001 From: Lauriichan Date: Thu, 20 Jan 2022 19:03:17 +0100 Subject: [PATCH 2/4] Update .gitignore --- vcompat-1_18_R1/.gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/vcompat-1_18_R1/.gitignore b/vcompat-1_18_R1/.gitignore index c71ea97..00d2ab7 100644 --- a/vcompat-1_18_R1/.gitignore +++ b/vcompat-1_18_R1/.gitignore @@ -1 +1,2 @@ /.apt_generated/ +/.apt_generated_tests/ From c46dc3cefa78d7dfc4b6f1c1912ed7a9778518c9 Mon Sep 17 00:00:00 2001 From: Lauriichan Date: Thu, 20 Jan 2022 19:31:42 +0100 Subject: [PATCH 3/4] Work on reflections for VersionCompatProvider --- vcompat-api/pom.xml | 110 +++++++++--------- .../vcompat/VersionCompatProvider.java | 20 +++- .../util/java/tools/ReflectionTools.java | 2 +- vcompat-core/pom.xml | 108 +++++++++-------- .../minecraft/vcompat/VersionCompat.java | 8 +- 5 files changed, 127 insertions(+), 121 deletions(-) diff --git a/vcompat-api/pom.xml b/vcompat-api/pom.xml index edae13e..3a9bd4a 100644 --- a/vcompat-api/pom.xml +++ b/vcompat-api/pom.xml @@ -1,63 +1,61 @@ - - 4.0.0 - - net.sourcewriters.minecraft - vcompat-parent - 3.0.0 - - vcompat-api + + 4.0.0 + + net.sourcewriters.minecraft + vcompat-parent + 3.0.0 + + vcompat-api - + - + - - com.syntaxphoenix.syntaxapi - java - 2.0.14 - - - com.syntaxphoenix.syntaxapi - nbt - 2.0.12 - - - com.syntaxphoenix.syntaxapi - version - 2.0.12 - - - com.syntaxphoenix.syntaxapi - key - 2.0.11 - - - com.syntaxphoenix.syntaxapi - random - 2.0.11 - - - com.syntaxphoenix.syntaxapi - logging - 2.0.11 - - - com.syntaxphoenix.syntaxapi - json-lib - 2.0.11 - + + com.syntaxphoenix.syntaxapi + java + 2.0.14 + + + com.syntaxphoenix.syntaxapi + nbt + 2.0.12 + + + com.syntaxphoenix.syntaxapi + version + 2.0.12 + + + com.syntaxphoenix.syntaxapi + key + 2.0.11 + + + com.syntaxphoenix.syntaxapi + random + 2.0.11 + + + com.syntaxphoenix.syntaxapi + logging + 2.0.11 + + + com.syntaxphoenix.syntaxapi + json-lib + 2.0.11 + - + - - jline - jline - 2.12.1 - provided - - + + jline + jline + 2.12.1 + provided + + @@ -93,4 +91,4 @@ - + \ No newline at end of file diff --git a/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/VersionCompatProvider.java b/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/VersionCompatProvider.java index 3cd2acc..6c5ec69 100644 --- a/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/VersionCompatProvider.java +++ b/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/VersionCompatProvider.java @@ -1,5 +1,6 @@ package net.sourcewriters.minecraft.vcompat; +import com.syntaxphoenix.syntaxapi.utils.java.Exceptions; import com.syntaxphoenix.syntaxapi.utils.java.tools.Container; import net.sourcewriters.minecraft.vcompat.provider.VersionControl; @@ -9,18 +10,27 @@ public abstract class VersionCompatProvider { protected static final Container PROVIDER = Container.of(); + private static final String IMPLEMENTATION_PATH = VersionCompatProvider.class.getPackageName() + ".VersionCompat"; public static VersionCompatProvider get() { if (PROVIDER.isPresent()) { return PROVIDER.get(); } - Object object = ReflectionTools.createInstance(ReflectionTools.getClass(VersionCompatProvider.class.getPackageName() + ".VersionCompat")); - if (object == null || !(object instanceof VersionCompatProvider)) { + try { + Class clazz = ReflectionTools.getClass(IMPLEMENTATION_PATH); + System.out.println("Class: " + (clazz == null)); + System.out.println("Class: " + IMPLEMENTATION_PATH); + Object object = clazz.getConstructor().newInstance(); + if (object == null || !(object instanceof VersionCompatProvider)) { + throw new IllegalStateException("Can't initialize VersionCompatProvider!"); + } + return PROVIDER.replace((VersionCompatProvider) object).lock().get(); + } catch (Exception exp) { + System.out.println(Exceptions.stackTraceToString(exp)); throw new IllegalStateException("Can't initialize VersionCompatProvider!"); } - return PROVIDER.replace((VersionCompatProvider) object).lock().get(); } - + protected final ClassLookupProvider lookupProvider = new ClassLookupProvider(); /* @@ -28,7 +38,7 @@ public static VersionCompatProvider get() { */ public abstract VersionControl getControl(); - + public final ClassLookupProvider getLookupProvider() { return lookupProvider; } diff --git a/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/util/java/tools/ReflectionTools.java b/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/util/java/tools/ReflectionTools.java index 951a991..a0ee007 100644 --- a/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/util/java/tools/ReflectionTools.java +++ b/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/util/java/tools/ReflectionTools.java @@ -43,7 +43,7 @@ public static Class arrayclass(Class clazz) { public static Class getClass(String classPath) { try { - return Class.forName(classPath); + return Class.forName(classPath, true, null); } catch (ClassNotFoundException ignored) { return null; } diff --git a/vcompat-core/pom.xml b/vcompat-core/pom.xml index 9e39d44..4d3a05a 100644 --- a/vcompat-core/pom.xml +++ b/vcompat-core/pom.xml @@ -1,15 +1,13 @@ - - 4.0.0 - - net.sourcewriters.minecraft - vcompat-parent - 3.0.0 - - vcompat + + 4.0.0 + + net.sourcewriters.minecraft + vcompat-parent + 3.0.0 + + vcompat - + @@ -36,48 +34,48 @@ vcompat-1_18_R1 3.0.0 - + - - - - maven-shade-plugin - 3.2.4 - - - package - - shade - - - - - - - *:* - - META-INF/*.SF - META-INF/*.DSA - META-INF/*.RSA - - - - - - com.syntaxphoenix.syntaxapi - net.sourcewriters.minecraft.vcompat.shaded.syntaxapi - - - - - - org.apache.maven.plugins - maven-deploy-plugin - 3.0.0-M2 - - false - - - - - + + + + maven-shade-plugin + 3.2.4 + + + package + + shade + + + + + + + *:* + + META-INF/*.SF + META-INF/*.DSA + META-INF/*.RSA + + + + + + com.syntaxphoenix.syntaxapi + net.sourcewriters.minecraft.vcompat.shaded.syntaxapi + + + + + + org.apache.maven.plugins + maven-deploy-plugin + 3.0.0-M2 + + false + + + + + \ No newline at end of file diff --git a/vcompat-core/src/main/java/net/sourcewriters/minecraft/vcompat/VersionCompat.java b/vcompat-core/src/main/java/net/sourcewriters/minecraft/vcompat/VersionCompat.java index 7cd068a..091f185 100644 --- a/vcompat-core/src/main/java/net/sourcewriters/minecraft/vcompat/VersionCompat.java +++ b/vcompat-core/src/main/java/net/sourcewriters/minecraft/vcompat/VersionCompat.java @@ -4,9 +4,9 @@ import net.sourcewriters.minecraft.vcompat.version.Versions; import net.sourcewriters.minecraft.vcompat.util.java.tools.ReflectionTools; -final class VersionCompat extends VersionCompatProvider { - - public static final String CLASSPATH = "%s.provider.impl.v%s.VersionControl%s"; +public final class VersionCompat extends VersionCompatProvider { + + private static final String VERSION_PATH = String.format("%s.provider.impl.v%s.VersionControl%s", VersionCompat.class.getPackageName(), Versions.getServerAsString(), Versions.getServerAsString()); private final VersionControl control; @@ -16,7 +16,7 @@ public VersionCompat() { } private final VersionControl initControl() { - Object object = ReflectionTools.createInstance(ReflectionTools.getClass(String.format(CLASSPATH, VersionCompat.class.getPackageName(), Versions.getServerAsString(), Versions.getServerAsString()))); + Object object = ReflectionTools.createInstance(ReflectionTools.getClass(VERSION_PATH)); if (object == null || !(object instanceof VersionControl)) { throw new IllegalStateException("Can't initialize VersionControl"); } From 48644ee53ddcc835b90b78b91407a2f9cde04297 Mon Sep 17 00:00:00 2001 From: Lauriichan Date: Thu, 20 Jan 2022 19:41:32 +0100 Subject: [PATCH 4/4] Fixed vCompat init --- .../minecraft/vcompat/VersionCompatProvider.java | 13 ++----------- .../vcompat/util/java/tools/ReflectionTools.java | 2 +- .../minecraft/vcompat/VersionCompat.java | 6 +++--- 3 files changed, 6 insertions(+), 15 deletions(-) diff --git a/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/VersionCompatProvider.java b/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/VersionCompatProvider.java index 6c5ec69..536f25b 100644 --- a/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/VersionCompatProvider.java +++ b/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/VersionCompatProvider.java @@ -1,11 +1,10 @@ package net.sourcewriters.minecraft.vcompat; -import com.syntaxphoenix.syntaxapi.utils.java.Exceptions; import com.syntaxphoenix.syntaxapi.utils.java.tools.Container; import net.sourcewriters.minecraft.vcompat.provider.VersionControl; import net.sourcewriters.minecraft.vcompat.provider.lookup.ClassLookupProvider; -import net.sourcewriters.minecraft.vcompat.util.java.tools.ReflectionTools; +import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.ClassLookup; public abstract class VersionCompatProvider { @@ -16,19 +15,11 @@ public static VersionCompatProvider get() { if (PROVIDER.isPresent()) { return PROVIDER.get(); } - try { - Class clazz = ReflectionTools.getClass(IMPLEMENTATION_PATH); - System.out.println("Class: " + (clazz == null)); - System.out.println("Class: " + IMPLEMENTATION_PATH); - Object object = clazz.getConstructor().newInstance(); + Object object = ClassLookup.of(IMPLEMENTATION_PATH).init(); if (object == null || !(object instanceof VersionCompatProvider)) { throw new IllegalStateException("Can't initialize VersionCompatProvider!"); } return PROVIDER.replace((VersionCompatProvider) object).lock().get(); - } catch (Exception exp) { - System.out.println(Exceptions.stackTraceToString(exp)); - throw new IllegalStateException("Can't initialize VersionCompatProvider!"); - } } protected final ClassLookupProvider lookupProvider = new ClassLookupProvider(); diff --git a/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/util/java/tools/ReflectionTools.java b/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/util/java/tools/ReflectionTools.java index a0ee007..951a991 100644 --- a/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/util/java/tools/ReflectionTools.java +++ b/vcompat-api/src/main/java/net/sourcewriters/minecraft/vcompat/util/java/tools/ReflectionTools.java @@ -43,7 +43,7 @@ public static Class arrayclass(Class clazz) { public static Class getClass(String classPath) { try { - return Class.forName(classPath, true, null); + return Class.forName(classPath); } catch (ClassNotFoundException ignored) { return null; } diff --git a/vcompat-core/src/main/java/net/sourcewriters/minecraft/vcompat/VersionCompat.java b/vcompat-core/src/main/java/net/sourcewriters/minecraft/vcompat/VersionCompat.java index 091f185..1801d34 100644 --- a/vcompat-core/src/main/java/net/sourcewriters/minecraft/vcompat/VersionCompat.java +++ b/vcompat-core/src/main/java/net/sourcewriters/minecraft/vcompat/VersionCompat.java @@ -1,12 +1,12 @@ package net.sourcewriters.minecraft.vcompat; import net.sourcewriters.minecraft.vcompat.provider.VersionControl; +import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.ClassLookup; import net.sourcewriters.minecraft.vcompat.version.Versions; -import net.sourcewriters.minecraft.vcompat.util.java.tools.ReflectionTools; public final class VersionCompat extends VersionCompatProvider { - private static final String VERSION_PATH = String.format("%s.provider.impl.v%s.VersionControl%s", VersionCompat.class.getPackageName(), Versions.getServerAsString(), Versions.getServerAsString()); + private static final String VERSION_PATH = String.format("%s.provider.impl.%s.VersionControl%s", VersionCompat.class.getPackageName(), Versions.getServerAsString(), Versions.getServerAsString().substring(1)); private final VersionControl control; @@ -16,7 +16,7 @@ public VersionCompat() { } private final VersionControl initControl() { - Object object = ReflectionTools.createInstance(ReflectionTools.getClass(VERSION_PATH)); + Object object = ClassLookup.of(VERSION_PATH).searchMethod("init", "init").run("init"); if (object == null || !(object instanceof VersionControl)) { throw new IllegalStateException("Can't initialize VersionControl"); }