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

TAKETODAY / today-infrastructure / 12850416574

19 Jan 2025 04:37AM UTC coverage: 79.916% (-0.003%) from 79.919%
12850416574

push

github

TAKETODAY
:art:

59446 of 79527 branches covered (74.75%)

Branch coverage included in aggregate %.

142674 of 173390 relevant lines covered (82.29%)

3.5 hits per line

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

60.87
today-context/src/main/java/infra/scripting/bsh/BshScriptEvaluator.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.scripting.bsh;
19

20
import java.io.IOException;
21
import java.io.StringReader;
22
import java.util.Map;
23

24
import bsh.EvalError;
25
import bsh.Interpreter;
26
import infra.beans.factory.BeanClassLoaderAware;
27
import infra.lang.Nullable;
28
import infra.scripting.ScriptCompilationException;
29
import infra.scripting.ScriptEvaluator;
30
import infra.scripting.ScriptSource;
31

32
/**
33
 * BeanShell-based implementation of Framework's {@link ScriptEvaluator} strategy interface.
34
 *
35
 * @author Juergen Hoeller
36
 * @author <a href="https://github.com/TAKETODAY">海子 Yang</a>
37
 * @see Interpreter#eval(String)
38
 * @since 4.0
39
 */
40
public class BshScriptEvaluator implements ScriptEvaluator, BeanClassLoaderAware {
41

42
  @Nullable
43
  private ClassLoader classLoader;
44

45
  /**
46
   * Construct a new BshScriptEvaluator.
47
   */
48
  public BshScriptEvaluator() {
2✔
49

50
  }
1✔
51

52
  /**
53
   * Construct a new BshScriptEvaluator.
54
   *
55
   * @param classLoader the ClassLoader to use for the {@link Interpreter}
56
   */
57
  public BshScriptEvaluator(ClassLoader classLoader) {
×
58
    this.classLoader = classLoader;
×
59
  }
×
60

61
  @Override
62
  public void setBeanClassLoader(ClassLoader classLoader) {
63
    this.classLoader = classLoader;
×
64
  }
×
65

66
  @Override
67
  @Nullable
68
  public Object evaluate(ScriptSource script) {
69
    return evaluate(script, null);
5✔
70
  }
71

72
  @Override
73
  @Nullable
74
  public Object evaluate(ScriptSource script, @Nullable Map<String, Object> arguments) {
75
    try {
76
      Interpreter interpreter = new Interpreter();
4✔
77
      interpreter.setClassLoader(this.classLoader);
4✔
78
      if (arguments != null) {
2✔
79
        for (Map.Entry<String, Object> entry : arguments.entrySet()) {
11✔
80
          interpreter.set(entry.getKey(), entry.getValue());
7✔
81
        }
1✔
82
      }
83
      return interpreter.eval(new StringReader(script.getScriptAsString()));
8✔
84
    }
85
    catch (IOException ex) {
×
86
      throw new ScriptCompilationException(script, "Cannot access BeanShell script", ex);
×
87
    }
88
    catch (EvalError ex) {
×
89
      throw new ScriptCompilationException(script, ex);
×
90
    }
91
  }
92

93
}
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