Skip to content

Commit

Permalink
Override getResources in DefaultDeprecatedClassLoader
Browse files Browse the repository at this point in the history
This fixes #10347

In #9898 we introduced a DefaultDeprecatedClassLoader,
which overrides getResource() but not getResources(). This causes some issues.
This PR fixes the issue by correctly overriding getResources() method.
  • Loading branch information
blindpirate committed Aug 23, 2019
1 parent f0b9d60 commit 07e3e0d
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 9 deletions.
Expand Up @@ -23,6 +23,7 @@
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,27 @@ 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,16 +93,14 @@ 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));
}

private void maybeEmitDeprecationWarning() {
if (!deprecationFired) {
DeprecationLogger.nagUserOfDeprecated(BUILDSRC_IN_SETTINGS_DEPRECATION_WARNING);
deprecationFired = true;
}
DeprecationLogger.nagUserOfDeprecated(BUILDSRC_IN_SETTINGS_DEPRECATION_WARNING);
deprecationFired = true;
}

@Override
Expand All @@ -94,13 +111,13 @@ public void visit(ClassLoaderVisitor visitor) {

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

// not sure if this is required as its the parent of
// deprecatedUsageLoader already
if(nonDeprecatedParent instanceof Closeable){
if (nonDeprecatedParent instanceof Closeable) {
((Closeable) nonDeprecatedParent).close();
}
}
Expand Down
@@ -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
}
}

0 comments on commit 07e3e0d

Please sign in to comment.