Skip to content

Commit f0f60ab

Browse files
authored
Move spec internals methods to be static method calls (#2209)
This removes relying on Groovy being able to call package-private methods in other-package subclasses, which does not work anymore with Groovy 5.
1 parent 4ad40b1 commit f0f60ab

File tree

15 files changed

+398
-377
lines changed

15 files changed

+398
-377
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.spockframework.compiler;
1818

1919
import org.codehaus.groovy.runtime.wrappers.PojoWrapper;
20-
import org.spockframework.lang.SpecInternals;
2120
import org.spockframework.mock.runtime.MockController;
2221
import org.spockframework.runtime.*;
2322
import org.spockframework.runtime.extension.RepeatedExtensionAnnotations;
@@ -48,8 +47,8 @@ public class AstNodeCache {
4847
public final ClassNode DataVariableMultiplicationFactor = ClassHelper.makeWithoutCaching(DataVariableMultiplicationFactor.class);
4948
public final ClassNode BlockInfo = ClassHelper.makeWithoutCaching(BlockInfo.class);
5049

51-
public final MethodNode SpecInternals_GetSpecificationContext =
52-
SpecInternals.getDeclaredMethods(Identifiers.GET_SPECIFICATION_CONTEXT).get(0);
50+
public final MethodNode Specification_GetSpecificationContext =
51+
Specification.getDeclaredMethods(Identifiers.GET_SPECIFICATION_CONTEXT).get(0);
5352

5453
public final MethodNode SpockRuntime_VerifyCondition =
5554
SpockRuntime.getDeclaredMethods(org.spockframework.runtime.SpockRuntime.VERIFY_CONDITION).get(0);

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().SpecInternals));
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/SpecRewriter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ public VariableExpression captureOldValue(Expression oldValue) {
683683

684684
public MethodCallExpression getSpecificationContext() {
685685
return createDirectMethodCall(VariableExpression.THIS_EXPRESSION,
686-
nodeCache.SpecInternals_GetSpecificationContext, ArgumentListExpression.EMPTY_ARGUMENTS);
686+
nodeCache.Specification_GetSpecificationContext, ArgumentListExpression.EMPTY_ARGUMENTS);
687687
}
688688

689689
@Override

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.SpecInternals));
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: 0 additions & 351 deletions
This file was deleted.

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
package org.spockframework.mock;
1616

17+
import org.spockframework.runtime.SpecInternals;
1718
import org.spockframework.runtime.extension.IDefaultValueProviderExtension;
1819
import org.spockframework.util.ReflectionUtil;
1920
import org.spockframework.util.ThreadSafe;
@@ -160,7 +161,8 @@ private Object createDummy(IMockInvocation invocation) {
160161
Class<?> type = invocation.getMethod().getReturnType();
161162
Type genericType = invocation.getMethod().getExactReturnType();
162163
Specification spec = invocation.getMockObject().getSpecification();
163-
return spec.createMock("dummy", genericType, MockNature.STUB, GroovyObject.class.isAssignableFrom(type) ?
164-
MockImplementation.GROOVY : MockImplementation.JAVA, emptyMap(), null);
164+
return GroovyObject.class.isAssignableFrom(type)
165+
? SpecInternals.GroovyStubImpl(spec, "dummy", genericType)
166+
: SpecInternals.StubImpl(spec, "dummy", genericType);
165167
}
166168
}

0 commit comments

Comments
 (0)