Skip to content

Commit 6f9c8a0

Browse files
committed
argument arrays replaced by List
1 parent 687df83 commit 6f9c8a0

File tree

12 files changed

+55
-49
lines changed

12 files changed

+55
-49
lines changed

jcp-tests/jcp-test-maven-action/jcp-test-maven-action-action/src/main/java/com/igormaznitsa/jcp/it/CustomPreprocessorExtension.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ public boolean hasAction(int arity) {
1717
}
1818

1919
@Override
20-
public boolean hasUserFunction(String name, int arity) {
20+
public boolean hasUserFunction(String name, Set<Integer> arity) {
2121
if ("hellofunc".equals(name)) {
22-
return arity == ANY_ARITY || arity == 1;
22+
return arity.isEmpty() || arity.contains(1);
2323
}
2424
return false;
2525
}
2626

2727
@Override
28-
public boolean processAction(PreprocessorContext context, Value[] parameters) {
29-
System.out.println("Called action for parameters: " + Arrays.toString(parameters));
28+
public boolean processAction(PreprocessorContext context, List<Value> parameters) {
29+
System.out.println("Called action for parameters: " + parameters);
3030
return true;
3131
}
3232

jcp/src/main/java/com/igormaznitsa/jcp/directives/ActionDirectiveHandler.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@
2828
import com.igormaznitsa.jcp.expression.ExpressionItem;
2929
import com.igormaznitsa.jcp.expression.ExpressionParser;
3030
import com.igormaznitsa.jcp.expression.ExpressionTree;
31-
import com.igormaznitsa.jcp.expression.Value;
3231
import com.igormaznitsa.jcp.extension.PreprocessorExtension;
3332
import java.io.IOException;
3433
import java.io.PushbackReader;
3534
import java.io.StringReader;
3635
import java.util.ArrayList;
3736
import java.util.List;
37+
import java.util.stream.Collectors;
3838

3939
/**
4040
* The class implements the //#action directive handler
@@ -83,14 +83,9 @@ public AfterDirectiveProcessingBehaviour execute(final String string,
8383
null);
8484
}
8585

86-
final Value[] argValues = new Value[args.size()];
87-
int index = 0;
88-
for (final ExpressionTree expr : args) {
89-
final Value val = Expression.evalTree(expr, context);
90-
argValues[index++] = val;
91-
}
92-
93-
if (!extension.processAction(context, argValues)) {
86+
if (!extension.processAction(context,
87+
args.stream().map(x -> Expression.evalTree(x, context)).collect(
88+
Collectors.toList()))) {
9489
throw context.makeException("Unable to process an action", null);
9590
}
9691
} catch (IOException ex) {

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
package com.igormaznitsa.jcp.expression;
2323

24+
import static com.igormaznitsa.jcp.expression.ExpressionTreeElement.ANY_ARITY;
25+
2426
import com.igormaznitsa.jcp.context.PreprocessorContext;
2527
import com.igormaznitsa.jcp.exceptions.FilePositionInfo;
2628
import com.igormaznitsa.jcp.exceptions.PreprocessorException;
@@ -140,6 +142,9 @@ private ExpressionTreeElement evalFunction(
140142
final List<List<ValueType>> allowedSignatures = functionElement.getAllowedArgumentTypes();
141143
List<ValueType> allowed = null;
142144
for (final List<ValueType> current : allowedSignatures) {
145+
if (current.size() != arguments.size()) {
146+
continue;
147+
}
143148
boolean allCompatible = true;
144149

145150
int thatIndex = 0;
@@ -161,9 +166,13 @@ private ExpressionTreeElement evalFunction(
161166
}
162167

163168
if (allowed == null) {
164-
throw context.makeException(
165-
"[Expression]Unsupported argument detected for '" + functionElement.getName() + '\'',
166-
null);
169+
if (functionElement.getArity().contains(ANY_ARITY)) {
170+
signature.append(ValueType.ANY.getSignature());
171+
} else {
172+
throw context.makeException(
173+
"[Expression]Unsupported argument detected for '" + functionElement.getName() + '\'',
174+
null);
175+
}
167176
}
168177

169178
if (functionElement instanceof FunctionDefinedByUser) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,8 @@ ExpressionItem nextItem(final PushbackReader reader, final PreprocessorContext c
609609
context.getPreprocessorExtensions().stream()
610610
.filter(x -> x.isAllowed(context))
611611
.filter(x -> x.hasUserFunction(userFunctionName,
612-
PreprocessorExtension.ANY_ARITY)).findFirst().orElse(null);
612+
Set.of()))
613+
.findFirst().orElse(null);
613614

614615
if (preprocessorExtension == null) {
615616
throw context.makeException(

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,10 @@ public void fillArguments(final List<ExpressionTree> arguments) {
344344
this.includeStack, null);
345345
}
346346

347-
if (childrenSlots.length != arguments.size()) {
348-
throw new PreprocessorException("Wrong argument list size", this.sourceString,
347+
if (!this.expectedArities.contains(ANY_ARITY) &&
348+
!this.expectedArities.contains(arguments.size())) {
349+
throw new PreprocessorException(
350+
"Wrong argument list size, expected arities: " + this.expectedArities, this.sourceString,
349351
this.includeStack, null);
350352
}
351353

jcp/src/main/java/com/igormaznitsa/jcp/expression/functions/FunctionDefinedByUser.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,10 @@ public Value execute(final PreprocessorContext context, final List<Value> values
8181
null);
8282
}
8383

84+
final Set<Integer> expectedArity = Set.of(values.size());
85+
8486
final PreprocessorExtension extension =
85-
extensionList.stream().filter(x -> x.hasUserFunction(this.name, values.size()))
87+
extensionList.stream().filter(x -> x.hasUserFunction(this.name, expectedArity))
8688
.findFirst().orElseThrow(() -> context
8789
.makeException(
8890
"Can't find any preprocessor extension to process function " + this.name + " for " +

jcp/src/main/java/com/igormaznitsa/jcp/extension/LogPreprocessorExtension.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
import com.igormaznitsa.jcp.containers.TextFileDataContainer;
55
import com.igormaznitsa.jcp.context.PreprocessorContext;
66
import com.igormaznitsa.jcp.expression.Value;
7-
import java.util.Arrays;
87
import java.util.List;
98
import java.util.Set;
109
import java.util.regex.Matcher;
1110
import java.util.regex.Pattern;
11+
import java.util.stream.Collectors;
1212

1313
/**
1414
* Preprocessor extension which just make info logging for arguments of action calls and also
@@ -42,13 +42,14 @@ public boolean hasAction(int arity) {
4242
}
4343

4444
@Override
45-
public boolean hasUserFunction(String name, int arity) {
45+
public boolean hasUserFunction(String name, Set<Integer> arity) {
4646
return true;
4747
}
4848

4949
@Override
50-
public boolean processAction(final PreprocessorContext context, final Value[] parameters) {
51-
context.logInfo(String.format("Called action: %s at %s", Arrays.toString(parameters),
50+
public boolean processAction(final PreprocessorContext context, final List<Value> parameters) {
51+
context.logInfo(String.format("Called action: %s at %s", parameters.stream().map(
52+
Value::toString).collect(Collectors.joining(",")),
5253
findPosition(context)));
5354
return true;
5455
}

jcp/src/main/java/com/igormaznitsa/jcp/extension/PreprocessorExtension.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,6 @@
3636
*/
3737
public interface PreprocessorExtension extends PreprocessorContextAware, ExecutionAllowable {
3838

39-
/**
40-
* Undefined arity. In case of functions it means to check only name.
41-
*/
42-
int ANY_ARITY = -1;
43-
4439
@Override
4540
default boolean isAllowed(PreprocessorContext context) {
4641
return true;
@@ -59,12 +54,11 @@ default boolean isAllowed(PreprocessorContext context) {
5954
* Allows to check that the extension contains a user defined function with required number of parameters.
6055
*
6156
* @param name name of the function to be checked, must not be null
62-
* @param arity number of parameters for action, if ANY_ARITY then check only by name
57+
* @param arity set of numbers of parameters for action, if empty set then check only name
6358
* @return true if such user defined function is provided by the extension
64-
* @see #ANY_ARITY
65-
* @since 7.2.2
59+
* @since 7.3.0
6660
*/
67-
boolean hasUserFunction(String name, int arity);
61+
boolean hasUserFunction(String name, Set<Integer> arity);
6862

6963
/**
7064
* To process an action (it will be called if the preprocessor is met
@@ -74,8 +68,9 @@ default boolean isAllowed(PreprocessorContext context) {
7468
* @param parameters the parameters of the action directive, must not be null
7569
* @return true if the action has been processed successfully or false, if it
7670
* is false then exception will be thrown and preprocessing will be stopped
71+
* @since 7.3.0
7772
*/
78-
boolean processAction(PreprocessorContext context, Value[] parameters);
73+
boolean processAction(PreprocessorContext context, List<Value> parameters);
7974

8075
/**
8176
* Call to process a user function (such functions start with $)
@@ -85,7 +80,7 @@ default boolean isAllowed(PreprocessorContext context) {
8580
* must not be null
8681
* @param arguments the function arguments, must not be null
8782
* @return a calculated value, it must not be null
88-
* @since 7.1.2
83+
* @since 7.3.0
8984
*/
9085
Value processUserFunction(PreprocessorContext context, String functionName,
9186
List<Value> arguments);

jcp/src/test/java/com/igormaznitsa/jcp/context/PreprocessorContextTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,12 +265,12 @@ public boolean hasAction(int arity) {
265265
}
266266

267267
@Override
268-
public boolean hasUserFunction(String name, int arity) {
268+
public boolean hasUserFunction(String name, Set<Integer> arity) {
269269
return true;
270270
}
271271

272272
@Override
273-
public boolean processAction(PreprocessorContext context, Value[] parameters) {
273+
public boolean processAction(PreprocessorContext context, List<Value> parameters) {
274274
throw new UnsupportedOperationException(
275275
"Not supported yet.");
276276
}

jcp/src/test/java/com/igormaznitsa/jcp/directives/ActionDirectiveHandlerTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import static org.junit.Assert.assertFalse;
2626
import static org.junit.Assert.assertTrue;
2727
import static org.mockito.ArgumentMatchers.anyInt;
28+
import static org.mockito.ArgumentMatchers.anyList;
2829
import static org.mockito.ArgumentMatchers.anyString;
2930
import static org.mockito.Mockito.any;
3031
import static org.mockito.Mockito.eq;
@@ -35,6 +36,7 @@
3536
import com.igormaznitsa.jcp.context.PreprocessorContext;
3637
import com.igormaznitsa.jcp.expression.Value;
3738
import com.igormaznitsa.jcp.extension.PreprocessorExtension;
39+
import java.util.List;
3840
import org.junit.Test;
3941
import org.mockito.Mockito;
4042

@@ -46,10 +48,10 @@ public class ActionDirectiveHandlerTest extends AbstractDirectiveHandlerAcceptan
4648
@Override
4749
public void testExecution() throws Exception {
4850
final PreprocessorExtension mockup = mock(PreprocessorExtension.class);
49-
when(mockup.hasUserFunction(anyString(), anyInt())).thenReturn(true);
51+
when(mockup.hasUserFunction(anyString(), any())).thenReturn(true);
5052
when(mockup.hasAction(anyInt())).thenReturn(true);
5153
when(mockup.isAllowed(any())).thenReturn(true);
52-
when(mockup.processAction(any(PreprocessorContext.class), any(Value[].class))).thenReturn(
54+
when(mockup.processAction(any(PreprocessorContext.class), anyList())).thenReturn(
5355
Boolean.TRUE);
5456

5557
assertFilePreprocessing("directive_action.txt", false, mockup, null);
@@ -62,7 +64,7 @@ public void testExecution() throws Exception {
6264
final Value val6 = Value.valueOf("hello,");
6365

6466
verify(mockup).processAction(any(PreprocessorContext.class),
65-
eq(new Value[] {val1, val2, val3, val4, val5, val6}));
67+
eq(List.of(val1, val2, val3, val4, val5, val6)));
6668
}
6769

6870
@Test

0 commit comments

Comments
 (0)