Skip to content

Commit

Permalink
Fixed bug 562
Browse files Browse the repository at this point in the history
  • Loading branch information
jlink committed Apr 14, 2024
1 parent 5a70952 commit bf1832d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
3 changes: 0 additions & 3 deletions TODO.md
@@ -1,8 +1,5 @@
# 1.8.5

- Fix class inheritance bug: https://github.com/jqwik-team/jqwik/issues/562


# 1.8.x
- Replace jsr305 with jspecify

Expand Down
Expand Up @@ -102,6 +102,10 @@ public static <T> T newInstance(Constructor<T> constructor, Object... args) {
* Find all {@linkplain Method methods} as in ReflectionSupport.findMethods(..) but also use outer classes to look for
* methods.
*
* <p>
* Duplicate methods (through) inheritance are de-duplicated. The first occurrence of a method is kept.
* </p>
*
* @param clazz The class in which you start the search
* @param predicate The condition to check for all candidate methods
* @param traversalMode Traverse hierarchy up or down. Determines the order in resulting list.
Expand All @@ -113,11 +117,11 @@ public static List<Method> findMethodsPotentiallyOuter(
HierarchyTraversalMode traversalMode
) {
List<Class<?>> searchClasses = getDeclaringClasses(clazz, traversalMode);
List<Method> foundMethods = new ArrayList<>();
Set<Method> foundMethods = new LinkedHashSet<>();
for (Class<?> searchClass : searchClasses) {
foundMethods.addAll(ReflectionSupport.findMethods(searchClass, predicate, traversalMode));
}
return foundMethods;
return new ArrayList<>(foundMethods);
}

/**
Expand Down
Expand Up @@ -6,6 +6,8 @@
import java.util.function.*;
import java.util.stream.*;

import org.junit.platform.commons.support.*;

import net.jqwik.api.*;
import net.jqwik.api.constraints.*;

Expand Down Expand Up @@ -182,6 +184,30 @@ void getFunctionMethod() {
assertThat(JqwikReflectionSupport.getFunctionMethod(DataOutput.class)).isNotPresent();
}

@Example
void findMethodsPotentiallyOuter() {
List<Method> methods = JqwikReflectionSupport.findMethodsPotentiallyOuter(
AbstractClass.ConcreteSubclass.class,
method -> method.getName().startsWith("method"),
HierarchyTraversalMode.BOTTOM_UP
);
assertThat(methods).hasSize(2);

List<Method> methodsInSub = JqwikReflectionSupport.findMethodsPotentiallyOuter(
AbstractClass.ConcreteSubclass.class,
method -> method.getName().startsWith("methodInConcreteSubclass"),
HierarchyTraversalMode.BOTTOM_UP
);
assertThat(methodsInSub).hasSize(1);

List<Method> methodsInBase = JqwikReflectionSupport.findMethodsPotentiallyOuter(
AbstractClass.ConcreteSubclass.class,
method -> method.getName().startsWith("methodInAbstractClass"),
HierarchyTraversalMode.BOTTOM_UP
);
assertThat(methodsInBase).hasSize(1);
}

private static class Outer {

Inner createInner() {
Expand Down Expand Up @@ -225,3 +251,13 @@ private static class OuterWithConstructor {

}
}

abstract class AbstractClass {

void methodInAbstractClass() {}

static class ConcreteSubclass extends AbstractClass {

void methodInConcreteSubclass() {}
}
}

0 comments on commit bf1832d

Please sign in to comment.