Skip to content

Commit 5e09361

Browse files
committed
refactoring of Expression
1 parent d10963b commit 5e09361

File tree

5 files changed

+40
-48
lines changed

5 files changed

+40
-48
lines changed

jcp/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,14 +363,14 @@
363363
<directory>src/main/resources</directory>
364364
<filtering>false</filtering>
365365
<excludes>
366-
<exclude>/jcpversion.props</exclude>
366+
<exclude>/jcpversion.properties</exclude>
367367
</excludes>
368368
</resource>
369369
<resource>
370370
<directory>src/main/resources</directory>
371371
<filtering>true</filtering>
372372
<includes>
373-
<include>**/jcpversion.props</include>
373+
<include>**/jcpversion.properties</include>
374374
</includes>
375375
</resource>
376376
</resources>

jcp/src/main/java/com/igormaznitsa/jcp/InfoHelper.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
package com.igormaznitsa.jcp;
2323

2424
import static com.igormaznitsa.jcp.context.JCPSpecialVariableProcessor.getReference;
25+
import static java.util.Objects.requireNonNull;
2526
import static java.util.stream.Collectors.toList;
2627

2728
import com.igormaznitsa.jcp.cmdline.CommandLineHandler;
@@ -35,7 +36,6 @@
3536
import java.util.ArrayList;
3637
import java.util.List;
3738
import java.util.Locale;
38-
import java.util.Objects;
3939
import java.util.Properties;
4040

4141
public final class InfoHelper {
@@ -48,13 +48,13 @@ public final class InfoHelper {
4848
public static final int YEAR;
4949

5050
static {
51-
final String path = "/jcpversion.props";
51+
final String path = "/jcpversion.properties";
5252
try (final InputStream stream = InfoHelper.class.getResourceAsStream(path)) {
5353
final Properties props = new Properties();
5454
props.load(stream);
55-
VERSION = Objects.requireNonNull(props.getProperty("version"));
56-
URL = Objects.requireNonNull(props.getProperty("url"));
57-
YEAR = Integer.parseInt(Objects.requireNonNull(props.getProperty("year")).trim());
55+
VERSION = requireNonNull(props.getProperty("version"));
56+
URL = requireNonNull(props.getProperty("url"));
57+
YEAR = Integer.parseInt(requireNonNull(props.getProperty("year")).trim());
5858
} catch (IOException ex) {
5959
throw new IllegalStateException("Can't read resource: " + path, ex);
6060
}

jcp/src/main/java/com/igormaznitsa/jcp/expression/Expression.java

Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
package com.igormaznitsa.jcp.expression;
2323

24-
import com.igormaznitsa.jcp.context.PreprocessingState;
2524
import com.igormaznitsa.jcp.context.PreprocessorContext;
2625
import com.igormaznitsa.jcp.exceptions.FilePositionInfo;
2726
import com.igormaznitsa.jcp.exceptions.PreprocessorException;
@@ -51,21 +50,12 @@ public class Expression {
5150
*/
5251
private static final Class<?>[] OPERATOR_SIGNATURE_2 = new Class<?>[] {Value.class, Value.class};
5352

54-
/**
55-
* The variable contains the preprocessor context for the expression, it can be null
56-
*/
57-
private final PreprocessorContext context;
58-
5953
/**
6054
* The variable contains the expression tree
6155
*/
6256
private final ExpressionTree expressionTree;
6357

64-
private Expression(final PreprocessorContext context, final ExpressionTree tree) {
65-
if (tree == null) {
66-
throw context.makeException("[Expression]The expression tree is null", null);
67-
}
68-
this.context = context;
58+
private Expression(final ExpressionTree tree) {
6959
this.expressionTree = tree;
7060
}
7161

@@ -97,13 +87,13 @@ public static Value evalExpression(final String expression, final PreprocessorCo
9787
*/
9888

9989
public static Value evalTree(final ExpressionTree tree, final PreprocessorContext context) {
100-
final Expression exp = new Expression(context, tree);
101-
return exp.eval(context.getPreprocessingState());
90+
final Expression exp = new Expression(tree);
91+
return exp.eval(context);
10292
}
10393

10494

10595
private ExpressionTreeElement evalFunction(final ExpressionTreeElement functionElement,
106-
final PreprocessingState state) {
96+
final PreprocessorContext context) {
10797
final AbstractFunction function = (AbstractFunction) functionElement.getItem();
10898

10999
final int arity = function.getArity();
@@ -114,8 +104,8 @@ private ExpressionTreeElement evalFunction(final ExpressionTreeElement functionE
114104
final FilePositionInfo[] stack;
115105
final String sources;
116106

117-
stack = state.makeIncludeStack();
118-
sources = state.getLastReadString();
107+
stack = context.getPreprocessingState().makeIncludeStack();
108+
sources = context.getPreprocessingState().getLastReadString();
119109

120110
final StringBuilder signature = new StringBuilder(AbstractFunction.EXECUTION_PREFIX);
121111

@@ -125,14 +115,14 @@ private ExpressionTreeElement evalFunction(final ExpressionTreeElement functionE
125115

126116
for (int i = 0; i < arity; i++) {
127117
final ExpressionTreeElement item =
128-
calculateTreeElement(functionElement.getChildForIndex(i), state);
118+
this.calculateTreeElement(functionElement.getChildForIndex(i), context);
129119

130120
final ExpressionItem itemValue = item.getItem();
131121

132122
if (itemValue instanceof Value) {
133123
arguments[i] = (Value) itemValue;
134124
} else {
135-
throw this.context.makeException(
125+
throw context.makeException(
136126
"[Expression]Wrong argument type detected for the '" + function.getName() +
137127
"' function", null);
138128
}
@@ -162,7 +152,7 @@ private ExpressionTreeElement evalFunction(final ExpressionTreeElement functionE
162152
}
163153

164154
if (allowed == null) {
165-
throw this.context.makeException(
155+
throw context.makeException(
166156
"[Expression]Unsupported argument detected for '" + function.getName() + '\'', null);
167157
}
168158

@@ -171,7 +161,7 @@ private ExpressionTreeElement evalFunction(final ExpressionTreeElement functionE
171161
try {
172162
return new ExpressionTreeElement(userFunction.execute(context, arguments), stack, sources);
173163
} catch (Exception unexpected) {
174-
throw this.context
164+
throw context
175165
.makeException("[Expression]Unexpected exception during a user function processing",
176166
unexpected);
177167
}
@@ -186,21 +176,21 @@ private ExpressionTreeElement evalFunction(final ExpressionTreeElement functionE
186176
final Value result = (Value) method.invoke(function, callArgs);
187177

188178
if (!result.getType().isCompatible(function.getResultType())) {
189-
throw this.context.makeException("[Expression]Unsupported function result detected [" +
179+
throw context.makeException("[Expression]Unsupported function result detected [" +
190180
result.getType().getSignature() + ']', null);
191181
}
192182

193183
return new ExpressionTreeElement(result, stack, sources);
194184
} catch (NoSuchMethodException unexpected) {
195-
throw this.context.makeException(
185+
throw context.makeException(
196186
"[Expression]Can't find a function method to process data [" + signature +
197187
']', unexpected);
198188
} catch (Exception unexpected) {
199189
final Throwable cause = unexpected.getCause();
200190
if (cause instanceof PreprocessorException) {
201191
throw (PreprocessorException) cause;
202192
}
203-
throw this.context.makeException(
193+
throw context.makeException(
204194
"[Expression]Can't execute a function method to process data [" +
205195
function.getClass().getName() + '.' + signature + ']', unexpected);
206196
}
@@ -209,7 +199,7 @@ private ExpressionTreeElement evalFunction(final ExpressionTreeElement functionE
209199

210200

211201
private ExpressionTreeElement evalOperator(final ExpressionTreeElement operatorElement,
212-
final PreprocessingState state) {
202+
final PreprocessorContext context) {
213203
final AbstractOperator operator = (AbstractOperator) operatorElement.getItem();
214204

215205
final int arity = operator.getArity();
@@ -224,25 +214,25 @@ private ExpressionTreeElement evalOperator(final ExpressionTreeElement operatorE
224214
final FilePositionInfo[] stack;
225215
final String sources;
226216

227-
stack = state.makeIncludeStack();
228-
sources = state.getLastReadString();
217+
stack = context.getPreprocessingState().makeIncludeStack();
218+
sources = context.getPreprocessingState().getLastReadString();
229219

230220
for (int i = 0; i < arity; i++) {
231221
final ExpressionTreeElement arg = operatorElement.getChildForIndex(i);
232222
if (arg == ExpressionTreeElement.EMPTY_SLOT) {
233-
throw this.context.makeException(
223+
throw context.makeException(
234224
"[Expression]There is not needed argument for the operator [" + operator.getKeyword() +
235225
']', null);
236226
}
237227

238-
final ExpressionTreeElement currentElement = calculateTreeElement(arg, state);
228+
final ExpressionTreeElement currentElement = calculateTreeElement(arg, context);
239229

240230
final ExpressionItem item = currentElement.getItem();
241231

242232
if (item instanceof Value) {
243233
arguments[i] = (Value) item;
244234
} else {
245-
throw this.context.makeException(
235+
throw context.makeException(
246236
"[Expression]Non-value detected for the '" + operator.getKeyword() + "' operator",
247237
null);
248238
}
@@ -284,7 +274,7 @@ private ExpressionTreeElement evalOperator(final ExpressionTreeElement operatorE
284274
}
285275

286276
if (executeMethod == null) {
287-
throw this.context.makeException(
277+
throw context.makeException(
288278
"[Expression]Unsupported arguments detected for operator '" + operator.getKeyword() +
289279
"' " + Arrays.toString(arguments), null);
290280
}
@@ -302,15 +292,15 @@ private ExpressionTreeElement evalOperator(final ExpressionTreeElement operatorE
302292
throw new RuntimeException(
303293
"Invocation exception during '" + operator.getKeyword() + "' processing", thr);
304294
} catch (Exception unexpected) {
305-
throw this.context
295+
throw context
306296
.makeException("[Exception]Exception during '" + operator.getKeyword() + "' processing",
307297
unexpected);
308298
}
309299
}
310300

311301

312302
private ExpressionTreeElement calculateTreeElement(final ExpressionTreeElement element,
313-
final PreprocessingState state) {
303+
final PreprocessorContext context) {
314304
ExpressionTreeElement treeElement = element;
315305

316306
switch (element.getItem().getExpressionItemType()) {
@@ -326,38 +316,39 @@ private ExpressionTreeElement calculateTreeElement(final ExpressionTreeElement e
326316
throw new RuntimeException("Unknown variable [" + name + ']');
327317
} else {
328318
treeElement =
329-
new ExpressionTreeElement(value, state.makeIncludeStack(), state.getLastReadString());
319+
new ExpressionTreeElement(value, context.getPreprocessingState().makeIncludeStack(),
320+
context.getPreprocessingState().getLastReadString());
330321
}
331322
}
332323
break;
333324
case OPERATOR: {
334-
treeElement = evalOperator(element, state);
325+
treeElement = this.evalOperator(element, context);
335326
}
336327
break;
337328
case FUNCTION: {
338-
treeElement = evalFunction(element, state);
329+
treeElement = evalFunction(element, context);
339330
}
340331
break;
341332
}
342333
return treeElement;
343334
}
344335

345336

346-
private Value eval(final PreprocessingState state) {
337+
private Value eval(final PreprocessorContext context) {
347338
if (expressionTree.isEmpty()) {
348-
throw this.context.makeException("[Expression]The expression is empty", null);
339+
throw context.makeException("[Expression]The expression is empty", null);
349340
}
350-
final ExpressionTreeElement result = calculateTreeElement(expressionTree.getRoot(), state);
341+
final ExpressionTreeElement result = calculateTreeElement(expressionTree.getRoot(), context);
351342
final ExpressionItem resultItem = result.getItem();
352343

353344
if (resultItem == null) {
354-
throw this.context.makeException("[Expression]Expression doesn't have result", null);
345+
throw context.makeException("[Expression]Expression doesn't have result", null);
355346
}
356347

357348
if (resultItem instanceof Value) {
358349
return (Value) resultItem;
359350
} else {
360-
throw this.context
351+
throw context
361352
.makeException("[Expression]The expression returns non-value result [" + resultItem + ']',
362353
null);
363354
}

pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,11 @@
164164
<plugin>
165165
<groupId>org.codehaus.mojo</groupId>
166166
<artifactId>build-helper-maven-plugin</artifactId>
167-
<version>3.0.0</version>
167+
<version>3.4.0</version>
168168
</plugin>
169169
</plugins>
170170
</pluginManagement>
171+
171172
<plugins>
172173
<plugin>
173174
<groupId>org.codehaus.mojo</groupId>

0 commit comments

Comments
 (0)