• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

aspectran / aspectran / #4075

19 Feb 2025 12:58PM CUT coverage: 35.181% (-0.004%) from 35.185%
#4075

push

github

topframe
Update

20 of 155 new or added lines in 9 files covered. (12.9%)

5 existing lines in 3 files now uncovered.

14252 of 40510 relevant lines covered (35.18%)

0.35 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

68.93
/core/src/main/java/com/aspectran/core/context/rule/assistant/ActivityRuleAssistant.java
1
/*
2
 * Copyright (c) 2008-2025 The Aspectran Project
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package com.aspectran.core.context.rule.assistant;
17

18
import com.aspectran.core.adapter.ApplicationAdapter;
19
import com.aspectran.core.component.aspect.AspectRuleRegistry;
20
import com.aspectran.core.component.bean.BeanRuleRegistry;
21
import com.aspectran.core.component.schedule.ScheduleRuleRegistry;
22
import com.aspectran.core.component.template.TemplateRuleRegistry;
23
import com.aspectran.core.component.translet.TransletRuleRegistry;
24
import com.aspectran.core.context.asel.token.Token;
25
import com.aspectran.core.context.asel.value.ValueExpression;
26
import com.aspectran.core.context.env.EnvironmentProfiles;
27
import com.aspectran.core.context.rule.AspectRule;
28
import com.aspectran.core.context.rule.AutowireRule;
29
import com.aspectran.core.context.rule.AutowireTargetRule;
30
import com.aspectran.core.context.rule.BeanRule;
31
import com.aspectran.core.context.rule.DescriptionRule;
32
import com.aspectran.core.context.rule.EnvironmentRule;
33
import com.aspectran.core.context.rule.IllegalRuleException;
34
import com.aspectran.core.context.rule.InvokeActionRule;
35
import com.aspectran.core.context.rule.ItemRule;
36
import com.aspectran.core.context.rule.ItemRuleMap;
37
import com.aspectran.core.context.rule.ItemRuleUtils;
38
import com.aspectran.core.context.rule.ScheduleRule;
39
import com.aspectran.core.context.rule.TemplateRule;
40
import com.aspectran.core.context.rule.TransletRule;
41
import com.aspectran.core.context.rule.ability.BeanReferenceable;
42
import com.aspectran.core.context.rule.appender.RuleAppendHandler;
43
import com.aspectran.core.context.rule.type.AutowireTargetType;
44
import com.aspectran.core.context.rule.type.DefaultSettingType;
45
import com.aspectran.core.context.rule.type.ItemValueType;
46
import com.aspectran.core.context.rule.type.TokenDirectiveType;
47
import com.aspectran.core.context.rule.type.TokenType;
48
import com.aspectran.core.context.rule.util.Namespace;
49
import com.aspectran.core.context.rule.util.TextStyler;
50
import com.aspectran.utils.Assert;
51
import com.aspectran.utils.StringUtils;
52
import com.aspectran.utils.annotation.jsr305.NonNull;
53
import com.aspectran.utils.annotation.jsr305.Nullable;
54

55
import java.lang.reflect.Field;
56
import java.lang.reflect.Method;
57
import java.lang.reflect.Modifier;
58
import java.util.Collection;
59
import java.util.HashMap;
60
import java.util.HashSet;
61
import java.util.Iterator;
62
import java.util.LinkedList;
63
import java.util.List;
64
import java.util.Map;
65
import java.util.Set;
66

67
/**
68
 * The Class ActivityRuleAssistant.
69
 *
70
 * <p>Created: 2008. 04. 01 PM 10:25:35</p>
71
 */
72
public class ActivityRuleAssistant {
73

74
    private final boolean shallow;
75

76
    private final ClassLoader classLoader;
77

78
    private final ApplicationAdapter applicationAdapter;
79

80
    private final EnvironmentProfiles environmentProfiles;
81

82
    private Map<DefaultSettingType, String> settings;
83

84
    private List<EnvironmentRule> environmentRules;
85

86
    private Map<String, String> typeAliases;
87

88
    private AspectRuleRegistry aspectRuleRegistry;
89

90
    private BeanRuleRegistry beanRuleRegistry;
91

92
    private ScheduleRuleRegistry scheduleRuleRegistry;
93

94
    private TransletRuleRegistry transletRuleRegistry;
95

96
    private TemplateRuleRegistry templateRuleRegistry;
97

98
    private BeanReferenceInspector beanReferenceInspector;
99

100
    private AssistantLocal assistantLocal;
101

102
    private RuleAppendHandler ruleAppendHandler;
103

104
    protected ActivityRuleAssistant() {
1✔
105
        this.shallow = true;
1✔
106
        this.classLoader = null;
1✔
107
        this.applicationAdapter = null;
1✔
108
        this.environmentProfiles = null;
1✔
109
    }
1✔
110

111
    public ActivityRuleAssistant(ClassLoader classLoader,
112
                                 ApplicationAdapter applicationAdapter,
113
                                 EnvironmentProfiles environmentProfiles) {
1✔
114
        Assert.notNull(classLoader, "classLoader must not be null");
1✔
115
        Assert.notNull(applicationAdapter, "applicationAdapter must not be null");
1✔
116
        Assert.notNull(environmentProfiles, "environmentProfiles must not be null");
1✔
117
        this.shallow = false;
1✔
118
        this.classLoader = classLoader;
1✔
119
        this.applicationAdapter = applicationAdapter;
1✔
120
        this.environmentProfiles = environmentProfiles;
1✔
121
    }
1✔
122

123
    public void prepare() {
124
        settings = new HashMap<>();
1✔
125
        environmentRules = new LinkedList<>();
1✔
126
        typeAliases = new HashMap<>();
1✔
127
        assistantLocal = new AssistantLocal(this);
1✔
128

129
        if (!shallow) {
1✔
130
            aspectRuleRegistry = new AspectRuleRegistry();
1✔
131
            beanRuleRegistry = new BeanRuleRegistry(classLoader);
1✔
132
            transletRuleRegistry = new TransletRuleRegistry(getBasePath(), classLoader);
1✔
133
            scheduleRuleRegistry = new ScheduleRuleRegistry();
1✔
134
            templateRuleRegistry = new TemplateRuleRegistry();
1✔
135

136
            transletRuleRegistry.setAssistantLocal(assistantLocal);
1✔
137
            scheduleRuleRegistry.setAssistantLocal(assistantLocal);
1✔
138
            templateRuleRegistry.setAssistantLocal(assistantLocal);
1✔
139

140
            beanReferenceInspector = new BeanReferenceInspector();
1✔
141
        }
142
    }
1✔
143

144
    public void release() {
145
        settings = null;
1✔
146
        environmentRules = null;
1✔
147
        typeAliases = null;
1✔
148
        assistantLocal = null;
1✔
149

150
        if (!shallow) {
1✔
151
            scheduleRuleRegistry.setAssistantLocal(null);
1✔
152
            transletRuleRegistry.setAssistantLocal(null);
1✔
153
            templateRuleRegistry.setAssistantLocal(null);
1✔
154

155
            aspectRuleRegistry = null;
1✔
156
            beanRuleRegistry = null;
1✔
157
            scheduleRuleRegistry = null;
1✔
158
            transletRuleRegistry = null;
1✔
159
            templateRuleRegistry = null;
1✔
160

161
            beanReferenceInspector = null;
1✔
162
        }
163
    }
1✔
164

165
    public ClassLoader getClassLoader() {
166
        Assert.notNull(classLoader, "ClassLoader is not set");
1✔
167
        return classLoader;
1✔
168
    }
169

170
    public ApplicationAdapter getApplicationAdapter() {
171
        return applicationAdapter;
1✔
172
    }
173

174
    public String getBasePath() {
175
        if (applicationAdapter != null) {
1✔
176
            return applicationAdapter.getBasePathString();
1✔
177
        } else {
178
            return null;
1✔
179
        }
180
    }
181

182
    public EnvironmentProfiles getEnvironmentProfiles() {
183
        return environmentProfiles;
1✔
184
    }
185

186
    /**
187
     * Gets the settings.
188
     * @return the settings
189
     */
190
    public Map<DefaultSettingType, String> getSettings() {
191
        return settings;
1✔
192
    }
193

194
    /**
195
     * Gets the setting value.
196
     * @param settingType the setting type
197
     * @return the setting
198
     */
199
    public Object getSetting(DefaultSettingType settingType) {
200
        return settings.get(settingType);
×
201
    }
202

203
    /**
204
     * Puts the setting value.
205
     * @param name the name
206
     * @param value the value
207
     * @throws IllegalRuleException if an unknown setting name is found
208
     */
209
    public void putSetting(String name, String value) throws IllegalRuleException {
210
        if (StringUtils.isEmpty(name)) {
1✔
211
            throw new IllegalRuleException("Default setting name must not be null or empty");
×
212
        }
213
        DefaultSettingType settingType = DefaultSettingType.resolve(name);
1✔
214
        if (settingType == null) {
1✔
215
            throw new IllegalRuleException("No such default setting name as '" + name + "'");
×
216
        }
217
        settings.put(settingType, value);
1✔
218
    }
1✔
219

220
    /**
221
     * Apply settings.
222
     */
223
    public void applySettings() {
224
        DefaultSettings defaultSettings = assistantLocal.touchDefaultSettings();
1✔
225
        defaultSettings.apply(getSettings());
1✔
226
    }
1✔
227

228
    /**
229
     * Gets the environment rules.
230
     * @return the environment rules
231
     */
232
    public List<EnvironmentRule> getEnvironmentRules() {
233
        return environmentRules;
1✔
234
    }
235

236
    /**
237
     * Adds the environment rule.
238
     * @param environmentRule the environment rule
239
     */
240
    public void addEnvironmentRule(EnvironmentRule environmentRule) {
241
        environmentRules.add(environmentRule);
1✔
242
    }
1✔
243

244
    /**
245
     * Gets the type aliases.
246
     * @return the type aliases
247
     */
248
    public Map<String, String> getTypeAliases() {
249
        return typeAliases;
1✔
250
    }
251

252
    /**
253
     * Adds a type alias to use for simplifying complex type signatures.
254
     * A type alias is defined by assigning the type to the alias.
255
     * @param alias the name of the alias
256
     * @param type the type identifier that you are creating an alias for
257
     */
258
    public void addTypeAlias(String alias, String type) {
259
        typeAliases.put(alias, type);
×
260
    }
×
261

262
    /**
263
     * Returns a type of aliased type that is defined by assigning the type to the alias.
264
     * @param alias the name of the alias
265
     * @return the aliased type
266
     */
267
    public String getAliasedType(String alias) {
268
        return typeAliases.get(alias);
1✔
269
    }
270

271
    /**
272
     * Returns a type of aliased type that is defined by assigning the type to the alias.
273
     * If aliased type is not found, it returns alias.
274
     * @param alias the name of the alias
275
     * @return the aliased type
276
     */
277
    public String resolveAliasType(String alias) {
278
        String type = getAliasedType(alias);
1✔
279
        return (type == null ? alias: type);
1✔
280
    }
281

282
    /**
283
     * Returns the translet name of the prefix and suffix are combined.
284
     * @param transletName the translet name
285
     * @return the string
286
     */
287
    public String applyTransletNamePattern(String transletName) {
288
        if (transletName == null) {
1✔
289
            return null;
×
290
        }
291
        return Namespace.applyTransletNamePattern(
1✔
292
            assistantLocal.getDefaultSettings(), transletName, true);
1✔
293
    }
294

295
    /**
296
     * Gets the assistant local.
297
     * @return the assistant local
298
     */
299
    public AssistantLocal getAssistantLocal() {
300
        return assistantLocal;
1✔
301
    }
302

303
    /**
304
     * Sets the assistant local.
305
     * @param newAssistantLocal the new assistant local
306
     */
307
    private void setAssistantLocal(AssistantLocal newAssistantLocal) {
308
        this.assistantLocal = newAssistantLocal;
1✔
309
        scheduleRuleRegistry.setAssistantLocal(newAssistantLocal);
1✔
310
        transletRuleRegistry.setAssistantLocal(newAssistantLocal);
1✔
311
        templateRuleRegistry.setAssistantLocal(newAssistantLocal);
1✔
312
    }
1✔
313

314
    /**
315
     * Backup the assistant local.
316
     * @return the assistant local
317
     */
318
    public AssistantLocal backupAssistantLocal() {
319
        AssistantLocal oldAssistantLocal = assistantLocal;
1✔
320
        AssistantLocal newAssistantLocal = assistantLocal.replicate();
1✔
321
        setAssistantLocal(newAssistantLocal);
1✔
322
        return oldAssistantLocal;
1✔
323
    }
324

325
    /**
326
     * Restore the assistant local.
327
     * @param oldAssistantLocal the old assistant local
328
     */
329
    public void restoreAssistantLocal(AssistantLocal oldAssistantLocal) {
330
        setAssistantLocal(oldAssistantLocal);
1✔
331
    }
1✔
332

333
    /**
334
     * Returns whether the pointcut pattern validation is required.
335
     * @return true if pointcut pattern validation is required
336
     */
337
    public boolean isPointcutPatternVerifiable() {
338
        DefaultSettings defaultSettings = assistantLocal.getDefaultSettings();
1✔
339
        return (defaultSettings != null && defaultSettings.isPointcutPatternVerifiable());
1✔
340
    }
341

342
    public void resolveBeanClass(BeanRule beanRule) throws IllegalRuleException {
343
        if (beanRule != null && !beanRule.isFactoryOffered() && beanRule.getClassName() != null) {
1✔
344
            Class<?> beanClass = loadClass(beanRule.getClassName(), beanRule);
1✔
345
            beanRule.setBeanClass(beanClass);
1✔
346
        }
347
    }
1✔
348

349
    /**
350
     * Resolve bean class for factory bean rule.
351
     * @param beanRule the bean rule
352
     * @throws IllegalRuleException if an illegal rule is found
353
     */
354
    public void resolveFactoryBeanClass(BeanRule beanRule) throws IllegalRuleException {
355
        if (beanRule != null && beanRule.isFactoryOffered() && beanRule.getFactoryBeanId() != null) {
1✔
356
            Class<?> beanClass = resolveDirectiveBeanClass(beanRule.getFactoryBeanId(), beanRule);
1✔
357
            if (beanClass != null) {
1✔
358
                beanRule.setFactoryBeanClass(beanClass);
×
359
                reserveBeanReference(beanClass, beanRule);
×
360
            } else {
361
                reserveBeanReference(beanRule.getFactoryBeanId(), beanRule);
1✔
362
            }
363
        }
364
    }
1✔
365

366
    /**
367
     * Resolve bean class for the aspect rule.
368
     * @param aspectRule the aspect rule
369
     * @throws IllegalRuleException if an illegal rule is found
370
     */
371
    public void resolveAdviceBeanClass(@NonNull AspectRule aspectRule) throws IllegalRuleException {
372
        String beanIdOrClass = aspectRule.getAdviceBeanId();
1✔
373
        if (beanIdOrClass != null) {
1✔
374
            Class<?> beanClass = resolveDirectiveBeanClass(beanIdOrClass, aspectRule);
1✔
375
            if (beanClass != null) {
1✔
376
                aspectRule.setAdviceBeanClass(beanClass);
×
377
                reserveBeanReference(beanClass, aspectRule);
×
378
            } else {
379
                reserveBeanReference(beanIdOrClass, aspectRule);
1✔
380
            }
381
        }
382
    }
1✔
383

384
    /**
385
     * Resolve bean class for bean method action rule.
386
     * @param invokeActionRule the invoke action rule
387
     * @throws IllegalRuleException if an illegal rule is found
388
     */
389
    public void resolveActionBeanClass(@NonNull InvokeActionRule invokeActionRule) throws IllegalRuleException {
390
        String beanIdOrClass = invokeActionRule.getBeanId();
1✔
391
        if (beanIdOrClass != null) {
1✔
392
            Class<?> beanClass = resolveDirectiveBeanClass(beanIdOrClass, invokeActionRule);
1✔
393
            if (beanClass != null) {
1✔
394
                invokeActionRule.setBeanClass(beanClass);
×
395
                reserveBeanReference(beanClass, invokeActionRule);
×
396
            } else {
397
                reserveBeanReference(beanIdOrClass, invokeActionRule);
1✔
398
            }
399
        }
400
    }
1✔
401

402
    /**
403
     * Resolve bean class.
404
     * @param itemRule the item rule
405
     * @throws IllegalRuleException if an illegal rule is found
406
     */
407
    public void resolveBeanClass(@Nullable ItemRule itemRule) throws IllegalRuleException {
408
        if (itemRule != null) {
1✔
409
            if (itemRule.getValueType() == ItemValueType.BEAN) {
1✔
410
                if (itemRule.isListableType()) {
1✔
411
                    if (itemRule.getBeanRuleList() != null) {
×
412
                        for (BeanRule beanRule : itemRule.getBeanRuleList()) {
×
413
                            resolveBeanClass(beanRule);
×
414
                        }
×
415
                    }
416
                } else if (itemRule.isMappableType()) {
1✔
417
                    if (itemRule.getBeanRuleMap() != null) {
×
418
                        for (BeanRule beanRule : itemRule.getBeanRuleMap().values()) {
×
419
                            resolveBeanClass(beanRule);
×
420
                        }
×
421
                    }
422
                } else {
423
                    resolveBeanClass(itemRule.getBeanRule());
1✔
424
                }
425
            } else {
426
                Iterator<Token[]> it = ItemRuleUtils.tokenIterator(itemRule);
1✔
427
                if (it != null) {
1✔
428
                    while (it.hasNext()) {
1✔
429
                        Token[] tokens = it.next();
1✔
430
                        if (tokens != null) {
1✔
431
                            for (Token token : tokens) {
1✔
432
                                resolveBeanClass(token);
1✔
433
                            }
434
                        }
435
                    }
1✔
436
                }
437
            }
438
        }
439
    }
1✔
440

441
    /**
442
     * Resolve bean class for token.
443
     * @param tokens an array of tokens
444
     * @throws IllegalRuleException if an illegal rule is found
445
     */
446
    public void resolveBeanClass(@Nullable Token[] tokens) throws IllegalRuleException {
447
        if (tokens != null) {
1✔
448
            for (Token token : tokens) {
1✔
449
                resolveBeanClass(token);
1✔
450
            }
451
        }
452
    }
1✔
453

454
    /**
455
     * Resolve bean class for token.
456
     * @param token the token
457
     * @throws IllegalRuleException if an illegal rule is found
458
     */
459
    public void resolveBeanClass(Token token) throws IllegalRuleException {
460
        resolveBeanClass(token, token);
1✔
461
    }
1✔
462

463
    private void resolveBeanClass(@Nullable Token token, @Nullable BeanReferenceable referenceable)
464
            throws IllegalRuleException {
465
        if (token != null && token.getType() == TokenType.BEAN) {
1✔
466
            if (token.getDirectiveType() == TokenDirectiveType.FIELD) {
1✔
467
                if (token.getGetterName() == null) {
×
468
                    throw new IllegalRuleException("Token with no target field name specified: " + token);
×
469
                }
470
                Class<?> beanClass = loadClass(token.getValue(), token);
×
471
                try {
472
                    Field field = beanClass.getField(token.getGetterName());
×
NEW
473
                    token.setValueProvider(field);
×
474
                    if (!Modifier.isStatic(field.getModifiers())) {
×
475
                        reserveBeanReference(beanClass, referenceable);
×
476
                    }
477
                } catch (NoSuchFieldException e) {
×
478
                    throw new IllegalRuleException("Could not access field: " + token.getGetterName() +
×
479
                            " on " + ruleAppendHandler.getCurrentRuleAppender().getQualifiedName() + " " + token, e);
×
480
                }
×
481
            } else if (token.getDirectiveType() == TokenDirectiveType.METHOD) {
1✔
482
                if (token.getGetterName() == null) {
×
483
                    throw new IllegalRuleException("Token with no target method name specified: " + token);
×
484
                }
485
                Class<?> beanClass = loadClass(token.getValue(), token);
×
486
                try {
487
                    Method method = beanClass.getMethod(token.getGetterName());
×
NEW
488
                    token.setValueProvider(method);
×
489
                    if (!Modifier.isStatic(method.getModifiers())) {
×
490
                        reserveBeanReference(beanClass, referenceable);
×
491
                    }
492
                } catch (NoSuchMethodException e) {
×
493
                    throw new IllegalRuleException("Could not access method: " + token.getGetterName() +
×
494
                            " on " + ruleAppendHandler.getCurrentRuleAppender().getQualifiedName() + " " + token, e);
×
495
                }
×
496
            } else if (token.getDirectiveType() == TokenDirectiveType.CLASS) {
1✔
497
                Class<?> beanClass = loadClass(token.getValue(), token);
1✔
498
                token.setValueProvider(beanClass);
1✔
499
                reserveBeanReference(beanClass, referenceable);
1✔
500
            } else {
1✔
501
                reserveBeanReference(token.getName(), referenceable);
1✔
502
            }
503
        }
504
    }
1✔
505

506
    private void resolveBeanClass(Token[] tokens, BeanReferenceable referenceable) throws IllegalRuleException {
507
        if (tokens != null) {
1✔
508
            for (Token token : tokens) {
1✔
509
                resolveBeanClass(token, referenceable);
1✔
510
            }
511
        }
512
    }
1✔
513

514
    /**
515
     * Resolve bean class for the autowire rule.
516
     * @param autowireRule the autowire rule
517
     * @throws IllegalRuleException if an illegal rule is found
518
     */
519
    public void resolveBeanClass(@Nullable AutowireRule autowireRule) throws IllegalRuleException {
520
        if (autowireRule != null) {
1✔
521
            if (autowireRule.getTargetType() == AutowireTargetType.FIELD) {
1✔
522
                AutowireTargetRule autowireTargetRule = AutowireRule.getAutowireTargetRule(autowireRule);
1✔
523
                if (autowireRule.isRequired() && autowireTargetRule != null && !autowireTargetRule.isInnerBean()) {
1✔
524
                    resolveBeanClassOrReference(autowireRule, autowireTargetRule, true);
1✔
525
                }
526
            } else if (autowireRule.getTargetType() == AutowireTargetType.FIELD_VALUE) {
1✔
527
                AutowireTargetRule autowireTargetRule = AutowireRule.getAutowireTargetRule(autowireRule);
1✔
528
                if (autowireRule.isRequired() && autowireTargetRule != null && !autowireTargetRule.isInnerBean()) {
1✔
NEW
529
                    resolveBeanClassOrReference(autowireRule, autowireTargetRule, false);
×
530
                }
531
            } else if (autowireRule.getTargetType() == AutowireTargetType.METHOD ||
1✔
532
                autowireRule.getTargetType() == AutowireTargetType.CONSTRUCTOR) {
1✔
533
                AutowireTargetRule[] autowireTargetRules = autowireRule.getAutowireTargetRules();
1✔
534
                if (autowireRule.isRequired() && autowireTargetRules != null) {
1✔
535
                    for (AutowireTargetRule autowireTargetRule : autowireTargetRules) {
1✔
536
                        if (!autowireTargetRule.isInnerBean()) {
1✔
537
                            resolveBeanClassOrReference(autowireRule, autowireTargetRule, true);
1✔
538
                        }
539
                    }
540
                }
541
            }
542
        }
543
    }
1✔
544

545
    private void resolveBeanClassOrReference(
546
            AutowireRule autowireRule, @NonNull AutowireTargetRule autowireTargetRule, boolean forReference)
547
            throws IllegalRuleException {
548
        ValueExpression valueExpression = autowireTargetRule.getValueExpression();
1✔
549
        if (valueExpression != null) {
1✔
550
            Token[] tokens = valueExpression.getTokens();
1✔
551
            resolveBeanClass(tokens, autowireRule);
1✔
552
        } else if (forReference) {
1✔
553
            Class<?> type = autowireTargetRule.getType();
1✔
554
            String qualifier = autowireTargetRule.getQualifier();
1✔
555
            reserveBeanReference(qualifier, type, autowireRule);
1✔
556
        }
557
    }
1✔
558

559
    /**
560
     * Resolve bean class for the schedule rule.
561
     * @param scheduleRule the schedule rule
562
     * @throws IllegalRuleException if an illegal rule is found
563
     */
564
    public void resolveBeanClass(ScheduleRule scheduleRule) throws IllegalRuleException {
565
        if (scheduleRule != null) {
1✔
566
            String beanId = scheduleRule.getSchedulerBeanId();
1✔
567
            if (beanId != null) {
1✔
568
                Class<?> beanClass = resolveDirectiveBeanClass(beanId, scheduleRule);
1✔
569
                if (beanClass != null) {
1✔
570
                    scheduleRule.setSchedulerBeanClass(beanClass);
×
571
                    reserveBeanReference(beanClass, scheduleRule);
×
572
                } else {
573
                    reserveBeanReference(beanId, scheduleRule);
1✔
574
                }
575
            }
576
        }
577
    }
1✔
578

579
    /**
580
     * Resolve bean class for the template rule.
581
     * @param templateRule the template rule
582
     * @throws IllegalRuleException if an illegal rule is found
583
     */
584
    public void resolveBeanClass(TemplateRule templateRule) throws IllegalRuleException {
585
        if (templateRule != null) {
1✔
586
            String beanId = templateRule.getEngineBeanId();
1✔
587
            if (beanId != null) {
1✔
588
                Class<?> beanClass = resolveDirectiveBeanClass(beanId, templateRule);
×
589
                if (beanClass != null) {
×
590
                    templateRule.setEngineBeanClass(beanClass);
×
591
                    reserveBeanReference(beanClass, templateRule);
×
592
                } else {
593
                    reserveBeanReference(beanId, templateRule);
×
594
                }
595
            } else {
×
596
                resolveBeanClass(templateRule.getTemplateTokens());
1✔
597
            }
598
        }
599
    }
1✔
600

601
    private Class<?> resolveDirectiveBeanClass(String beanIdOrClass, Object referer) throws IllegalRuleException {
602
        if (beanIdOrClass != null && beanIdOrClass.startsWith(BeanRule.CLASS_DIRECTIVE_PREFIX)) {
1✔
603
            String className = beanIdOrClass.substring(BeanRule.CLASS_DIRECTIVE_PREFIX.length());
×
604
            return loadClass(className, referer);
×
605
        } else {
606
            return null;
1✔
607
        }
608
    }
609

610
    private Class<?> loadClass(String className, Object referer) throws IllegalRuleException {
611
        try {
612
            return getClassLoader().loadClass(className);
1✔
613
        } catch (ClassNotFoundException e) {
×
614
            throw new IllegalRuleException("Unable to load class: " + className +
×
615
                    " on " + ruleAppendHandler.getCurrentRuleAppender().getQualifiedName() + " " + referer, e);
×
616
        }
617
    }
618

619
    public void reserveBeanReference(String beanId, BeanReferenceable referenceable) {
620
        reserveBeanReference(beanId, null, referenceable);
1✔
621
    }
1✔
622

623
    public void reserveBeanReference(Class<?> beanClass, BeanReferenceable referenceable) {
624
        reserveBeanReference(null, beanClass, referenceable);
1✔
625
    }
1✔
626

627
    public void reserveBeanReference(String beanId, Class<?> beanClass, BeanReferenceable referenceable) {
628
        beanReferenceInspector.reserve(beanId, beanClass, referenceable, ruleAppendHandler.getCurrentRuleAppender());
1✔
629
    }
1✔
630

631
    /**
632
     * Returns the bean reference inspector.
633
     * @return the bean reference inspector
634
     */
635
    public BeanReferenceInspector getBeanReferenceInspector() {
636
        return beanReferenceInspector;
1✔
637
    }
638

639
    /**
640
     * Adds the aspect rule.
641
     * @param aspectRule the aspect rule to add
642
     * @throws IllegalRuleException if an illegal rule is found
643
     */
644
    public void addAspectRule(AspectRule aspectRule) throws IllegalRuleException {
645
        aspectRuleRegistry.addAspectRule(aspectRule);
1✔
646
    }
1✔
647

648
    /**
649
     * Adds the bean rule.
650
     * @param beanRule the bean rule to add
651
     * @throws IllegalRuleException if an error occurs while adding a bean rule
652
     */
653
    public void addBeanRule(BeanRule beanRule) throws IllegalRuleException {
654
        beanRuleRegistry.addBeanRule(beanRule);
1✔
655
    }
1✔
656

657
    public void addInnerBeanRule(BeanRule beanRule) throws IllegalRuleException {
658
        beanRuleRegistry.addInnerBeanRule(beanRule);
1✔
659
    }
1✔
660

661
    /**
662
     * Adds the schedule rule.
663
     * @param scheduleRule the schedule rule to add
664
     * @throws IllegalRuleException if an illegal rule is found
665
     */
666
    public void addScheduleRule(ScheduleRule scheduleRule) throws IllegalRuleException {
667
        scheduleRuleRegistry.addScheduleRule(scheduleRule);
1✔
668
    }
1✔
669

670
    /**
671
     * Add the translet rule.
672
     * @param transletRule the translet rule to add
673
     * @throws IllegalRuleException if an illegal rule is found
674
     */
675
    public void addTransletRule(TransletRule transletRule) throws IllegalRuleException {
676
        transletRuleRegistry.addTransletRule(transletRule);
1✔
677
    }
1✔
678

679
    /**
680
     * Add the template rule.
681
     * @param templateRule the template rule to add
682
     * @throws IllegalRuleException if an illegal rule is found
683
     */
684
    public void addTemplateRule(TemplateRule templateRule) throws IllegalRuleException {
685
        templateRuleRegistry.addTemplateRule(templateRule);
1✔
686
    }
1✔
687

688
    /**
689
     * Gets the aspect rule registry.
690
     * @return the aspect rule registry
691
     */
692
    public AspectRuleRegistry getAspectRuleRegistry() {
693
        return aspectRuleRegistry;
1✔
694
    }
695

696
    /**
697
     * Returns the bean rule registry.
698
     * @return the bean rule registry
699
     */
700
    public BeanRuleRegistry getBeanRuleRegistry() {
701
        return beanRuleRegistry;
1✔
702
    }
703

704
    /**
705
     * Returns the schedule rule registry.
706
     * @return the template rule registry
707
     */
708
    public ScheduleRuleRegistry getScheduleRuleRegistry() {
709
        return scheduleRuleRegistry;
1✔
710
    }
711

712
    /**
713
     * Returns the translet rule registry.
714
     * @return the translet rule registry
715
     */
716
    public TransletRuleRegistry getTransletRuleRegistry() {
717
        return transletRuleRegistry;
1✔
718
    }
719

720
    /**
721
     * Returns the template rule registry.
722
     * @return the template rule registry
723
     */
724
    public TemplateRuleRegistry getTemplateRuleRegistry() {
725
        return templateRuleRegistry;
1✔
726
    }
727

728
    /**
729
     * Returns all Aspect rules.
730
     * @return the aspect rules
731
     */
732
    public Collection<AspectRule> getAspectRules() {
733
        return aspectRuleRegistry.getAspectRules();
×
734
    }
735

736
    /**
737
     * Returns all bean rules.
738
     * @return the bean rules
739
     */
740
    public Collection<BeanRule> getBeanRules() {
741
        Collection<BeanRule> idBasedBeanRules = beanRuleRegistry.getIdBasedBeanRules();
×
742
        Collection<Set<BeanRule>> typeBasedBeanRules = beanRuleRegistry.getTypeBasedBeanRules();
×
743
        Collection<BeanRule> configurableBeanRules = beanRuleRegistry.getConfigurableBeanRules();
×
744

745
        int capacity = idBasedBeanRules.size();
×
746
        for (Set<BeanRule> brs : typeBasedBeanRules) {
×
747
            capacity += brs.size();
×
748
        }
×
749
        capacity += configurableBeanRules.size();
×
750
        capacity = (int)(capacity / 0.9f) + 1;
×
751

752
        Set<BeanRule> beanRuleSet = new HashSet<>(capacity, 0.9f);
×
753
        beanRuleSet.addAll(idBasedBeanRules);
×
754
        for (Set<BeanRule> brs : typeBasedBeanRules) {
×
755
            beanRuleSet.addAll(brs);
×
756
        }
×
757
        beanRuleSet.addAll(configurableBeanRules);
×
758
        return beanRuleSet;
×
759
    }
760

761
    /**
762
     * Returns all schedule rules.
763
     * @return the schedule rules
764
     */
765
    public Collection<ScheduleRule> getScheduleRules() {
766
        return scheduleRuleRegistry.getScheduleRules();
×
767
    }
768

769
    /**
770
     * Returns all translet rules.
771
     * @return the translet rules
772
     */
773
    public Collection<TransletRule> getTransletRules() {
774
        return transletRuleRegistry.getTransletRules();
×
775
    }
776

777
    /**
778
     * Returns all template rules.
779
     * @return the template rules
780
     */
781
    public Collection<TemplateRule> getTemplateRules() {
782
        return templateRuleRegistry.getTemplateRules();
×
783
    }
784

785
    /**
786
     * Returns the rule append handler.
787
     * @return the rule append handler
788
     */
789
    public RuleAppendHandler getRuleAppendHandler() {
790
        return ruleAppendHandler;
1✔
791
    }
792

793
    /**
794
     * Sets the rule append handler.
795
     * @param ruleAppendHandler the new rule append handler
796
     */
797
    public void setRuleAppendHandler(RuleAppendHandler ruleAppendHandler) {
798
        this.ruleAppendHandler = ruleAppendHandler;
1✔
799
    }
1✔
800

801
    /**
802
     * Removes the last rule appender after rule parsing is complete.
803
     */
804
    public void clearCurrentRuleAppender() {
805
        if (ruleAppendHandler != null) {
1✔
806
            ruleAppendHandler.setCurrentRuleAppender(null);
1✔
807
        }
808
    }
1✔
809

810
    public DescriptionRule profiling(@NonNull DescriptionRule newDr, @Nullable DescriptionRule oldDr) {
811
        if (newDr.getProfiles() != null && getEnvironmentProfiles() != null) {
1✔
812
            if (getEnvironmentProfiles().acceptsProfiles(newDr.getProfiles())) {
×
813
                return mergeDescriptionRule(newDr, oldDr);
×
814
            } else {
815
                if (oldDr == null) {
×
816
                    DescriptionRule dr = new DescriptionRule();
×
817
                    dr.addCandidate(newDr);
×
818
                    return dr;
×
819
                } else {
820
                    oldDr.addCandidate(newDr);
×
821
                    return oldDr;
×
822
                }
823
            }
824
        } else {
825
            return mergeDescriptionRule(newDr, oldDr);
1✔
826
        }
827
    }
828

829
    @NonNull
830
    private DescriptionRule mergeDescriptionRule(@NonNull DescriptionRule newDr, @Nullable DescriptionRule oldDr) {
831
        if (oldDr == null) {
1✔
832
            if (newDr.getContent() != null) {
1✔
833
                String formatted = TextStyler.styling(newDr.getContent(), newDr.getContentStyle());
1✔
834
                newDr.setFormattedContent(formatted);
1✔
835
            }
836
            return newDr;
1✔
837
        }
838
        DescriptionRule dr = new DescriptionRule();
1✔
839
        if (newDr.getContent() != null) {
1✔
840
            String formatted = TextStyler.styling(newDr.getContent(), newDr.getContentStyle());
1✔
841
            if (oldDr.getFormattedContent() != null) {
1✔
842
                formatted = oldDr.getFormattedContent() + formatted;
1✔
843
            }
844
            dr.setFormattedContent(formatted);
1✔
845
        } else if (oldDr.getFormattedContent() != null) {
1✔
846
            dr.setFormattedContent(oldDr.getFormattedContent());
×
847
        }
848
        oldDr.setFormattedContent(null);
1✔
849
        if (oldDr.getCandidates() == null) {
1✔
850
            dr.addCandidate(oldDr);
1✔
851
        } else {
852
            dr.setCandidates(oldDr.getCandidates());
×
853
            oldDr.setCandidates(null);
×
854
        }
855
        dr.addCandidate(newDr);
1✔
856
        return dr;
1✔
857
    }
858

859
    public ItemRuleMap profiling(@NonNull ItemRuleMap newIrm, @Nullable ItemRuleMap oldIrm) {
860
        if (newIrm.getProfiles() != null && getEnvironmentProfiles() != null) {
1✔
861
            if (getEnvironmentProfiles().acceptsProfiles(newIrm.getProfiles())) {
×
862
                return mergeItemRuleMap(newIrm, oldIrm);
×
863
            } else if (oldIrm == null) {
×
864
                ItemRuleMap irm = new ItemRuleMap();
×
865
                irm.addCandidate(newIrm);
×
866
                return irm;
×
867
            } else {
868
                oldIrm.addCandidate(newIrm);
×
869
                return oldIrm;
×
870
            }
871
        } else {
872
            return mergeItemRuleMap(newIrm, oldIrm);
1✔
873
        }
874
    }
875

876
    private ItemRuleMap mergeItemRuleMap(@NonNull ItemRuleMap newIrm, ItemRuleMap oldIrm) {
877
        if (oldIrm == null) {
1✔
878
            return newIrm;
1✔
879
        }
880
        ItemRuleMap irm = new ItemRuleMap();
×
881
        irm.putAll(oldIrm);
×
882
        irm.putAll(newIrm);
×
883
        if (oldIrm.getCandidates() == null) {
×
884
            irm.addCandidate(oldIrm);
×
885
        } else {
886
            irm.setCandidates(oldIrm.getCandidates());
×
887
            oldIrm.setCandidates(null);
×
888
            oldIrm.clear();
×
889
        }
890
        irm.addCandidate(newIrm);
×
891
        return irm;
×
892
    }
893

894
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc