Ensure ClassPathResources with same path and ClassLoader are equal

Prior to this commit, if two ClassPathResource instances were
constructed differently (one from an absolute path and one from a path
relative to a Class) but had the same absolute path and the same
ClassLoader, they were effectively equal, but ClassPathResource#equals
returned false.

This commit addresses this by revising the logic in
ClassPathResource#equals accordingly.

Closes gh-29263
This commit is contained in:
Sam Brannen
2022-10-05 16:26:58 +02:00
parent 0aa9d9d535
commit a380ca2750
2 changed files with 33 additions and 4 deletions

View File

@@ -24,7 +24,6 @@ import java.net.URL;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
/**
@@ -271,9 +270,14 @@ public class ClassPathResource extends AbstractFileResolvingResource {
if (!(other instanceof ClassPathResource otherRes)) {
return false;
}
return (this.path.equals(otherRes.path) &&
ObjectUtils.nullSafeEquals(this.classLoader, otherRes.classLoader) &&
ObjectUtils.nullSafeEquals(this.clazz, otherRes.clazz));
if (!this.absolutePath.equals(otherRes.absolutePath)) {
return false;
}
ClassLoader thisClassLoader =
(this.classLoader != null ? this.classLoader : this.clazz.getClassLoader());
ClassLoader otherClassLoader =
(otherRes.classLoader != null ? otherRes.classLoader : otherRes.clazz.getClassLoader());
return thisClassLoader.equals(otherClassLoader);
}
/**