public static <U> void processFieldContextAnnotation(U instance, ApplicationContext appContext) { processFieldContextAnnotation(instance.getClass(), instance, appContext); }
private static <U> void processFieldContextAnnotation(Class contentHandlerClass, U instance, ApplicationContext appContext) { Field[] fields = contentHandlerClass.getDeclaredFields(); // Work back up the Inheritance tree first... Class superClass = contentHandlerClass.getSuperclass(); if(superClass != null && ContentHandler.class.isAssignableFrom(superClass)) { processFieldContextAnnotation(superClass, instance, appContext); } for (Field field : fields) { AppContext appContextAnnotation = field.getAnnotation(AppContext.class); if(appContextAnnotation != null) { try { ClassUtil.setField(field, instance, appContext); } catch (IllegalAccessException e) { throw new SmooksConfigurationException("Failed to set ApplicationContext value on '" + getLongMemberName(field) + "'.", e); } } } }
/** * Configure the supplied {@link org.dhatim.delivery.ContentHandler} instance using the supplied * {@link SmooksResourceConfiguration} and {@link org.dhatim.container.ApplicationContext} instances. * @param instance The instance to be configured. * @param config The configuration. * @param appContext Associated application context. * @return The configured ContentHandler instance. * @throws SmooksConfigurationException Invalid field annotations. */ public static <U> U configure(U instance, SmooksResourceConfiguration config, ApplicationContext appContext) throws SmooksConfigurationException { AssertArgument.isNotNull(appContext, "appContext"); // process the field annotations (@AppContext)... processFieldContextAnnotation(instance, appContext); // Attach global parameters... config.attachGlobalParameters(appContext); // TODO: Add by-setter-method injection support for the app context return configure(instance, config); }
Configurator.processFieldContextAnnotation(visitor, applicationContext); Configurator.processFieldConfigAnnotations(visitor, resourceConfig, false); Configurator.initialise(visitor);