Skip to content

Commit 2f4bd1b

Browse files
committed
fixup! Replace ByteBuddy proxying with direct soft assertion support
1 parent f3d5beb commit 2f4bd1b

16 files changed

Lines changed: 75 additions & 116 deletions

assertj-core/src/main/java/org/assertj/core/api/AbstractAssert.java

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,9 +1191,7 @@ public SELF doesNotHaveSameHashCodeAs(Object other) {
11911191
* @return a new {@link AbstractListAssert}.
11921192
*/
11931193
protected <E> AbstractListAssert<?, List<? extends E>, E, ObjectAssert<E>> newListAssertInstance(List<? extends E> newActual) {
1194-
ListAssert<E> listAssert = new ListAssert<>(newActual);
1195-
listAssert.withAssertionState(myself);
1196-
return listAssert;
1194+
return new ListAssert<>(newActual);
11971195
}
11981196

11991197
SELF withAssertionState(@SuppressWarnings("rawtypes") AbstractAssert assertInstance) {
@@ -1205,16 +1203,8 @@ SELF withAssertionState(@SuppressWarnings("rawtypes") AbstractAssert assertInsta
12051203

12061204
private void propagateAssertionInfoFrom(AbstractAssert<?, ?> assertInstance) {
12071205
this.info.useRepresentation(assertInstance.info.representation());
1208-
if (assertInstance.info.overridingErrorMessage() != null) {
1209-
// When overridingErrorMessage is set, always propagate both it and the description
1210-
// (even if null) so the overridingErrorMessage is used as-is without any description prefix.
1211-
this.info.overridingErrorMessage(assertInstance.info.overridingErrorMessage());
1212-
this.info.description(assertInstance.info.description());
1213-
} else if (assertInstance.info.description() != null) {
1214-
// Only overwrite description if the source has one set.
1215-
// This preserves descriptions set by extracting/navigation methods (via .as()).
1216-
this.info.description(assertInstance.info.description());
1217-
}
1206+
this.info.overridingErrorMessage(assertInstance.info.overridingErrorMessage());
1207+
this.info.description(assertInstance.info.description());
12181208
}
12191209

12201210
// this method is meant to be overridden and made public in subclasses that want to expose it
@@ -1271,7 +1261,7 @@ protected RecursiveAssertionAssert usingRecursiveAssertion() {
12711261
String extractedPropertyOrFieldDescription = extractedDescriptionOf(propertyOrField);
12721262
String description = mostRelevantDescription(info.description(), extractedPropertyOrFieldDescription);
12731263
@SuppressWarnings("unchecked")
1274-
ASSERT result = (ASSERT) assertFactory.createAssert(value).as(description).withAssertionState(myself);
1264+
ASSERT result = (ASSERT) assertFactory.createAssert(value).withAssertionState(myself).as(description);
12751265
return result;
12761266
}
12771267

assertj-core/src/main/java/org/assertj/core/api/AbstractCollectionAssert.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,9 @@ private FactoryBasedAssert(ACTUAL actual, AssertFactory<ELEMENT, ELEMENT_ASSERT>
169169
this.assertFactory = assertFactory;
170170
}
171171

172-
@SuppressWarnings("unchecked")
173172
@Override
174-
protected ELEMENT_ASSERT toAssert(ELEMENT value, String description) {
175-
return assertFactory.createAssert(value).as(description);
173+
protected ELEMENT_ASSERT toAssert(ELEMENT value) {
174+
return assertFactory.createAssert(value);
176175
}
177176

178177
@SuppressWarnings("unchecked")

assertj-core/src/main/java/org/assertj/core/api/AbstractIterableAssert.java

Lines changed: 24 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -891,7 +891,7 @@ public AbstractListAssert<?, List<?>, Object, ObjectAssert<Object>> extracting(S
891891
List<Object> values = FieldsOrPropertiesExtractor.extract(actual, byName(propertyOrField));
892892
String extractedDescription = extractedDescriptionOf(propertyOrField);
893893
String description = mostRelevantDescription(info.description(), extractedDescription);
894-
return propagateState(newListAssertInstanceForMethodsChangingElementType(values).as(description));
894+
return newListAssertInstanceForMethodsChangingElementType(values).as(description);
895895
}
896896

897897
/**
@@ -939,7 +939,7 @@ public AbstractListAssert<?, List<?>, Object, ObjectAssert<Object>> extractingRe
939939
List<Object> values = FieldsOrPropertiesExtractor.extract(actual, resultOf(method));
940940
String extractedDescription = extractedDescriptionOfMethod(method);
941941
String description = mostRelevantDescription(info.description(), extractedDescription);
942-
return propagateState(newListAssertInstanceForMethodsChangingElementType(values).as(description));
942+
return newListAssertInstanceForMethodsChangingElementType(values).as(description);
943943
}
944944

945945
/**
@@ -989,7 +989,7 @@ public <P> AbstractListAssert<?, List<? extends P>, P, ObjectAssert<P>> extracti
989989
List<P> values = (List<P>) FieldsOrPropertiesExtractor.extract(actual, resultOf(method));
990990
String extractedDescription = extractedDescriptionOfMethod(method);
991991
String description = mostRelevantDescription(info.description(), extractedDescription);
992-
return propagateState(newListAssertInstanceForMethodsChangingElementType(values).as(description));
992+
return newListAssertInstanceForMethodsChangingElementType(values).as(description);
993993
}
994994

995995
/**
@@ -1079,7 +1079,7 @@ public <P> AbstractListAssert<?, List<? extends P>, P, ObjectAssert<P>> extracti
10791079
List<P> values = (List<P>) FieldsOrPropertiesExtractor.extract(actual, byName(propertyOrField));
10801080
String extractedDescription = extractedDescriptionOf(propertyOrField);
10811081
String description = mostRelevantDescription(info.description(), extractedDescription);
1082-
return propagateState(newListAssertInstanceForMethodsChangingElementType(values).as(description));
1082+
return newListAssertInstanceForMethodsChangingElementType(values).as(description);
10831083
}
10841084

10851085
/**
@@ -1171,7 +1171,7 @@ public AbstractListAssert<?, List<? extends Tuple>, Tuple, ObjectAssert<Tuple>>
11711171
List<Tuple> values = FieldsOrPropertiesExtractor.extract(actual, byName(propertiesOrFields));
11721172
String extractedDescription = extractedDescriptionOf(propertiesOrFields);
11731173
String description = mostRelevantDescription(info.description(), extractedDescription);
1174-
return propagateState(newListAssertInstanceForMethodsChangingElementType(values).as(description));
1174+
return newListAssertInstanceForMethodsChangingElementType(values).as(description);
11751175
}
11761176

11771177
/**
@@ -1213,7 +1213,7 @@ public <V> AbstractListAssert<?, List<? extends V>, V, ObjectAssert<V>> extracti
12131213
private <V> AbstractListAssert<?, List<? extends V>, V, ObjectAssert<V>> internalExtracting(Function<? super ELEMENT, V> extractor) {
12141214
if (actual == null) throwAssertionError(shouldNotBeNull());
12151215
List<V> values = FieldsOrPropertiesExtractor.extract(actual, extractor);
1216-
return propagateState(newListAssertInstanceForMethodsChangingElementType(values));
1216+
return newListAssertInstanceForMethodsChangingElementType(values);
12171217
}
12181218

12191219
/**
@@ -1348,21 +1348,7 @@ private <V> AbstractListAssert<?, List<? extends V>, V, ObjectAssert<V>> newList
13481348
// extracting names we get a List<String> which is mot suitable for the age comparator
13491349
usingDefaultElementComparator();
13501350
}
1351-
return newListAssertInstance(values);
1352-
}
1353-
1354-
/**
1355-
* Called after an extracting/mapping/filtering method creates a new assert and sets its description.
1356-
* Propagates the soft assertion collector and assertion state from the parent, with the description
1357-
* being the most relevant between the parent's existing description and the new one.
1358-
* <p>
1359-
* This method must be called AFTER {@code .as(description)} so that {@code withAssertionState}
1360-
* overwrites the description with the parent's if the parent had one set.
1361-
*/
1362-
@SuppressWarnings("rawtypes")
1363-
private <A extends AbstractAssert> A propagateState(A result) {
1364-
result.withAssertionState(myself);
1365-
return result;
1351+
return newListAssertInstance(values).withAssertionState(myself);
13661352
}
13671353

13681354
/**
@@ -1514,7 +1500,7 @@ private <V> AbstractListAssert<?, List<? extends V>, V, ObjectAssert<V>> doFlatE
15141500
List<V> result = FieldsOrPropertiesExtractor.extract(actual, extractor).stream()
15151501
.flatMap(Collection::stream)
15161502
.collect(toList());
1517-
return propagateState(newListAssertInstanceForMethodsChangingElementType(result));
1503+
return newListAssertInstanceForMethodsChangingElementType(result);
15181504
}
15191505

15201506
/**
@@ -1588,7 +1574,7 @@ protected AbstractListAssert<?, List<?>, Object, ObjectAssert<Object>> flatExtra
15881574
Stream<? extends ELEMENT> actualStream = stream(actual.spliterator(), false);
15891575
List<Object> result = actualStream.flatMap(element -> Stream.of(extractors).map(extractor -> extractor.apply(element)))
15901576
.collect(toList());
1591-
return propagateState(newListAssertInstanceForMethodsChangingElementType(result));
1577+
return newListAssertInstanceForMethodsChangingElementType(result);
15921578
}
15931579

15941580
/**
@@ -1723,7 +1709,7 @@ public AbstractListAssert<?, List<?>, Object, ObjectAssert<Object>> flatExtracti
17231709
CommonErrors.wrongElementTypeForFlatExtracting(group);
17241710
}
17251711
}
1726-
return propagateState(newListAssertInstanceForMethodsChangingElementType(extractedValues));
1712+
return newListAssertInstanceForMethodsChangingElementType(extractedValues);
17271713
}
17281714

17291715
/**
@@ -1790,7 +1776,7 @@ protected AbstractListAssert<?, List<? extends Tuple>, Tuple, ObjectAssert<Tuple
17901776
.map(extractor -> extractor.apply(objectToExtractValueFrom))
17911777
.toArray());
17921778
List<Tuple> tuples = stream(actual.spliterator(), false).map(tupleExtractor).collect(toList());
1793-
return propagateState(newListAssertInstanceForMethodsChangingElementType(tuples));
1779+
return newListAssertInstanceForMethodsChangingElementType(tuples);
17941780
}
17951781

17961782
/**
@@ -1875,7 +1861,7 @@ public AbstractListAssert<?, List<?>, Object, ObjectAssert<Object>> flatExtracti
18751861
List<Object> extractedValues = FieldsOrPropertiesExtractor.extract(actual, byName(fieldOrPropertyNames)).stream()
18761862
.flatMap(tuple -> tuple.toList().stream())
18771863
.collect(toList());
1878-
return propagateState(newListAssertInstanceForMethodsChangingElementType(extractedValues));
1864+
return newListAssertInstanceForMethodsChangingElementType(extractedValues);
18791865
}
18801866

18811867
@Override
@@ -2834,9 +2820,7 @@ public ELEMENT_ASSERT first() {
28342820

28352821
private ELEMENT_ASSERT internalFirst() {
28362822
isNotEmpty();
2837-
ELEMENT_ASSERT result = toAssert(actual.iterator().next(), navigationDescription("check first element"));
2838-
result.withAssertionState(myself);
2839-
return result;
2823+
return toAssert(actual.iterator().next(), navigationDescription("check first element"));
28402824
}
28412825

28422826
/**
@@ -2900,9 +2884,7 @@ public ELEMENT_ASSERT last() {
29002884

29012885
private ELEMENT_ASSERT internalLast() {
29022886
isNotEmpty();
2903-
ELEMENT_ASSERT result = toAssert(lastElement(), navigationDescription("check last element"));
2904-
result.withAssertionState(myself);
2905-
return result;
2887+
return toAssert(lastElement(), navigationDescription("check last element"));
29062888
}
29072889

29082890
@SuppressWarnings("unchecked")
@@ -3055,9 +3037,7 @@ private ELEMENT_ASSERT internalElement(int index) {
30553037
elementAtIndex = actualIterator.next();
30563038
}
30573039

3058-
ELEMENT_ASSERT result = toAssert(elementAtIndex, navigationDescription("element at index " + index));
3059-
result.withAssertionState(myself);
3060-
return result;
3040+
return toAssert(elementAtIndex, navigationDescription("element at index " + index));
30613041
}
30623042

30633043
/**
@@ -3143,9 +3123,7 @@ public ELEMENT_ASSERT singleElement() {
31433123

31443124
private ELEMENT_ASSERT internalSingleElement() {
31453125
iterables.assertHasSize(info, actual, 1);
3146-
ELEMENT_ASSERT result = toAssert(actual.iterator().next(), navigationDescription("check single element"));
3147-
result.withAssertionState(myself);
3148-
return result;
3126+
return toAssert(actual.iterator().next(), navigationDescription("check single element"));
31493127
}
31503128

31513129
/**
@@ -3159,7 +3137,12 @@ private ELEMENT_ASSERT internalSingleElement() {
31593137
* @param description describes the element, ex: "check first element" for {@link #first()}, used in assertion description.
31603138
* @return the assertion for the given element
31613139
*/
3162-
protected abstract ELEMENT_ASSERT toAssert(ELEMENT value, String description);
3140+
@SuppressWarnings("unchecked")
3141+
protected ELEMENT_ASSERT toAssert(ELEMENT value, String description) {
3142+
return toAssert(value).withAssertionState(myself).as(description);
3143+
}
3144+
3145+
protected abstract ELEMENT_ASSERT toAssert(ELEMENT value);
31633146

31643147
protected String navigationDescription(String propertyName) {
31653148
String text = descriptionText();
@@ -3657,10 +3640,9 @@ private FactoryBasedAssert(ACTUAL actual, AssertFactory<ELEMENT, ELEMENT_ASSERT>
36573640
this.assertFactory = assertFactory;
36583641
}
36593642

3660-
@SuppressWarnings("unchecked")
36613643
@Override
3662-
protected ELEMENT_ASSERT toAssert(ELEMENT value, String description) {
3663-
return assertFactory.createAssert(value).as(description);
3644+
protected ELEMENT_ASSERT toAssert(ELEMENT value) {
3645+
return assertFactory.createAssert(value);
36643646
}
36653647

36663648
@SuppressWarnings("unchecked")

assertj-core/src/main/java/org/assertj/core/api/AbstractListAssert.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -426,10 +426,9 @@ private FactoryBasedAssert(ACTUAL actual, AssertFactory<ELEMENT, ELEMENT_ASSERT>
426426
this.assertFactory = assertFactory;
427427
}
428428

429-
@SuppressWarnings("unchecked")
430429
@Override
431-
protected ELEMENT_ASSERT toAssert(ELEMENT value, String description) {
432-
return assertFactory.createAssert(value).as(description);
430+
protected ELEMENT_ASSERT toAssert(ELEMENT value) {
431+
return assertFactory.createAssert(value);
433432
}
434433

435434
@SuppressWarnings("unchecked")

assertj-core/src/main/java/org/assertj/core/api/AbstractMapAssert.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1935,7 +1935,7 @@ public AbstractListAssert<?, List<?>, Object, ObjectAssert<Object>> flatExtracti
19351935
List<Object> valuesFlattened = flatten(values.toList());
19361936
String extractedPropertiesOrFieldsDescription = extractedDescriptionOf(keys);
19371937
String description = mostRelevantDescription(info.description(), extractedPropertiesOrFieldsDescription);
1938-
return newListAssertInstance(valuesFlattened).as(description);
1938+
return newListAssertInstance(valuesFlattened).withAssertionState(myself).as(description);
19391939
}
19401940

19411941
/**

assertj-core/src/main/java/org/assertj/core/api/AbstractObjectArrayAssert.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3491,9 +3491,7 @@ public SELF noneMatch(Predicate<? super ELEMENT> predicate, String predicateDesc
34913491
*/
34923492
@Override
34933493
protected <E> AbstractListAssert<?, List<? extends E>, E, ObjectAssert<E>> newListAssertInstance(List<? extends E> newActual) {
3494-
ListAssert<E> result = new ListAssert<>(newActual);
3495-
result.withAssertionState(myself);
3496-
return result;
3494+
return new ListAssert<>(newActual);
34973495
}
34983496

34993497
/**
@@ -3770,9 +3768,7 @@ private static String removeAssert(String text) {
37703768
}
37713769

37723770
private ObjectAssert<ELEMENT> toAssert(ELEMENT value, String description) {
3773-
ObjectAssert<ELEMENT> result = new ObjectAssert<>(value);
3774-
result.withAssertionState(myself);
3775-
return result.as(description);
3771+
return new ObjectAssert<>(value).withAssertionState(myself).as(description);
37763772
}
37773773

37783774
// lazy init TypeComparators

assertj-core/src/main/java/org/assertj/core/api/AbstractObjectAssert.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ public AbstractListAssert<?, List<?>, Object, ObjectAssert<Object>> extracting(S
411411
Tuple values = byName(propertiesOrFields).apply(actual);
412412
String extractedPropertiesOrFieldsDescription = extractedDescriptionOf(propertiesOrFields);
413413
String description = mostRelevantDescription(info.description(), extractedPropertiesOrFieldsDescription);
414-
return newListAssertInstance(values.toList()).as(description).withAssertionState(myself);
414+
return newListAssertInstance(values.toList()).withAssertionState(myself).as(description);
415415
}
416416

417417
/**
@@ -899,9 +899,7 @@ public RecursiveAssertionAssert usingRecursiveAssertion(RecursiveAssertionConfig
899899

900900
// override for proxyable friendly AbstractObjectAssert
901901
protected <T> AbstractObjectAssert<?, T> newObjectAssert(T objectUnderTest) {
902-
ObjectAssert<T> result = new ObjectAssert<>(objectUnderTest);
903-
result.withAssertionState(myself);
904-
return result;
902+
return new ObjectAssert<>(objectUnderTest);
905903
}
906904

907905
@SuppressWarnings({ "rawtypes" })

assertj-core/src/main/java/org/assertj/core/api/AbstractOptionalAssert.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -458,9 +458,7 @@ public SELF containsSame(VALUE expectedValue) {
458458
@CheckReturnValue
459459
public <U> AbstractOptionalAssert<?, U> flatMap(Function<? super VALUE, Optional<U>> mapper) {
460460
isNotNull();
461-
OptionalAssert<U> result = assertThat(actual.flatMap(mapper));
462-
result.withAssertionState(myself);
463-
return result;
461+
return assertThat(actual.flatMap(mapper)).withAssertionState(myself);
464462
}
465463

466464
/**

assertj-core/src/main/java/org/assertj/core/api/AbstractThrowableAssert.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -859,9 +859,7 @@ private boolean isNotAnInstanceOfAny(Class<? extends Throwable>[] exceptionTypes
859859
*/
860860
public AbstractStringAssert<?> message() {
861861
objects.assertNotNull(info, actual);
862-
StringAssert result = new StringAssert(actual.getMessage());
863-
result.withAssertionState(myself);
864-
return result;
862+
return new StringAssert(actual.getMessage()).withAssertionState(myself);
865863
}
866864

867865
}

assertj-core/src/main/java/org/assertj/core/api/CollectionAssert.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ public CollectionAssert(Collection<? extends ELEMENT> actual) {
4141
}
4242

4343
@Override
44-
protected ObjectAssert<ELEMENT> toAssert(ELEMENT value, String description) {
45-
return new ObjectAssert<>(value).as(description);
44+
protected ObjectAssert<ELEMENT> toAssert(ELEMENT value) {
45+
return new ObjectAssert<>(value);
4646
}
4747

4848
@Override

0 commit comments

Comments
 (0)