diff options
author | LexManos <LexManos@gmail.com> | 2020-04-04 14:01:02 -0700 |
---|---|---|
committer | LexManos <LexManos@gmail.com> | 2020-04-04 14:01:02 -0700 |
commit | 977462b1780785a4ce9500646df07b6a4f638461 (patch) | |
tree | f4ee2ea5eee222668b1c3c868cc468e6d4314ba5 | |
parent | c5ae801c3a51aa6abf05c2eb5d6b3ebe592d02ab (diff) | |
download | Artifactural-977462b1780785a4ce9500646df07b6a4f638461.tar.gz Artifactural-977462b1780785a4ce9500646df07b6a4f638461.tar.bz2 Artifactural-977462b1780785a4ce9500646df07b6a4f638461.zip |
Use Method handles to bypass J12+ reflection blocking attempt.
Publish as Multi-Release jar as this API is only available on J9+
-rw-r--r-- | build.gradle | 62 | ||||
-rw-r--r-- | settings.gradle | 1 | ||||
-rw-r--r-- | src/gradlecomp/java/com/amadornes/artifactural/gradle/ModifierAccess.java | 55 | ||||
-rw-r--r-- | src/gradlecomp/java/com/amadornes/artifactural/gradle/ReflectionUtils.java | 31 | ||||
-rw-r--r-- | src/java9/com/amadornes/artifactural/gradle/ModifierAccess.java | 57 |
5 files changed, 173 insertions, 33 deletions
diff --git a/build.gradle b/build.gradle index d6cbd04..8b5242f 100644 --- a/build.gradle +++ b/build.gradle @@ -27,6 +27,7 @@ import java.util.jar.JarFile apply plugin: 'java' apply plugin: 'maven-publish' +apply plugin: 'eclipse' group = 'net.minecraftforge' version = gitVersion() @@ -44,6 +45,7 @@ sourceSets { api shared gradlecomp + java9 } repositories { @@ -60,6 +62,8 @@ configurations { } dependencies { + java9Implementation files(sourceSets.main.output.classesDirs) { builtBy compileJava } + sharedImplementation sourceSets.api.output sharedImplementation 'commons-io:commons-io:2.4' @@ -74,6 +78,43 @@ dependencies { } +// Default all standard Java compile tasks to Java 8 +// We'll specify Java 9 only for the java9 compile task +tasks.withType(JavaCompile) { + options.encoding = 'utf-8' + options.deprecation = true + sourceCompatibility = 8 + targetCompatibility = 8 + options.compilerArgs.addAll(['--release', '8']) +} + +project(':artifactural9') { + apply plugin: 'java' + apply plugin: 'eclipse' + sourceCompatibility = targetCompatibility = 9 + group = rootProject.group + + sourceSets { + java9.java.srcDirs = [rootProject.file('src/java9').getAbsolutePath()] + } + + eclipse { + project { + name rootProject.name + '9' + linkedResource name: 'java9', type: '2', location: rootProject.file('src/java9').getAbsolutePath() + } + jdt { + sourceCompatibility = targetCompatibility = 9 + } + } + + tasks.withType(JavaCompile) { + options.encoding = 'utf-8' + sourceCompatibility = 9 + targetCompatibility = 9 + options.compilerArgs.addAll(['--release', '9']) + } +} def gradleRepositoryAdapterPath = Paths.get("com", "amadornes", "artifactural", "gradle", "GradleRepositoryAdapter.class") def classesDirs = sourceSets.gradlecomp.output.classesDirs.getFiles().first().toPath() @@ -159,8 +200,6 @@ class PatchGradleRepositoryAdapter extends DefaultTask { } } - - task patchConstructor(type: PatchGradleRepositoryAdapter) { constructorName = _constructorName constructorDesc = _constructorDesc @@ -178,12 +217,22 @@ jar { } from patchConstructor.outputs + + into('META-INF/versions/9') { + from project(':artifactural9').sourceSets.java9.output + } + + manifest { + attributes( + 'Multi-Release': 'true' + ) + } } jar.doLast { def jarPath = it.outputs.files.getFiles().first() def jarFile = new JarFile(jarPath) - def entry = jarFile.getEntry(gradleRepositoryAdapterPath.toString()) + def entry = jarFile.getEntry(gradleRepositoryAdapterPath.toString().replace('\\', '/')) def stream = jarFile.getInputStream(entry) ClassReader reader = new ClassReader(stream) @@ -264,10 +313,15 @@ publishing { username = project.properties.mavenUser password = project.properties.mavenPassword } - url 'http://files.minecraftforge.net/maven/manage/upload' + url 'https://files.minecraftforge.net/maven/manage/upload' } else { url 'file://' + rootProject.file('repo').getAbsolutePath() } } } } + +if (!JavaVersion.current().java9Compatible) { + println("You must build this with JDK 9") + System.exit(1) +} diff --git a/settings.gradle b/settings.gradle index f01cf99..c0a8d84 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1 +1,2 @@ rootProject.name = 'artifactural' +include 'artifactural9' diff --git a/src/gradlecomp/java/com/amadornes/artifactural/gradle/ModifierAccess.java b/src/gradlecomp/java/com/amadornes/artifactural/gradle/ModifierAccess.java new file mode 100644 index 0000000..0a84755 --- /dev/null +++ b/src/gradlecomp/java/com/amadornes/artifactural/gradle/ModifierAccess.java @@ -0,0 +1,55 @@ +/* + * Artifactural + * Copyright (c) 2018. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation version 2.1 + * of the License. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package com.amadornes.artifactural.gradle; + +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; + +public class ModifierAccess { + private static Field MODIFIER_ACCESS = null; + private static boolean accessAttempted = false; + + public static synchronized boolean definalize(Field target) { + if ((target.getModifiers() & Modifier.FINAL) == 0) { + return true; + } + + if (MODIFIER_ACCESS == null && !accessAttempted) { + try { + final Field modifiers = Field.class.getDeclaredField("modifiers"); + modifiers.setAccessible(true); + MODIFIER_ACCESS = modifiers; + } catch (NoSuchFieldException e) { + throw new RuntimeException("Could not access Field.modifiers to definalize reflection object. Use Java 8, current version: " + System.getProperty("java.version"), e); + } + accessAttempted = true; + } + if (MODIFIER_ACCESS != null) { + try { + MODIFIER_ACCESS.setInt(target, target.getModifiers() & ~Modifier.FINAL); + } catch (IllegalArgumentException | IllegalAccessException e) { + throw new RuntimeException("Could not definalize field " + target.getDeclaringClass().getName() + "." + target.getName(), e); + } + return true; + } + return false; + } + +} diff --git a/src/gradlecomp/java/com/amadornes/artifactural/gradle/ReflectionUtils.java b/src/gradlecomp/java/com/amadornes/artifactural/gradle/ReflectionUtils.java index d3078d5..9ee484c 100644 --- a/src/gradlecomp/java/com/amadornes/artifactural/gradle/ReflectionUtils.java +++ b/src/gradlecomp/java/com/amadornes/artifactural/gradle/ReflectionUtils.java @@ -22,6 +22,7 @@ package com.amadornes.artifactural.gradle; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier; +import java.util.function.Function; import java.util.function.UnaryOperator; public class ReflectionUtils { @@ -69,7 +70,7 @@ public class ReflectionUtils { for (Field f : clazz.getDeclaredFields()) { if (f.getName().equals(name)) { f.setAccessible(true); - if (!definalize(f)) { + if (!ModifierAccess.definalize(f)) { System.out.println("Could not definalize field " + f.getDeclaringClass().getName() + "." + f.getName() + " Exception ate, lets see if it works"); } return f; @@ -80,34 +81,6 @@ public class ReflectionUtils { return null; } - private static Field MODIFIER_ACCESS = null; - private static boolean accessAttempted = false; - private static synchronized boolean definalize(Field f) { - if ((f.getModifiers() & Modifier.FINAL) == 0) { - return true; - } - - if (MODIFIER_ACCESS == null && !accessAttempted) { - try { - Field modifiers = Field.class.getDeclaredField("modifiers"); - modifiers.setAccessible(true); - MODIFIER_ACCESS = modifiers; - } catch (NoSuchFieldException e) { - System.out.println("Could not access Field.modifiers to definalize reflection object. This happens on JVMs > 12, going to see if things work, if not use JVM 8-11"); - } - accessAttempted = true; - } - if (MODIFIER_ACCESS != null) { - try { - MODIFIER_ACCESS.setInt(f, f.getModifiers() & ~Modifier.FINAL); - } catch (IllegalArgumentException | IllegalAccessException e) { - throw new RuntimeException("Could not definalize field " + f.getDeclaringClass().getName() + "." + f.getName(), e); - } - return true; - } - return false; - } - /** * Invokes a method (can be private). */ diff --git a/src/java9/com/amadornes/artifactural/gradle/ModifierAccess.java b/src/java9/com/amadornes/artifactural/gradle/ModifierAccess.java new file mode 100644 index 0000000..69e7dff --- /dev/null +++ b/src/java9/com/amadornes/artifactural/gradle/ModifierAccess.java @@ -0,0 +1,57 @@ +/* + * Artifactural + * Copyright (c) 2018. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation version 2.1 + * of the License. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package com.amadornes.artifactural.gradle; + +import java.lang.invoke.MethodHandles; +import java.lang.invoke.MethodHandles.Lookup; +import java.lang.invoke.VarHandle; +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; + +public class ModifierAccess { + private static VarHandle MODIFIER_ACCESS = null; + private static boolean accessAttempted = false; + + public static synchronized boolean definalize(Field target) { + if ((target.getModifiers() & Modifier.FINAL) == 0) { + return true; + } + + if (MODIFIER_ACCESS == null && !accessAttempted) { + try { + Lookup lookup = MethodHandles.privateLookupIn(Field.class, MethodHandles.lookup()); + MODIFIER_ACCESS = lookup.findVarHandle(Field.class, "modifiers", int.class); + } catch (NoSuchFieldException | IllegalAccessException e) { + throw new RuntimeException("Could not access Field.modifiers to definalize reflection object. Use Java 8, current version: " + System.getProperty("java.version"), e); + } + accessAttempted = true; + } + if (MODIFIER_ACCESS != null) { + try { + MODIFIER_ACCESS.set(target, target.getModifiers() & ~Modifier.FINAL); + } catch (IllegalArgumentException e) { + throw new RuntimeException("Could not definalize field " + target.getDeclaringClass().getName() + "." + target.getName(), e); + } + return true; + } + return false; + } + +} |