1
1
/*
2
- * Copyright 2002-2012 the original author or authors.
2
+ * Copyright 2002-2021 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
16
16
17
17
package org .springframework .core ;
18
18
19
+ import java .security .ProtectionDomain ;
20
+
21
+ import org .springframework .lang .Nullable ;
22
+
19
23
/**
20
24
* Interface to be implemented by a reloading-aware ClassLoader
21
25
* (e.g. a Groovy-based ClassLoader). Detected for example by
@@ -34,10 +38,41 @@ public interface SmartClassLoader {
34
38
* Determine whether the given class is reloadable (in this ClassLoader).
35
39
* <p>Typically used to check whether the result may be cached (for this
36
40
* ClassLoader) or whether it should be reobtained every time.
41
+ * The default implementation always returns {@code false}.
37
42
* @param clazz the class to check (usually loaded from this ClassLoader)
38
43
* @return whether the class should be expected to appear in a reloaded
39
44
* version (with a different {@code Class} object) later on
40
45
*/
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
+ }
42
77
43
78
}
0 commit comments