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

TAKETODAY / today-infrastructure / 16489896461

24 Jul 2025 06:51AM UTC coverage: 81.782%. Remained the same
16489896461

push

github

TAKETODAY
:sparkles: LogMessage API

59446 of 77637 branches covered (76.57%)

Branch coverage included in aggregate %.

140767 of 167176 relevant lines covered (84.2%)

3.6 hits per line

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

90.91
today-context/src/main/java/infra/context/expression/MethodBasedEvaluationContext.java
1
/*
2
 * Copyright 2017 - 2025 the original author or authors.
3
 *
4
 * This program is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see [https://www.gnu.org/licenses/]
16
 */
17

18
package infra.context.expression;
19

20
import java.lang.reflect.Method;
21
import java.util.Arrays;
22

23
import infra.core.ParameterNameDiscoverer;
24
import infra.expression.EvaluationContext;
25
import infra.expression.spel.support.StandardEvaluationContext;
26
import infra.lang.Nullable;
27
import infra.util.ObjectUtils;
28

29
/**
30
 * A method-based {@link EvaluationContext} that
31
 * provides explicit support for method-based invocations.
32
 *
33
 * <p>Expose the actual method arguments using the following aliases:
34
 * <ol>
35
 * <li>pX where X is the index of the argument (p0 for the first argument)</li>
36
 * <li>aX where X is the index of the argument (a1 for the second argument)</li>
37
 * <li>the name of the parameter as discovered by a configurable {@link ParameterNameDiscoverer}</li>
38
 * </ol>
39
 *
40
 * @author Stephane Nicoll
41
 * @author Juergen Hoeller
42
 * @author <a href="https://github.com/TAKETODAY">Harry Yang</a>
43
 * @since 4.0 2021/12/25 17:39
44
 */
45
public class MethodBasedEvaluationContext extends StandardEvaluationContext {
46

47
  private final Method method;
48

49
  @Nullable
50
  private final Object[] arguments;
51

52
  private final ParameterNameDiscoverer parameterNameDiscoverer;
53

54
  private boolean argumentsLoaded = false;
6✔
55

56
  public MethodBasedEvaluationContext(@Nullable Object rootObject, Method method,
57
          @Nullable Object[] arguments, ParameterNameDiscoverer parameterNameDiscoverer) {
58
    super(rootObject);
3✔
59
    this.method = method;
3✔
60
    this.arguments = arguments;
3✔
61
    this.parameterNameDiscoverer = parameterNameDiscoverer;
3✔
62
  }
1✔
63

64
  public MethodBasedEvaluationContext(@Nullable Object rootObject, Method method, @Nullable Object[] arguments,
65
          ParameterNameDiscoverer parameterNameDiscoverer, StandardEvaluationContext shared) {
66
    super(rootObject, shared);
4✔
67
    this.method = method;
3✔
68
    this.arguments = arguments;
3✔
69
    this.parameterNameDiscoverer = parameterNameDiscoverer;
3✔
70
  }
1✔
71

72
  @Override
73
  @Nullable
74
  public Object lookupVariable(String name) {
75
    Object variable = super.lookupVariable(name);
4✔
76
    if (variable != null) {
2✔
77
      return variable;
2✔
78
    }
79
    if (!this.argumentsLoaded) {
3✔
80
      lazyLoadArguments();
2✔
81
      this.argumentsLoaded = true;
3✔
82
      variable = super.lookupVariable(name);
4✔
83
    }
84
    return variable;
2✔
85
  }
86

87
  /**
88
   * Load the param information only when needed.
89
   */
90
  protected void lazyLoadArguments() {
91
    // Shortcut if no args need to be loaded
92
    if (ObjectUtils.isEmpty(this.arguments)) {
4!
93
      return;
×
94
    }
95

96
    // Expose indexed variables as well as parameter names (if discoverable)
97
    String[] paramNames = this.parameterNameDiscoverer.getParameterNames(this.method);
6✔
98
    int paramCount = (paramNames != null ? paramNames.length : this.method.getParameterCount());
6!
99
    int argsCount = this.arguments.length;
4✔
100

101
    for (int i = 0; i < paramCount; i++) {
7✔
102
      Object value = null;
2✔
103
      if (argsCount > paramCount && i == paramCount - 1) {
8✔
104
        // Expose remaining arguments as vararg array for last parameter
105
        value = Arrays.copyOfRange(this.arguments, i, argsCount);
7✔
106
      }
107
      else if (argsCount > i) {
3✔
108
        // Actual argument found - otherwise left as null
109
        value = this.arguments[i];
5✔
110
      }
111
      setVariable("a" + i, value);
5✔
112
      setVariable("p" + i, value);
5✔
113
      if (paramNames != null && paramNames[i] != null) {
6!
114
        setVariable(paramNames[i], value);
6✔
115
      }
116
    }
117
  }
1✔
118

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

© 2026 Coveralls, Inc