• 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

66.67
today-context/src/main/java/infra/scripting/groovy/GroovyScriptEvaluator.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.groovy;
19

20
import org.codehaus.groovy.control.CompilerConfiguration;
21
import org.codehaus.groovy.control.customizers.CompilationCustomizer;
22

23
import java.io.IOException;
24
import java.util.Map;
25

26
import groovy.lang.Binding;
27
import groovy.lang.GroovyRuntimeException;
28
import groovy.lang.GroovyShell;
29
import infra.beans.factory.BeanClassLoaderAware;
30
import infra.lang.Nullable;
31
import infra.scripting.ScriptCompilationException;
32
import infra.scripting.ScriptEvaluator;
33
import infra.scripting.ScriptSource;
34
import infra.scripting.support.ResourceScriptSource;
35

36
/**
37
 * Groovy-based implementation of Framework's {@link ScriptEvaluator} strategy interface.
38
 *
39
 * @author Juergen Hoeller
40
 * @author <a href="https://github.com/TAKETODAY">海子 Yang</a>
41
 * @see GroovyShell#evaluate(String, String)
42
 * @since 4.0
43
 */
44
public class GroovyScriptEvaluator implements ScriptEvaluator, BeanClassLoaderAware {
45

46
  @Nullable
47
  private ClassLoader classLoader;
48

49
  private CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
5✔
50

51
  /**
52
   * Construct a new GroovyScriptEvaluator.
53
   */
54
  public GroovyScriptEvaluator() { }
3✔
55

56
  /**
57
   * Construct a new GroovyScriptEvaluator.
58
   *
59
   * @param classLoader the ClassLoader to use as a parent for the {@link GroovyShell}
60
   */
61
  public GroovyScriptEvaluator(@Nullable ClassLoader classLoader) {
×
62
    this.classLoader = classLoader;
×
63
  }
×
64

65
  /**
66
   * Set a custom compiler configuration for this evaluator.
67
   *
68
   * @see #setCompilationCustomizers
69
   */
70
  public void setCompilerConfiguration(@Nullable CompilerConfiguration compilerConfiguration) {
71
    this.compilerConfiguration =
1✔
72
            compilerConfiguration != null ? compilerConfiguration : new CompilerConfiguration();
6!
73
  }
1✔
74

75
  /**
76
   * Return this evaluator's compiler configuration (never {@code null}).
77
   *
78
   * @see #setCompilerConfiguration
79
   */
80
  public CompilerConfiguration getCompilerConfiguration() {
81
    return this.compilerConfiguration;
3✔
82
  }
83

84
  /**
85
   * Set one or more customizers to be applied to this evaluator's compiler configuration.
86
   * <p>Note that this modifies the shared compiler configuration held by this evaluator.
87
   *
88
   * @see #setCompilerConfiguration
89
   */
90
  public void setCompilationCustomizers(CompilationCustomizer... compilationCustomizers) {
91
    this.compilerConfiguration.addCompilationCustomizers(compilationCustomizers);
5✔
92
  }
1✔
93

94
  @Override
95
  public void setBeanClassLoader(ClassLoader classLoader) {
96
    this.classLoader = classLoader;
×
97
  }
×
98

99
  @Override
100
  @Nullable
101
  public Object evaluate(ScriptSource script) {
102
    return evaluate(script, null);
5✔
103
  }
104

105
  @Override
106
  @Nullable
107
  public Object evaluate(ScriptSource script, @Nullable Map<String, Object> arguments) {
108
    GroovyShell groovyShell = new GroovyShell(
12✔
109
            this.classLoader, new Binding(arguments), this.compilerConfiguration);
110
    try {
111
      String filename = script instanceof ResourceScriptSource ?
3✔
112
              ((ResourceScriptSource) script).getResource().getName() : null;
7✔
113
      if (filename != null) {
2✔
114
        return groovyShell.evaluate(script.getScriptAsString(), filename);
6✔
115
      }
116
      else {
117
        return groovyShell.evaluate(script.getScriptAsString());
5✔
118
      }
119
    }
120
    catch (IOException ex) {
×
121
      throw new ScriptCompilationException(script, "Cannot access Groovy script", ex);
×
122
    }
123
    catch (GroovyRuntimeException ex) {
×
124
      throw new ScriptCompilationException(script, ex);
×
125
    }
126
  }
127

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