Skip to content

Commit

Permalink
Reuse delegation instances (#1233)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphw authored and joel-costigliola committed Apr 22, 2018
1 parent b76fccc commit 6cb9e4c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/main/java/org/assertj/core/api/Assumptions.java
Expand Up @@ -66,6 +66,7 @@
import java.util.stream.LongStream;
import java.util.stream.Stream;

import net.bytebuddy.implementation.Implementation;
import org.assertj.core.api.ThrowableAssert.ThrowingCallable;
import org.assertj.core.util.CheckReturnValue;

Expand Down Expand Up @@ -93,6 +94,8 @@ public class Assumptions {
private static ByteBuddy BYTE_BUDDY = new ByteBuddy().with(TypeValidation.DISABLED)
.with(new AuxiliaryType.NamingStrategy.SuffixingRandom("Assertj$Assumptions"));

private static final Implementation ASSUMPTION = MethodDelegation.to(AssumptionMethodInterceptor.class);

private static final TypeCache<TypeCache.SimpleKey> CACHE = new TypeCache.WithInlineExpunction<>(Sort.SOFT);

private static final class AssumptionMethodInterceptor {
Expand Down Expand Up @@ -1166,7 +1169,7 @@ protected static <ASSERTION> Class<? extends ASSERTION> generateAssumptionClass(
return BYTE_BUDDY.subclass(assertionType)
// TODO ignore non assertion methods ?
.method(any())
.intercept(MethodDelegation.to(AssumptionMethodInterceptor.class))
.intercept(ASSUMPTION)
.make()
.load(Assumptions.class.getClassLoader(), classLoadingStrategy(assertionType))
.getLoaded();
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/org/assertj/core/api/SoftProxies.java
Expand Up @@ -29,6 +29,7 @@
import net.bytebuddy.description.modifier.Visibility;
import net.bytebuddy.dynamic.scaffold.TypeValidation;
import net.bytebuddy.implementation.FieldAccessor;
import net.bytebuddy.implementation.Implementation;
import net.bytebuddy.implementation.MethodDelegation;
import net.bytebuddy.implementation.auxiliary.AuxiliaryType;
import net.bytebuddy.matcher.ElementMatcher.Junction;
Expand Down Expand Up @@ -72,6 +73,9 @@ class SoftProxies {
private static final ByteBuddy BYTE_BUDDY = new ByteBuddy().with(new AuxiliaryType.NamingStrategy.SuffixingRandom("AssertJ$SoftProxies"))
.with(TypeValidation.DISABLED);

private static final Implementation PROXIFY_METHOD = MethodDelegation.to(ProxifyMethodChangingTheObjectUnderTest.class);
private static final Implementation ERROR_COLLECTOR = MethodDelegation.to(ErrorCollector.class);

private static final TypeCache<TypeCache.SimpleKey> CACHE = new TypeCache.WithInlineExpunction<>(Sort.SOFT);

private final ErrorCollector collector = new ErrorCollector();
Expand Down Expand Up @@ -140,11 +144,11 @@ static <V> Class<? extends V> generateProxyClass(Class<V> assertClass) {
ProxifyMethodChangingTheObjectUnderTest.class,
Visibility.PRIVATE)
.method(METHODS_CHANGING_THE_OBJECT_UNDER_TEST)
.intercept(MethodDelegation.to(ProxifyMethodChangingTheObjectUnderTest.class))
.intercept(PROXIFY_METHOD)
.defineField(ErrorCollector.FIELD_NAME, ErrorCollector.class, Visibility.PRIVATE)
.method(any().and(not(METHODS_CHANGING_THE_OBJECT_UNDER_TEST))
.and(not(METHODS_NOT_TO_PROXY)))
.intercept(MethodDelegation.to(ErrorCollector.class))
.intercept(ERROR_COLLECTOR)
.implement(AssertJProxySetup.class)
// set ProxifyMethodChangingTheObjectUnderTest and ErrorCollector fields on the generated proxy
.intercept(FieldAccessor.ofField(ProxifyMethodChangingTheObjectUnderTest.FIELD_NAME).setsArgumentAt(0)
Expand Down

0 comments on commit 6cb9e4c

Please sign in to comment.