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

Override getResources in DefaultDeprecatedClassLoader #10349

Closed
wants to merge 2 commits into from
Closed
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 @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class DefaultClassLoaderCache implements ClassLoaderCacheInternal, Stoppa
private final Map<ClassLoaderId, CachedClassLoader> byId = Maps.newHashMap();
private final Map<ClassLoaderSpec, CachedClassLoader> bySpec = Maps.newHashMap();
private final Set<ClassLoaderId> usedInThisBuild = Sets.newHashSet();
private final Set<CachedClassLoader> released = Sets.newHashSet();
private final ClasspathHasher classpathHasher;
private final HashingClassLoaderFactory classLoaderFactory;

Expand Down Expand Up @@ -75,6 +76,7 @@ public ClassLoader get(ClassLoaderId id, ClassPath classPath, @Nullable ClassLoa

if (cachedLoader != null) {
LOGGER.debug("Releasing previous classloader for {}", id);
released.add(cachedLoader);
cachedLoader.release(id);
}
return newLoader.classLoader;
Expand Down Expand Up @@ -140,6 +142,9 @@ public void stop() {
for (CachedClassLoader cachedClassLoader : byId.values()) {
ClassLoaderUtils.tryClose(cachedClassLoader.classLoader);
}
for (CachedClassLoader cachedClassLoader : released) {
ClassLoaderUtils.tryClose(cachedClassLoader.classLoader);
}
byId.clear();
bySpec.clear();
usedInThisBuild.clear();
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
}
}