diff options
Diffstat (limited to 'src/launch')
-rw-r--r-- | src/launch/lombok/launch/Main.java | 5 | ||||
-rw-r--r-- | src/launch/lombok/launch/ShadowClassLoader.java | 37 |
2 files changed, 36 insertions, 6 deletions
diff --git a/src/launch/lombok/launch/Main.java b/src/launch/lombok/launch/Main.java index 08298cc2..af65bdf8 100644 --- a/src/launch/lombok/launch/Main.java +++ b/src/launch/lombok/launch/Main.java @@ -34,6 +34,11 @@ class Main { return classLoader; } + static synchronized void prependClassLoader(ClassLoader loader) { + getShadowClassLoader(); + classLoader.prependParent(loader); + } + public static void main(String[] args) throws Throwable { ClassLoader cl = getShadowClassLoader(); Class<?> mc = cl.loadClass("lombok.core.Main"); diff --git a/src/launch/lombok/launch/ShadowClassLoader.java b/src/launch/lombok/launch/ShadowClassLoader.java index da377ae4..929681ce 100644 --- a/src/launch/lombok/launch/ShadowClassLoader.java +++ b/src/launch/lombok/launch/ShadowClassLoader.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2014-2018 The Project Lombok Authors. + * Copyright (C) 2014-2021 The Project Lombok Authors. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -38,6 +38,7 @@ import java.util.Collections; import java.util.Enumeration; import java.util.HashMap; import java.util.HashSet; +import java.util.IdentityHashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -102,6 +103,14 @@ class ShadowClassLoader extends ClassLoader { private final List<String> parentExclusion = new ArrayList<String>(); private final List<String> highlanders = new ArrayList<String>(); + private final Set<ClassLoader> prependedParentLoaders = Collections.newSetFromMap(new IdentityHashMap<ClassLoader, Boolean>()); + + public void prependParent(ClassLoader loader) { + if (loader == null) return; + if (loader == getParent()) return; + prependedParentLoaders.add(loader); + } + /** * @param source The 'parent' classloader. * @param sclSuffix The suffix of the shadowed class files in our own jar. For example, if this is {@code lombok}, then the class files in your jar should be {@code foo/Bar.SCL.lombok} and not {@code foo/Bar.class}. @@ -531,15 +540,31 @@ class ShadowClassLoader extends ClassLoader { String fileNameOfClass = name.replace(".", "/") + ".class"; URL res = getResource_(fileNameOfClass, true); if (res == null) { - if (!exclusionListMatch(fileNameOfClass)) try { - return super.loadClass(name, resolve); - } catch (ClassNotFoundException cnfe) { - res = getResource_("secondaryLoading.SCL." + sclSuffix + "/" + name.replace(".", "/") + ".SCL." + sclSuffix, true); - if (res == null) throw cnfe; + if (!exclusionListMatch(fileNameOfClass)) { + try { + // First search in the prepended classloaders, the class might be their already + for (ClassLoader pre : prependedParentLoaders) { + try { + Class<?> loadClass = pre.loadClass(name); + if (loadClass != null) return loadClass; + } catch (Throwable e) { + continue; + } + } + + return super.loadClass(name, resolve); + } catch (ClassNotFoundException cnfe) { + res = getResource_("secondaryLoading.SCL." + sclSuffix + "/" + name.replace(".", "/") + ".SCL." + sclSuffix, true); + if (res == null) throw cnfe; + } } } if (res == null) throw new ClassNotFoundException(name); + return urlToDefineClass(name, res, resolve); + } + + private Class<?> urlToDefineClass(String name, URL res, boolean resolve) throws ClassNotFoundException { byte[] b; int p = 0; try { |