diff options
author | LexManos <LexManos@gmail.com> | 2021-05-26 10:46:53 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-05-26 10:46:53 -0700 |
commit | 578f33b206c7325762a956a1f3e3171d5c6d9052 (patch) | |
tree | 22e8db707b69b433463446b0709f93a6df65c27f | |
parent | 12aeb6f4c7c09e749204707475a7f71b16c14133 (diff) | |
parent | c3948938178810000353830671e474abb9d32a2c (diff) | |
download | Artifactural-578f33b206c7325762a956a1f3e3171d5c6d9052.tar.gz Artifactural-578f33b206c7325762a956a1f3e3171d5c6d9052.tar.bz2 Artifactural-578f33b206c7325762a956a1f3e3171d5c6d9052.zip |
Remove JDK reflection hacks in favor of `Unsafe`
Merge pull request #4 from sciwhiz12/theUnsafe
-rw-r--r-- | Jenkinsfile | 30 | ||||
-rw-r--r-- | build.gradle | 129 | ||||
-rw-r--r-- | gradle/wrapper/gradle-wrapper.properties | 2 | ||||
-rw-r--r-- | settings.gradle | 3 | ||||
-rw-r--r-- | src/gradlecomp/java/net/minecraftforge/artifactural/gradle/ModifierAccess.java | 55 | ||||
-rw-r--r-- | src/gradlecomp/java/net/minecraftforge/artifactural/gradle/ReflectionUtils.java | 12 | ||||
-rw-r--r-- | src/java9/net/minecraftforge/artifactural/gradle/ModifierAccess.java | 57 |
7 files changed, 76 insertions, 212 deletions
diff --git a/Jenkinsfile b/Jenkinsfile index ff1ebbf..7e93cc7 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -3,18 +3,23 @@ pipeline { agent { docker { - image 'gradlewrapper:j10' + image 'gradle:jdk8' args '-v gradlecache:/gradlecache' } } environment { - GRADLE_ARGS = '-Dorg.gradle.daemon.idletimeout=5000 --console=plain' + GRADLE_ARGS = '-Dorg.gradle.daemon.idletimeout=5000' DISCORD_WEBHOOK = credentials('forge-discord-jenkins-webhook') DISCORD_PREFIX = "Job: Artifactural Branch: ${BRANCH_NAME} Build: #${BUILD_NUMBER}" JENKINS_HEAD = 'https://wiki.jenkins-ci.org/download/attachments/2916393/headshot.png' } stages { + stage('fetch') { + steps { + checkout scm + } + } stage('notify_start') { when { not { @@ -33,7 +38,9 @@ pipeline { } stage('buildandtest') { steps { - sh './gradlew ${GRADLE_ARGS} --refresh-dependencies --continue build test' + withGradle { + sh './gradlew ${GRADLE_ARGS} --refresh-dependencies --continue build test' + } script { env.MYGROUP = sh(returnStdout: true, script: './gradlew properties -q | grep "group:" | awk \'{print $2}\'').trim() env.MYARTIFACT = sh(returnStdout: true, script: './gradlew properties -q | grep "name:" | awk \'{print $2}\'').trim() @@ -47,12 +54,17 @@ pipeline { changeRequest() } } - environment { - FORGE_MAVEN = credentials('forge-maven-forge-user') - } steps { - sh './gradlew ${GRADLE_ARGS} publish -PmavenUser=${FORGE_MAVEN_USR} -PmavenPassword=${FORGE_MAVEN_PSW}' - sh 'curl --user ${FORGE_MAVEN} https://files.minecraftforge.net/maven/manage/promote/latest/${MYGROUP}.${MYARTIFACT}/${MYVERSION}' + withCredentials([usernamePassword(credentialsId: 'maven-forge-user', usernameVariable: 'MAVEN_USER', passwordVariable: 'MAVEN_PASSWORD')]) { + withGradle { + sh './gradlew ${GRADLE_ARGS} publish' + } + } + } + post { + success { + build job: 'filegenerator', parameters: [string(name: 'COMMAND', value: "promote ${env.MYGROUP}:${env.MYARTIFACT} ${env.MYVERSION} latest")], propagate: false, wait: false + } } } } @@ -72,4 +84,4 @@ pipeline { } } } -}
\ No newline at end of file +} diff --git a/build.gradle b/build.gradle index cb11dcb..8155cf7 100644 --- a/build.gradle +++ b/build.gradle @@ -11,7 +11,7 @@ plugins { id 'java-library' id 'maven-publish' id 'eclipse' - id 'net.minecrell.licenser' version '0.3' + id 'org.cadixdev.licenser' version '0.6.0' } group = 'net.minecraftforge' @@ -23,34 +23,35 @@ sourceSets { api shared gradlecomp - java9 } repositories { jcenter() mavenCentral() + maven { + url = 'https://maven.minecraftforge.net' + } } configurations { sharedImplementation.extendsFrom apiImplementation gradlecompImplementation.extendsFrom sharedImplementation - compile.extendsFrom sharedImplementation - compile.extendsFrom gradlecompImplementation + implementation.extendsFrom sharedImplementation + implementation.extendsFrom gradlecompImplementation } dependencies { - java9Implementation files(sourceSets.main.output.classesDirs) { builtBy compileJava } - sharedImplementation sourceSets.api.output gradlecompImplementation sourceSets.shared.output gradlecompImplementation gradleApi() gradlecompImplementation 'com.google.guava:guava:30.1-jre' + gradlecompImplementation 'net.minecraftforge:unsafe:0.2.0' - compile sourceSets.api.output - compile sourceSets.shared.output - compile sourceSets.gradlecomp.output + implementation sourceSets.api.output + implementation sourceSets.shared.output + implementation sourceSets.gradlecomp.output } @@ -58,109 +59,69 @@ tasks.withType(JavaCompile) { options.encoding = 'utf-8' options.deprecation = true } + java { toolchain.languageVersion = JavaLanguageVersion.of(8) + withSourcesJar() } -project(':artifactural9') { - apply plugin: 'java' - apply plugin: 'eclipse' - group = rootProject.group - java.toolchain.languageVersion = JavaLanguageVersion.of(9) - - 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' - javaCompiler = javaToolchains.compilerFor { - languageVersion = JavaLanguageVersion.of(9) - } - } -} - - - jar { from sourceSets.api.output from sourceSets.shared.output - from(sourceSets.gradlecomp.output) - - into('META-INF/versions/9') { - from project(':artifactural9').sourceSets.java9.output - } - - manifest { - attributes( - 'Multi-Release': 'true' - ) - } + from sourceSets.gradlecomp.output } -task sourcesJar(type: Jar, dependsOn: classes) { - classifier = 'sources' +sourcesJar { from sourceSets.api.allSource from sourceSets.shared.allSource from sourceSets.gradlecomp.allSource } - license { header = file("$rootDir/LICENSE-header.txt") } publishing { - publications { - mavenJava(MavenPublication) { - from components.java - artifact tasks.sourcesJar - pom { - groupId = project.group - version = project.version - artifactId = project.archivesBaseName - name = project.archivesBaseName - packaging = 'jar' - description = 'A Gradle artifact processing and management tool' + publications.create("mavenJava", MavenPublication) { + from components.java + pom { + groupId = project.group + version = project.version + artifactId = project.archivesBaseName + name = project.archivesBaseName + packaging = 'jar' + description = 'A Gradle artifact processing and management tool' + url = 'https://github.com/MinecraftForge/Artifactural/' + + scm { url = 'https://github.com/MinecraftForge/Artifactural/' - - scm { - url = 'https://github.com/MinecraftForge/Artifactural/' - connection = 'scm:git:git://github.com/MinecraftForge/Artifactural.git' - developerConnection = 'scm:git:git@github.com:MinecraftForge/Artifactural.git' - } - issueManagement { - system = 'github' - url = 'https://github.com/MinecraftForge/Artifactural/issues' - } - licenses { - license { - name = 'LGPL-2.1' - url = 'https://www.gnu.org/licenses/lgpl-2.1.txt' - distribution = 'repo' - } + connection = 'scm:git:git://github.com/MinecraftForge/Artifactural.git' + developerConnection = 'scm:git:git@github.com:MinecraftForge/Artifactural.git' + } + issueManagement { + system = 'github' + url = 'https://github.com/MinecraftForge/Artifactural/issues' + } + licenses { + license { + name = 'LGPL-2.1' + url = 'https://www.gnu.org/licenses/lgpl-2.1.txt' + distribution = 'repo' } } } } repositories { maven { - if (project.hasProperty('mavenPassword')) { + if (System.env.MAVEN_USER) { + url 'https://maven.minecraftforge.net/' + authentication { + basic(BasicAuthentication) + } credentials { - username = project.properties.mavenUser - password = project.properties.mavenPassword + username = System.env.MAVEN_USER ?: 'not' + password = System.env.MAVEN_PASSWORD ?: 'set' } - url 'https://files.minecraftforge.net/maven/manage/upload' } else { url 'file://' + rootProject.file('repo').getAbsolutePath() } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index da9702f..549d844 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-6.8-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-6.9-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/settings.gradle b/settings.gradle index c0a8d84..20e6c4f 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,2 +1 @@ -rootProject.name = 'artifactural' -include 'artifactural9' +rootProject.name = 'artifactural'
\ No newline at end of file diff --git a/src/gradlecomp/java/net/minecraftforge/artifactural/gradle/ModifierAccess.java b/src/gradlecomp/java/net/minecraftforge/artifactural/gradle/ModifierAccess.java deleted file mode 100644 index fb68144..0000000 --- a/src/gradlecomp/java/net/minecraftforge/artifactural/gradle/ModifierAccess.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Artifactural - * Copyright (c) 2018-2021. - * - * 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 net.minecraftforge.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/net/minecraftforge/artifactural/gradle/ReflectionUtils.java b/src/gradlecomp/java/net/minecraftforge/artifactural/gradle/ReflectionUtils.java index 4e18885..1c8cdac 100644 --- a/src/gradlecomp/java/net/minecraftforge/artifactural/gradle/ReflectionUtils.java +++ b/src/gradlecomp/java/net/minecraftforge/artifactural/gradle/ReflectionUtils.java @@ -19,8 +19,11 @@ package net.minecraftforge.artifactural.gradle; +import net.minecraftforge.fml.unsafe.UnsafeHacks; + import java.lang.reflect.Field; import java.lang.reflect.Method; +import java.lang.reflect.Modifier; import java.util.function.UnaryOperator; public class ReflectionUtils { @@ -39,7 +42,11 @@ public class ReflectionUtils { T oldV = (T)f.get(target); T newV = operator.apply(oldV); - f.set(target, newV); + if (Modifier.isFinal(f.getModifiers())) { + UnsafeHacks.setField(f, target, newV); + } else { + f.set(target, newV); + } if (f.get(target) != newV) { throw new IllegalStateException("Failed to set new value on " + f.getDeclaringClass().getName() + "." + f.getName()); @@ -68,9 +75,6 @@ public class ReflectionUtils { for (Field f : clazz.getDeclaredFields()) { if (f.getName().equals(name)) { f.setAccessible(true); - 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; } } diff --git a/src/java9/net/minecraftforge/artifactural/gradle/ModifierAccess.java b/src/java9/net/minecraftforge/artifactural/gradle/ModifierAccess.java deleted file mode 100644 index cf6f65f..0000000 --- a/src/java9/net/minecraftforge/artifactural/gradle/ModifierAccess.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * 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 net.minecraftforge.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; - } - -} |