Skip to content

Instantly share code, notes, and snippets.

@muffik
Created February 5, 2019 03:37
Show Gist options
  • Save muffik/081296958225d73e65ca6e07d79e3754 to your computer and use it in GitHub Desktop.
Save muffik/081296958225d73e65ca6e07d79e3754 to your computer and use it in GitHub Desktop.
EjbInjector
package ru.brbpm.tnuzdo.utils;
import javax.ejb.EJB;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.Arrays;
public class TestUtils {
private static void injectEjb(Object instance, Object fieldValue) throws Exception {
Class<?> instanceClazz = instance.getClass();
Class<?> valueClazz = fieldValue.getClass();
while (!instanceClazz.equals(Object.class)) {
Field[] fields = instanceClazz.getDeclaredFields();
for (Field field : fields) {
Annotation[] annotations = field.getDeclaredAnnotations();
for (Annotation annotation : annotations) {
if (EJB.class.equals(annotation.annotationType()) && (
valueClazz.isAssignableFrom(field.getType())
|| Arrays.asList(valueClazz.getInterfaces()).contains(field.getType())
)
) {
field.setAccessible(true);
field.set(instance, fieldValue);
}
}
}
instanceClazz = instanceClazz.getSuperclass();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment