-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
DefaultDeprecatedClassLoader.java
152 lines (129 loc) · 4.74 KB
/
DefaultDeprecatedClassLoader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* 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 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 {
private static final String BUILDSRC_IN_SETTINGS_DEPRECATION_WARNING = "Access to the buildSrc project and its dependencies in settings scripts";
private final ClassLoader deprecatedUsageLoader;
private final ClassLoader nonDeprecatedParent;
boolean deprecationFired;
public DefaultDeprecatedClassLoader(ClassLoader deprecatedUsageLoader, ClassLoader nonDeprecatedParent) {
super(null);
this.deprecatedUsageLoader = deprecatedUsageLoader;
this.nonDeprecatedParent = nonDeprecatedParent;
}
@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
return loadClass(name, false);
}
@Override
public URL getResource(String name) {
URL resource;
if (!deprecationFired) {
resource = nonDeprecatedParent.getResource(name);
if (resource != null) {
return resource;
}
}
resource = deprecatedUsageLoader.getResource(name);
if (resource != null) {
maybeEmitDeprecationWarning();
}
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) {
try {
return nonDeprecatedParent.loadClass(name);
} catch (ClassNotFoundException e) {
// Expected
}
}
try {
Class<?> deprecatedUsageClass = deprecatedUsageLoader.loadClass(name);
maybeEmitDeprecationWarning();
return deprecatedUsageClass;
} catch (ClassNotFoundException e) {
// Expected
}
throw new ClassNotFoundException(String.format("%s not found.", name));
}
private void maybeEmitDeprecationWarning() {
DeprecationLogger.nagUserOfDeprecated(BUILDSRC_IN_SETTINGS_DEPRECATION_WARNING);
deprecationFired = true;
}
@Override
public void visit(ClassLoaderVisitor visitor) {
visitor.visit(deprecatedUsageLoader);
visitor.visit(nonDeprecatedParent);
}
@Override
public void close() throws IOException {
if (deprecatedUsageLoader instanceof Closeable) {
((Closeable) deprecatedUsageLoader).close();
}
// not sure if this is required as its the parent of
// deprecatedUsageLoader already
if (nonDeprecatedParent instanceof Closeable) {
((Closeable) nonDeprecatedParent).close();
}
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
DefaultDeprecatedClassLoader that = (DefaultDeprecatedClassLoader) o;
if (deprecationFired != that.deprecationFired) {
return false;
}
if (!deprecatedUsageLoader.equals(that.deprecatedUsageLoader)) {
return false;
}
return nonDeprecatedParent.equals(that.nonDeprecatedParent);
}
@Override
public int hashCode() {
int result = deprecatedUsageLoader.hashCode();
result = 31 * result + nonDeprecatedParent.hashCode();
return result;
}
}