Skip to content

Commit 21bbf96

Browse files
committed
Switch from mixin to static method calls
1 parent 5b338ae commit 21bbf96

File tree

12 files changed

+171
-164
lines changed

12 files changed

+171
-164
lines changed

spock-core/src/main/java/org/spockframework/compiler/AstNodeCache.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public class AstNodeCache {
4242
public final ClassNode ErrorRethrower = ClassHelper.makeWithoutCaching(ErrorRethrower.class);
4343
public final ClassNode Specification = ClassHelper.makeWithoutCaching(Specification.class);
4444
public final ClassNode SpecInternals = ClassHelper.makeWithoutCaching(SpecInternals.class);
45+
public final ClassNode SpecialMethodCallTarget = ClassHelper.makeWithoutCaching(SpecialMethodCallTarget.class);
4546
public final ClassNode MockController = ClassHelper.makeWithoutCaching(MockController.class);
4647
public final ClassNode SpecificationContext = ClassHelper.makeWithoutCaching(SpecificationContext.class);
4748
public final ClassNode DataVariableMultiplication = ClassHelper.makeWithoutCaching(DataVariableMultiplication.class);

spock-core/src/main/java/org/spockframework/compiler/DeepBlockRewriter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ private boolean handleOldCall(MethodCallExpression expr) {
289289
return true;
290290
}
291291

292+
expr.setObjectExpression(new ClassExpression(resources.getAstNodeCache().SpecialMethodCallTarget));
292293
expr.setMethod(new ConstantExpression(expr.getMethodAsString() + "Impl"));
293294
List<Expression> args = AstUtil.getArgumentList(expr);
294295
if (args.size() != 1) {

spock-core/src/main/java/org/spockframework/compiler/SpecialMethodCall.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,19 @@ public class SpecialMethodCall implements ISpecialMethodCall {
4545
@Nullable
4646
private final ClosureExpression closureExpr;
4747
private final boolean conditionBlock;
48+
private final AstNodeCache nodeCache;
4849

4950
public SpecialMethodCall(String methodName, Expression inferredName, Expression inferredType,
5051
MethodCallExpression methodCallExpr, @Nullable BinaryExpression binaryExpr,
51-
@Nullable ClosureExpression closureExpr, boolean conditionBlock) {
52+
@Nullable ClosureExpression closureExpr, boolean conditionBlock, AstNodeCache nodeCache) {
5253
this.methodName = methodName;
5354
this.inferredName = inferredName;
5455
this.inferredType = inferredType;
5556
this.binaryExpr = binaryExpr;
5657
this.methodCallExpr = methodCallExpr;
5758
this.closureExpr = closureExpr;
5859
this.conditionBlock = conditionBlock;
60+
this.nodeCache = nodeCache;
5961
}
6062

6163
@Override
@@ -168,13 +170,15 @@ public ClosureExpression getClosureExpr() {
168170
@Override
169171
public void expand() {
170172
List<Expression> args = new ArrayList<>();
173+
args.add(VariableExpression.THIS_EXPRESSION);
171174
args.add(inferredName);
172175
args.add(inferredType);
173176
args.addAll(AstUtil.getArgumentList(methodCallExpr));
174177

175178
ArgumentListExpression argsExpr = new ArgumentListExpression(args);
176179
AstUtil.copySourcePosition(methodCallExpr.getArguments(), argsExpr);
177180
methodCallExpr.setArguments(argsExpr);
181+
methodCallExpr.setObjectExpression(new ClassExpression(nodeCache.SpecialMethodCallTarget));
178182
methodCallExpr.setMethod(new ConstantExpression(methodName + "Impl"));
179183
}
180184

@@ -208,7 +212,7 @@ public static SpecialMethodCall parse(MethodCallExpression methodCallExpr, @Null
208212
}
209213
wrapCastedConstructorArgs(arguments, nodeCache);
210214

211-
return new SpecialMethodCall(methodName, inferredName, inferredType, methodCallExpr, binaryExpr, closureExpr, conditionBlock);
215+
return new SpecialMethodCall(methodName, inferredName, inferredType, methodCallExpr, binaryExpr, closureExpr, conditionBlock, nodeCache);
212216
}
213217

214218
private static void wrapCastedConstructorArgs(List<Expression> arguments, AstNodeCache nodeCache) {

spock-core/src/main/java/org/spockframework/lang/SpecInternals.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,47 @@
1414

1515
package org.spockframework.lang;
1616

17+
import org.spockframework.mock.*;
18+
import org.spockframework.mock.runtime.*;
1719
import org.spockframework.runtime.*;
1820
import org.spockframework.util.*;
1921

22+
import java.lang.reflect.Type;
23+
import java.util.*;
24+
25+
import groovy.lang.Closure;
26+
27+
import static org.spockframework.util.ObjectUtil.uncheckedCast;
28+
2029
public abstract class SpecInternals {
2130
private final ISpecificationContext specificationContext = new SpecificationContext();
2231

2332
@Beta
2433
public ISpecificationContext getSpecificationContext() {
2534
return specificationContext;
2635
}
36+
37+
@Beta
38+
public <T> T createMock(@Nullable String name, Type type, MockNature nature,
39+
MockImplementation implementation, Map<String, Object> options, @Nullable Closure<?> closure) {
40+
return createMock(name, null, type, nature, implementation, options, closure);
41+
}
42+
43+
@Beta
44+
public <T> T createMock(@Nullable String name, T instance, Type type, MockNature nature,
45+
MockImplementation implementation, Map<String, Object> options, @Nullable Closure<?> closure) {
46+
Object mock = CompositeMockFactory.INSTANCE.create(
47+
new MockConfiguration(name, type, instance, nature, implementation, options), uncheckedCast(this));
48+
if (closure != null) {
49+
GroovyRuntimeUtil.invokeClosure(closure, mock);
50+
}
51+
return uncheckedCast(mock);
52+
}
53+
54+
@Beta
55+
public void createStaticMock(Type type, MockNature nature,
56+
Map<String, Object> options) {
57+
MockConfiguration configuration = new MockConfiguration(null, type, null, nature, MockImplementation.JAVA, options);
58+
JavaMockFactory.INSTANCE.createStaticMock(configuration, uncheckedCast(this));
59+
}
2760
}

spock-core/src/main/java/org/spockframework/mock/EmptyOrDummyResponse.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
package org.spockframework.mock;
1616

17-
import org.spockframework.runtime.GroovyRuntimeUtil;
1817
import org.spockframework.runtime.extension.IDefaultValueProviderExtension;
1918
import org.spockframework.util.ReflectionUtil;
2019
import org.spockframework.util.ThreadSafe;
@@ -161,8 +160,7 @@ private Object createDummy(IMockInvocation invocation) {
161160
Class<?> type = invocation.getMethod().getReturnType();
162161
Type genericType = invocation.getMethod().getExactReturnType();
163162
Specification spec = invocation.getMockObject().getSpecification();
164-
return GroovyRuntimeUtil.invokeMethod(spec, "createMock",
165-
"dummy", genericType, MockNature.STUB, GroovyObject.class.isAssignableFrom(type) ?
163+
return spec.createMock("dummy", genericType, MockNature.STUB, GroovyObject.class.isAssignableFrom(type) ?
166164
MockImplementation.GROOVY : MockImplementation.JAVA, emptyMap(), null);
167165
}
168166
}

0 commit comments

Comments
 (0)