Skip to content

Commit

Permalink
Merge pull request quarkusio#45222 from geoand/beancontainerimpl
Browse files Browse the repository at this point in the history
Replace reflection with MethodHandle in DefaultInstanceFactor
  • Loading branch information
geoand authored Dec 23, 2024
2 parents 0ea2a40 + a799237 commit 932e5a8
Showing 1 changed file with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package io.quarkus.arc.runtime;

import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.Arrays;
import java.util.function.Supplier;

Expand Down Expand Up @@ -85,26 +87,32 @@ public ManagedContext requestContext() {
*
* @param <T> represents the type that this factory can create
*/
private final class DefaultInstanceFactory<T> implements BeanContainer.Factory<T> {
private static final class DefaultInstanceFactory<T> implements BeanContainer.Factory<T> {

private static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup();
private static final MethodType VOID_TYPE = MethodType.methodType(void.class);

private final Class<T> type;

DefaultInstanceFactory(Class<T> type) {
this.type = type;
}

@SuppressWarnings("unchecked")
@Override
public BeanContainer.Instance<T> create() {
try {
T instance = type.getDeclaredConstructor().newInstance();
return new BeanContainer.Instance<T>() {
T instance = (T) LOOKUP.findConstructor(type, VOID_TYPE).invoke();
return new BeanContainer.Instance<>() {
@Override
public T get() {
return instance;
}
};
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
throw new RuntimeException(e);
} catch (RuntimeException | Error e) {
throw e;
} catch (Throwable t) {
throw new UndeclaredThrowableException(t);
}
}
}
Expand Down

0 comments on commit 932e5a8

Please sign in to comment.