From 6f82e71a91934bdcf0bbfb31e6dded2c4995ebaf Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Fri, 20 Dec 2024 10:59:02 +0200 Subject: [PATCH 1/2] Make DefaultInstanceFactory a static inner class --- .../src/main/java/io/quarkus/arc/runtime/BeanContainerImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extensions/arc/runtime/src/main/java/io/quarkus/arc/runtime/BeanContainerImpl.java b/extensions/arc/runtime/src/main/java/io/quarkus/arc/runtime/BeanContainerImpl.java index 51b2cf3627224..08e3750bcaaee 100644 --- a/extensions/arc/runtime/src/main/java/io/quarkus/arc/runtime/BeanContainerImpl.java +++ b/extensions/arc/runtime/src/main/java/io/quarkus/arc/runtime/BeanContainerImpl.java @@ -85,7 +85,7 @@ public ManagedContext requestContext() { * * @param represents the type that this factory can create */ - private final class DefaultInstanceFactory implements BeanContainer.Factory { + private static final class DefaultInstanceFactory implements BeanContainer.Factory { private final Class type; From a799237de90be77ecfdb6fa9b5cac5b740e4e308 Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Fri, 20 Dec 2024 11:05:50 +0200 Subject: [PATCH 2/2] Replace reflection with MethodHandle in DefaultInstanceFactory This shouldn't really change anything in practice, but MethodHandles are supposed to perform slightly better than old school reflection --- .../quarkus/arc/runtime/BeanContainerImpl.java | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/extensions/arc/runtime/src/main/java/io/quarkus/arc/runtime/BeanContainerImpl.java b/extensions/arc/runtime/src/main/java/io/quarkus/arc/runtime/BeanContainerImpl.java index 08e3750bcaaee..e9a2256497b99 100644 --- a/extensions/arc/runtime/src/main/java/io/quarkus/arc/runtime/BeanContainerImpl.java +++ b/extensions/arc/runtime/src/main/java/io/quarkus/arc/runtime/BeanContainerImpl.java @@ -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; @@ -87,24 +89,30 @@ public ManagedContext requestContext() { */ private static final class DefaultInstanceFactory implements BeanContainer.Factory { + private static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup(); + private static final MethodType VOID_TYPE = MethodType.methodType(void.class); + private final Class type; DefaultInstanceFactory(Class type) { this.type = type; } + @SuppressWarnings("unchecked") @Override public BeanContainer.Instance create() { try { - T instance = type.getDeclaredConstructor().newInstance(); - return new BeanContainer.Instance() { + 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); } } }