• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

TAKETODAY / today-infrastructure / 18154768944

01 Oct 2025 07:26AM UTC coverage: 81.882% (-0.005%) from 81.887%
18154768944

push

github

web-flow
Merge pull request #290 from TAKETODAY/dev/jspecify

jspecify

59788 of 78013 branches covered (76.64%)

Branch coverage included in aggregate %.

141239 of 167496 relevant lines covered (84.32%)

3.6 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

77.55
today-context/src/main/java/infra/context/support/ContextTypeMatchClassLoader.java
1
/*
2
 * Copyright 2017 - 2025 the original author or authors.
3
 *
4
 * This program is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see [https://www.gnu.org/licenses/]
16
 */
17

18
package infra.context.support;
19

20
import org.jspecify.annotations.Nullable;
21

22
import java.lang.reflect.Method;
23
import java.security.ProtectionDomain;
24
import java.util.concurrent.ConcurrentHashMap;
25

26
import infra.beans.factory.config.ConfigurableBeanFactory;
27
import infra.core.DecoratingClassLoader;
28
import infra.core.OverridingClassLoader;
29
import infra.core.SmartClassLoader;
30
import infra.logging.LoggerFactory;
31
import infra.util.ReflectionUtils;
32

33
/**
34
 * Special variant of an overriding ClassLoader, used for temporary type
35
 * matching in {@link AbstractApplicationContext}. Redefines classes from
36
 * a cached byte array for every {@code loadClass} call in order to
37
 * pick up recently loaded types in the parent ClassLoader.
38
 *
39
 * @author Juergen Hoeller
40
 * @author <a href="https://github.com/TAKETODAY">海子 Yang</a>
41
 * @see AbstractApplicationContext
42
 * @see ConfigurableBeanFactory#setTempClassLoader
43
 * @since 4.0 2021/10/30 17:50
44
 */
45
final class ContextTypeMatchClassLoader extends DecoratingClassLoader implements SmartClassLoader {
46

47
  static {
48
    ClassLoader.registerAsParallelCapable();
2✔
49
  }
50

51
  @Nullable
52
  private static final Method findLoadedClassMethod;
53

54
  static {
55
    // Try to enable findLoadedClass optimization which allows us to selectively
56
    // override classes that have not been loaded yet. If not accessible, we will
57
    // always override requested classes, even when the classes have been loaded
58
    // by the parent ClassLoader already and cannot be transformed anymore anyway.
59
    Method method;
60
    try {
61
      method = ClassLoader.class.getDeclaredMethod("findLoadedClass", String.class);
10✔
62
      ReflectionUtils.makeAccessible(method);
3✔
63
    }
64
    catch (Throwable ex) {
×
65
      method = null;
×
66
      // Typically a JDK 9+ InaccessibleObjectException...
67
      // Avoid through JVM startup with --add-opens=java.base/java.lang=ALL-UNNAMED
68
      LoggerFactory.getLogger(ContextTypeMatchClassLoader.class)
×
69
              .debug("ClassLoader.findLoadedClass not accessible -> will always override requested class", ex);
×
70
    }
1✔
71
    findLoadedClassMethod = method;
2✔
72
  }
1✔
73

74
  /** Cache for byte array per class name. */
75
  private final ConcurrentHashMap<String, byte[]> bytesCache = new ConcurrentHashMap<>(256);
6✔
76

77
  public ContextTypeMatchClassLoader(@Nullable ClassLoader parent) {
78
    super(parent);
3✔
79
  }
1✔
80

81
  @Override
82
  public Class<?> loadClass(String name) throws ClassNotFoundException {
83
    return new ContextOverridingClassLoader(getParent()).loadClass(name);
9✔
84
  }
85

86
  @Override
87
  public boolean isClassReloadable(Class<?> clazz) {
88
    return (clazz.getClassLoader() instanceof ContextOverridingClassLoader);
×
89
  }
90

91
  @Override
92
  public Class<?> publicDefineClass(String name, byte[] b, @Nullable ProtectionDomain protectionDomain) {
93
    return defineClass(name, b, 0, b.length, protectionDomain);
9✔
94
  }
95

96
  /**
97
   * ClassLoader to be created for each loaded class.
98
   * Caches class file content but redefines class for each call.
99
   */
100
  private class ContextOverridingClassLoader extends OverridingClassLoader {
101

102
    public ContextOverridingClassLoader(ClassLoader parent) {
3✔
103
      super(parent);
3✔
104
    }
1✔
105

106
    @Override
107
    protected boolean isEligibleForOverriding(String className) {
108
      if (isExcluded(className) || ContextTypeMatchClassLoader.this.isExcluded(className)) {
9!
109
        return false;
2✔
110
      }
111
      if (findLoadedClassMethod != null) {
2!
112
        ClassLoader parent = getParent();
3✔
113
        while (parent != null) {
2✔
114
          if (ReflectionUtils.invokeMethod(findLoadedClassMethod, parent, className) != null) {
10✔
115
            return false;
2✔
116
          }
117
          parent = parent.getParent();
4✔
118
        }
119
      }
120
      return true;
2✔
121
    }
122

123
    @Nullable
124
    @Override
125
    protected Class<?> loadClassForOverriding(String name) throws ClassNotFoundException {
126
      byte[] bytes = bytesCache.get(name);
7✔
127
      if (bytes == null) {
2!
128
        bytes = loadBytesForClass(name);
4✔
129
        if (bytes != null) {
2!
130
          bytesCache.put(name, bytes);
×
131
        }
132
        else {
133
          return null;
2✔
134
        }
135
      }
136
      return defineClass(name, bytes, 0, bytes.length);
×
137
    }
138
  }
139

140
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc