From 747c582804bdcd0409417fdd92f7a3042d80b6b5 Mon Sep 17 00:00:00 2001 From: Aaron Hill Date: Thu, 25 Apr 2019 16:44:20 -0400 Subject: Add compatibility with Gradle 4.10 and above This commits adds compatibility for Gradle 4.10 onward (tested on 4.10 and 5,4), while retainting compatibiliy with 4.9 Due to the ABI-incompatible changes in some internal gradle classes, we need to use bytecode manipulation to ensure that our built class files are compatible with both Gradle 4.9 and Gradle >= 4.10 Bytecode manipulation is performed at built time. A custom Gradle task is used to read in the compiled class files from disk, and write out a modified version to the final jar artifact. In order to make as few bytecode modifications as possible, this commit bumps the Gradle wrapper version to 4.10. This means that we're compiling against Gradle 4.10, and using bytecode manipulation to retain compatibility with 4.9. Doing the reverse (compiling against 4.9) would be significantly more difficult, as we would need to statically reference classes that exist in Gradle 4.10 but not 4.9 (specifically, RepositoryDescriptor) We perform two different bytecode transformations: 1. We modify the call to 'super()' in GradleRepositoryAdapter. In Gradle 4.10, the suepr constructor takes one argument, but in 4.9, it takes zero arguments. In order to allow GradleRepositoryAdapter to compile normally, we write a 'fake' call to 'super(null)', and replace with a call to 'super()'. 2. We delete the method 'getDescriptor' from GradleRepositoryAdapter. In Gradle 4.9, its return type does not exist, and will cause a NoClassDefFoundError when Gradle attempts to classload it via Class#getDeclaredMethods. However, it's necessary to include 'getDescriptor' so that GradleRepositoryAdapter (we need to override the abstract method in a parent class). --- .../gradle/Gradle410RepositoryAdapter.java | 42 +++++++++++++++++ .../gradle/GradleRepositoryAdapter.java | 54 +++++++++++++++++++++- 2 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 src/gradlecomp/java/com/amadornes/artifactural/gradle/Gradle410RepositoryAdapter.java (limited to 'src') diff --git a/src/gradlecomp/java/com/amadornes/artifactural/gradle/Gradle410RepositoryAdapter.java b/src/gradlecomp/java/com/amadornes/artifactural/gradle/Gradle410RepositoryAdapter.java new file mode 100644 index 0000000..ceaf989 --- /dev/null +++ b/src/gradlecomp/java/com/amadornes/artifactural/gradle/Gradle410RepositoryAdapter.java @@ -0,0 +1,42 @@ +/* + * 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 com.amadornes.artifactural.api.repository.Repository; +import org.gradle.api.internal.artifacts.repositories.DefaultMavenLocalArtifactRepository; +import org.gradle.api.internal.artifacts.repositories.descriptor.FlatDirRepositoryDescriptor; +import org.gradle.api.internal.artifacts.repositories.descriptor.RepositoryDescriptor; +import org.gradle.api.model.ObjectFactory; + +import java.util.ArrayList; + +public class Gradle410RepositoryAdapter extends GradleRepositoryAdapter { + + Gradle410RepositoryAdapter(ObjectFactory objectFactory, Repository repository, DefaultMavenLocalArtifactRepository local) { + super(objectFactory, repository, local); + } + + + @Override + public RepositoryDescriptor getDescriptor() { + return new FlatDirRepositoryDescriptor("ArtifacturalRepository", new ArrayList<>()); + } + +} diff --git a/src/gradlecomp/java/com/amadornes/artifactural/gradle/GradleRepositoryAdapter.java b/src/gradlecomp/java/com/amadornes/artifactural/gradle/GradleRepositoryAdapter.java index 64d6652..669ad4c 100644 --- a/src/gradlecomp/java/com/amadornes/artifactural/gradle/GradleRepositoryAdapter.java +++ b/src/gradlecomp/java/com/amadornes/artifactural/gradle/GradleRepositoryAdapter.java @@ -37,11 +37,14 @@ import org.gradle.api.internal.artifacts.ivyservice.resolveengine.artifact.Resol import org.gradle.api.internal.artifacts.repositories.AbstractArtifactRepository; import org.gradle.api.internal.artifacts.repositories.DefaultMavenLocalArtifactRepository; import org.gradle.api.internal.artifacts.repositories.ResolutionAwareRepository; +import org.gradle.api.internal.artifacts.repositories.descriptor.FlatDirRepositoryDescriptor; +import org.gradle.api.internal.artifacts.repositories.descriptor.RepositoryDescriptor; import org.gradle.api.internal.artifacts.repositories.resolver.ExternalResourceArtifactResolver; import org.gradle.api.internal.artifacts.repositories.resolver.ExternalResourceResolver; import org.gradle.api.internal.artifacts.repositories.resolver.MavenResolver; import org.gradle.api.internal.artifacts.repositories.resolver.MetadataFetchingCost; import org.gradle.api.internal.component.ArtifactType; +import org.gradle.api.model.ObjectFactory; import org.gradle.internal.action.InstantiatingAction; import org.gradle.internal.component.external.model.ModuleComponentResolveMetadata; import org.gradle.internal.component.external.model.ModuleDependencyMetadata; @@ -65,10 +68,12 @@ import org.gradle.internal.resource.local.LocalFileStandInExternalResource; import org.gradle.internal.resource.local.LocallyAvailableExternalResource; import org.gradle.internal.resource.metadata.ExternalResourceMetaData; import org.gradle.internal.resource.transfer.DefaultCacheAwareExternalResourceAccessor; +import org.gradle.util.GradleVersion; import java.io.File; import java.io.IOException; import java.net.URI; +import java.util.ArrayList; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -85,7 +90,18 @@ public class GradleRepositoryAdapter extends AbstractArtifactRepository implemen maven.setUrl(local); maven.setName(name); - GradleRepositoryAdapter repo = new GradleRepositoryAdapter(repository, maven); + GradleRepositoryAdapter repo; + + // On Gradle 4.10 above, we need to use the constructor with the 'ObjectFactory' parameter + // (which can be safely passed as null - see BaseMavenInstaller). + // We use Gradle410RepositoryAdapter, which actually overrides 'getDescriptor' + if (GradleVersion.current().compareTo(GradleVersion.version("4.10")) >= 0) { + repo = new Gradle410RepositoryAdapter(null, repository, maven); + } else { + // On versions of gradle older than 4.10, we use the no-arg super constructor + repo = new GradleRepositoryAdapter(repository, maven); + } + repo.setName(name); handler.add(repo); return repo; @@ -96,7 +112,25 @@ public class GradleRepositoryAdapter extends AbstractArtifactRepository implemen private final String root; private final LocatedArtifactCache cache; + + // This constructor is modified via bytecode manipulation in 'build.gradle' + // DO NOT change this without modifying 'build.gradle' + // This contructor is used on Gradle 4.9 and below private GradleRepositoryAdapter(Repository repository, DefaultMavenLocalArtifactRepository local) { + // This is replaced with a call to 'super()', with no arguments + super(null); + this.repository = repository; + this.local = local; + this.root = cleanRoot(local.getUrl()); + this.cache = new LocatedArtifactCache(new File(root)); + } + + + // This constructor is used on Gradle 4.10 and above + GradleRepositoryAdapter(ObjectFactory objectFactory, Repository repository, DefaultMavenLocalArtifactRepository local) { + super(objectFactory); + // This duplication from the above two-argument constructor is unfortunate, + // but unavoidable this.repository = repository; this.local = local; this.root = cleanRoot(local.getUrl()); @@ -182,6 +216,24 @@ public class GradleRepositoryAdapter extends AbstractArtifactRepository implemen }; } + // This method will be deleted entirely in build.gradle + // In order for this class to compile, this method needs to exist + // at compile-time. However, the class 'RepositoryDescriptor' doesn't + // exist in Gradle 4.9. If we try to classload a class + // that contains RepositoryDescriptor as a method return type, + // the JVM will try to classload RepositoryDescriptor, leading + // to a NoClassDefFoundError + + // To fix this, we strip out this method at build time. + // At runtime, we instantiate Gradle410RepositoryAdapter + // when we're running on Gradle 4.10 on above. + // This ensures that 'getDescriptor' exists on Gradle 4.10, + // and doesn't existon Gradle 4.9 + public RepositoryDescriptor getDescriptor() { + throw new Error("This method should be been stripped at build time!"); + } + + private static String cleanRoot(URI uri) { String ret = uri.normalize().getPath().replace('\\', '/'); if (!ret.endsWith("/")) ret += '/'; -- cgit