-
Notifications
You must be signed in to change notification settings - Fork 1.9k
GROOVY-11871: Support Maven Resolver based version of Grapes #2140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
paulk-asert
wants to merge
1
commit into
apache:master
Choose a base branch
from
paulk-asert:grapeMaven
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,968
−308
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package groovy.grape | ||
|
|
||
| import groovy.transform.CompileStatic | ||
| import org.apache.groovy.plugin.GroovyRunner | ||
| import org.apache.groovy.plugin.GroovyRunnerRegistry | ||
| import org.codehaus.groovy.reflection.CachedClass | ||
| import org.codehaus.groovy.reflection.ClassInfo | ||
| import org.codehaus.groovy.runtime.m12n.ExtensionModuleScanner | ||
| import org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl | ||
|
|
||
| import java.util.jar.JarFile | ||
| import java.util.zip.ZipEntry | ||
| import java.util.zip.ZipException | ||
| import java.util.zip.ZipFile | ||
|
|
||
| /** | ||
| * Utility methods shared between GrapeIvy and GrapeMaven implementations. | ||
| */ | ||
| @CompileStatic | ||
| class GrapeUtil { | ||
|
|
||
| private static final String METAINF_PREFIX = 'META-INF/services/' | ||
| private static final String RUNNER_PROVIDER_CONFIG = GroovyRunner.name | ||
| private static final boolean DEBUG_GRAPE = Boolean.getBoolean('groovy.grape.debug') | ||
|
|
||
| /** | ||
| * Adds a URI to a classloader's classpath via reflection. | ||
| */ | ||
| static void addURL(ClassLoader loader, URI uri) { | ||
| // Dynamic invocation needed as addURL is not part of ClassLoader interface | ||
| loader.metaClass.invokeMethod(loader, 'addURL', uri.toURL()) | ||
| } | ||
|
|
||
| /** | ||
| * Processes and registers category methods (extension modules) from a JAR file. | ||
| * | ||
| * @param loader the classloader to register methods with | ||
| * @param file the JAR file to process | ||
| */ | ||
| static void processExtensionMethods(ClassLoader loader, File file) { | ||
| // register extension methods if jar | ||
| if (file.getName().toLowerCase().endsWith('.jar')) { | ||
| def mcRegistry = GroovySystem.metaClassRegistry | ||
| if (mcRegistry instanceof MetaClassRegistryImpl) { | ||
| try (JarFile jar = new JarFile(file)) { | ||
| ZipEntry entry = jar.getEntry(ExtensionModuleScanner.MODULE_META_INF_FILE) | ||
| if (!entry) { | ||
| entry = jar.getEntry(ExtensionModuleScanner.LEGACY_MODULE_META_INF_FILE) | ||
| } | ||
| if (entry) { | ||
| Properties props = new Properties() | ||
|
|
||
| try (InputStream is = jar.getInputStream(entry)) { | ||
| props.load(is) | ||
| } | ||
|
|
||
| Map<CachedClass, List<MetaMethod>> metaMethods = [:] | ||
| mcRegistry.registerExtensionModuleFromProperties(props, loader, metaMethods) | ||
| // add old methods to the map | ||
| metaMethods.each { CachedClass c, List<MetaMethod> methods -> | ||
| // GROOVY-5543: if a module was loaded using grab, there are chances that subclasses | ||
| // have their own ClassInfo, and we must change them as well! | ||
| Set<CachedClass> classesToBeUpdated = [c].toSet() | ||
| ClassInfo.onAllClassInfo { ClassInfo info -> | ||
| if (c.getTheClass().isAssignableFrom(info.getCachedClass().getTheClass())) { | ||
| classesToBeUpdated << info.getCachedClass() | ||
| } | ||
| } | ||
| classesToBeUpdated*.addNewMopMethods(methods) | ||
| } | ||
| } | ||
| } catch (ZipException e) { | ||
| throw new RuntimeException("Grape could not load jar '$file'", e) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Searches the given File for known service provider configuration files to process. | ||
| * | ||
| * @param loader used to locate service provider files | ||
| * @param f ZipFile in which to search for services | ||
| * @return a collection of service provider files that were found | ||
| */ | ||
| static Collection<String> processMetaInfServices(ClassLoader loader, File f) { | ||
| List<String> services = [] | ||
| try (ZipFile zf = new ZipFile(f)) { | ||
| // TODO: remove in a future release (replaced by GroovyRunnerRegistry) | ||
| String providerConfig = 'org.codehaus.groovy.plugins.Runners' | ||
| ZipEntry pluginRunners = zf.getEntry(METAINF_PREFIX + providerConfig) | ||
| if (pluginRunners != null) { | ||
| services.add(providerConfig) | ||
|
|
||
| try (InputStream is = zf.getInputStream(pluginRunners)) { | ||
| processRunners(is, f.getName(), loader) | ||
| } | ||
| } | ||
| // GroovyRunners are loaded per ClassLoader using a ServiceLoader so here | ||
| // it only needs to be indicated that the service provider file was found | ||
| if (zf.getEntry(METAINF_PREFIX + RUNNER_PROVIDER_CONFIG) != null) { | ||
| services.add(RUNNER_PROVIDER_CONFIG) | ||
| } | ||
| } catch (ZipException ignore) { | ||
| // ignore files we can't process, e.g. non-jar/zip artifacts | ||
| if (DEBUG_GRAPE) { | ||
| System.err.println "Grape could not process file '$f' for service provider configuration: ${ignore.message}" | ||
| } | ||
| } | ||
| services | ||
| } | ||
|
|
||
| /** | ||
| * Processes and registers Groovy runner implementations from a service provider file. | ||
| * | ||
| * @param is the input stream containing runner class names | ||
| * @param name the name to register the runners under | ||
| * @param loader the classloader to load runner classes from | ||
| */ | ||
| static void processRunners(InputStream is, String name, ClassLoader loader) { | ||
| GroovyRunnerRegistry registry = GroovyRunnerRegistry.instance | ||
| is.getText().readLines()*.trim().each { String line -> | ||
| if (!line.isEmpty() && line[0] != '#') { | ||
| try { | ||
| registry[name] = (GroovyRunner) loader.loadClass(line).getDeclaredConstructor().newInstance() | ||
| } catch (Exception e) { | ||
| throw new IllegalStateException("Error registering runner class '$line'", e) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| static boolean checkForRunner(Collection<String> services) { | ||
| services.contains(RUNNER_PROVIDER_CONFIG) | ||
| } | ||
|
|
||
| static void registryLoad(ClassLoader classLoader) { | ||
| GroovyRunnerRegistry.instance.load(classLoader) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GrapeUtilis@CompileStaticbutaddURLusesloader.metaClass.invokeMethod(...), which relies on Groovy's dynamic metaClass property and is likely to fail static compilation. Consider markingaddURLas@CompileDynamic(as the old Ivy implementation did) or using a statically-compilable reflective call (e.g., viaInvokerHelper/reflection) to invokeaddURL.