1
1
/*
2
- * Copyright 2002-2019 the original author or authors.
2
+ * Copyright 2002-2024 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.
17
17
package org .springframework .instrument .classloading .jboss ;
18
18
19
19
import java .lang .instrument .ClassFileTransformer ;
20
+ import java .lang .reflect .Constructor ;
20
21
import java .lang .reflect .Field ;
21
22
import java .lang .reflect .Method ;
22
23
26
27
import org .springframework .util .Assert ;
27
28
import org .springframework .util .ClassUtils ;
28
29
import org .springframework .util .ReflectionUtils ;
30
+ import org .springframework .util .function .ThrowingFunction ;
29
31
30
32
/**
31
33
* {@link LoadTimeWeaver} implementation for JBoss's instrumentable ClassLoader.
32
34
* Thanks to Ales Justin and Marius Bogoevici for the initial prototype.
33
35
*
34
- * <p>As of Spring Framework 5.0, this weaver supports WildFly 8+.
35
- * As of Spring Framework 5 .1.5, it also supports WildFly 13+ .
36
+ * <p>This weaver supports WildFly 13-23 (DelegatingClassFileTransformer) as well as
37
+ * WildFly 24+ (DelegatingClassTransformer), as of Spring Framework 6 .1.15 .
36
38
*
37
39
* @author Costin Leau
38
40
* @author Juergen Hoeller
39
41
* @since 3.0
40
42
*/
41
43
public class JBossLoadTimeWeaver implements LoadTimeWeaver {
42
44
43
- private static final String DELEGATING_TRANSFORMER_CLASS_NAME =
45
+ private static final String LEGACY_DELEGATING_TRANSFORMER_CLASS_NAME =
44
46
"org.jboss.as.server.deployment.module.DelegatingClassFileTransformer" ;
45
47
48
+ private static final String DELEGATING_TRANSFORMER_CLASS_NAME =
49
+ "org.jboss.as.server.deployment.module.DelegatingClassTransformer" ;
50
+
51
+ private static final String CLASS_TRANSFORMER_CLASS_NAME =
52
+ "org.jboss.modules.ClassTransformer" ;
53
+
46
54
private static final String WRAPPER_TRANSFORMER_CLASS_NAME =
47
55
"org.jboss.modules.JLIClassTransformer" ;
48
56
@@ -53,6 +61,8 @@ public class JBossLoadTimeWeaver implements LoadTimeWeaver {
53
61
54
62
private final Method addTransformer ;
55
63
64
+ private final ThrowingFunction <Object , Object > adaptTransformer ;
65
+
56
66
57
67
/**
58
68
* Create a new instance of the {@link JBossLoadTimeWeaver} class using
@@ -91,18 +101,29 @@ public JBossLoadTimeWeaver(@Nullable ClassLoader classLoader) {
91
101
wrappedTransformer .setAccessible (true );
92
102
suggestedTransformer = wrappedTransformer .get (suggestedTransformer );
93
103
}
94
- if (!suggestedTransformer .getClass ().getName ().equals (DELEGATING_TRANSFORMER_CLASS_NAME )) {
104
+
105
+ Class <?> transformerType = ClassFileTransformer .class ;
106
+ if (suggestedTransformer .getClass ().getName ().equals (LEGACY_DELEGATING_TRANSFORMER_CLASS_NAME )) {
107
+ this .adaptTransformer = (t -> t );
108
+ }
109
+ else if (suggestedTransformer .getClass ().getName ().equals (DELEGATING_TRANSFORMER_CLASS_NAME )) {
110
+ transformerType = classLoader .loadClass (CLASS_TRANSFORMER_CLASS_NAME );
111
+ Constructor <?> adaptedTransformer = classLoader .loadClass (WRAPPER_TRANSFORMER_CLASS_NAME )
112
+ .getConstructor (ClassFileTransformer .class );
113
+ this .adaptTransformer = adaptedTransformer ::newInstance ;
114
+ }
115
+ else {
95
116
throw new IllegalStateException (
96
- "Transformer not of the expected type DelegatingClassFileTransformer : " +
117
+ "Transformer not of expected type DelegatingClass(File)Transformer : " +
97
118
suggestedTransformer .getClass ().getName ());
98
119
}
99
120
this .delegatingTransformer = suggestedTransformer ;
100
121
101
122
Method addTransformer = ReflectionUtils .findMethod (this .delegatingTransformer .getClass (),
102
- "addTransformer" , ClassFileTransformer . class );
123
+ "addTransformer" , transformerType );
103
124
if (addTransformer == null ) {
104
125
throw new IllegalArgumentException (
105
- "Could not find 'addTransformer' method on JBoss DelegatingClassFileTransformer : " +
126
+ "Could not find 'addTransformer' method on JBoss DelegatingClass(File)Transformer : " +
106
127
this .delegatingTransformer .getClass ().getName ());
107
128
}
108
129
addTransformer .setAccessible (true );
@@ -117,7 +138,7 @@ public JBossLoadTimeWeaver(@Nullable ClassLoader classLoader) {
117
138
@ Override
118
139
public void addTransformer (ClassFileTransformer transformer ) {
119
140
try {
120
- this .addTransformer .invoke (this .delegatingTransformer , transformer );
141
+ this .addTransformer .invoke (this .delegatingTransformer , this . adaptTransformer . apply ( transformer ) );
121
142
}
122
143
catch (Throwable ex ) {
123
144
throw new IllegalStateException ("Could not add transformer on JBoss ClassLoader: " + this .classLoader , ex );
0 commit comments