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

OnDemandExtensions: flatten stack a bit by inlining Unchecked #2440

Merged
merged 2 commits into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,8 @@ public UnableToCreateExtensionException(String format, Object... args) {
public UnableToCreateExtensionException(Throwable throwable, String format, Object... args) {
super(format(format, args), throwable);
}

public UnableToCreateExtensionException(Throwable cause) {
super(cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,18 @@
*/
package org.jdbi.v3.core.internal;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Stream;

import org.jdbi.v3.core.Jdbi;
import org.jdbi.v3.core.config.JdbiConfig;
import org.jdbi.v3.core.extension.Extensions;
import org.jdbi.v3.core.internal.exceptions.Unchecked;
import org.jdbi.v3.core.internal.exceptions.Sneaky;

import static org.jdbi.v3.core.internal.JdbiClassUtils.EQUALS_METHOD;
import static org.jdbi.v3.core.internal.JdbiClassUtils.HASHCODE_METHOD;
Expand Down Expand Up @@ -73,23 +71,25 @@ private Object createProxy(Jdbi jdbi, Class<?> extensionType, Class<?>... extraT
return jdbi.withExtension(extensionType, extension -> invoke(extension, method, args));
};

Class<?>[] types = Stream.of(
Stream.of(extensionType),
Arrays.stream(extensionType.getInterfaces()),
Arrays.stream(extraTypes))
.flatMap(Function.identity())
.distinct()
.toArray(Class[]::new);
return Proxy.newProxyInstance(extensionType.getClassLoader(), types, handler);
var types = new LinkedHashSet<Class<?>>();
types.add(extensionType);
types.addAll(Arrays.asList(extensionType.getInterfaces()));
types.addAll(Arrays.asList(extraTypes));
return Proxy.newProxyInstance(extensionType.getClassLoader(), types.toArray(new Class<?>[0]), handler);
}

private static Object invoke(Object target, Method method, Object[] args) {
if (Proxy.isProxyClass(target.getClass())) {
InvocationHandler handler = Proxy.getInvocationHandler(target);
return Unchecked.<Object[], Object>function(params -> handler.invoke(target, method, params)).apply(args);
} else {
MethodHandle handle = Unchecked.function(MethodHandles.lookup()::unreflect).apply(method).bindTo(target);
return Unchecked.<Object[], Object>function(handle::invokeWithArguments).apply(args);
try {
if (Proxy.isProxyClass(target.getClass())) {
return Proxy.getInvocationHandler(target)
.invoke(target, method, args);
} else {
return MethodHandles.lookup().unreflect(method)
.bindTo(target)
.invokeWithArguments(args);
}
} catch (Throwable t) {
throw Sneaky.throwAnyway(t);
}
}

Expand Down