in src/main/java/org/apache/sling/scripting/java/impl/ServletWrapper.java [185:233]
private void injectFields(final Servlet servlet) {
for (Field field : servlet.getClass().getDeclaredFields()) {
if (field.isAnnotationPresent(Inject.class)) {
field.setAccessible(true);
try {
Type type = field.getGenericType();
if (type instanceof Class) {
Class<?> injectedClass = (Class<?>) type;
if (injectedClass.isInstance(scriptHelper)) {
field.set(servlet, scriptHelper);
} else if (injectedClass.isArray()) {
Object[] services = scriptHelper.getServices(injectedClass.getComponentType(), null);
Object arr = Array.newInstance(injectedClass.getComponentType(), services.length);
for (int i = 0; i < services.length; i++) {
Array.set(arr, i, services[i]);
}
field.set(servlet, arr);
} else {
field.set(servlet, scriptHelper.getService(injectedClass));
}
} else if (type instanceof ParameterizedType) {
ParameterizedType ptype = (ParameterizedType) type;
if (ptype.getActualTypeArguments().length != 1) {
logger.warn("Field {} of {} has more than one type parameter.", field.getName(), sourcePath);
continue;
}
Class<?> collectionType = (Class<?>) ptype.getRawType();
if (!(collectionType.equals(Collection.class) ||
collectionType.equals(List.class))) {
logger.warn("Field {} of {} was not an injectable collection type.", field.getName(), sourcePath);
continue;
}
Class<?> serviceType = (Class<?>) ptype.getActualTypeArguments()[0];
Object[] services = scriptHelper.getServices(serviceType, null);
field.set(servlet, Arrays.asList(services));
} else {
logger.warn("Field {} of {} was not an injectable type.", field.getName(), sourcePath);
}
} catch (final IllegalArgumentException e) {
logger.error(String.format("Unable to inject into field %s of %s.", field.getName(), sourcePath), e);
} catch (final IllegalAccessException e) {
logger.error(String.format("Unable to inject into field %s of %s.", field.getName(), sourcePath), e);
} finally {
field.setAccessible(false);
}
}
}
}