Skip to content

Commit 30f1eee

Browse files
committed
Polish Javadoc and implementation of ClassPathResource
1 parent 61cc7c0 commit 30f1eee

File tree

1 file changed

+22
-25
lines changed

1 file changed

+22
-25
lines changed

spring-core/src/main/java/org/springframework/core/io/ClassPathResource.java

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.springframework.lang.Nullable;
2525
import org.springframework.util.Assert;
2626
import org.springframework.util.ClassUtils;
27+
import org.springframework.util.ObjectUtils;
2728
import org.springframework.util.StringUtils;
2829

2930
/**
@@ -164,7 +165,7 @@ public boolean isReadable() {
164165
}
165166

166167
/**
167-
* Resolves a URL for the underlying class path resource.
168+
* Resolves a {@link URL} for the underlying class path resource.
168169
* @return the resolved URL, or {@code null} if not resolvable
169170
*/
170171
@Nullable
@@ -174,10 +175,10 @@ protected URL resolveURL() {
174175
return this.clazz.getResource(this.path);
175176
}
176177
else if (this.classLoader != null) {
177-
return this.classLoader.getResource(this.path);
178+
return this.classLoader.getResource(this.absolutePath);
178179
}
179180
else {
180-
return ClassLoader.getSystemResource(this.path);
181+
return ClassLoader.getSystemResource(this.absolutePath);
181182
}
182183
}
183184
catch (IllegalArgumentException ex) {
@@ -188,9 +189,11 @@ else if (this.classLoader != null) {
188189
}
189190

190191
/**
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.
192194
* @see ClassLoader#getResourceAsStream(String)
193195
* @see Class#getResourceAsStream(String)
196+
* @see ClassLoader#getSystemResourceAsStream(String)
194197
*/
195198
@Override
196199
public InputStream getInputStream() throws IOException {
@@ -199,10 +202,10 @@ public InputStream getInputStream() throws IOException {
199202
is = this.clazz.getResourceAsStream(this.path);
200203
}
201204
else if (this.classLoader != null) {
202-
is = this.classLoader.getResourceAsStream(this.path);
205+
is = this.classLoader.getResourceAsStream(this.absolutePath);
203206
}
204207
else {
205-
is = ClassLoader.getSystemResourceAsStream(this.path);
208+
is = ClassLoader.getSystemResourceAsStream(this.absolutePath);
206209
}
207210
if (is == null) {
208211
throw new FileNotFoundException(getDescription() + " cannot be opened because it does not exist");
@@ -227,8 +230,7 @@ public URL getURL() throws IOException {
227230

228231
/**
229232
* 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.
232234
* @see StringUtils#applyRelativePath(String, String)
233235
*/
234236
@Override
@@ -246,7 +248,7 @@ public Resource createRelative(String relativePath) {
246248
@Override
247249
@Nullable
248250
public String getFilename() {
249-
return StringUtils.getFilename(this.path);
251+
return StringUtils.getFilename(this.absolutePath);
250252
}
251253

252254
/**
@@ -260,29 +262,24 @@ public String getDescription() {
260262

261263

262264
/**
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()
264269
*/
265270
@Override
266-
public boolean equals(@Nullable Object other) {
267-
if (this == other) {
271+
public boolean equals(@Nullable Object obj) {
272+
if (this == obj) {
268273
return true;
269274
}
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()));
281278
}
282279

283280
/**
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()
286283
*/
287284
@Override
288285
public int hashCode() {

0 commit comments

Comments
 (0)