• 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

29.73
today-context/src/main/java/infra/instrument/classloading/TomcatLoadTimeWeaver.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.instrument.classloading;
19

20
import org.jspecify.annotations.Nullable;
21

22
import java.lang.instrument.ClassFileTransformer;
23
import java.lang.reflect.InvocationTargetException;
24
import java.lang.reflect.Method;
25

26
import infra.core.OverridingClassLoader;
27
import infra.lang.Assert;
28
import infra.util.ClassUtils;
29
import infra.util.ReflectionUtils;
30

31
/**
32
 * {@link LoadTimeWeaver} implementation
33
 * for Tomcat's new {@code org.apache.tomcat.InstrumentableClassLoader}.
34
 * Also capable of handling Framework's TomcatInstrumentableClassLoader when encountered.
35
 *
36
 * @author Juergen Hoeller
37
 * @author <a href="https://github.com/TAKETODAY">Harry Yang</a>
38
 * @since 4.0
39
 */
40
public class TomcatLoadTimeWeaver implements LoadTimeWeaver {
41

42
  static final String INSTRUMENTABLE_LOADER_CLASS_NAME = "org.apache.tomcat.InstrumentableClassLoader";
43

44
  private final ClassLoader classLoader;
45
  private final Method copyMethod;
46
  private final Method addTransformerMethod;
47

48
  /**
49
   * Create a new instance of the {@link TomcatLoadTimeWeaver} class using
50
   * the default {@link ClassLoader class loader}.
51
   *
52
   * @see ClassUtils#getDefaultClassLoader()
53
   */
54
  public TomcatLoadTimeWeaver() {
55
    this(ClassUtils.getDefaultClassLoader());
×
56
  }
×
57

58
  /**
59
   * Create a new instance of the {@link TomcatLoadTimeWeaver} class using
60
   * the supplied {@link ClassLoader}.
61
   *
62
   * @param classLoader the {@code ClassLoader} to delegate to for weaving
63
   */
64
  public TomcatLoadTimeWeaver(@Nullable ClassLoader classLoader) {
2✔
65
    Assert.notNull(classLoader, "ClassLoader is required");
3✔
66
    this.classLoader = classLoader;
3✔
67

68
    Class<?> instrumentableLoaderClass;
69
    try {
70
      instrumentableLoaderClass = classLoader.loadClass(INSTRUMENTABLE_LOADER_CLASS_NAME);
×
71
      if (!instrumentableLoaderClass.isInstance(classLoader)) {
×
72
        // Could still be a custom variant of a convention-compatible ClassLoader
73
        instrumentableLoaderClass = classLoader.getClass();
×
74
      }
75
    }
76
    catch (ClassNotFoundException ex) {
1✔
77
      // We're on an earlier version of Tomcat, probably with Framework's TomcatInstrumentableClassLoader
78
      instrumentableLoaderClass = classLoader.getClass();
3✔
79
    }
×
80

81
    try {
82
      this.addTransformerMethod = instrumentableLoaderClass.getMethod("addTransformer", ClassFileTransformer.class);
11✔
83
      // Check for Tomcat's new copyWithoutTransformers on InstrumentableClassLoader first
84
      Method copyMethod = ReflectionUtils.getMethodIfAvailable(instrumentableLoaderClass, "copyWithoutTransformers");
6✔
85
      if (copyMethod == null) {
2!
86
        // Fallback: expecting TomcatInstrumentableClassLoader's getThrowawayClassLoader
87
        copyMethod = instrumentableLoaderClass.getMethod("getThrowawayClassLoader");
×
88
      }
89
      this.copyMethod = copyMethod;
×
90
    }
91
    catch (Throwable ex) {
1✔
92
      throw new IllegalStateException(
6✔
93
              "Could not initialize TomcatLoadTimeWeaver because Tomcat API classes are not available", ex);
94
    }
×
95
  }
×
96

97
  @Override
98
  public void addTransformer(ClassFileTransformer transformer) {
99
    try {
100
      this.addTransformerMethod.invoke(this.classLoader, transformer);
×
101
    }
102
    catch (InvocationTargetException ex) {
×
103
      throw new IllegalStateException("Tomcat addTransformer method threw exception", ex.getCause());
×
104
    }
105
    catch (Throwable ex) {
×
106
      throw new IllegalStateException("Could not invoke Tomcat addTransformer method", ex);
×
107
    }
×
108
  }
×
109

110
  @Override
111
  public ClassLoader getInstrumentableClassLoader() {
112
    return this.classLoader;
×
113
  }
114

115
  @Override
116
  public ClassLoader getThrowawayClassLoader() {
117
    try {
118
      return new OverridingClassLoader(this.classLoader, (ClassLoader) this.copyMethod.invoke(this.classLoader));
×
119
    }
120
    catch (InvocationTargetException ex) {
×
121
      throw new IllegalStateException("Tomcat copy method threw exception", ex.getCause());
×
122
    }
123
    catch (Throwable ex) {
×
124
      throw new IllegalStateException("Could not invoke Tomcat copy method", ex);
×
125
    }
126
  }
127

128
}
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