|
| 1 | +package hudson.util; |
| 2 | + |
| 3 | +import java.util.Objects; |
| 4 | +import org.kohsuke.accmod.Restricted; |
| 5 | +import org.kohsuke.accmod.restrictions.NoExternalUse; |
| 6 | + |
| 7 | +/** |
| 8 | + * A {@link ClassLoader} that does not define any classes itself but delegates class loading to |
| 9 | + * other class loaders to avoid the JDK's per-class-name locking and lock retention. |
| 10 | + * |
| 11 | + * <p>This class first attempts to load classes via its {@link ClassLoader#getParent} class loader, |
| 12 | + * then falls back to {@link ClassLoader#findClass} to allow for custom delegation logic. |
| 13 | + * |
| 14 | + * <p>In a parallel-capable {@link ClassLoader}, the JDK maintains a per-name lock object |
| 15 | + * indefinitely. In Jenkins, many class loading misses across many loaders can accumulate hundreds |
| 16 | + * of thousands of such locks, retaining significant memory. This loader never defines classes and |
| 17 | + * bypasses {@link ClassLoader}'s default {@code loadClass} locking; it delegates to the parent |
| 18 | + * first and then to {@code findClass} for custom delegation. |
| 19 | + * |
| 20 | + * <p>The actual defining loader (parent or a delegate) still performs the necessary synchronization |
| 21 | + * and class definition. A runtime guard ({@link #verify}) throws if this loader ever becomes the |
| 22 | + * defining loader. |
| 23 | + * |
| 24 | + * <p>Subclasses must not call {@code defineClass}; implement delegation in {@code findClass} if |
| 25 | + * needed and do not mark subclasses as parallel-capable. |
| 26 | + * |
| 27 | + * @author Dmytro Ukhlov |
| 28 | + */ |
| 29 | +@Restricted(NoExternalUse.class) |
| 30 | +public abstract class DelegatingClassLoader extends ClassLoader { |
| 31 | + protected DelegatingClassLoader(String name, ClassLoader parent) { |
| 32 | + super(name, Objects.requireNonNull(parent)); |
| 33 | + } |
| 34 | + |
| 35 | + public DelegatingClassLoader(ClassLoader parent) { |
| 36 | + super(Objects.requireNonNull(parent)); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Parent-first delegation without synchronizing on {@link #getClassLoadingLock(String)}. This |
| 41 | + * prevents creation/retention of per-name lock objects in a loader that does not define |
| 42 | + * classes. The defining loader downstream still serializes class definition as required. |
| 43 | + * |
| 44 | + * @param name The binary name of the class |
| 45 | + * @param resolve If {@code true} then resolve the class |
| 46 | + * @return The resulting {@link Class} object |
| 47 | + * @throws ClassNotFoundException If the class could not be found |
| 48 | + */ |
| 49 | + @Override |
| 50 | + protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { |
| 51 | + Class<?> c = null; |
| 52 | + try { |
| 53 | + c = getParent().loadClass(name); |
| 54 | + } catch (ClassNotFoundException e) { |
| 55 | + // ClassNotFoundException thrown if class not found |
| 56 | + // from the non-null parent class loader |
| 57 | + } |
| 58 | + |
| 59 | + if (c == null) { |
| 60 | + // If still not found, then invoke findClass in order |
| 61 | + // to find the class. |
| 62 | + c = findClass(name); |
| 63 | + } |
| 64 | + |
| 65 | + verify(c); |
| 66 | + if (resolve) { |
| 67 | + resolveClass(c); |
| 68 | + } |
| 69 | + return c; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Safety check to ensure this delegating loader never becomes the defining loader. |
| 74 | + * |
| 75 | + * <p>Fails fast if a subclass erroneously defines a class here, which would violate the |
| 76 | + * delegation-only contract and could reintroduce locking/retention issues. |
| 77 | + */ |
| 78 | + private void verify(Class<?> clazz) { |
| 79 | + if (clazz.getClassLoader() == this) { |
| 80 | + throw new IllegalStateException("DelegatingClassLoader must not be the defining loader: " + clazz.getName()); |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments