2
2
3
3
import static net .bytebuddy .matcher .ElementMatchers .isMethod ;
4
4
5
- import java .lang .invoke .TypeDescriptor ;
6
5
import java .security .ProtectionDomain ;
7
6
import java .util .*;
8
7
import net .bytebuddy .agent .builder .AgentBuilder ;
15
14
import org .slf4j .Logger ;
16
15
import org .slf4j .LoggerFactory ;
17
16
import rocks .inspectit .gepard .agent .internal .instrumentation .InstrumentationState ;
17
+ import rocks .inspectit .gepard .agent .internal .instrumentation .InstrumentedType ;
18
18
import rocks .inspectit .gepard .agent .resolver .ConfigurationResolver ;
19
19
import rocks .inspectit .gepard .agent .transformation .advice .InspectitAdvice ;
20
20
@@ -31,16 +31,9 @@ public class DynamicTransformer implements AgentBuilder.Transformer {
31
31
/** The instrumentation state of the agent */
32
32
private final InstrumentationState instrumentationState ;
33
33
34
- /**
35
- * Set of instrumented types used by this transformer to prevent to call Class.forName() for every
36
- * call of transform()
37
- */
38
- private final Set <TypeDescription > instrumentedTypes ;
39
-
40
34
DynamicTransformer (ConfigurationResolver resolver , InstrumentationState instrumentationState ) {
41
35
this .resolver = resolver ;
42
36
this .instrumentationState = instrumentationState ;
43
- this .instrumentedTypes = new HashSet <>();
44
37
}
45
38
46
39
/**
@@ -60,68 +53,22 @@ public DynamicType.Builder<?> transform(
60
53
ClassLoader classLoader ,
61
54
JavaModule module ,
62
55
ProtectionDomain protectionDomain ) {
56
+ InstrumentedType currentType = new InstrumentedType (typeDescription .getName (), classLoader );
63
57
if (resolver .shouldInstrument (typeDescription )) {
64
- log .info ("Adding transformation to {}" , typeDescription .getName ()); // TODO log.debug()
58
+ log .debug ("Adding transformation to {}" , typeDescription .getName ());
65
59
66
60
// Currently, all methods of the type are instrumented
67
61
ElementMatcher <? super MethodDescription > elementMatcher = isMethod ();
68
62
builder = builder .visit (Advice .to (InspectitAdvice .class ).on (elementMatcher ));
69
63
70
64
// Mark type as instrumented
71
- // TODO das macht noch Probleme im ScopeTest...
72
- addInstrumentation (typeDescription , classLoader );
73
- } else if (instrumentedTypes .contains (typeDescription )) {
74
- log .info ("Removing transformation from {}" , typeDescription .getName ()); // TODO log.debug()
65
+ instrumentationState .addInstrumentedType (currentType );
66
+ } else if (instrumentationState .isInstrumented (currentType )) {
67
+ log .debug ("Removing transformation from {}" , typeDescription .getName ());
75
68
// Mark type as uninstrumented or deinstrumented
76
- invalidateInstrumentation ( typeDescription , classLoader );
69
+ instrumentationState . invalidateInstrumentedType ( currentType );
77
70
}
78
71
79
72
return builder ;
80
73
}
81
-
82
- /**
83
- * Marks the class as instrumented.
84
- *
85
- * @param typeDescription the class type
86
- * @param classLoader the classloader, used for accessing the class object
87
- */
88
- private void addInstrumentation (TypeDescription typeDescription , ClassLoader classLoader ) {
89
- Class <?> instrumentedClass = toClass (typeDescription , classLoader );
90
- if (Objects .nonNull (instrumentedClass )) {
91
- instrumentationState .addInstrumentation (instrumentedClass );
92
- instrumentedTypes .add (typeDescription );
93
- }
94
- }
95
-
96
- /**
97
- * Removes the class from marked instrumentations.
98
- *
99
- * @param typeDescription the class type description
100
- * @param classLoader the classloader, used for accessing the class object
101
- */
102
- private void invalidateInstrumentation (TypeDescription typeDescription , ClassLoader classLoader ) {
103
- Class <?> deinstrumentedClass = toClass (typeDescription , classLoader );
104
- if (Objects .nonNull (deinstrumentedClass )) {
105
- instrumentationState .invalidateInstrumentation (deinstrumentedClass );
106
- instrumentedTypes .remove (typeDescription );
107
- }
108
- }
109
-
110
- /**
111
- * Converts a bytebuddy {@link TypeDescription} to a {@link Class} object. The class is not
112
- * initialized.
113
- *
114
- * @param type the type description
115
- * @param classLoader the classloader, used for accessing the class object
116
- * @return the class object of the provided type description
117
- */
118
- private Class <?> toClass (TypeDescription type , ClassLoader classLoader ) {
119
- Class <?> clazz = null ;
120
- try {
121
- clazz = Class .forName (type .getName (), false , classLoader );
122
- } catch (Exception e ) {
123
- log .error ("Could not update instrumentation state for {}" , type .getName (), e );
124
- }
125
- return clazz ;
126
- }
127
74
}
0 commit comments