Skip to content

Commit

Permalink
Ensure the mapped method is compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
Lenni0451 committed Jun 22, 2024
1 parent 96bc8e1 commit 4823e0b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/main/java/net/lenni0451/reflect/proxy/ProxyBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ public Function<Method, Method> getMethodMapper() {
/**
* Set the mapper for the methods that should be overridden by the proxy class.<br>
* The mapper can be used to change the owner of an overridden method.<br>
* The owner must be a super class of the original owner.
* The owner must be a super class of the original owner.<br>
* The super call will not be changed by the mapper.
*
* @param methodMapper The method mapper
* @return This builder
Expand Down
21 changes: 15 additions & 6 deletions src/main/java/net/lenni0451/reflect/proxy/internal/ProxyUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
* Utils for creating proxy classes.
Expand Down Expand Up @@ -70,10 +68,21 @@ public static Method[] mapMethods(final Method[] methods, final Function<Method,
for (int i = 0; i < methods.length; i++) {
Method method = methods[i];
Method mappedMethod = methodMapper.apply(method);
if (method.equals(mappedMethod)) continue;
if (mappedMethod == null || method.equals(mappedMethod)) continue;

if (!method.getName().equals(mappedMethod.getName())) {
throw new IllegalArgumentException(String.format("Method '%s' has mismatching name (%1$s != %s)", method.getName(), mappedMethod.getName()));
}
if (!Arrays.equals(method.getParameterTypes(), mappedMethod.getParameterTypes())) {
String originalParameters = Arrays.stream(method.getParameterTypes()).map(Class::getSimpleName).collect(Collectors.joining(", "));
String mappedParameters = Arrays.stream(mappedMethod.getParameterTypes()).map(Class::getSimpleName).collect(Collectors.joining(", "));
throw new IllegalArgumentException(String.format("Method '%s' has mismatching parameter types (%s != %s)", mappedMethod.getName(), originalParameters, mappedParameters));
}
if (method.getReturnType() != mappedMethod.getReturnType()) {
throw new IllegalArgumentException(String.format("Method '%s' has mismatching return type (%s != %s)", mappedMethod.getName(), method.getReturnType().getSimpleName(), mappedMethod.getReturnType().getSimpleName()));
}
if (!mappedMethod.getDeclaringClass().isAssignableFrom(method.getDeclaringClass())) {
throw new IllegalArgumentException("The method '" + method.getName() + "' in class '" + method.getDeclaringClass().getName() + "' is not assignable to the method in the proxy class");
throw new IllegalArgumentException(String.format("Declaring class of method '%s' is not assignable from original declaring class (%s != %s)", mappedMethod.getName(), mappedMethod.getDeclaringClass().getName(), method.getDeclaringClass().getName()));
}
methods[i] = mappedMethod;
originalMethods[i] = method;
Expand Down

0 comments on commit 4823e0b

Please sign in to comment.