Skip to content

Commit

Permalink
Revert "Apply method predicate before searching type hierarchy"
Browse files Browse the repository at this point in the history
This commit reverts the functional changes from commit 64ed21a and
disables the associated tests for the time being.

See #3498
See #3500
See #3553
Closes #3600

(cherry picked from commit c2f49f6)
  • Loading branch information
sbrannen committed Jan 15, 2024
1 parent 63d464d commit 43c105a
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,40 @@ on GitHub.

==== Deprecations and Breaking Changes

* Field predicates are no longer applied eagerly while searching the type hierarchy. This
reverts changes made in 5.10.1 that affected `findFields(...)` and `streamFields(...)`
in `ReflectionSupport` as well as `findAnnotatedFields(...)` and
`findAnnotatedFieldValues(...)` in `AnnotationSupport`.
- See issues link:https://github.com/junit-team/junit5/issues/3638[#3638] and
link:https://github.com/junit-team/junit5/issues/3553[#3553] for details.
* Field predicates are no longer applied eagerly while searching the type hierarchy.
- This reverts changes made in 5.10.1 that affected `findFields(...)` and
`streamFields(...)` in `ReflectionSupport` as well as `findAnnotatedFields(...)` and
`findAnnotatedFieldValues(...)` in `AnnotationSupport`.
- See issue link:https://github.com/junit-team/junit5/issues/3638[#3638] for details.
* Method predicates are no longer applied eagerly while searching the type hierarchy.
- This reverts changes made in 5.10.1 that affected `findMethods(...)` and
`streamMethods(...)` in `ReflectionSupport` as well as `findAnnotatedMethods(...)` in
`AnnotationSupport`.
- See issue link:https://github.com/junit-team/junit5/issues/3600[#3600] for details.


[[release-notes-5.10.2-junit-jupiter]]
=== JUnit Jupiter

==== Bug Fixes

* ❓
* JUnit Jupiter once again properly detects when a `@Test` method is overridden in a
subclass.
- See issue link:https://github.com/junit-team/junit5/issues/3600[#3600] for details.

==== Deprecations and Breaking Changes

* A package-private static field annotated with `@TempDir` is once again _shadowed_ by a
non-static field annotated with `@TempDir` when the non-static field resides in a
different package and has the same name as the static field. This reverts changes made
in 5.10.1.
- See issues link:https://github.com/junit-team/junit5/issues/3638[#3638] and
link:https://github.com/junit-team/junit5/issues/3553[#3553] for details.
different package and has the same name as the static field.
- This reverts changes made in 5.10.1.
- See issue link:https://github.com/junit-team/junit5/issues/3638[#3638] for details.
* A package-private class-level lifecycle method annotated with `@BeforeAll` or
`@AfterAll` is once again _shadowed_ by a method-level lifecycle method annotated with
`@BeforeEach` or `@AfterEach` when the method-level lifecycle method resides in a
different package and has the same name as the class-level lifecycle method.
- This reverts changes made in 5.10.1.
- See issue link:https://github.com/junit-team/junit5/issues/3600[#3600] for details.


[[release-notes-5.10.2-junit-vintage]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1490,27 +1490,29 @@ public static Stream<Method> streamMethods(Class<?> clazz, Predicate<Method> pre
Preconditions.notNull(predicate, "Predicate must not be null");
Preconditions.notNull(traversalMode, "HierarchyTraversalMode must not be null");

return findAllMethodsInHierarchy(clazz, predicate, traversalMode).stream().distinct();
// @formatter:off
return findAllMethodsInHierarchy(clazz, traversalMode).stream()
.filter(predicate)
.distinct();
// @formatter:on
}

/**
* Find all non-synthetic methods in the superclass and interface hierarchy,
* excluding Object, that match the specified {@code predicate}.
* excluding Object.
*/
private static List<Method> findAllMethodsInHierarchy(Class<?> clazz, Predicate<Method> predicate,
HierarchyTraversalMode traversalMode) {

private static List<Method> findAllMethodsInHierarchy(Class<?> clazz, HierarchyTraversalMode traversalMode) {
Preconditions.notNull(clazz, "Class must not be null");
Preconditions.notNull(traversalMode, "HierarchyTraversalMode must not be null");

// @formatter:off
List<Method> localMethods = getDeclaredMethods(clazz, traversalMode).stream()
.filter(predicate.and(method -> !method.isSynthetic()))
.filter(method -> !method.isSynthetic())
.collect(toList());
List<Method> superclassMethods = getSuperclassMethods(clazz, predicate, traversalMode).stream()
List<Method> superclassMethods = getSuperclassMethods(clazz, traversalMode).stream()
.filter(method -> !isMethodShadowedByLocalMethods(method, localMethods))
.collect(toList());
List<Method> interfaceMethods = getInterfaceMethods(clazz, predicate, traversalMode).stream()
List<Method> interfaceMethods = getInterfaceMethods(clazz, traversalMode).stream()
.filter(method -> !isMethodShadowedByLocalMethods(method, localMethods))
.collect(toList());
// @formatter:on
Expand Down Expand Up @@ -1646,18 +1648,16 @@ private static int defaultMethodSorter(Method method1, Method method2) {
return comparison;
}

private static List<Method> getInterfaceMethods(Class<?> clazz, Predicate<Method> predicate,
HierarchyTraversalMode traversalMode) {

private static List<Method> getInterfaceMethods(Class<?> clazz, HierarchyTraversalMode traversalMode) {
List<Method> allInterfaceMethods = new ArrayList<>();
for (Class<?> ifc : clazz.getInterfaces()) {

// @formatter:off
List<Method> localInterfaceMethods = getMethods(ifc).stream()
.filter(predicate.and(method -> !isAbstract(method)))
.filter(m -> !isAbstract(m))
.collect(toList());

List<Method> superinterfaceMethods = getInterfaceMethods(ifc, predicate, traversalMode).stream()
List<Method> superinterfaceMethods = getInterfaceMethods(ifc, traversalMode).stream()
.filter(method -> !isMethodShadowedByLocalMethods(method, localInterfaceMethods))
.collect(toList());
// @formatter:on
Expand Down Expand Up @@ -1707,14 +1707,12 @@ private static boolean isFieldShadowedByLocalFields(Field field, List<Field> loc
return localFields.stream().anyMatch(local -> local.getName().equals(field.getName()));
}

private static List<Method> getSuperclassMethods(Class<?> clazz, Predicate<Method> predicate,
HierarchyTraversalMode traversalMode) {

private static List<Method> getSuperclassMethods(Class<?> clazz, HierarchyTraversalMode traversalMode) {
Class<?> superclass = clazz.getSuperclass();
if (!isSearchable(superclass)) {
return Collections.emptyList();
}
return findAllMethodsInHierarchy(superclass, predicate, traversalMode);
return findAllMethodsInHierarchy(superclass, traversalMode);
}

private static boolean isMethodShadowedByLocalMethods(Method method, List<Method> localMethods) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,10 +391,11 @@ void findAnnotatedMethodsForAnnotationUsedInClassAndSuperclassHierarchyDown() th
}

/**
* @see https://github.com/junit-team/junit5/issues/3498
* @see https://github.com/junit-team/junit5/issues/3553
*/
@Disabled("Until #3553 is resolved")
@Test
void findAnnotatedMethodsAppliesPredicateBeforeSearchingTypeHierarchy() throws Exception {
void findAnnotatedMethodsDoesNotAllowInstanceMethodToHideStaticMethod() throws Exception {
final String BEFORE = "before";
Class<?> superclass = SuperclassWithStaticPackagePrivateBeforeMethod.class;
Method beforeAllMethod = superclass.getDeclaredMethod(BEFORE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1352,10 +1352,11 @@ void findMethodsIgnoresBridgeMethods() throws Exception {
}

/**
* @see https://github.com/junit-team/junit5/issues/3498
* @see https://github.com/junit-team/junit5/issues/3553
*/
@Disabled("Until #3553 is resolved")
@Test
void findMethodsAppliesPredicateBeforeSearchingTypeHierarchy() throws Exception {
void findMethodsDoesNotAllowInstanceMethodToHideStaticMethod() throws Exception {
final String BEFORE = "before";
Class<?> superclass = SuperclassWithStaticPackagePrivateBeforeMethod.class;
Method staticMethod = superclass.getDeclaredMethod(BEFORE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import org.junit.jupiter.api.BeforeAll;

/**
* @see https://github.com/junit-team/junit5/issues/3498
* @see https://github.com/junit-team/junit5/issues/3553
*/
public class SuperclassWithStaticPackagePrivateBeforeMethod {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import org.junit.platform.commons.util.pkg1.SuperclassWithStaticPackagePrivateBeforeMethod;

/**
* @see https://github.com/junit-team/junit5/issues/3498
* @see https://github.com/junit-team/junit5/issues/3553
*/
public class SubclassWithNonStaticPackagePrivateBeforeMethod extends SuperclassWithStaticPackagePrivateBeforeMethod {

Expand Down

0 comments on commit 43c105a

Please sign in to comment.