Skip to content

Commit 10155ea

Browse files
committed
Simpler handling of LoadedClasses.srcUrl; no apparent need for canonicalize any more
1 parent 8598f5e commit 10155ea

File tree

2 files changed

+6
-21
lines changed

2 files changed

+6
-21
lines changed

src/main/java/org/jenkinsci/plugins/workflow/libs/LibraryStep.java

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,11 @@ public static final class LoadedClasses extends GroovyObjectSupport implements S
251251
private final @NonNull String prefix;
252252
/** {@link Class#getName} minus package prefix */
253253
private final @CheckForNull String clazz;
254-
/** {@code file:/…/libs/NAME/src/} */
254+
/** {@code jar:file:/…/libs/$hash.jar!/} */
255255
private final @NonNull String srcUrl;
256256

257257
LoadedClasses(String library, String libraryDirectoryName, boolean trusted, Boolean changelog, Run<?,?> run) {
258-
this(library, trusted, changelog, "", null, /* cf. LibraryAdder.retrieve */ new File(run.getRootDir(), "libs/" + libraryDirectoryName + ".jar").toURI().toString());
258+
this(library, trusted, changelog, "", null, /* cf. LibraryAdder.retrieve */ "jar:" + new File(run.getRootDir(), "libs/" + libraryDirectoryName + ".jar").toURI() + "!/");
259259
}
260260

261261
LoadedClasses(String library, boolean trusted, Boolean changelog, String prefix, String clazz, String srcUrl) {
@@ -357,11 +357,8 @@ private Class<?> loadClass(String name) {
357357
throw new IllegalAccessException(name + " had no defined code source");
358358
}
359359
String loc = codeSource.getLocation().toString();
360-
LOGGER.info(() -> "TODO got " + loc);
361-
String actual = canonicalize(loc);
362-
String srcUrlC = canonicalize(srcUrl); // do not do this in constructor: path might not actually exist
363-
if (!actual.startsWith(srcUrlC)) {
364-
throw new IllegalAccessException(name + " was defined in " + actual + " which was not inside " + srcUrlC);
360+
if (!loc.startsWith(srcUrl)) {
361+
throw new IllegalAccessException(name + " was defined in " + loc + " which was not inside " + srcUrl);
365362
}
366363
if (!Modifier.isPublic(c.getModifiers())) { // unlikely since Groovy makes classes implicitly public
367364
throw new IllegalAccessException(c + " is not public");
@@ -374,20 +371,6 @@ private Class<?> loadClass(String name) {
374371
}
375372
}
376373

377-
private static String canonicalize(String uri) {
378-
if (uri.startsWith("jar:") && uri.contains("!/")) {
379-
// See warning in CpsGroovyShell.parseClass.
380-
uri = uri.substring(4, uri.indexOf("!/"));
381-
}
382-
if (uri.startsWith("file:/")) {
383-
try {
384-
return Paths.get(new URI(uri)).toRealPath().toUri().toString();
385-
} catch (IOException | URISyntaxException x) {
386-
LOGGER.log(Level.WARNING, "could not canonicalize " + uri, x);
387-
}
388-
}
389-
return uri;
390-
}
391374

392375
}
393376

src/test/java/org/jenkinsci/plugins/workflow/libs/LibraryStepTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ public class LibraryStepTest {
129129
r.assertLogContains("using constant vs. constant", b);
130130
}
131131

132+
// TODO test LoadedClasses.srcUrl after reload build started prior to dir2Jar (@OldData)
133+
132134
@Test public void missingProperty() throws Exception {
133135
sampleRepo.init();
134136
sampleRepo.write("src/some/pkg/MyClass.groovy", "package some.pkg; class MyClass { }");

0 commit comments

Comments
 (0)