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

ConstructorInstanceFactory: clean up some warnings and flatten stack a bit #2658

Merged
merged 1 commit into from Mar 14, 2024
Merged
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
Expand Up @@ -27,15 +27,15 @@
import java.util.function.Supplier;
import java.util.stream.Stream;

import org.jdbi.v3.core.internal.exceptions.Unchecked;
import org.jdbi.v3.core.internal.exceptions.Sneaky;

import static java.util.Collections.synchronizedMap;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.toUnmodifiableList;

class ConstructorInstanceFactory<T> extends InstanceFactory<T> {
private static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup();
private static final Map<Constructor, ConstructorHandleAndTypes> CONSTRUCTOR_CACHE = synchronizedMap(new WeakHashMap<>());
private static final Map<Constructor<?>, ConstructorHandleAndTypes> CONSTRUCTOR_CACHE = synchronizedMap(new WeakHashMap<>());

private final Constructor<T> constructor;
private final List<Type> types;
Expand All @@ -54,9 +54,14 @@ List<Type> getTypes() {
return types;
}

@SuppressWarnings("unchecked")
@Override
T newInstance(Object... params) {
return (T) Unchecked.<Object[], Object>function(constructorHandle::invokeWithArguments).apply(params);
try {
return (T) constructorHandle.invokeWithArguments(params);
} catch (Throwable e) {
throw Sneaky.throwAnyway(e);
}
}

@Override
Expand Down