Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Java] Define step definitions and hooks with minimal ceremony #2415

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
32 changes: 29 additions & 3 deletions java/src/main/java/io/cucumber/java/MethodScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;

import static io.cucumber.core.resource.ClasspathSupport.classPathScanningExplanation;
import static io.cucumber.java.InvalidMethodException.createInvalidMethodException;
import static java.lang.reflect.Modifier.isAbstract;
import static java.lang.reflect.Modifier.isPrivate;
import static java.lang.reflect.Modifier.isPublic;
import static java.lang.reflect.Modifier.isStatic;

Expand All @@ -29,12 +34,15 @@ static void scan(Class<?> aClass, BiConsumer<Method, Annotation> consumer) {
if (!isInstantiable(aClass)) {
return;
}
for (Method method : safelyGetMethods(aClass)) {
for (Method method : safelyGetPublicMethods(aClass)) {
scan(consumer, aClass, method);
}
for (Method method : safelyGetNonPublicMethods(aClass)) {
scan(consumer, aClass, method);
}
}

private static Method[] safelyGetMethods(Class<?> aClass) {
private static Method[] safelyGetPublicMethods(Class<?> aClass) {
try {
return aClass.getMethods();
} catch (NoClassDefFoundError e) {
Expand All @@ -44,8 +52,26 @@ private static Method[] safelyGetMethods(Class<?> aClass) {
return new Method[0];
}

private static List<Method> safelyGetNonPublicMethods(Class<?> aClass) {
try {
return Arrays.stream(aClass.getDeclaredMethods())
.filter(MethodScanner::hasAcceptableModifiers)
.collect(Collectors.toList());
} catch (NoClassDefFoundError e) {
log.warn(e,
() -> "Failed to load methods of class '" + aClass.getName() + "'.\n" + classPathScanningExplanation());
}
return Collections.emptyList();
}

private static boolean hasAcceptableModifiers(Method aMethod) {
return !isPrivate(aMethod.getModifiers())
&& !isPublic(aMethod.getModifiers())
&& !isAbstract(aMethod.getModifiers());
}

private static boolean isInstantiable(Class<?> clazz) {
return isPublic(clazz.getModifiers())
return !isPrivate(clazz.getModifiers())
&& !isAbstract(clazz.getModifiers())
&& (isStatic(clazz.getModifiers()) || clazz.getEnclosingClass() == null);
}
Expand Down
113 changes: 110 additions & 3 deletions java/src/test/java/io/cucumber/java/MethodScannerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,45 @@ void createBackend() {
}

@Test
void scan_finds_annotated_methods() throws NoSuchMethodException {
Method method = BaseSteps.class.getMethod("m");
void scan_finds_annotated_methods_in_public_class() throws NoSuchMethodException {
Method publicMethod = BaseSteps.class.getMethod("m");
Method packagePrivateMethod = BaseSteps.class.getDeclaredMethod("n");
Method protectedMethod = BaseSteps.class.getDeclaredMethod("o");
MethodScanner.scan(BaseSteps.class, backend);
assertThat(scanResult, contains(new SimpleEntry<>(method, method.getAnnotations()[0])));
assertThat(scanResult,
contains(new SimpleEntry<>(publicMethod, publicMethod.getAnnotations()[0]),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using org.hamcrest.collection.IsMapContaining.hasEntry for all these.

Would have been better if I had done that the first time round, but better late then never.

new SimpleEntry<>(packagePrivateMethod, packagePrivateMethod.getAnnotations()[0]),
new SimpleEntry<>(protectedMethod, protectedMethod.getAnnotations()[0])));
}

@Test
void scan_finds_annotated_methods_in_protected_class() throws NoSuchMethodException {
Method publicMethod = ProtectedSteps.class.getMethod("m");
Method packagePrivateMethod = ProtectedSteps.class.getDeclaredMethod("n");
Method protectedMethod = ProtectedSteps.class.getDeclaredMethod("o");
MethodScanner.scan(ProtectedSteps.class, backend);
assertThat(scanResult,
contains(new SimpleEntry<>(publicMethod, publicMethod.getAnnotations()[0]),
new SimpleEntry<>(packagePrivateMethod, packagePrivateMethod.getAnnotations()[0]),
new SimpleEntry<>(protectedMethod, protectedMethod.getAnnotations()[0])));
}

@Test
void scan_finds_annotated_methods_in_package_private_class() throws NoSuchMethodException {
Method publicMethod = PackagePrivateSteps.class.getMethod("m");
Method packagePrivateMethod = PackagePrivateSteps.class.getDeclaredMethod("n");
Method protectedMethod = PackagePrivateSteps.class.getDeclaredMethod("o");
MethodScanner.scan(PackagePrivateSteps.class, backend);
assertThat(scanResult,
contains(new SimpleEntry<>(publicMethod, publicMethod.getAnnotations()[0]),
new SimpleEntry<>(packagePrivateMethod, packagePrivateMethod.getAnnotations()[0]),
new SimpleEntry<>(protectedMethod, protectedMethod.getAnnotations()[0])));
}

@Test
void scan_ignores_private_class() {
MethodScanner.scan(PrivateSteps.class, backend);
assertThat(scanResult, empty());
}

@Test
Expand Down Expand Up @@ -70,6 +105,78 @@ public static class BaseSteps {
public void m() {
}

@Before
void n() {
}

@Before
protected void o() {
}

@Before
private void p() {
}

}

protected static class ProtectedSteps {

@Before
public void m() {
}

@Before
void n() {
}

@Before
protected void o() {
}

@Before
private void p() {
}

}

static class PackagePrivateSteps {

@Before
public void m() {
}

@Before
void n() {
}

@Before
protected void o() {
}

@Before
private void p() {
}

}

private static class PrivateSteps {

@Before
public void m() {
}

@Before
void n() {
}

@Before
protected void o() {
}

@Before
private void p() {
}

}

@SuppressWarnings("InnerClassMayBeStatic")
Expand Down