1717
1818package com .megaease .easeagent .core .plugin ;
1919
20+ import com .megaease .easeagent .config .ConfigUtils ;
2021import com .megaease .easeagent .config .Configs ;
2122import com .megaease .easeagent .core .plugin .matcher .ClassTransformation ;
2223import com .megaease .easeagent .core .plugin .matcher .MethodTransformation ;
2324import com .megaease .easeagent .core .plugin .registry .PluginRegistry ;
2425import com .megaease .easeagent .core .plugin .transformer .CompoundPluginTransformer ;
2526import com .megaease .easeagent .core .plugin .transformer .DynamicFieldTransformer ;
2627import com .megaease .easeagent .core .plugin .transformer .ForAdviceTransformer ;
28+ import com .megaease .easeagent .core .plugin .transformer .TypeFieldTransformer ;
2729import com .megaease .easeagent .log4j2 .Logger ;
2830import com .megaease .easeagent .log4j2 .LoggerFactory ;
2931import com .megaease .easeagent .plugin .AgentPlugin ;
30- import com .megaease .easeagent .plugin .interceptor . InterceptorProvider ;
32+ import com .megaease .easeagent .plugin .CodeVersion ;
3133import com .megaease .easeagent .plugin .Ordered ;
3234import com .megaease .easeagent .plugin .Points ;
3335import com .megaease .easeagent .plugin .field .AgentDynamicFieldAccessor ;
36+ import com .megaease .easeagent .plugin .interceptor .InterceptorProvider ;
37+ import com .megaease .easeagent .plugin .utils .common .StringUtils ;
3438import net .bytebuddy .agent .builder .AgentBuilder ;
3539
3640import java .util .*;
@@ -46,19 +50,27 @@ private PluginLoader() {
4650
4751 public static AgentBuilder load (AgentBuilder ab , Configs conf ) {
4852 pluginLoad ();
53+ pointsLoad (conf );
4954 providerLoad ();
50- Set <ClassTransformation > sortedTransformations = pointsLoad ();
55+ Set <ClassTransformation > sortedTransformations = classTransformationLoad ();
5156
5257 for (ClassTransformation transformation : sortedTransformations ) {
5358 ab = ab .type (transformation .getClassMatcher (), transformation .getClassloaderMatcher ())
54- .transform (compound (transformation .isHasDynamicField (), transformation .getMethodTransformations ()));
59+ .transform (compound (transformation .isHasDynamicField (), transformation .getMethodTransformations (), transformation . getTypeFieldAccessor () ));
5560 }
5661 return ab ;
5762 }
5863
5964 public static void providerLoad () {
6065 for (InterceptorProvider provider : BaseLoader .load (InterceptorProvider .class )) {
61- log .debug ("loading provider:{}" , provider .getClass ().getName ());
66+ String pointsClassName = PluginRegistry .getPointsClassName (provider .getAdviceTo ());
67+ Points points = PluginRegistry .getPoints (pointsClassName );
68+ if (points == null ) {
69+ log .debug ("Unload provider:{}, can not found Points<{}>" , provider .getClass ().getName (), pointsClassName );
70+ continue ;
71+ } else {
72+ log .debug ("Loading provider:{}" , provider .getClass ().getName ());
73+ }
6274
6375 try {
6476 log .debug ("provider for:{} at {}" ,
@@ -73,14 +85,14 @@ public static void providerLoad() {
7385 }
7486 }
7587
76- public static Set <ClassTransformation > pointsLoad () {
77- List <Points > points = BaseLoader . load ( Points . class );
88+ public static Set <ClassTransformation > classTransformationLoad () {
89+ Collection <Points > points = PluginRegistry . getPoints ( );
7890 return points .stream ().map (point -> {
7991 try {
80- return PluginRegistry .register (point );
92+ return PluginRegistry .registerClassTransformation (point );
8193 } catch (Exception e ) {
8294 log .error (
83- "Unable to load points in [class {}]" ,
95+ "Unable to load classTransformation in [class {}]" ,
8496 point .getClass ().getName (),
8597 e );
8698 return null ;
@@ -111,12 +123,54 @@ public static void pluginLoad() {
111123 }
112124 }
113125
126+ public static void pointsLoad (Configs conf ) {
127+ for (Points points : BaseLoader .load (Points .class )) {
128+ if (!isCodeVersion (points , conf )) {
129+ continue ;
130+ } else {
131+ log .info ("Loading points [class Points<{}>]" , points .getClass ().getName ());
132+ }
133+
134+ try {
135+ PluginRegistry .register (points );
136+ } catch (Exception | LinkageError e ) {
137+ log .error (
138+ "Unable to load extension [class {}]" ,
139+ points .getClass ().getName (),
140+ e );
141+ }
142+ }
143+ }
144+
145+ public static boolean isCodeVersion (Points points , Configs conf ) {
146+ CodeVersion codeVersion = points .codeVersions ();
147+ if (codeVersion .isEmpty ()) {
148+ return true ;
149+ }
150+ String versionKey = ConfigUtils .buildCodeVersionKey (codeVersion .getKey ());
151+ Set <String > versions = new HashSet <>(conf .getStringList (versionKey ));
152+ if (versions .isEmpty ()) {
153+ versions = Points .DEFAULT_VERSIONS ;
154+ }
155+ Set <String > pointVersions = codeVersion .getVersions ();
156+ for (String version : versions ) {
157+ if (pointVersions .contains (version )) {
158+ return true ;
159+ }
160+ }
161+ log .info ("Unload points [class Points<{}>], the config [{}={}] is not in Points.codeVersions()=[{}:{}]" ,
162+ points .getClass ().getCanonicalName (), versionKey , String .join ("," , versions ),
163+ codeVersion .getKey (), String .join ("," , codeVersion .getVersions ()));
164+ return false ;
165+ }
166+
167+
114168 /**
115169 * @param methodTransformations method matchers under a special classMatcher
116170 * @return transform
117171 */
118172 public static AgentBuilder .Transformer compound (boolean hasDynamicField ,
119- Iterable <MethodTransformation > methodTransformations ) {
173+ Iterable <MethodTransformation > methodTransformations , String typeFieldAccessor ) {
120174 List <AgentBuilder .Transformer > agentTransformers = StreamSupport
121175 .stream (methodTransformations .spliterator (), false )
122176 .map (ForAdviceTransformer ::new )
@@ -126,6 +180,10 @@ public static AgentBuilder.Transformer compound(boolean hasDynamicField,
126180 agentTransformers .add (new DynamicFieldTransformer (AgentDynamicFieldAccessor .DYNAMIC_FIELD_NAME ));
127181 }
128182
183+ if (StringUtils .hasText (typeFieldAccessor )) {
184+ agentTransformers .add (new TypeFieldTransformer (typeFieldAccessor ));
185+ }
186+
129187 return new CompoundPluginTransformer (agentTransformers );
130188 }
131189}
0 commit comments