31
31
import java .util .Map ;
32
32
import java .util .Properties ;
33
33
import java .util .Set ;
34
+ import java .util .function .Supplier ;
34
35
import java .util .stream .Stream ;
35
36
36
37
import ch .qos .logback .classic .joran .JoranConfigurator ;
37
38
import ch .qos .logback .core .Context ;
38
39
import ch .qos .logback .core .CoreConstants ;
39
40
import ch .qos .logback .core .joran .spi .ElementSelector ;
40
41
import ch .qos .logback .core .joran .spi .RuleStore ;
42
+ import ch .qos .logback .core .joran .util .PropertySetter ;
41
43
import ch .qos .logback .core .joran .util .beans .BeanDescription ;
42
44
import ch .qos .logback .core .model .ComponentModel ;
43
45
import ch .qos .logback .core .model .Model ;
46
48
import ch .qos .logback .core .model .processor .ModelInterpretationContext ;
47
49
import ch .qos .logback .core .spi .ContextAware ;
48
50
import ch .qos .logback .core .spi .ContextAwareBase ;
51
+ import ch .qos .logback .core .util .AggregationType ;
49
52
50
53
import org .springframework .aot .generate .GenerationContext ;
51
54
import org .springframework .aot .hint .MemberCategory ;
62
65
import org .springframework .core .io .support .PropertiesLoaderUtils ;
63
66
import org .springframework .util .ClassUtils ;
64
67
import org .springframework .util .ReflectionUtils ;
68
+ import org .springframework .util .function .SingletonSupplier ;
65
69
66
70
/**
67
71
* Extended version of the Logback {@link JoranConfigurator} that adds additional Spring
@@ -201,32 +205,64 @@ private Set<Class<? extends Serializable>> serializationTypes(Model model) {
201
205
}
202
206
203
207
private Set <String > reflectionTypes (Model model ) {
208
+ return reflectionTypes (model , () -> null );
209
+ }
210
+
211
+ private Set <String > reflectionTypes (Model model , Supplier <Object > parent ) {
204
212
Set <String > reflectionTypes = new HashSet <>();
205
- if (model instanceof ComponentModel ) {
206
- String className = ((ComponentModel ) model ).getClassName ();
207
- processComponent (className , reflectionTypes );
208
- }
209
- String tag = model .getTag ();
210
- if (tag != null ) {
211
- String componentType = this .modelInterpretationContext .getDefaultNestedComponentRegistry ()
212
- .findDefaultComponentTypeByTag (tag );
213
+ Class <?> componentType = determineType (model , parent );
214
+ if (componentType != null ) {
213
215
processComponent (componentType , reflectionTypes );
214
216
}
217
+ Supplier <Object > componentSupplier = SingletonSupplier .ofNullable (() -> instantiate (componentType ));
215
218
for (Model submodel : model .getSubModels ()) {
216
- reflectionTypes .addAll (reflectionTypes (submodel ));
219
+ reflectionTypes .addAll (reflectionTypes (submodel , componentSupplier ));
217
220
}
218
221
return reflectionTypes ;
219
222
}
220
223
221
- private void processComponent (String componentTypeName , Set <String > reflectionTypes ) {
222
- if (componentTypeName != null ) {
223
- componentTypeName = this .modelInterpretationContext .getImport (componentTypeName );
224
- BeanDescription beanDescription = this .modelInterpretationContext .getBeanDescriptionCache ()
225
- .getBeanDescription (loadComponentType (componentTypeName ));
226
- reflectionTypes .addAll (parameterTypesNames (beanDescription .getPropertyNameToAdder ().values ()));
227
- reflectionTypes .addAll (parameterTypesNames (beanDescription .getPropertyNameToSetter ().values ()));
228
- reflectionTypes .add (componentTypeName );
224
+ private Class <?> determineType (Model model , Supplier <Object > parentSupplier ) {
225
+ String className = null ;
226
+ if (model instanceof ComponentModel ) {
227
+ className = ((ComponentModel ) model ).getClassName ();
228
+ }
229
+ if (className == null ) {
230
+ String tag = model .getTag ();
231
+ if (tag != null ) {
232
+ className = this .modelInterpretationContext .getDefaultNestedComponentRegistry ()
233
+ .findDefaultComponentTypeByTag (tag );
234
+ if (className == null ) {
235
+ Class <?> type = inferTypeFromParent (parentSupplier , tag );
236
+ if (type != null ) {
237
+ return type ;
238
+ }
239
+ }
240
+ }
241
+ }
242
+ if (className != null ) {
243
+ className = this .modelInterpretationContext .getImport (className );
244
+ return loadComponentType (className );
245
+ }
246
+ return null ;
247
+ }
248
+
249
+ private Class <?> inferTypeFromParent (Supplier <Object > parentSupplier , String tag ) {
250
+ Object parent = parentSupplier .get ();
251
+ if (parent != null ) {
252
+ try {
253
+ Class <?> typeFromSetter = new PropertySetter (
254
+ this .modelInterpretationContext .getBeanDescriptionCache (), parent )
255
+ .getClassNameViaImplicitRules (tag , AggregationType .AS_COMPLEX_PROPERTY ,
256
+ this .modelInterpretationContext .getDefaultNestedComponentRegistry ());
257
+ if (typeFromSetter != null ) {
258
+ return typeFromSetter ;
259
+ }
260
+ }
261
+ catch (Exception ex ) {
262
+ // Continue
263
+ }
229
264
}
265
+ return null ;
230
266
}
231
267
232
268
private Class <?> loadComponentType (String componentType ) {
@@ -238,6 +274,23 @@ private Class<?> loadComponentType(String componentType) {
238
274
}
239
275
}
240
276
277
+ private Object instantiate (Class <?> type ) {
278
+ try {
279
+ return type .getConstructor ().newInstance ();
280
+ }
281
+ catch (Exception ex ) {
282
+ return null ;
283
+ }
284
+ }
285
+
286
+ private void processComponent (Class <?> componentType , Set <String > reflectionTypes ) {
287
+ BeanDescription beanDescription = this .modelInterpretationContext .getBeanDescriptionCache ()
288
+ .getBeanDescription (componentType );
289
+ reflectionTypes .addAll (parameterTypesNames (beanDescription .getPropertyNameToAdder ().values ()));
290
+ reflectionTypes .addAll (parameterTypesNames (beanDescription .getPropertyNameToSetter ().values ()));
291
+ reflectionTypes .add (componentType .getCanonicalName ());
292
+ }
293
+
241
294
private Collection <String > parameterTypesNames (Collection <Method > methods ) {
242
295
return methods .stream ()
243
296
.filter ((method ) -> !method .getDeclaringClass ().equals (ContextAware .class )
0 commit comments