From a070a5d19ffe1d20944214d3219f0c3c8ec2c8c0 Mon Sep 17 00:00:00 2001 From: Rafael Winterhalter Date: Tue, 3 Jan 2023 22:34:58 +0100 Subject: [PATCH] Avoid class loader creation if not needed. --- .../build/gradle/AbstractByteBuddyTask.java | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/byte-buddy-gradle-plugin/src/main/java/net/bytebuddy/build/gradle/AbstractByteBuddyTask.java b/byte-buddy-gradle-plugin/src/main/java/net/bytebuddy/build/gradle/AbstractByteBuddyTask.java index d2f0851286..6ab8694219 100644 --- a/byte-buddy-gradle-plugin/src/main/java/net/bytebuddy/build/gradle/AbstractByteBuddyTask.java +++ b/byte-buddy-gradle-plugin/src/main/java/net/bytebuddy/build/gradle/AbstractByteBuddyTask.java @@ -371,7 +371,7 @@ protected void doApply(Plugin.Engine.Source source, Plugin.Engine.Target target) throw new IllegalStateException("Source and target cannot be equal: " + source()); } List transformations = new ArrayList(getTransformations()); - ClassLoader classLoader = ByteBuddySkippingUrlClassLoader.of(getClass().getClassLoader(), discoverySet(), classPath()); + ClassLoader classLoader = ByteBuddySkippingUrlClassLoader.of(getClass().getClassLoader(), discoverySet()); Plugin.Engine.Summary summary; try { if (discovery.isDiscover(transformations)) { @@ -465,7 +465,7 @@ protected void doApply(Plugin.Engine.Source source, Plugin.Engine.Target target) classFileLocator.close(); } } finally { - if (classLoader instanceof Closeable) { + if (classLoader instanceof Closeable && classLoader instanceof ByteBuddySkippingUrlClassLoader) { ((Closeable) classLoader).close(); } } @@ -580,21 +580,23 @@ protected ByteBuddySkippingUrlClassLoader(ClassLoader parent, URL[] url) { * * @param classLoader The class loader of the Byte Buddy plugin. * @param discoverySet The source set to discover plugins from or {@code null} if no source set is used. - * @param classPath The configured class path. * @return The resolved class loader. */ - protected static ClassLoader of(ClassLoader classLoader, @MaybeNull Iterable discoverySet, Iterable classPath) { + protected static ClassLoader of(ClassLoader classLoader, @MaybeNull Iterable discoverySet) { + if (discoverySet == null) { + return classLoader; + } List urls = new ArrayList(); - for (File file : discoverySet == null - ? classPath - : discoverySet) { + for (File file : discoverySet) { try { urls.add(file.toURI().toURL()); } catch (MalformedURLException e) { throw new IllegalStateException(e); } } - return new ByteBuddySkippingUrlClassLoader(classLoader, urls.toArray(new URL[0])); + return urls.isEmpty() + ? classLoader + : new ByteBuddySkippingUrlClassLoader(classLoader, urls.toArray(new URL[0])); } @Override