From 212b6d506aa0ecf41f9870c38eec5b95e24a5855 Mon Sep 17 00:00:00 2001 From: "James R. Perkins" Date: Thu, 15 Aug 2024 18:05:03 -0700 Subject: [PATCH] Do not use internal JUnit utilities. Signed-off-by: James R. Perkins --- .../InjectionProducerExtension.java | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/extension/src/main/java/dev/resteasy/junit/extension/extensions/InjectionProducerExtension.java b/extension/src/main/java/dev/resteasy/junit/extension/extensions/InjectionProducerExtension.java index fbc0c13..2a287ef 100644 --- a/extension/src/main/java/dev/resteasy/junit/extension/extensions/InjectionProducerExtension.java +++ b/extension/src/main/java/dev/resteasy/junit/extension/extensions/InjectionProducerExtension.java @@ -5,10 +5,8 @@ package dev.resteasy.junit.extension.extensions; -import static org.junit.platform.commons.util.AnnotationUtils.findAnnotatedFields; -import static org.junit.platform.commons.util.ReflectionUtils.makeAccessible; - import java.lang.reflect.Field; +import java.lang.reflect.Modifier; import java.util.ServiceLoader; import java.util.concurrent.BlockingDeque; import java.util.concurrent.LinkedBlockingDeque; @@ -24,8 +22,7 @@ import org.junit.jupiter.api.extension.ParameterContext; import org.junit.jupiter.api.extension.ParameterResolutionException; import org.junit.jupiter.api.extension.ParameterResolver; -import org.junit.platform.commons.util.ExceptionUtils; -import org.junit.platform.commons.util.ReflectionUtils; +import org.junit.platform.commons.support.AnnotationSupport; import dev.resteasy.junit.extension.api.InjectionProducer; @@ -109,18 +106,18 @@ public void afterAll(final ExtensionContext context) { } private void injectStaticFields(final ExtensionContext context, final Class testClass) { - injectFields(context, null, testClass, ReflectionUtils::isStatic); + injectFields(context, null, testClass, (f) -> Modifier.isStatic(f.getModifiers())); } private void injectInstanceFields(final ExtensionContext context, final Object instance) { - injectFields(context, instance, instance.getClass(), ReflectionUtils::isNotStatic); + injectFields(context, instance, instance.getClass(), (f) -> !Modifier.isStatic(f.getModifiers())); } private void injectFields(final ExtensionContext context, final Object testInstance, final Class testClass, final Predicate predicate) { - findAnnotatedFields(testClass, Inject.class, predicate).forEach(field -> { - if (ReflectionUtils.isFinal(field)) { + AnnotationSupport.findAnnotatedFields(testClass, Inject.class, predicate).forEach(field -> { + if (Modifier.isFinal(field.getModifiers())) { throw new ExtensionConfigurationException( String.format("Field '%s' cannot be final for injecting a REST client.", field)); } @@ -142,9 +139,15 @@ private void injectFields(final ExtensionContext context, final Object testInsta if (value instanceof AutoCloseable) { resources.add((AutoCloseable) value); } - makeAccessible(field).set(testInstance, value); + if (field.trySetAccessible()) { + field.set(testInstance, value); + } else { + throw new ParameterResolutionException( + String.format("Could not make field %s accessible for injection.", field)); + } } catch (Throwable e) { - throw ExceptionUtils.throwAsUncheckedException(e); + throw new ParameterResolutionException( + String.format("Could not make field %s accessible for injection.", field)); } }); }