Skip to content
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

Fix regression #10369

Merged
merged 3 commits into from
Aug 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,33 @@
package org.gradle.initialization.buildsrc

import org.gradle.integtests.fixtures.AbstractIntegrationSpec
import spock.lang.Issue

class BuildSrcDeprecatedInSettingsIntegrationTest extends AbstractIntegrationSpec {

@Issue("https://github.com/gradle/gradle/issues/10347")
def "can use JsonSlurper from settings"() {
file('buildSrc/build.gradle') << """
apply plugin: 'groovy'
"""
settingsFile << """
apply from: "other.gradle"
"""
file("other.gradle") << """
def slurped = new groovy.json.JsonSlurper().parseText("{}")
println "Slurped " + slurped
"""
expect:
succeeds("help")

}

def "Using buildSrc classes in settings is deprecated"() {
file('buildSrc/build.gradle') << """
apply plugin: 'groovy'

repositories {
mavenCentral()
${mavenCentralRepository()}
}

dependencies {
Expand Down Expand Up @@ -85,7 +103,7 @@ class BuildSrcDeprecatedInSettingsIntegrationTest extends AbstractIntegrationSpe
apply plugin: 'groovy'

repositories {
mavenCentral()
${mavenCentralRepository()}
}

dependencies {
Expand Down Expand Up @@ -154,7 +172,7 @@ class BuildSrcDeprecatedInSettingsIntegrationTest extends AbstractIntegrationSpe
apply plugin: 'groovy'

repositories {
mavenCentral()
${mavenCentralRepository()}
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@

package org.gradle.api.internal.initialization;

import org.gradle.internal.classloader.ClassLoaderUtils;
import org.gradle.internal.classloader.ClassLoaderVisitor;
import org.gradle.internal.classloader.DeprecatedClassloader;
import org.gradle.util.DeprecationLogger;

import java.io.Closeable;
import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;

public class DefaultDeprecatedClassLoader extends ClassLoader implements DeprecatedClassloader {

Expand All @@ -46,7 +47,7 @@ public Class<?> loadClass(String name) throws ClassNotFoundException {
@Override
public URL getResource(String name) {
URL resource;
if(!deprecationFired) {
if (!deprecationFired) {
resource = nonDeprecatedParent.getResource(name);
if (resource != null) {
return resource;
Expand All @@ -60,9 +61,26 @@ public URL getResource(String name) {
return resource;
}

@Override
public Enumeration<URL> getResources(String name) throws IOException {
Enumeration<URL> resources;
if (!deprecationFired) {
resources = nonDeprecatedParent.getResources(name);
if (resources.hasMoreElements()) {
return resources;
}
}

resources = deprecatedUsageLoader.getResources(name);
if (resources.hasMoreElements()) {
maybeEmitDeprecationWarning();
}
return resources;
}

@Override
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
if(!deprecationFired) {
if (!deprecationFired) {
try {
return nonDeprecatedParent.loadClass(name);
} catch (ClassNotFoundException e) {
Expand All @@ -74,7 +92,7 @@ protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundE
maybeEmitDeprecationWarning();
return deprecatedUsageClass;
} catch (ClassNotFoundException e) {
// Expected
// Expected
}
throw new ClassNotFoundException(String.format("%s not found.", name));
}
Expand All @@ -94,15 +112,11 @@ public void visit(ClassLoaderVisitor visitor) {

@Override
public void close() throws IOException {
if(deprecatedUsageLoader instanceof Closeable){
((Closeable) deprecatedUsageLoader).close();
}
ClassLoaderUtils.tryClose(deprecatedUsageLoader);

// not sure if this is required as its the parent of
// deprecatedUsageLoader already
if(nonDeprecatedParent instanceof Closeable){
((Closeable) nonDeprecatedParent).close();
}
ClassLoaderUtils.tryClose(nonDeprecatedParent);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,17 @@ public ClassLoaderScope deprecated() {
@Override
public ClassLoader getExportClassLoader() {
if (deprecatedExportClassloader == null) {
ClassLoaderId id = this.id.exportId();
ClassLoaderId id = this.id.child("deprecated-only").exportId();
deprecatedExportClassloader = new DefaultDeprecatedClassLoader(buildLockedLoader(id, deprecatedClasspath), super.getExportClassLoader());
classLoaderCache.put(id, deprecatedExportClassloader);
}
return deprecatedExportClassloader;
}

@Override
public ClassLoader getLocalClassLoader() {
if (deprecatedLocalClassloader == null) {
ClassLoaderId id = this.id.localId();
ClassLoaderId id = this.id.child("deprecated-only").localId();
deprecatedLocalClassloader = new DefaultDeprecatedClassLoader(buildLockedLoader(id, deprecatedClasspath), super.getLocalClassLoader());
classLoaderCache.put(id, deprecatedLocalClassloader);
}
return deprecatedLocalClassloader;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2019 the original author or authors.
*
* Licensed 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 org.gradle.api.internal.initialization

import com.google.common.collect.Iterators
import spock.lang.Specification
import spock.lang.Subject

class DefaultDeprecatedClassLoaderTest extends Specification {
ClassLoader nonDeprecatedParent = Mock()
ClassLoader deprecatedUsageLoader = Mock()

String resource = 'testResource'
URL url1 = new URL('https://a.com')
URL url2 = new URL('https://b.com')

@Subject
DefaultDeprecatedClassLoader classLoader = new DefaultDeprecatedClassLoader(deprecatedUsageLoader, nonDeprecatedParent)

def 'can get resource from nonDeprecatedParent'() {
when:
1 * nonDeprecatedParent.getResource(resource) >> url1
0 * _._

then:
classLoader.getResource(resource) == url1
}

def 'can get resource from deprecatedUsageLoader'() {
when:
1 * nonDeprecatedParent.getResource(resource) >> null
1 * deprecatedUsageLoader.getResource(resource) >> url2

then:
classLoader.getResource(resource) == url2
}

def 'can get resources from nonDeprecatedParent'() {
when:
1 * nonDeprecatedParent.getResources(resource) >> Iterators.asEnumeration([url1].iterator())
0 * _._

then:
classLoader.getResources(resource).nextElement() == url1
}

def 'can get resources from deprecatedUsageLoader'() {
when:
1 * nonDeprecatedParent.getResources(resource) >> Iterators.asEnumeration([].iterator())
1 * deprecatedUsageLoader.getResources(resource) >> Iterators.asEnumeration([url2].iterator())

then:
classLoader.getResources(resource).nextElement() == url2
}
}