Skip to content

Commit

Permalink
Contextualized constructors POC
Browse files Browse the repository at this point in the history
Signed-off-by: Maxim Nesen <[email protected]>
  • Loading branch information
senivam committed Nov 16, 2023
1 parent 17c4a2c commit b028a61
Showing 1 changed file with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Set;
Expand Down Expand Up @@ -118,7 +119,8 @@ public void preDestroy(final T instance) {
*/
private static <T> T produce(InjectionTarget<T> target, CreationalContext<T> ctx, InjectionManager im, Class<T> clazz) {
try {
return target.produce(ctx);
return hasContextAnnotatedConstructors(clazz) ? im.create(clazz)
: target.produce(ctx);
} catch (Exception e) {
LOGGER.fine(LocalizationMessages.CDI_FAILED_LOADING(clazz, e.getMessage()));
try {
Expand All @@ -133,6 +135,35 @@ private static <T> T produce(InjectionTarget<T> target, CreationalContext<T> ctx
}
}

private static boolean hasContextAnnotatedConstructors(Class clazz) {
final Constructor<?>[] constructors = clazz.getConstructors();
if (constructors != null && constructors.length > 0) {
for (final Constructor<?> constructor : constructors) {
final Annotation[][] annotations = constructor.getParameterAnnotations();
if (annotations != null && annotations.length > 0) {
for (final Annotation[] annotations1 : annotations) {
if (hasContext(annotations1)) {
return true;
}
}
}
}
}
return false;
}

private static boolean hasContext(Annotation[] annotations) {
if (annotations != null && annotations.length > 0) {
for (final Annotation annotation : annotations) {
if ("jakarta.ws.rs.core.Context".equals(annotation.annotationType().getName())
|| "javax.ws.rs.core.Context".equals(annotation.annotationType().getName())) {
return true;
}
}
}
return false;
}

@SuppressWarnings(value = "unchecked")
/* package */ T _provide() {
final T instance = referenceProvider.getInstance(clazz);
Expand Down

0 comments on commit b028a61

Please sign in to comment.