24
24
import org .springframework .lang .Nullable ;
25
25
import org .springframework .util .Assert ;
26
26
import org .springframework .util .ClassUtils ;
27
+ import org .springframework .util .ObjectUtils ;
27
28
import org .springframework .util .StringUtils ;
28
29
29
30
/**
@@ -164,7 +165,7 @@ public boolean isReadable() {
164
165
}
165
166
166
167
/**
167
- * Resolves a URL for the underlying class path resource.
168
+ * Resolves a {@link URL} for the underlying class path resource.
168
169
* @return the resolved URL, or {@code null} if not resolvable
169
170
*/
170
171
@ Nullable
@@ -174,10 +175,10 @@ protected URL resolveURL() {
174
175
return this .clazz .getResource (this .path );
175
176
}
176
177
else if (this .classLoader != null ) {
177
- return this .classLoader .getResource (this .path );
178
+ return this .classLoader .getResource (this .absolutePath );
178
179
}
179
180
else {
180
- return ClassLoader .getSystemResource (this .path );
181
+ return ClassLoader .getSystemResource (this .absolutePath );
181
182
}
182
183
}
183
184
catch (IllegalArgumentException ex ) {
@@ -188,9 +189,11 @@ else if (this.classLoader != null) {
188
189
}
189
190
190
191
/**
191
- * This implementation opens an InputStream for the given class path resource.
192
+ * This implementation opens an {@link InputStream} for the underlying class
193
+ * path resource, if available.
192
194
* @see ClassLoader#getResourceAsStream(String)
193
195
* @see Class#getResourceAsStream(String)
196
+ * @see ClassLoader#getSystemResourceAsStream(String)
194
197
*/
195
198
@ Override
196
199
public InputStream getInputStream () throws IOException {
@@ -199,10 +202,10 @@ public InputStream getInputStream() throws IOException {
199
202
is = this .clazz .getResourceAsStream (this .path );
200
203
}
201
204
else if (this .classLoader != null ) {
202
- is = this .classLoader .getResourceAsStream (this .path );
205
+ is = this .classLoader .getResourceAsStream (this .absolutePath );
203
206
}
204
207
else {
205
- is = ClassLoader .getSystemResourceAsStream (this .path );
208
+ is = ClassLoader .getSystemResourceAsStream (this .absolutePath );
206
209
}
207
210
if (is == null ) {
208
211
throw new FileNotFoundException (getDescription () + " cannot be opened because it does not exist" );
@@ -227,8 +230,7 @@ public URL getURL() throws IOException {
227
230
228
231
/**
229
232
* This implementation creates a {@code ClassPathResource}, applying the given
230
- * path relative to the {@link #getPath() path} of the underlying resource of
231
- * this descriptor.
233
+ * path relative to the path used to create this descriptor.
232
234
* @see StringUtils#applyRelativePath(String, String)
233
235
*/
234
236
@ Override
@@ -246,7 +248,7 @@ public Resource createRelative(String relativePath) {
246
248
@ Override
247
249
@ Nullable
248
250
public String getFilename () {
249
- return StringUtils .getFilename (this .path );
251
+ return StringUtils .getFilename (this .absolutePath );
250
252
}
251
253
252
254
/**
@@ -260,29 +262,24 @@ public String getDescription() {
260
262
261
263
262
264
/**
263
- * This implementation compares the underlying class path locations.
265
+ * This implementation compares the underlying class path locations and
266
+ * associated class loaders.
267
+ * @see #getPath()
268
+ * @see #getClassLoader()
264
269
*/
265
270
@ Override
266
- public boolean equals (@ Nullable Object other ) {
267
- if (this == other ) {
271
+ public boolean equals (@ Nullable Object obj ) {
272
+ if (this == obj ) {
268
273
return true ;
269
274
}
270
- if (!(other instanceof ClassPathResource otherRes )) {
271
- return false ;
272
- }
273
- if (!this .absolutePath .equals (otherRes .absolutePath )) {
274
- return false ;
275
- }
276
- ClassLoader thisClassLoader =
277
- (this .classLoader != null ? this .classLoader : this .clazz .getClassLoader ());
278
- ClassLoader otherClassLoader =
279
- (otherRes .classLoader != null ? otherRes .classLoader : otherRes .clazz .getClassLoader ());
280
- return thisClassLoader .equals (otherClassLoader );
275
+ return ((obj instanceof ClassPathResource other ) &&
276
+ this .absolutePath .equals (other .absolutePath ) &&
277
+ ObjectUtils .nullSafeEquals (getClassLoader (), other .getClassLoader ()));
281
278
}
282
279
283
280
/**
284
- * This implementation returns the hash code of the underlying
285
- * class path location.
281
+ * This implementation returns the hash code of the underlying class path location.
282
+ * @see #getPath()
286
283
*/
287
284
@ Override
288
285
public int hashCode () {
0 commit comments