Refactor annotation element retrieval
Signed-off-by: Simon Vacek <simonvacky@email.cz>
This commit is contained in:
parent
ef54d20be1
commit
5a6ac58107
8 changed files with 31 additions and 67 deletions
|
@ -53,12 +53,6 @@
|
|||
<groupId>com.microsoft.sqlserver</groupId>
|
||||
<artifactId>mssql-jdbc</artifactId>
|
||||
</dependency>
|
||||
<!-- Temporary dependency until we figure out how we want to support OAuth -->
|
||||
<dependency>
|
||||
<groupId>com.nimbusds</groupId>
|
||||
<artifactId>oauth2-oidc-sdk</artifactId>
|
||||
<version>11.13</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -25,10 +25,10 @@ public class InstanceContext<T, A extends Annotation> {
|
|||
this.supplier = supplier;
|
||||
this.annotation = annotation;
|
||||
this.requestedValueType = requestedValueType;
|
||||
this.config = supplier.getConfig(annotation);
|
||||
this.lifeCycle = supplier.getLifeCycle(annotation);
|
||||
this.ref = supplier.getRef(annotation);
|
||||
this.realmRef = supplier.getRealmRef(annotation);
|
||||
this.config = (Class<?>) supplier.getAnnotationElementValue(annotation, SupplierHelpers.CONFIG);
|
||||
this.lifeCycle = (LifeCycle) supplier.getAnnotationElementValue(annotation, SupplierHelpers.LIFECYCLE);
|
||||
this.ref = (String) supplier.getAnnotationElementValue(annotation, SupplierHelpers.REF);
|
||||
this.realmRef = (String) supplier.getAnnotationElementValue(annotation, SupplierHelpers.REALM_REF);
|
||||
}
|
||||
|
||||
public InstanceContext(Registry registry, Supplier<T, A> supplier, Class<? extends T> requestedValueType, String ref, Class<?> config) {
|
||||
|
|
|
@ -2,7 +2,6 @@ package org.keycloak.test.framework.injection;
|
|||
|
||||
import org.jboss.logging.Logger;
|
||||
import org.junit.jupiter.api.extension.ExtensionContext;
|
||||
import org.keycloak.test.framework.annotations.InjectRealm;
|
||||
import org.keycloak.test.framework.config.Config;
|
||||
import org.keycloak.test.framework.realm.DefaultRealmConfig;
|
||||
import org.keycloak.test.framework.realm.ManagedRealm;
|
||||
|
@ -62,7 +61,7 @@ public class Registry {
|
|||
|
||||
private <T> T getDeployedDependency(Class<T> typeClass, InstanceContext dependent) {
|
||||
InstanceContext dependency;
|
||||
if(!dependent.getRealmRef().equals("")) {
|
||||
if(typeClass.equals(ManagedRealm.class) && !dependent.getRealmRef().equals("")) {
|
||||
dependency = getDeployedInstance(typeClass, dependent.getRealmRef());
|
||||
} else {
|
||||
dependency = getDeployedInstance(typeClass);
|
||||
|
@ -85,7 +84,7 @@ public class Registry {
|
|||
private <T> T getRequestedDependency(Class<T> typeClass, InstanceContext dependent) {
|
||||
InstanceContext dependency;
|
||||
RequestedInstance requestedDependency;
|
||||
if(!dependent.getRealmRef().equals("")) {
|
||||
if(typeClass.equals(ManagedRealm.class) && !dependent.getRealmRef().equals("")) {
|
||||
requestedDependency = getRequestedInstance(typeClass, dependent.getRealmRef());
|
||||
} else {
|
||||
requestedDependency = getRequestedInstance(typeClass);
|
||||
|
@ -256,7 +255,7 @@ public class Registry {
|
|||
Supplier supplier = i.getSupplier();
|
||||
if (supplier.getAnnotationClass().equals(a.annotationType())
|
||||
&& valueType.isAssignableFrom(i.getValue().getClass())
|
||||
&& supplier.getRef(a).equals(i.getRef()) ) {
|
||||
&& supplier.getAnnotationElementValue(a, SupplierHelpers.REF).equals(i.getRef()) ) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,15 +9,13 @@ public class RequestedInstance<T, A extends Annotation> {
|
|||
private final Class<? extends T> valueType;
|
||||
private final LifeCycle lifeCycle;
|
||||
private final String ref;
|
||||
private final String realmRef;
|
||||
|
||||
public RequestedInstance(Supplier<T, A> supplier, A annotation, Class<? extends T> valueType) {
|
||||
this.supplier = supplier;
|
||||
this.annotation = annotation;
|
||||
this.valueType = valueType;
|
||||
this.lifeCycle = supplier.getLifeCycle(annotation);
|
||||
this.ref = supplier.getRef(annotation);
|
||||
this.realmRef = supplier.getRealmRef(annotation);
|
||||
this.lifeCycle = (LifeCycle) supplier.getAnnotationElementValue(annotation, SupplierHelpers.LIFECYCLE);
|
||||
this.ref = (String) supplier.getAnnotationElementValue(annotation, SupplierHelpers.REF);
|
||||
}
|
||||
|
||||
public Supplier<T, A> getSupplier() {
|
||||
|
@ -40,7 +38,4 @@ public class RequestedInstance<T, A extends Annotation> {
|
|||
return ref;
|
||||
}
|
||||
|
||||
public String getRealmRef() {
|
||||
return realmRef;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,60 +13,32 @@ public interface Supplier<T, S extends Annotation> {
|
|||
|
||||
T getValue(InstanceContext<T, S> instanceContext);
|
||||
|
||||
default Class<?> getConfig(S annotation) {
|
||||
default Object getAnnotationElementValue(S annotation, String annotationAttribute) {
|
||||
if (annotation != null) {
|
||||
Optional<Method> config = Arrays.stream(annotation.annotationType().getMethods()).filter(m -> m.getName().equals("config")).findFirst();
|
||||
if (config.isPresent()) {
|
||||
Optional<Method> annotationMethod = Arrays.stream(annotation.annotationType().getMethods()).filter(m -> m.getName().equals(annotationAttribute)).findFirst();
|
||||
if (annotationMethod.isPresent()) {
|
||||
try {
|
||||
return (Class<?>) config.get().invoke(annotation);
|
||||
return annotationMethod.get().invoke(annotation);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
return getAnnotationElementValue(annotationAttribute);
|
||||
}
|
||||
|
||||
default LifeCycle getLifeCycle(S annotation) {
|
||||
if (annotation != null) {
|
||||
Optional<Method> lifecycle = Arrays.stream(annotation.annotationType().getMethods()).filter(m -> m.getName().equals("lifecycle")).findFirst();
|
||||
if (lifecycle.isPresent()) {
|
||||
try {
|
||||
return (LifeCycle) lifecycle.get().invoke(annotation);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
default Object getAnnotationElementValue(String annotationAttribute) {
|
||||
switch (annotationAttribute) {
|
||||
case SupplierHelpers.LIFECYCLE -> {
|
||||
return this.getDefaultLifecycle();
|
||||
}
|
||||
case SupplierHelpers.REF, SupplierHelpers.REALM_REF -> {
|
||||
return "";
|
||||
}
|
||||
default -> {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return getDefaultLifecycle();
|
||||
}
|
||||
|
||||
default String getRef(S annotation) {
|
||||
if (annotation != null) {
|
||||
Optional<Method> ref = Arrays.stream(annotation.annotationType().getMethods()).filter(m -> m.getName().equals("ref")).findFirst();
|
||||
if (ref.isPresent()) {
|
||||
try {
|
||||
return (String) ref.get().invoke(annotation);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
default String getRealmRef(S annotation) {
|
||||
if (annotation != null) {
|
||||
Optional<Method> realmRef = Arrays.stream(annotation.annotationType().getMethods()).filter(m -> m.getName().equals("realmRef")).findFirst();
|
||||
if (realmRef.isPresent()) {
|
||||
try {
|
||||
return (String) realmRef.get().invoke(annotation);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
default LifeCycle getDefaultLifecycle() {
|
||||
|
|
|
@ -4,6 +4,11 @@ import java.lang.reflect.Constructor;
|
|||
|
||||
public class SupplierHelpers {
|
||||
|
||||
public static final String CONFIG = "config";
|
||||
public static final String LIFECYCLE = "lifecycle";
|
||||
public static final String REF = "ref";
|
||||
public static final String REALM_REF = "realmRef";
|
||||
|
||||
public static <T> T getInstance(Class<T> clazz) {
|
||||
try {
|
||||
Constructor<T> declaredConstructor = clazz.getDeclaredConstructor();
|
||||
|
@ -13,5 +18,4 @@ public class SupplierHelpers {
|
|||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -34,7 +34,7 @@ public class OAuthClientSupplier implements Supplier<OAuthClient, InjectOAuthCli
|
|||
}
|
||||
|
||||
@Override
|
||||
public LifeCycle getLifeCycle(InjectOAuthClient annotation) {
|
||||
public LifeCycle getDefaultLifecycle() {
|
||||
return LifeCycle.GLOBAL;
|
||||
}
|
||||
|
||||
|
|
|
@ -34,7 +34,7 @@ public abstract class AbstractWebDriverSupplier implements Supplier<WebDriver, I
|
|||
}
|
||||
|
||||
@Override
|
||||
public LifeCycle getLifeCycle(InjectWebDriver annotation) {
|
||||
public LifeCycle getDefaultLifecycle() {
|
||||
return LifeCycle.GLOBAL;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue