public void inject() { Map<AnnotatedElement, Class> toInject = new HashMap<>(); List<Class<?>> classes = ClassUtils.getAllSuperclasses(companion.getClass()); classes.add(0, companion.getClass()); Collections.reverse(classes); for (Field field : getAllFields(classes)) { Class aClass = injectionAnnotation(field); if (aClass != null) { toInject.put(field, aClass); } } for (Method method : companion.getClass().getMethods()) { Class aClass = injectionAnnotation(method); if (aClass != null) { toInject.put(method, aClass); } } for (Map.Entry<AnnotatedElement, Class> entry : toInject.entrySet()) { doInjection(entry.getKey(), entry.getValue()); } }
private List<Field> getAllFields(List<Class<?>> classes) { List<Field> list = new ArrayList<>(); for (Class c : classes) { if (c != Object.class) { for (Field field : c.getDeclaredFields()) { int idx = indexOfFieldWithSameName(list, field); if (idx > -1) list.set(idx, field); else list.add(field); } } } return list; }
protected void initCompanion(Element companionsElem, AbstractFrame frame) { String clientTypeId = AppConfig.getClientType().toString().toLowerCase(); Element element = companionsElem.element(clientTypeId); if (element != null) { String className = element.attributeValue("class"); if (!StringUtils.isBlank(className)) { Class aClass = getScripting().loadClassNN(className); Object companion; try { companion = aClass.newInstance(); frame.setCompanion(companion); CompanionDependencyInjector cdi = new CompanionDependencyInjector(frame, companion); cdi.setBeanLocator(beanLocator); cdi.inject(); } catch (Exception e) { throw new RuntimeException("Unable to init companion for frame", e); } } } }
throw new IllegalStateException("Can inject to fields and setter methods only"); Object instance = getInjectedInstance(type, name, element); if (instance == null) { log.warn("Unable to find an instance of type " + type + " named " + name); } else { assignValue(element, instance);
getContext().addInjectTask((c, w) -> { CompanionDependencyInjector cdi = new CompanionDependencyInjector((LegacyFrame) controller, companion); cdi.setBeanLocator(beanLocator); cdi.inject(); });