11/*
2- * Copyright 2002-2012 the original author or authors.
2+ * Copyright 2002-2021 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
1616
1717package org .springframework .core ;
1818
19+ import java .security .ProtectionDomain ;
20+
21+ import org .springframework .lang .Nullable ;
22+
1923/**
2024 * Interface to be implemented by a reloading-aware ClassLoader
2125 * (e.g. a Groovy-based ClassLoader). Detected for example by
@@ -34,10 +38,41 @@ public interface SmartClassLoader {
3438 * Determine whether the given class is reloadable (in this ClassLoader).
3539 * <p>Typically used to check whether the result may be cached (for this
3640 * ClassLoader) or whether it should be reobtained every time.
41+ * The default implementation always returns {@code false}.
3742 * @param clazz the class to check (usually loaded from this ClassLoader)
3843 * @return whether the class should be expected to appear in a reloaded
3944 * version (with a different {@code Class} object) later on
4045 */
41- boolean isClassReloadable (Class <?> clazz );
46+ default boolean isClassReloadable (Class <?> clazz ) {
47+ return false ;
48+ }
49+
50+ /**
51+ * Define a custom class (typically a CGLIB proxy class) in this class loader.
52+ * <p>This is a public equivalent of the protected
53+ * {@code defineClass(String, byte[], int, int, ProtectionDomain)} method
54+ * in {@link ClassLoader} which is traditionally invoked via reflection.
55+ * A concrete implementation in a custom class loader should simply delegate
56+ * to that protected method in order to make classloader-specific definitions
57+ * publicly available without "illegal access" warnings on JDK 9+:
58+ * {@code return defineClass(name, b, 0, b.length, protectionDomain)}.
59+ * Note that the JDK 9+ {@code Lookup#defineClass} method does not support
60+ * a custom target class loader for the new definition; it rather always
61+ * defines the class in the same class loader as the lookup's context class.
62+ * @param name the name of the class
63+ * @param b the bytes defining the class
64+ * @param protectionDomain the protection domain for the class, if any
65+ * @return the newly created class
66+ * @throws LinkageError in case of a bad class definition
67+ * @throws SecurityException in case of an invalid definition attempt
68+ * @throws UnsupportedOperationException in case of a custom definition attempt
69+ * not being possible (thrown by the default implementation in this interface)
70+ * @since 5.3.4
71+ * @see ClassLoader#defineClass(String, byte[], int, int, ProtectionDomain)
72+ * @see java.lang.invoke.MethodHandles.Lookup#defineClass(byte[])
73+ */
74+ default Class <?> publicDefineClass (String name , byte [] b , @ Nullable ProtectionDomain protectionDomain ) {
75+ throw new UnsupportedOperationException ();
76+ }
4277
4378}
0 commit comments