diff --git a/spock-core/src/main/java/org/spockframework/compiler/AstNodeCache.java b/spock-core/src/main/java/org/spockframework/compiler/AstNodeCache.java index bbe9395a20..458a1fafb0 100644 --- a/spock-core/src/main/java/org/spockframework/compiler/AstNodeCache.java +++ b/spock-core/src/main/java/org/spockframework/compiler/AstNodeCache.java @@ -17,7 +17,6 @@ package org.spockframework.compiler; import org.codehaus.groovy.runtime.wrappers.PojoWrapper; -import org.spockframework.lang.SpecInternals; import org.spockframework.mock.runtime.MockController; import org.spockframework.runtime.*; import org.spockframework.runtime.extension.RepeatedExtensionAnnotations; @@ -48,8 +47,8 @@ public class AstNodeCache { public final ClassNode DataVariableMultiplicationFactor = ClassHelper.makeWithoutCaching(DataVariableMultiplicationFactor.class); public final ClassNode BlockInfo = ClassHelper.makeWithoutCaching(BlockInfo.class); - public final MethodNode SpecInternals_GetSpecificationContext = - SpecInternals.getDeclaredMethods(Identifiers.GET_SPECIFICATION_CONTEXT).get(0); + public final MethodNode Specification_GetSpecificationContext = + Specification.getDeclaredMethods(Identifiers.GET_SPECIFICATION_CONTEXT).get(0); public final MethodNode SpockRuntime_VerifyCondition = SpockRuntime.getDeclaredMethods(org.spockframework.runtime.SpockRuntime.VERIFY_CONDITION).get(0); diff --git a/spock-core/src/main/java/org/spockframework/compiler/DeepBlockRewriter.java b/spock-core/src/main/java/org/spockframework/compiler/DeepBlockRewriter.java index 06e08cc8ca..556266da33 100644 --- a/spock-core/src/main/java/org/spockframework/compiler/DeepBlockRewriter.java +++ b/spock-core/src/main/java/org/spockframework/compiler/DeepBlockRewriter.java @@ -289,6 +289,7 @@ private boolean handleOldCall(MethodCallExpression expr) { return true; } + expr.setObjectExpression(new ClassExpression(resources.getAstNodeCache().SpecInternals)); expr.setMethod(new ConstantExpression(expr.getMethodAsString() + "Impl")); List args = AstUtil.getArgumentList(expr); if (args.size() != 1) { diff --git a/spock-core/src/main/java/org/spockframework/compiler/SpecRewriter.java b/spock-core/src/main/java/org/spockframework/compiler/SpecRewriter.java index eecf363e4a..1098c08db2 100644 --- a/spock-core/src/main/java/org/spockframework/compiler/SpecRewriter.java +++ b/spock-core/src/main/java/org/spockframework/compiler/SpecRewriter.java @@ -683,7 +683,7 @@ public VariableExpression captureOldValue(Expression oldValue) { public MethodCallExpression getSpecificationContext() { return createDirectMethodCall(VariableExpression.THIS_EXPRESSION, - nodeCache.SpecInternals_GetSpecificationContext, ArgumentListExpression.EMPTY_ARGUMENTS); + nodeCache.Specification_GetSpecificationContext, ArgumentListExpression.EMPTY_ARGUMENTS); } @Override diff --git a/spock-core/src/main/java/org/spockframework/compiler/SpecialMethodCall.java b/spock-core/src/main/java/org/spockframework/compiler/SpecialMethodCall.java index d2fc0c7c98..fd61b43e78 100644 --- a/spock-core/src/main/java/org/spockframework/compiler/SpecialMethodCall.java +++ b/spock-core/src/main/java/org/spockframework/compiler/SpecialMethodCall.java @@ -45,10 +45,11 @@ public class SpecialMethodCall implements ISpecialMethodCall { @Nullable private final ClosureExpression closureExpr; private final boolean conditionBlock; + private final AstNodeCache nodeCache; public SpecialMethodCall(String methodName, Expression inferredName, Expression inferredType, MethodCallExpression methodCallExpr, @Nullable BinaryExpression binaryExpr, - @Nullable ClosureExpression closureExpr, boolean conditionBlock) { + @Nullable ClosureExpression closureExpr, boolean conditionBlock, AstNodeCache nodeCache) { this.methodName = methodName; this.inferredName = inferredName; this.inferredType = inferredType; @@ -56,6 +57,7 @@ public SpecialMethodCall(String methodName, Expression inferredName, Expression this.methodCallExpr = methodCallExpr; this.closureExpr = closureExpr; this.conditionBlock = conditionBlock; + this.nodeCache = nodeCache; } @Override @@ -168,6 +170,7 @@ public ClosureExpression getClosureExpr() { @Override public void expand() { List args = new ArrayList<>(); + args.add(VariableExpression.THIS_EXPRESSION); args.add(inferredName); args.add(inferredType); args.addAll(AstUtil.getArgumentList(methodCallExpr)); @@ -175,6 +178,7 @@ public void expand() { ArgumentListExpression argsExpr = new ArgumentListExpression(args); AstUtil.copySourcePosition(methodCallExpr.getArguments(), argsExpr); methodCallExpr.setArguments(argsExpr); + methodCallExpr.setObjectExpression(new ClassExpression(nodeCache.SpecInternals)); methodCallExpr.setMethod(new ConstantExpression(methodName + "Impl")); } @@ -208,7 +212,7 @@ public static SpecialMethodCall parse(MethodCallExpression methodCallExpr, @Null } wrapCastedConstructorArgs(arguments, nodeCache); - return new SpecialMethodCall(methodName, inferredName, inferredType, methodCallExpr, binaryExpr, closureExpr, conditionBlock); + return new SpecialMethodCall(methodName, inferredName, inferredType, methodCallExpr, binaryExpr, closureExpr, conditionBlock, nodeCache); } private static void wrapCastedConstructorArgs(List arguments, AstNodeCache nodeCache) { diff --git a/spock-core/src/main/java/org/spockframework/lang/SpecInternals.java b/spock-core/src/main/java/org/spockframework/lang/SpecInternals.java deleted file mode 100644 index 68263a7e9f..0000000000 --- a/spock-core/src/main/java/org/spockframework/lang/SpecInternals.java +++ /dev/null @@ -1,351 +0,0 @@ -/* - * Copyright 2012 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * https://www.apache.org/licenses/LICENSE-2.0 - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.spockframework.lang; - -import groovy.transform.stc.ClosureParams; -import groovy.transform.stc.SecondParam; -import groovy.transform.stc.ThirdParam; -import org.spockframework.mock.*; -import org.spockframework.mock.runtime.*; -import org.spockframework.runtime.*; -import org.spockframework.util.*; -import spock.lang.Specification; - -import java.lang.reflect.Type; -import java.util.*; - -import groovy.lang.Closure; -import spock.mock.IMockMakerSettings; - -import static java.util.Collections.emptyMap; -import static java.util.Collections.singletonMap; -import static org.spockframework.util.ObjectUtil.uncheckedCast; - -@SuppressWarnings("UnusedDeclaration") -public abstract class SpecInternals { - private static final MockUtil MOCK_UTIL = new MockUtil(); - private final ISpecificationContext specificationContext = new SpecificationContext(); - - @Beta - public ISpecificationContext getSpecificationContext() { - return specificationContext; - } - - @Beta - public T createMock(@Nullable String name, Type type, MockNature nature, - MockImplementation implementation, Map options, @Nullable Closure closure) { - return createMock(name, null, type, nature, implementation, options, closure); - } - - @Beta - public T createMock(@Nullable String name, T instance, Type type, MockNature nature, - MockImplementation implementation, Map options, @Nullable Closure closure) { - Object mock = CompositeMockFactory.INSTANCE.create( - new MockConfiguration(name, type, instance, nature, implementation, options), (Specification) this); - if (closure != null) { - GroovyRuntimeUtil.invokeClosure(closure, mock); - } - return uncheckedCast(mock); - } - - private void createStaticMock(Type type, MockNature nature, - Map options) { - MockConfiguration configuration = new MockConfiguration(null, type, null, nature, MockImplementation.JAVA, options); - JavaMockFactory.INSTANCE.createStaticMock(configuration, (Specification) this); - } - - T oldImpl(T expression) { - return expression; - } - - T thrownImpl(String inferredName, Class inferredType) { - return inferredType.cast(checkExceptionThrown(inferredType)); - } - - Throwable thrownImpl(String inferredName, Class inferredType, Class specifiedType) { - return checkExceptionThrown(specifiedType == null ? inferredType : specifiedType); - } - - Throwable checkExceptionThrown(Class exceptionType) { - if (exceptionType == null) { - throw new InvalidSpecException("Thrown exception type cannot be inferred automatically. " + - "Please specify a type explicitly (e.g. 'thrown(MyException)')."); - } - - if (!Throwable.class.isAssignableFrom(exceptionType)) - throw new InvalidSpecException( - "Invalid exception condition: '%s' is not a (subclass of) java.lang.Throwable" - ).withArgs(exceptionType.getSimpleName()); - - Throwable actual = specificationContext.getThrownException(); - if (exceptionType.isInstance(actual)) return actual; - - throw new WrongExceptionThrownError(exceptionType, actual); - } - - T MockImpl(String inferredName, Class inferredType) { - return createMockImpl(inferredName, inferredType, MockNature.MOCK, MockImplementation.JAVA, emptyMap(), null, null); - } - - T MockImpl(String inferredName, Class inferredType, @ClosureParams(SecondParam.FirstGenericType.class) Closure closure) { - return createMockImpl(inferredName, inferredType, MockNature.MOCK, MockImplementation.JAVA, emptyMap(), null, closure); - } - - T MockImpl(String inferredName, Class inferredType, Map options) { - return createMockImpl(inferredName, inferredType, MockNature.MOCK, MockImplementation.JAVA, options, null, null); - } - - T MockImpl(String inferredName, Class inferredType, Map options, Closure closure) { - return createMockImpl(inferredName, inferredType, MockNature.MOCK, MockImplementation.JAVA, options, null, closure); - } - - T MockImpl(String inferredName, Class inferredType, Class specifiedType) { - return createMockImpl(inferredName, inferredType, MockNature.MOCK, MockImplementation.JAVA, emptyMap(), specifiedType, null); - } - - T MockImpl(String inferredName, Class inferredType, Class specifiedType, Closure closure) { - return createMockImpl(inferredName, inferredType, MockNature.MOCK, MockImplementation.JAVA, emptyMap(), specifiedType, closure); - } - - T MockImpl(String inferredName, Class inferredType, Map options, Class specifiedType) { - return createMockImpl(inferredName, inferredType, MockNature.MOCK, MockImplementation.JAVA, options, specifiedType, null); - } - - T MockImpl(String inferredName, Class inferredType, Map options, Class specifiedType, Closure closure) { - return createMockImpl(inferredName, inferredType, MockNature.MOCK, MockImplementation.JAVA, options, specifiedType, closure); - } - - T StubImpl(String inferredName, Class inferredType) { - return createMockImpl(inferredName, inferredType, MockNature.STUB, MockImplementation.JAVA, emptyMap(), null, null); - } - - T StubImpl(String inferredName, Class inferredType, @ClosureParams(SecondParam.FirstGenericType.class) Closure closure) { - return createMockImpl(inferredName, inferredType, MockNature.STUB, MockImplementation.JAVA, emptyMap(), null, closure); - } - - T StubImpl(String inferredName, Class inferredType, Map options) { - return createMockImpl(inferredName, inferredType, MockNature.STUB, MockImplementation.JAVA, options, null, null); - } - - T StubImpl(String inferredName, Class inferredType, Map options, Closure closure) { - return createMockImpl(inferredName, inferredType, MockNature.STUB, MockImplementation.JAVA, options, null, closure); - } - - T StubImpl(String inferredName, Class inferredType, Class specifiedType) { - return createMockImpl(inferredName, inferredType, MockNature.STUB, MockImplementation.JAVA, emptyMap(), specifiedType, null); - } - - T StubImpl(String inferredName, Class inferredType, Class specifiedType, Closure closure) { - return createMockImpl(inferredName, inferredType, MockNature.STUB, MockImplementation.JAVA, emptyMap(), specifiedType, closure); - } - - T StubImpl(String inferredName, Class inferredType, Map options, Class specifiedType) { - return createMockImpl(inferredName, inferredType, MockNature.STUB, MockImplementation.JAVA, options, specifiedType, null); - } - - T StubImpl(String inferredName, Class inferredType, Map options, Class specifiedType, Closure closure) { - return createMockImpl(inferredName, inferredType, MockNature.STUB, MockImplementation.JAVA, options, specifiedType, closure); - } - - T SpyImpl(String inferredName, Class inferredType) { - return createMockImpl(inferredName, inferredType, MockNature.SPY, MockImplementation.JAVA, emptyMap(), null, null); - } - - T SpyImpl(String inferredName, Class inferredType, @ClosureParams(SecondParam.FirstGenericType.class) Closure closure) { - return createMockImpl(inferredName, inferredType, MockNature.SPY, MockImplementation.JAVA, emptyMap(), null, closure); - } - - T SpyImpl(String inferredName, Class inferredType, Map options) { - return createMockImpl(inferredName, inferredType, MockNature.SPY, MockImplementation.JAVA, options, null, null); - } - - T SpyImpl(String inferredName, Class inferredType, Map options, Closure closure) { - return createMockImpl(inferredName, inferredType, MockNature.SPY, MockImplementation.JAVA, options, null, closure); - } - - T SpyImpl(String inferredName, Class inferredType, Class specifiedType) { - return createMockImpl(inferredName, inferredType, MockNature.SPY, MockImplementation.JAVA, emptyMap(), specifiedType, null); - } - - T SpyImpl(String inferredName, Class inferredType, Class specifiedType, Closure closure) { - return createMockImpl(inferredName, inferredType, MockNature.SPY, MockImplementation.JAVA, emptyMap(), specifiedType, closure); - } - - T SpyImpl(String inferredName, Class inferredType, Map options, Class specifiedType) { - return createMockImpl(inferredName, inferredType, MockNature.SPY, MockImplementation.JAVA, options, specifiedType, null); - } - - T SpyImpl(String inferredName, Class inferredType, Map options, Class specifiedType, Closure closure) { - return createMockImpl(inferredName, inferredType, MockNature.SPY, MockImplementation.JAVA, options, specifiedType, closure); - } - - T SpyImpl(String inferredName, Class inferredType, T instance) { - return SpyImpl(inferredName, inferredType, instance, null); - } - - T SpyImpl(String inferredName, Class inferredType, T instance, @ClosureParams(ThirdParam.class) Closure closure) { - if (instance == null) { - throw new SpockException("Spy instance may not be null"); - } - if (MOCK_UTIL.isMock(instance)) { - throw new SpockException("Spy instance may not be another mock object."); - } - return createMockImpl(inferredName, uncheckedCast(instance.getClass()), instance, MockNature.SPY, MockImplementation.JAVA, singletonMap("useObjenesis", true), null, closure); - } - - T GroovyMockImpl(String inferredName, Class inferredType) { - return createMockImpl(inferredName, inferredType, MockNature.MOCK, MockImplementation.GROOVY, emptyMap(), null, null); - } - - T GroovyMockImpl(String inferredName, Class inferredType, @ClosureParams(SecondParam.FirstGenericType.class) Closure closure) { - return createMockImpl(inferredName, inferredType, MockNature.MOCK, MockImplementation.GROOVY, emptyMap(), null, closure); - } - - T GroovyMockImpl(String inferredName, Class inferredType, Map options) { - return createMockImpl(inferredName, inferredType, MockNature.MOCK, MockImplementation.GROOVY, options, null, null); - } - - T GroovyMockImpl(String inferredName, Class inferredType, Map options, Closure closure) { - return createMockImpl(inferredName, inferredType, MockNature.MOCK, MockImplementation.GROOVY, options, null, closure); - } - - T GroovyMockImpl(String inferredName, Class inferredType, Class specifiedType) { - return createMockImpl(inferredName, inferredType, MockNature.MOCK, MockImplementation.GROOVY, emptyMap(), specifiedType, null); - } - - T GroovyMockImpl(String inferredName, Class inferredType, Class specifiedType, Closure closure) { - return createMockImpl(inferredName, inferredType, MockNature.MOCK, MockImplementation.GROOVY, emptyMap(), specifiedType, closure); - } - - T GroovyMockImpl(String inferredName, Class inferredType, Map options, Class specifiedType) { - return createMockImpl(inferredName, inferredType, MockNature.MOCK, MockImplementation.GROOVY, options, specifiedType, null); - } - - T GroovyMockImpl(String inferredName, Class inferredType, Map options, Class specifiedType, Closure closure) { - return createMockImpl(inferredName, inferredType, MockNature.MOCK, MockImplementation.GROOVY, options, specifiedType, closure); - } - - T GroovyStubImpl(String inferredName, Class inferredType) { - return createMockImpl(inferredName, inferredType, MockNature.STUB, MockImplementation.GROOVY, emptyMap(), null, null); - } - - T GroovyStubImpl(String inferredName, Class inferredType, @ClosureParams(SecondParam.FirstGenericType.class) Closure closure) { - return createMockImpl(inferredName, inferredType, MockNature.STUB, MockImplementation.GROOVY, emptyMap(), null, closure); - } - - T GroovyStubImpl(String inferredName, Class inferredType, Map options) { - return createMockImpl(inferredName, inferredType, MockNature.STUB, MockImplementation.GROOVY, options, null, null); - } - - T GroovyStubImpl(String inferredName, Class inferredType, Map options, Closure closure) { - return createMockImpl(inferredName, inferredType, MockNature.STUB, MockImplementation.GROOVY, options, null, closure); - } - - T GroovyStubImpl(String inferredName, Class inferredType, Class specifiedType) { - return createMockImpl(inferredName, inferredType, MockNature.STUB, MockImplementation.GROOVY, emptyMap(), specifiedType, null); - } - - T GroovyStubImpl(String inferredName, Class inferredType, Class specifiedType, Closure closure) { - return createMockImpl(inferredName, inferredType, MockNature.STUB, MockImplementation.GROOVY, emptyMap(), specifiedType, closure); - } - - T GroovyStubImpl(String inferredName, Class inferredType, Map options, Class specifiedType) { - return createMockImpl(inferredName, inferredType, MockNature.STUB, MockImplementation.GROOVY, options, specifiedType, null); - } - - T GroovyStubImpl(String inferredName, Class inferredType, Map options, Class specifiedType, Closure closure) { - return createMockImpl(inferredName, inferredType, MockNature.STUB, MockImplementation.GROOVY, options, specifiedType, closure); - } - - T GroovySpyImpl(String inferredName, Class inferredType) { - return createMockImpl(inferredName, inferredType, MockNature.SPY, MockImplementation.GROOVY, emptyMap(), null, null); - } - - T GroovySpyImpl(String inferredName, Class inferredType, @ClosureParams(SecondParam.FirstGenericType.class) Closure closure) { - return createMockImpl(inferredName, inferredType, MockNature.SPY, MockImplementation.GROOVY, emptyMap(), null, closure); - } - - T GroovySpyImpl(String inferredName, Class inferredType, Map options) { - return createMockImpl(inferredName, inferredType, MockNature.SPY, MockImplementation.GROOVY, options, null, null); - } - - T GroovySpyImpl(String inferredName, Class inferredType, Map options, Closure closure) { - return createMockImpl(inferredName, inferredType, MockNature.SPY, MockImplementation.GROOVY, options, null, closure); - } - - T GroovySpyImpl(String inferredName, Class inferredType, Class specifiedType) { - return createMockImpl(inferredName, inferredType, MockNature.SPY, MockImplementation.GROOVY, emptyMap(), specifiedType, null); - } - - T GroovySpyImpl(String inferredName, Class inferredType, Class specifiedType, Closure closure) { - return createMockImpl(inferredName, inferredType, MockNature.SPY, MockImplementation.GROOVY, emptyMap(), specifiedType, closure); - } - - T GroovySpyImpl(String inferredName, Class inferredType, Map options, Class specifiedType) { - return createMockImpl(inferredName, inferredType, MockNature.SPY, MockImplementation.GROOVY, options, specifiedType, null); - } - - T GroovySpyImpl(String inferredName, Class inferredType, Map options, Class specifiedType, Closure closure) { - return createMockImpl(inferredName, inferredType, MockNature.SPY, MockImplementation.GROOVY, options, specifiedType, closure); - } - - T GroovySpyImpl(String inferredName, Class inferredType, T instance) { - return GroovySpyImpl(inferredName, inferredType, instance, null); - } - - T GroovySpyImpl(String inferredName, Class inferredType, T instance, Closure closure) { - if (instance == null) { - throw new SpockException("GroovySpy instance may not be null"); - } - if (MOCK_UTIL.isMock(instance)) { - throw new SpockException("GroovySpy instance may not be another mock object."); - } - return createMockImpl(inferredName, instance.getClass(), instance, MockNature.SPY, MockImplementation.GROOVY, singletonMap("useObjenesis", true), null, closure); - } - - private T createMockImpl(String inferredName, Class inferredType, MockNature nature, - MockImplementation implementation, Map options, Class specifiedType, Closure closure) { - return createMockImpl(inferredName, inferredType, null, nature, implementation, options, specifiedType, closure); - } - - private T createMockImpl(String inferredName, Class inferredType, T instance, MockNature nature, - MockImplementation implementation, Map options, Class specifiedType, Closure closure) { - Type effectiveType = specifiedType != null ? specifiedType : options.containsKey("type") ? (Type) options.get("type") : inferredType; - if (effectiveType == null) { - throw new InvalidSpecException(nature + " object type cannot be inferred automatically. " + - "Please specify a type explicitly (e.g. '" + nature + "(Person)')."); - } - return createMock(inferredName, instance, effectiveType, nature, implementation, options, closure); - } - - void SpyStaticImpl(String inferredName, Class inferredType, Class specifiedType) { - createStaticMockImpl(MockNature.SPY, specifiedType, null); - } - - void SpyStaticImpl(String inferredName, Class inferredType, Class specifiedType, IMockMakerSettings mockMakerSettings) { - createStaticMockImpl(MockNature.SPY, specifiedType, mockMakerSettings); - } - - private void createStaticMockImpl(MockNature nature, Class specifiedType, @Nullable IMockMakerSettings mockMakerSettings) { - if (specifiedType == null) { - throw new InvalidSpecException("The type must not be null."); - } - Map options = emptyMap(); - if (mockMakerSettings != null) { - options = Collections.singletonMap("mockMaker", mockMakerSettings); - } - createStaticMock(specifiedType, nature, options); - } -} diff --git a/spock-core/src/main/java/org/spockframework/mock/EmptyOrDummyResponse.java b/spock-core/src/main/java/org/spockframework/mock/EmptyOrDummyResponse.java index b788ef3b08..46d9573b5b 100644 --- a/spock-core/src/main/java/org/spockframework/mock/EmptyOrDummyResponse.java +++ b/spock-core/src/main/java/org/spockframework/mock/EmptyOrDummyResponse.java @@ -14,6 +14,7 @@ package org.spockframework.mock; +import org.spockframework.runtime.SpecInternals; import org.spockframework.runtime.extension.IDefaultValueProviderExtension; import org.spockframework.util.ReflectionUtil; import org.spockframework.util.ThreadSafe; @@ -160,7 +161,8 @@ private Object createDummy(IMockInvocation invocation) { Class type = invocation.getMethod().getReturnType(); Type genericType = invocation.getMethod().getExactReturnType(); Specification spec = invocation.getMockObject().getSpecification(); - return spec.createMock("dummy", genericType, MockNature.STUB, GroovyObject.class.isAssignableFrom(type) ? - MockImplementation.GROOVY : MockImplementation.JAVA, emptyMap(), null); + return GroovyObject.class.isAssignableFrom(type) + ? SpecInternals.GroovyStubImpl(spec, "dummy", genericType) + : SpecInternals.StubImpl(spec, "dummy", genericType); } } diff --git a/spock-core/src/main/java/org/spockframework/runtime/SpecInternals.java b/spock-core/src/main/java/org/spockframework/runtime/SpecInternals.java new file mode 100644 index 0000000000..82cd07d6ae --- /dev/null +++ b/spock-core/src/main/java/org/spockframework/runtime/SpecInternals.java @@ -0,0 +1,353 @@ +/* + * Copyright 2012 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * https://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.spockframework.runtime; + +import groovy.transform.stc.ClosureParams; +import groovy.transform.stc.SecondParam; +import groovy.transform.stc.ThirdParam; +import org.spockframework.mock.*; +import org.spockframework.mock.runtime.*; +import org.spockframework.util.*; +import spock.lang.Specification; + +import java.lang.reflect.Type; +import java.util.*; + +import groovy.lang.Closure; +import spock.mock.IMockMakerSettings; + +import static java.util.Collections.emptyMap; +import static java.util.Collections.singletonMap; +import static org.spockframework.util.ObjectUtil.uncheckedCast; + +/** + * Internal class for {@link Specification} implementation methods used by AST-transformed code. + * + *

This class is not intended for direct use. + * + * @since 2.4 + */ +@SuppressWarnings("UnusedDeclaration") +public final class SpecInternals { + private SpecInternals() { + } + + private static final MockUtil MOCK_UTIL = new MockUtil(); + + private static T createMock(Specification specification, @Nullable String name, Type type, MockNature nature, + MockImplementation implementation, Map options, @Nullable Closure closure) { + return createMock(specification, name, null, type, nature, implementation, options, closure); + } + + private static T createMock(Specification specification, @Nullable String name, T instance, Type type, MockNature nature, + MockImplementation implementation, Map options, @Nullable Closure closure) { + Object mock = CompositeMockFactory.INSTANCE.create( + new MockConfiguration(name, type, instance, nature, implementation, options), specification); + if (closure != null) { + GroovyRuntimeUtil.invokeClosure(closure, mock); + } + return uncheckedCast(mock); + } + + private static void createStaticMock(Specification specification, Type type, MockNature nature, + Map options) { + MockConfiguration configuration = new MockConfiguration(null, type, null, nature, MockImplementation.JAVA, options); + JavaMockFactory.INSTANCE.createStaticMock(configuration, specification); + } + + public static T oldImpl(T expression) { + return expression; + } + + public static T thrownImpl(Specification specification, String inferredName, Class inferredType) { + return inferredType.cast(checkExceptionThrown(specification, inferredType)); + } + + public static Throwable thrownImpl(Specification specification, String inferredName, Class inferredType, Class specifiedType) { + return checkExceptionThrown(specification, specifiedType == null ? inferredType : specifiedType); + } + + public static Throwable checkExceptionThrown(Specification specification, Class exceptionType) { + if (exceptionType == null) { + throw new InvalidSpecException("Thrown exception type cannot be inferred automatically. " + + "Please specify a type explicitly (e.g. 'thrown(MyException)')."); + } + + if (!Throwable.class.isAssignableFrom(exceptionType)) + throw new InvalidSpecException( + "Invalid exception condition: '%s' is not a (subclass of) java.lang.Throwable" + ).withArgs(exceptionType.getSimpleName()); + + SpecificationContext specificationContext = uncheckedCast(specification.getSpecificationContext()); + Throwable actual = specificationContext.getThrownException(); + if (exceptionType.isInstance(actual)) return actual; + + throw new WrongExceptionThrownError(exceptionType, actual); + } + + public static T MockImpl(Specification specification, String inferredName, Type inferredType) { + return createMockImpl(specification, inferredName, inferredType, MockNature.MOCK, MockImplementation.JAVA, emptyMap(), null, null); + } + + public static T MockImpl(Specification specification, String inferredName, Type inferredType, @ClosureParams(SecondParam.FirstGenericType.class) Closure closure) { + return createMockImpl(specification, inferredName, inferredType, MockNature.MOCK, MockImplementation.JAVA, emptyMap(), null, closure); + } + + public static T MockImpl(Specification specification, String inferredName, Type inferredType, Map options) { + return createMockImpl(specification, inferredName, inferredType, MockNature.MOCK, MockImplementation.JAVA, options, null, null); + } + + public static T MockImpl(Specification specification, String inferredName, Type inferredType, Map options, Closure closure) { + return createMockImpl(specification, inferredName, inferredType, MockNature.MOCK, MockImplementation.JAVA, options, null, closure); + } + + public static T MockImpl(Specification specification, String inferredName, Type inferredType, Class specifiedType) { + return createMockImpl(specification, inferredName, inferredType, MockNature.MOCK, MockImplementation.JAVA, emptyMap(), specifiedType, null); + } + + public static T MockImpl(Specification specification, String inferredName, Type inferredType, Class specifiedType, Closure closure) { + return createMockImpl(specification, inferredName, inferredType, MockNature.MOCK, MockImplementation.JAVA, emptyMap(), specifiedType, closure); + } + + public static T MockImpl(Specification specification, String inferredName, Type inferredType, Map options, Class specifiedType) { + return createMockImpl(specification, inferredName, inferredType, MockNature.MOCK, MockImplementation.JAVA, options, specifiedType, null); + } + + public static T MockImpl(Specification specification, String inferredName, Type inferredType, Map options, Class specifiedType, Closure closure) { + return createMockImpl(specification, inferredName, inferredType, MockNature.MOCK, MockImplementation.JAVA, options, specifiedType, closure); + } + + public static T StubImpl(Specification specification, String inferredName, Type inferredType) { + return createMockImpl(specification, inferredName, inferredType, MockNature.STUB, MockImplementation.JAVA, emptyMap(), null, null); + } + + public static T StubImpl(Specification specification, String inferredName, Type inferredType, @ClosureParams(SecondParam.FirstGenericType.class) Closure closure) { + return createMockImpl(specification, inferredName, inferredType, MockNature.STUB, MockImplementation.JAVA, emptyMap(), null, closure); + } + + public static T StubImpl(Specification specification, String inferredName, Type inferredType, Map options) { + return createMockImpl(specification, inferredName, inferredType, MockNature.STUB, MockImplementation.JAVA, options, null, null); + } + + public static T StubImpl(Specification specification, String inferredName, Type inferredType, Map options, Closure closure) { + return createMockImpl(specification, inferredName, inferredType, MockNature.STUB, MockImplementation.JAVA, options, null, closure); + } + + public static T StubImpl(Specification specification, String inferredName, Type inferredType, Class specifiedType) { + return createMockImpl(specification, inferredName, inferredType, MockNature.STUB, MockImplementation.JAVA, emptyMap(), specifiedType, null); + } + + public static T StubImpl(Specification specification, String inferredName, Type inferredType, Class specifiedType, Closure closure) { + return createMockImpl(specification, inferredName, inferredType, MockNature.STUB, MockImplementation.JAVA, emptyMap(), specifiedType, closure); + } + + public static T StubImpl(Specification specification, String inferredName, Type inferredType, Map options, Class specifiedType) { + return createMockImpl(specification, inferredName, inferredType, MockNature.STUB, MockImplementation.JAVA, options, specifiedType, null); + } + + public static T StubImpl(Specification specification, String inferredName, Type inferredType, Map options, Class specifiedType, Closure closure) { + return createMockImpl(specification, inferredName, inferredType, MockNature.STUB, MockImplementation.JAVA, options, specifiedType, closure); + } + + public static T SpyImpl(Specification specification, String inferredName, Type inferredType) { + return createMockImpl(specification, inferredName, inferredType, MockNature.SPY, MockImplementation.JAVA, emptyMap(), null, null); + } + + public static T SpyImpl(Specification specification, String inferredName, Type inferredType, @ClosureParams(SecondParam.FirstGenericType.class) Closure closure) { + return createMockImpl(specification, inferredName, inferredType, MockNature.SPY, MockImplementation.JAVA, emptyMap(), null, closure); + } + + public static T SpyImpl(Specification specification, String inferredName, Type inferredType, Map options) { + return createMockImpl(specification, inferredName, inferredType, MockNature.SPY, MockImplementation.JAVA, options, null, null); + } + + public static T SpyImpl(Specification specification, String inferredName, Type inferredType, Map options, Closure closure) { + return createMockImpl(specification, inferredName, inferredType, MockNature.SPY, MockImplementation.JAVA, options, null, closure); + } + + public static T SpyImpl(Specification specification, String inferredName, Type inferredType, Class specifiedType) { + return createMockImpl(specification, inferredName, inferredType, MockNature.SPY, MockImplementation.JAVA, emptyMap(), specifiedType, null); + } + + public static T SpyImpl(Specification specification, String inferredName, Type inferredType, Class specifiedType, Closure closure) { + return createMockImpl(specification, inferredName, inferredType, MockNature.SPY, MockImplementation.JAVA, emptyMap(), specifiedType, closure); + } + + public static T SpyImpl(Specification specification, String inferredName, Type inferredType, Map options, Class specifiedType) { + return createMockImpl(specification, inferredName, inferredType, MockNature.SPY, MockImplementation.JAVA, options, specifiedType, null); + } + + public static T SpyImpl(Specification specification, String inferredName, Type inferredType, Map options, Class specifiedType, Closure closure) { + return createMockImpl(specification, inferredName, inferredType, MockNature.SPY, MockImplementation.JAVA, options, specifiedType, closure); + } + + public static T SpyImpl(Specification specification, String inferredName, Type inferredType, T instance) { + return SpyImpl(specification, inferredName, inferredType, instance, null); + } + + public static T SpyImpl(Specification specification, String inferredName, Type inferredType, T instance, @ClosureParams(ThirdParam.class) Closure closure) { + if (instance == null) { + throw new SpockException("Spy instance may not be null"); + } + if (MOCK_UTIL.isMock(instance)) { + throw new SpockException("Spy instance may not be another mock object."); + } + return createMockImpl(specification, inferredName, uncheckedCast(instance.getClass()), instance, MockNature.SPY, MockImplementation.JAVA, singletonMap("useObjenesis", true), null, closure); + } + + public static T GroovyMockImpl(Specification specification, String inferredName, Type inferredType) { + return createMockImpl(specification, inferredName, inferredType, MockNature.MOCK, MockImplementation.GROOVY, emptyMap(), null, null); + } + + public static T GroovyMockImpl(Specification specification, String inferredName, Type inferredType, @ClosureParams(SecondParam.FirstGenericType.class) Closure closure) { + return createMockImpl(specification, inferredName, inferredType, MockNature.MOCK, MockImplementation.GROOVY, emptyMap(), null, closure); + } + + public static T GroovyMockImpl(Specification specification, String inferredName, Type inferredType, Map options) { + return createMockImpl(specification, inferredName, inferredType, MockNature.MOCK, MockImplementation.GROOVY, options, null, null); + } + + public static T GroovyMockImpl(Specification specification, String inferredName, Type inferredType, Map options, Closure closure) { + return createMockImpl(specification, inferredName, inferredType, MockNature.MOCK, MockImplementation.GROOVY, options, null, closure); + } + + public static T GroovyMockImpl(Specification specification, String inferredName, Type inferredType, Class specifiedType) { + return createMockImpl(specification, inferredName, inferredType, MockNature.MOCK, MockImplementation.GROOVY, emptyMap(), specifiedType, null); + } + + public static T GroovyMockImpl(Specification specification, String inferredName, Type inferredType, Class specifiedType, Closure closure) { + return createMockImpl(specification, inferredName, inferredType, MockNature.MOCK, MockImplementation.GROOVY, emptyMap(), specifiedType, closure); + } + + public static T GroovyMockImpl(Specification specification, String inferredName, Type inferredType, Map options, Class specifiedType) { + return createMockImpl(specification, inferredName, inferredType, MockNature.MOCK, MockImplementation.GROOVY, options, specifiedType, null); + } + + public static T GroovyMockImpl(Specification specification, String inferredName, Type inferredType, Map options, Class specifiedType, Closure closure) { + return createMockImpl(specification, inferredName, inferredType, MockNature.MOCK, MockImplementation.GROOVY, options, specifiedType, closure); + } + + public static T GroovyStubImpl(Specification specification, String inferredName, Type inferredType) { + return createMockImpl(specification, inferredName, inferredType, MockNature.STUB, MockImplementation.GROOVY, emptyMap(), null, null); + } + + public static T GroovyStubImpl(Specification specification, String inferredName, Type inferredType, @ClosureParams(SecondParam.FirstGenericType.class) Closure closure) { + return createMockImpl(specification, inferredName, inferredType, MockNature.STUB, MockImplementation.GROOVY, emptyMap(), null, closure); + } + + public static T GroovyStubImpl(Specification specification, String inferredName, Type inferredType, Map options) { + return createMockImpl(specification, inferredName, inferredType, MockNature.STUB, MockImplementation.GROOVY, options, null, null); + } + + public static T GroovyStubImpl(Specification specification, String inferredName, Type inferredType, Map options, Closure closure) { + return createMockImpl(specification, inferredName, inferredType, MockNature.STUB, MockImplementation.GROOVY, options, null, closure); + } + + public static T GroovyStubImpl(Specification specification, String inferredName, Type inferredType, Class specifiedType) { + return createMockImpl(specification, inferredName, inferredType, MockNature.STUB, MockImplementation.GROOVY, emptyMap(), specifiedType, null); + } + + public static T GroovyStubImpl(Specification specification, String inferredName, Type inferredType, Class specifiedType, Closure closure) { + return createMockImpl(specification, inferredName, inferredType, MockNature.STUB, MockImplementation.GROOVY, emptyMap(), specifiedType, closure); + } + + public static T GroovyStubImpl(Specification specification, String inferredName, Type inferredType, Map options, Class specifiedType) { + return createMockImpl(specification, inferredName, inferredType, MockNature.STUB, MockImplementation.GROOVY, options, specifiedType, null); + } + + public static T GroovyStubImpl(Specification specification, String inferredName, Type inferredType, Map options, Class specifiedType, Closure closure) { + return createMockImpl(specification, inferredName, inferredType, MockNature.STUB, MockImplementation.GROOVY, options, specifiedType, closure); + } + + public static T GroovySpyImpl(Specification specification, String inferredName, Type inferredType) { + return createMockImpl(specification, inferredName, inferredType, MockNature.SPY, MockImplementation.GROOVY, emptyMap(), null, null); + } + + public static T GroovySpyImpl(Specification specification, String inferredName, Type inferredType, @ClosureParams(SecondParam.FirstGenericType.class) Closure closure) { + return createMockImpl(specification, inferredName, inferredType, MockNature.SPY, MockImplementation.GROOVY, emptyMap(), null, closure); + } + + public static T GroovySpyImpl(Specification specification, String inferredName, Type inferredType, Map options) { + return createMockImpl(specification, inferredName, inferredType, MockNature.SPY, MockImplementation.GROOVY, options, null, null); + } + + public static T GroovySpyImpl(Specification specification, String inferredName, Type inferredType, Map options, Closure closure) { + return createMockImpl(specification, inferredName, inferredType, MockNature.SPY, MockImplementation.GROOVY, options, null, closure); + } + + public static T GroovySpyImpl(Specification specification, String inferredName, Type inferredType, Class specifiedType) { + return createMockImpl(specification, inferredName, inferredType, MockNature.SPY, MockImplementation.GROOVY, emptyMap(), specifiedType, null); + } + + public static T GroovySpyImpl(Specification specification, String inferredName, Type inferredType, Class specifiedType, Closure closure) { + return createMockImpl(specification, inferredName, inferredType, MockNature.SPY, MockImplementation.GROOVY, emptyMap(), specifiedType, closure); + } + + public static T GroovySpyImpl(Specification specification, String inferredName, Type inferredType, Map options, Class specifiedType) { + return createMockImpl(specification, inferredName, inferredType, MockNature.SPY, MockImplementation.GROOVY, options, specifiedType, null); + } + + public static T GroovySpyImpl(Specification specification, String inferredName, Type inferredType, Map options, Class specifiedType, Closure closure) { + return createMockImpl(specification, inferredName, inferredType, MockNature.SPY, MockImplementation.GROOVY, options, specifiedType, closure); + } + + public static T GroovySpyImpl(Specification specification, String inferredName, Type inferredType, T instance) { + return GroovySpyImpl(specification, inferredName, inferredType, instance, null); + } + + public static T GroovySpyImpl(Specification specification, String inferredName, Type inferredType, T instance, Closure closure) { + if (instance == null) { + throw new SpockException("GroovySpy instance may not be null"); + } + if (MOCK_UTIL.isMock(instance)) { + throw new SpockException("GroovySpy instance may not be another mock object."); + } + return createMockImpl(specification, inferredName, instance.getClass(), instance, MockNature.SPY, MockImplementation.GROOVY, singletonMap("useObjenesis", true), null, closure); + } + + private static T createMockImpl(Specification specification, String inferredName, Type inferredType, MockNature nature, + MockImplementation implementation, Map options, Class specifiedType, Closure closure) { + return createMockImpl(specification, inferredName, inferredType, null, nature, implementation, options, specifiedType, closure); + } + + private static T createMockImpl(Specification specification, String inferredName, Type inferredType, T instance, MockNature nature, + MockImplementation implementation, Map options, Class specifiedType, Closure closure) { + Type effectiveType = specifiedType != null ? specifiedType : options.containsKey("type") ? (Type) options.get("type") : inferredType; + if (effectiveType == null) { + throw new InvalidSpecException(nature + " object type cannot be inferred automatically. " + + "Please specify a type explicitly (e.g. '" + nature + "(Person)')."); + } + return createMock(specification, inferredName, instance, effectiveType, nature, implementation, options, closure); + } + + public static void SpyStaticImpl(Specification specification, String inferredName, Type inferredType, Class specifiedType) { + createStaticMockImpl(specification, MockNature.SPY, specifiedType, null); + } + + public static void SpyStaticImpl(Specification specification, String inferredName, Type inferredType, Class specifiedType, IMockMakerSettings mockMakerSettings) { + createStaticMockImpl(specification, MockNature.SPY, specifiedType, mockMakerSettings); + } + + private static void createStaticMockImpl(Specification specification, MockNature nature, Class specifiedType, @Nullable IMockMakerSettings mockMakerSettings) { + if (specifiedType == null) { + throw new InvalidSpecException("The type must not be null."); + } + Map options = emptyMap(); + if (mockMakerSettings != null) { + options = Collections.singletonMap("mockMaker", mockMakerSettings); + } + createStaticMock(specification, specifiedType, nature, options); + } +} diff --git a/spock-core/src/main/java/spock/lang/Specification.java b/spock-core/src/main/java/spock/lang/Specification.java index 396ab4d87b..9aa0a3d48d 100644 --- a/spock-core/src/main/java/spock/lang/Specification.java +++ b/spock-core/src/main/java/spock/lang/Specification.java @@ -19,6 +19,7 @@ import groovy.lang.DelegatesTo; import groovy.transform.stc.*; import org.junit.platform.commons.annotation.Testable; +import org.spockframework.lang.ISpecificationContext; import org.spockframework.lang.Wildcard; import org.spockframework.runtime.*; import org.spockframework.util.Beta; @@ -48,6 +49,17 @@ public abstract class Specification extends MockingApi { */ public static final Object _ = Wildcard.INSTANCE; + private final ISpecificationContext specificationContext = new SpecificationContext(); + + /** + * Returns the current execution context of this specification. + * This is mostly used internally, but could, for example, be used to log the current iteration. + */ + @Beta + public ISpecificationContext getSpecificationContext() { + return specificationContext; + } + /** * Specifies that the preceding when block should throw an exception. * May only occur as the initializer expression of a typed variable declaration diff --git a/spock-core/src/main/java/spock/mock/MockingApi.java b/spock-core/src/main/java/spock/mock/MockingApi.java index 765cb51778..b4d081638c 100644 --- a/spock-core/src/main/java/spock/mock/MockingApi.java +++ b/spock-core/src/main/java/spock/mock/MockingApi.java @@ -22,11 +22,12 @@ import groovy.transform.stc.ClosureParams; import groovy.transform.stc.FirstParam; import groovy.transform.stc.SecondParam; -import org.spockframework.lang.SpecInternals; +import spock.lang.Specification; import org.spockframework.mock.*; import org.spockframework.runtime.GroovyRuntimeUtil; import org.spockframework.runtime.InvalidSpecException; import org.spockframework.util.Beta; +import org.spockframework.util.ObjectUtil; import groovy.lang.Closure; @@ -76,7 +77,7 @@ * */ @SuppressWarnings({"unused", "SameReturnValue"}) -public class MockingApi extends SpecInternals implements MockFactory { +public class MockingApi implements MockFactory { /** * Encloses one or more interaction definitions in a then block. * Required when an interaction definition uses a statement that doesn't @@ -1672,7 +1673,7 @@ public void SpyStatic(Class type, IMockMakerSettings mockMakerSettings) { * @param code the code to execute */ public void runWithThreadAwareMocks(Runnable code) { - getSpecificationContext().getThreadAwareMockController().runWithThreadAwareMocks(code); + ObjectUtil.uncheckedCast(this).getSpecificationContext().getThreadAwareMockController().runWithThreadAwareMocks(code); } /** @@ -1686,7 +1687,7 @@ public void runWithThreadAwareMocks(Runnable code) { * @return the return value of the executed code */ public R withActiveThreadAwareMocks(Callable code) { - return getSpecificationContext().getThreadAwareMockController().withActiveThreadAwareMocks(code); + return ObjectUtil.uncheckedCast(this).getSpecificationContext().getThreadAwareMockController().withActiveThreadAwareMocks(code); } private InvalidSpecException invalidMockCreation() { diff --git a/spock-specs/src/test/groovy/org/spockframework/smoke/StackTraceFiltering.groovy b/spock-specs/src/test/groovy/org/spockframework/smoke/StackTraceFiltering.groovy index ca6aac043e..f5876178dc 100644 --- a/spock-specs/src/test/groovy/org/spockframework/smoke/StackTraceFiltering.groovy +++ b/spock-specs/src/test/groovy/org/spockframework/smoke/StackTraceFiltering.groovy @@ -302,8 +302,6 @@ thrown(RuntimeException) WrongExceptionThrownError e = thrown() stackTraceLooksLike e, """ -org.spockframework.lang.SpecInternals|checkExceptionThrown|- -org.spockframework.lang.SpecInternals|thrownImpl|- apackage.ASpec|a feature|5 """ diff --git a/spock-specs/src/test/resources/snapshots/org/spockframework/smoke/ast/AstSpec/Primitive_types_are_used_in_AST_transformation-groovy4.txt b/spock-specs/src/test/resources/snapshots/org/spockframework/smoke/ast/AstSpec/Primitive_types_are_used_in_AST_transformation-groovy4.txt index 4a58402510..308dd49922 100644 --- a/spock-specs/src/test/resources/snapshots/org/spockframework/smoke/ast/AstSpec/Primitive_types_are_used_in_AST_transformation-groovy4.txt +++ b/spock-specs/src/test/resources/snapshots/org/spockframework/smoke/ast/AstSpec/Primitive_types_are_used_in_AST_transformation-groovy4.txt @@ -114,7 +114,7 @@ public class apackage/TestSpec extends spock/lang/Specification implements groov ACONST_NULL POP ALOAD 0 - INVOKEVIRTUAL org/spockframework/lang/SpecInternals.getSpecificationContext ()Lorg/spockframework/lang/ISpecificationContext; + INVOKEVIRTUAL spock/lang/Specification.getSpecificationContext ()Lorg/spockframework/lang/ISpecificationContext; INVOKEDYNAMIC cast(Lorg/spockframework/lang/ISpecificationContext;)Lorg/spockframework/runtime/SpecificationContext; [ // handle kind 0x6 : INVOKESTATIC org/codehaus/groovy/vmplugin/v8/IndyInterface.bootstrap(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/String;I)Ljava/lang/invoke/CallSite; @@ -136,7 +136,7 @@ public class apackage/TestSpec extends spock/lang/Specification implements groov ASTORE 5 L15 ALOAD 0 - INVOKEVIRTUAL org/spockframework/lang/SpecInternals.getSpecificationContext ()Lorg/spockframework/lang/ISpecificationContext; + INVOKEVIRTUAL spock/lang/Specification.getSpecificationContext ()Lorg/spockframework/lang/ISpecificationContext; INVOKEDYNAMIC cast(Lorg/spockframework/lang/ISpecificationContext;)Lorg/spockframework/runtime/SpecificationContext; [ // handle kind 0x6 : INVOKESTATIC org/codehaus/groovy/vmplugin/v8/IndyInterface.bootstrap(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/String;I)Ljava/lang/invoke/CallSite; @@ -173,16 +173,17 @@ public class apackage/TestSpec extends spock/lang/Specification implements groov POP L17 LINENUMBER 8 L17 + LDC Lorg/spockframework/runtime/SpecInternals;.class ALOAD 0 ACONST_NULL ACONST_NULL LDC Ljava/lang/RuntimeException;.class - INVOKEDYNAMIC invoke(Lapackage/TestSpec;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Class;)Ljava/lang/Object; [ + INVOKEDYNAMIC invoke(Ljava/lang/Class;Lapackage/TestSpec;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Class;)Ljava/lang/Object; [ // handle kind 0x6 : INVOKESTATIC org/codehaus/groovy/vmplugin/v8/IndyInterface.bootstrap(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/String;I)Ljava/lang/invoke/CallSite; // arguments: "thrownImpl", - 2 + 0 ] POP ALOAD 0 @@ -191,7 +192,7 @@ public class apackage/TestSpec extends spock/lang/Specification implements groov ACONST_NULL POP ALOAD 0 - INVOKEVIRTUAL org/spockframework/lang/SpecInternals.getSpecificationContext ()Lorg/spockframework/lang/ISpecificationContext; + INVOKEVIRTUAL spock/lang/Specification.getSpecificationContext ()Lorg/spockframework/lang/ISpecificationContext; INVOKEDYNAMIC cast(Lorg/spockframework/lang/ISpecificationContext;)Lorg/spockframework/runtime/SpecificationContext; [ // handle kind 0x6 : INVOKESTATIC org/codehaus/groovy/vmplugin/v8/IndyInterface.bootstrap(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/String;I)Ljava/lang/invoke/CallSite; diff --git a/spock-specs/src/test/resources/snapshots/org/spockframework/smoke/ast/AstSpec/Primitive_types_are_used_in_AST_transformation.txt b/spock-specs/src/test/resources/snapshots/org/spockframework/smoke/ast/AstSpec/Primitive_types_are_used_in_AST_transformation.txt index a1027835f7..55ebf848a5 100644 --- a/spock-specs/src/test/resources/snapshots/org/spockframework/smoke/ast/AstSpec/Primitive_types_are_used_in_AST_transformation.txt +++ b/spock-specs/src/test/resources/snapshots/org/spockframework/smoke/ast/AstSpec/Primitive_types_are_used_in_AST_transformation.txt @@ -103,7 +103,7 @@ public class apackage/TestSpec extends spock/lang/Specification implements groov ACONST_NULL POP ALOAD 0 - INVOKEVIRTUAL org/spockframework/lang/SpecInternals.getSpecificationContext ()Lorg/spockframework/lang/ISpecificationContext; + INVOKEVIRTUAL spock/lang/Specification.getSpecificationContext ()Lorg/spockframework/lang/ISpecificationContext; LDC Lorg/spockframework/runtime/SpecificationContext;.class INVOKESTATIC org/codehaus/groovy/runtime/ScriptBytecodeAdapter.castToType (Ljava/lang/Object;Ljava/lang/Class;)Ljava/lang/Object; CHECKCAST org/spockframework/runtime/SpecificationContext @@ -124,7 +124,7 @@ public class apackage/TestSpec extends spock/lang/Specification implements groov ASTORE 6 L15 ALOAD 0 - INVOKEVIRTUAL org/spockframework/lang/SpecInternals.getSpecificationContext ()Lorg/spockframework/lang/ISpecificationContext; + INVOKEVIRTUAL spock/lang/Specification.getSpecificationContext ()Lorg/spockframework/lang/ISpecificationContext; LDC Lorg/spockframework/runtime/SpecificationContext;.class INVOKESTATIC org/codehaus/groovy/runtime/ScriptBytecodeAdapter.castToType (Ljava/lang/Object;Ljava/lang/Class;)Ljava/lang/Object; CHECKCAST org/spockframework/runtime/SpecificationContext @@ -160,11 +160,12 @@ public class apackage/TestSpec extends spock/lang/Specification implements groov ALOAD 1 LDC 2 AALOAD + LDC Lorg/spockframework/runtime/SpecInternals;.class ALOAD 0 ACONST_NULL ACONST_NULL LDC Ljava/lang/RuntimeException;.class - INVOKEINTERFACE org/codehaus/groovy/runtime/callsite/CallSite.callCurrent (Lgroovy/lang/GroovyObject;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; (itf) + INVOKEINTERFACE org/codehaus/groovy/runtime/callsite/CallSite.call (Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; (itf) POP ALOAD 0 ICONST_2 @@ -172,7 +173,7 @@ public class apackage/TestSpec extends spock/lang/Specification implements groov ACONST_NULL POP ALOAD 0 - INVOKEVIRTUAL org/spockframework/lang/SpecInternals.getSpecificationContext ()Lorg/spockframework/lang/ISpecificationContext; + INVOKEVIRTUAL spock/lang/Specification.getSpecificationContext ()Lorg/spockframework/lang/ISpecificationContext; LDC Lorg/spockframework/runtime/SpecificationContext;.class INVOKESTATIC org/codehaus/groovy/runtime/ScriptBytecodeAdapter.castToType (Ljava/lang/Object;Ljava/lang/Class;)Ljava/lang/Object; CHECKCAST org/spockframework/runtime/SpecificationContext diff --git a/spock-specs/src/test/resources/snapshots/org/spockframework/smoke/ast/condition/ExceptionConditionsAstSpec/thrown_rewrite_keeps_correct_method_reference.groovy b/spock-specs/src/test/resources/snapshots/org/spockframework/smoke/ast/condition/ExceptionConditionsAstSpec/thrown_rewrite_keeps_correct_method_reference.groovy index 8977bec80f..6cd5d1d06a 100644 --- a/spock-specs/src/test/resources/snapshots/org/spockframework/smoke/ast/condition/ExceptionConditionsAstSpec/thrown_rewrite_keeps_correct_method_reference.groovy +++ b/spock-specs/src/test/resources/snapshots/org/spockframework/smoke/ast/condition/ExceptionConditionsAstSpec/thrown_rewrite_keeps_correct_method_reference.groovy @@ -21,7 +21,7 @@ public void $spock_feature_0_0() { } org.spockframework.runtime.SpockRuntime.callBlockExited(this, 0) org.spockframework.runtime.SpockRuntime.callBlockEntered(this, 1) - this.thrownImpl(null, null, java.lang.IllegalStateException) + org.spockframework.runtime.SpecInternals.thrownImpl(this, null, null, java.lang.IllegalStateException) org.spockframework.runtime.SpockRuntime.callBlockExited(this, 1) this.getSpecificationContext().getMockController().leaveScope() } diff --git a/spock-specs/src/test/resources/snapshots/org/spockframework/smoke/ast/condition/ExceptionConditionsAstSpec/thrown_rewrite_keeps_correct_method_reference_for_multi_assignments.groovy b/spock-specs/src/test/resources/snapshots/org/spockframework/smoke/ast/condition/ExceptionConditionsAstSpec/thrown_rewrite_keeps_correct_method_reference_for_multi_assignments.groovy index 0a302a10ce..6bf8c68aec 100644 --- a/spock-specs/src/test/resources/snapshots/org/spockframework/smoke/ast/condition/ExceptionConditionsAstSpec/thrown_rewrite_keeps_correct_method_reference_for_multi_assignments.groovy +++ b/spock-specs/src/test/resources/snapshots/org/spockframework/smoke/ast/condition/ExceptionConditionsAstSpec/thrown_rewrite_keeps_correct_method_reference_for_multi_assignments.groovy @@ -21,7 +21,7 @@ public void $spock_feature_0_0() { } org.spockframework.runtime.SpockRuntime.callBlockExited(this, 0) org.spockframework.runtime.SpockRuntime.callBlockEntered(this, 1) - this.thrownImpl(null, null, java.lang.IllegalStateException) + org.spockframework.runtime.SpecInternals.thrownImpl(this, null, null, java.lang.IllegalStateException) org.spockframework.runtime.SpockRuntime.callBlockExited(this, 1) this.getSpecificationContext().getMockController().leaveScope() } diff --git a/spock-specs/src/test/resources/snapshots/org/spockframework/smoke/ast/mock/MocksAstSpec/simple_interaction.groovy b/spock-specs/src/test/resources/snapshots/org/spockframework/smoke/ast/mock/MocksAstSpec/simple_interaction.groovy index d1e0a23ccd..2b63ee3b75 100644 --- a/spock-specs/src/test/resources/snapshots/org/spockframework/smoke/ast/mock/MocksAstSpec/simple_interaction.groovy +++ b/spock-specs/src/test/resources/snapshots/org/spockframework/smoke/ast/mock/MocksAstSpec/simple_interaction.groovy @@ -7,7 +7,7 @@ class ASpec extends Specification { @org.spockframework.runtime.model.FeatureMetadata(name = 'a feature', ordinal = 0, line = 1, blocks = [@org.spockframework.runtime.model.BlockMetadata(kind = org.spockframework.runtime.model.BlockKind.SETUP, texts = []), @org.spockframework.runtime.model.BlockMetadata(kind = org.spockframework.runtime.model.BlockKind.WHEN, texts = []), @org.spockframework.runtime.model.BlockMetadata(kind = org.spockframework.runtime.model.BlockKind.THEN, texts = [])], parameterNames = []) public void $spock_feature_0_0() { org.spockframework.runtime.SpockRuntime.callBlockEntered(this, 0) - java.util.List list = this.MockImpl('list', java.util.List) + java.util.List list = org.spockframework.runtime.SpecInternals.MockImpl(this, 'list', java.util.List) this.getSpecificationContext().getMockController().enterScope() this.getSpecificationContext().getMockController().addInteraction(new org.spockframework.mock.runtime.InteractionBuilder(8, 5, '1 * list.add(1)').setFixedCount(1).addEqualTarget(list).addEqualMethodName('add').setArgListKind(true, false).addEqualArg(1).build()) org.spockframework.runtime.SpockRuntime.callBlockExited(this, 0)