Created
February 5, 2019 03:37
-
-
Save muffik/081296958225d73e65ca6e07d79e3754 to your computer and use it in GitHub Desktop.
EjbInjector
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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