forked from apache/groovy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArithmeticShell.groovy
157 lines (148 loc) · 6.64 KB
/
ArithmeticShell.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import org.codehaus.groovy.control.CompilerConfiguration
import org.codehaus.groovy.control.MultipleCompilationErrorsException
import org.codehaus.groovy.control.customizers.ImportCustomizer
import org.codehaus.groovy.control.customizers.SecureASTCustomizer
import org.codehaus.groovy.control.messages.ExceptionMessage
import static org.codehaus.groovy.syntax.Types.*
import org.codehaus.groovy.ast.stmt.BlockStatement
import org.codehaus.groovy.ast.stmt.ExpressionStatement
import org.codehaus.groovy.ast.expr.BinaryExpression
import org.codehaus.groovy.ast.expr.ConstantExpression
import org.codehaus.groovy.ast.expr.MethodCallExpression
import org.codehaus.groovy.ast.expr.StaticMethodCallExpression
import org.codehaus.groovy.ast.expr.ArgumentListExpression
import org.codehaus.groovy.ast.expr.PropertyExpression
import org.codehaus.groovy.ast.expr.UnaryMinusExpression
import org.codehaus.groovy.ast.expr.UnaryPlusExpression
import org.codehaus.groovy.ast.expr.PrefixExpression
import org.codehaus.groovy.ast.expr.PostfixExpression
import org.codehaus.groovy.ast.expr.TernaryExpression
import org.codehaus.groovy.ast.expr.ElvisOperatorExpression
import org.codehaus.groovy.ast.expr.BooleanExpression
import org.codehaus.groovy.ast.expr.ClassExpression
/**
* The arithmetic shell is similar to a GroovyShell in that it can evaluate text as
* code and return a result. It is not a subclass of GroovyShell because it does not widen
* the contract of GroovyShell, instead it narrows it. Using one of these shells like a
* GroovyShell would result in many runtime errors.
*
* @author Hamlet D'Arcy ([email protected])
* @author Guillaume Laforge
*/
class ArithmeticShell {
/**
* Compiles the text into a Groovy object and then executes it, returning the result.
* @param text
* the script to evaluate typed as a string
* @throws SecurityException
* most likely the script is doing something other than arithmetic
* @throws IllegalStateException
* if the script returns something other than a number
*/
Number evaluate(String text) {
try {
final ImportCustomizer imports = new ImportCustomizer().addStaticStars('java.lang.Math') // add static import of java.lang.Math
final SecureASTCustomizer secure = new SecureASTCustomizer()
secure.with {
closuresAllowed = false
methodDefinitionAllowed = false
importsWhitelist = []
staticImportsWhitelist = []
staticStarImportsWhitelist = ['java.lang.Math'] // only java.lang.Math is allowed
tokensWhitelist = [
PLUS,
MINUS,
MULTIPLY,
DIVIDE,
MOD,
POWER,
PLUS_PLUS,
MINUS_MINUS,
COMPARE_EQUAL,
COMPARE_NOT_EQUAL,
COMPARE_LESS_THAN,
COMPARE_LESS_THAN_EQUAL,
COMPARE_GREATER_THAN,
COMPARE_GREATER_THAN_EQUAL,
].asImmutable()
constantTypesClassesWhiteList = [
Integer,
Float,
Long,
Double,
BigDecimal,
Integer.TYPE,
Long.TYPE,
Float.TYPE,
Double.TYPE
].asImmutable()
receiversClassesWhiteList = [
Math,
Integer,
Float,
Double,
Long,
BigDecimal
].asImmutable()
statementsWhitelist = [
BlockStatement,
ExpressionStatement
].asImmutable()
expressionsWhitelist = [
BinaryExpression,
ConstantExpression,
MethodCallExpression,
StaticMethodCallExpression,
ArgumentListExpression,
PropertyExpression,
UnaryMinusExpression,
UnaryPlusExpression,
PrefixExpression,
PostfixExpression,
TernaryExpression,
ElvisOperatorExpression,
BooleanExpression,
// ClassExpression needed for processing of MethodCallExpression, PropertyExpression
// and StaticMethodCallExpression
ClassExpression
].asImmutable()
}
CompilerConfiguration config = new CompilerConfiguration()
config.addCompilationCustomizers(imports, secure)
GroovyClassLoader loader = new GroovyClassLoader(this.class.classLoader, config)
Class clazz = loader.parseClass(text)
Script script = (Script) clazz.newInstance();
Object result = script.run()
if (!(result instanceof Number)) throw new IllegalStateException("Script returned a non-number: $result");
return (Number) result
} catch (SecurityException ex) {
throw new SecurityException("Could not evaluate script: $text", ex)
} catch (MultipleCompilationErrorsException mce) {
//this allows compilation errors to be seen by the user
mce.errorCollector.errors.each {
if (it instanceof ExceptionMessage && it.cause instanceof SecurityException) {
throw it.cause
}
}
throw mce
}
}
}