Skip to content

Commit

Permalink
Fixed super methods causing problems
Browse files Browse the repository at this point in the history
  • Loading branch information
Lenni0451 committed Mar 28, 2024
1 parent 3c0447a commit d414b4d
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/main/java/net/lenni0451/lambdaevents/utils/EventUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,15 @@ public static List<MethodHandler> getMethods(final Class<?> owner, final Predica
if (registerSuperHandler) getSuperClasses(classes, owner);
else classes.add(owner);

Set<MethodID> methodIDs = new HashSet<>();
for (Class<?> current : classes) {
for (Method method : current.getDeclaredMethods()) {
EventHandler annotation = method.getDeclaredAnnotation(EventHandler.class);
if (annotation == null) continue; //Doesn't have the annotation
if (!accept.test(method)) continue; //Doesn't match the predicate

MethodID id = new MethodID(method.getName(), method.getParameterTypes());
if (!Modifier.isPrivate(method.getModifiers()) && !methodIDs.add(id)) continue; //Already added this method
handler.add(new MethodHandler(current, annotation, method));
}
}
Expand Down Expand Up @@ -266,6 +269,17 @@ public static void getSuperClasses(final Set<Class<?>> classes, final Class<?> c
}


/**
* A class to identify a method by its name and parameters.
*/
@Data
private static class MethodID {
@Nonnull
private final String name;
@Nonnull
private final Class<?>[] params;
}

/**
* A wrapper class for an event handler method with it's {@link EventHandler} annotation.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package net.lenni0451.lambdaevents;

import net.lenni0451.lambdaevents.generator.ReflectionGenerator;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class OverriddenPrivateSuperTest {

private int calledSuper;
private int calledExtending;

@BeforeEach
public void reset() {
this.calledSuper = 0;
this.calledExtending = 0;
}

@Test
void overriddenPrivateHandler() {
LambdaManager manager = LambdaManager.basic(new ReflectionGenerator());
manager.registerSuper(new OverriddenHandler());
manager.call("Test");

assertEquals(this.calledSuper, 1);
assertEquals(this.calledExtending, 1);
}


public class BaseClass {
@EventHandler
private void onEvent(final String s) {
OverriddenPrivateSuperTest.this.calledSuper++;
}
}

public class OverriddenHandler extends BaseClass {
@EventHandler
private void onEvent(final String s) {
OverriddenPrivateSuperTest.this.calledExtending++;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package net.lenni0451.lambdaevents;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import static net.lenni0451.lambdaevents.TestManager.DATA_SOURCE;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class OverriddenSuperRegisterTest {

private int calledSuper;
private int calledExtending;

@BeforeEach
public void reset() {
this.calledSuper = 0;
this.calledExtending = 0;
}

@ParameterizedTest
@MethodSource(DATA_SOURCE)
void overriddenHandler(final LambdaManager manager) {
manager.registerSuper(new OverriddenHandler());
manager.call("Test");

assertEquals(this.calledSuper, 0); //should be 1 if it was possible to call the actual super method
assertEquals(this.calledExtending, 1); //would be 2 if the super method wasn't filtered out
}

@ParameterizedTest
@MethodSource(DATA_SOURCE)
void overriddenMethod(final LambdaManager manager) {
manager.registerSuper(new OverriddenMethod());
manager.call("Test");

assertEquals(this.calledSuper, 0);
assertEquals(this.calledExtending, 1);
}

@ParameterizedTest
@MethodSource(DATA_SOURCE)
void overriddenMethodWithoutSuper(final LambdaManager manager) {
manager.register(new OverriddenMethod());
manager.call("Test");

assertEquals(this.calledSuper, 0);
assertEquals(this.calledExtending, 0);
}


public class BaseClass {
@EventHandler
public void onEvent(final String s) {
OverriddenSuperRegisterTest.this.calledSuper++;
}
}

public class OverriddenHandler extends BaseClass {
@Override
@EventHandler
public void onEvent(final String s) {
OverriddenSuperRegisterTest.this.calledExtending++;
}
}

public class OverriddenMethod extends BaseClass {
@Override
public void onEvent(final String s) {
OverriddenSuperRegisterTest.this.calledExtending++;
}
}

}

0 comments on commit d414b4d

Please sign in to comment.