Skip to content

Commit c0b7411

Browse files
committed
Use manifest from class’s jar when defining its package
Previously, when defining a package for a class, LaunchedURLClassLoader would use the manifest from the first location that contained the required package. If the package was split across multiple locations, this could lead to the manifest from a jar other than the one that contains the class being used. This commit updates LaunchedURLClassLoader so that it will use the manifest of the jar file that contains the class which triggered the definition of the package. Closes gh-5485
1 parent 1d09903 commit c0b7411

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/LaunchedURLClassLoader.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ private void definePackageIfNecessary(String className) {
105105
String packageName = className.substring(0, lastDot);
106106
if (getPackage(packageName) == null) {
107107
try {
108-
definePackage(packageName);
108+
definePackage(className, packageName);
109109
}
110110
catch (IllegalArgumentException ex) {
111111
// Tolerate race condition due to being parallel capable
@@ -122,17 +122,19 @@ private void definePackageIfNecessary(String className) {
122122
}
123123
}
124124

125-
private void definePackage(final String packageName) {
125+
private void definePackage(final String className, final String packageName) {
126126
try {
127127
AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
128128
@Override
129129
public Object run() throws ClassNotFoundException {
130130
String packageEntryName = packageName.replace(".", "/") + "/";
131+
String classEntryName = className.replace(".", "/") + ".class";
131132
for (URL url : getURLs()) {
132133
try {
133134
if (url.getContent() instanceof JarFile) {
134135
JarFile jarFile = (JarFile) url.getContent();
135-
if (jarFile.getEntry(packageEntryName) != null
136+
if (jarFile.getEntry(classEntryName) != null
137+
&& jarFile.getEntry(packageEntryName) != null
136138
&& jarFile.getManifest() != null) {
137139
definePackage(packageName, jarFile.getManifest(),
138140
url);

0 commit comments

Comments
 (0)