|
28 | 28 | import org.spockframework.util.AbstractExpressionConverter; |
29 | 29 | import org.spockframework.util.Assert; |
30 | 30 | import org.spockframework.util.Identifiers; |
| 31 | +import org.spockframework.util.ReflectionUtil; |
31 | 32 | import org.spockframework.util.TextUtil; |
32 | 33 |
|
| 34 | +import java.lang.reflect.Constructor; |
| 35 | +import java.lang.reflect.Method; |
33 | 36 | import java.util.ArrayList; |
34 | 37 | import java.util.List; |
35 | 38 | import java.util.regex.Pattern; |
|
59 | 62 | public class ConditionRewriter extends AbstractExpressionConverter<Expression> implements GroovyCodeVisitorCompat { |
60 | 63 | private static final Pattern COMMENTS_PATTERN = Pattern.compile("/\\*.*?\\*/|//.*$"); |
61 | 64 | private static final String THROWABLE = "$spock_condition_throwable"; |
| 65 | + private static final Constructor<RangeExpression> RANGE_EXPRESSION_CONSTRUCTOR_GROOVY_4 = ReflectionUtil.getDeclaredConstructorBySignature(RangeExpression.class, Expression.class, Expression.class, Boolean.TYPE, Boolean.TYPE); |
| 66 | + private static final Method RANGE_EXPRESSION_IS_EXCLUSIVE_LEFT = ReflectionUtil.getDeclaredMethodBySignature(RangeExpression.class, "isExclusiveLeft"); |
| 67 | + private static final Method RANGE_EXPRESSION_IS_EXCLUSIVE_RIGHT = ReflectionUtil.getDeclaredMethodBySignature(RangeExpression.class, "isExclusiveRight"); |
62 | 68 |
|
63 | 69 | private final IRewriteResources resources; |
64 | 70 |
|
@@ -340,11 +346,20 @@ public void visitListExpression(ListExpression expr) { |
340 | 346 |
|
341 | 347 | @Override |
342 | 348 | public void visitRangeExpression(RangeExpression expr) { |
343 | | - RangeExpression conversion = |
344 | | - new RangeExpression( |
345 | | - convert(expr.getFrom()), |
346 | | - convert(expr.getTo()), |
347 | | - expr.isInclusive()); |
| 349 | + RangeExpression conversion; |
| 350 | + if (RANGE_EXPRESSION_CONSTRUCTOR_GROOVY_4 != null) { |
| 351 | + conversion = ReflectionUtil.newInstance(RANGE_EXPRESSION_CONSTRUCTOR_GROOVY_4, |
| 352 | + convert(expr.getFrom()), |
| 353 | + convert(expr.getTo()), |
| 354 | + ReflectionUtil.invokeMethod(expr, RANGE_EXPRESSION_IS_EXCLUSIVE_LEFT), |
| 355 | + ReflectionUtil.invokeMethod(expr, RANGE_EXPRESSION_IS_EXCLUSIVE_RIGHT) |
| 356 | + ); |
| 357 | + } else { |
| 358 | + conversion = new RangeExpression( |
| 359 | + convert(expr.getFrom()), |
| 360 | + convert(expr.getTo()), |
| 361 | + expr.isInclusive()); |
| 362 | + } |
348 | 363 |
|
349 | 364 | conversion.setSourcePosition(expr); |
350 | 365 | result = record(conversion); |
|
0 commit comments