in src/main/java/org/apache/sling/models/impl/model/AbstractInjectableElement.java [145:241]
private static Object getDefaultValue(AnnotatedElement element, Type type, InjectAnnotationProcessor2 annotationProcessor) {
if (annotationProcessor != null && annotationProcessor.hasDefault()) {
return annotationProcessor.getDefault();
}
Default defaultAnnotation = element.getAnnotation(Default.class);
if (defaultAnnotation == null) {
return null;
}
Object value = null;
if (type instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType)type;
Type rawType = parameterizedType.getRawType();
if ((rawType == Collection.class || rawType == List.class)
&& parameterizedType.getActualTypeArguments().length > 0) {
Type itemType = parameterizedType.getActualTypeArguments()[0];
if (itemType == String.class) {
value = arrayToTypedList(defaultAnnotation.values());
} else if (itemType == Integer.class) {
value = arrayToTypedList(defaultAnnotation.intValues());
} else if (itemType == Long.class) {
value = arrayToTypedList(defaultAnnotation.longValues());
} else if (itemType == Boolean.class) {
value = arrayToTypedList(defaultAnnotation.booleanValues());
} else if (itemType == Short.class) {
value = arrayToTypedList(defaultAnnotation.shortValues());
} else if (itemType == Float.class) {
value = arrayToTypedList(defaultAnnotation.floatValues());
} else if (itemType == Double.class) {
value = arrayToTypedList(defaultAnnotation.doubleValues());
} else {
log.warn("Default values for {} List/Collection are not supported", itemType);
}
}
else {
log.warn("Cannot provide default for {}", type);
}
}
else if (type instanceof Class) {
Class<?> injectedClass = (Class<?>) type;
if (injectedClass.isArray()) {
Class<?> componentType = injectedClass.getComponentType();
if (componentType == String.class) {
value = defaultAnnotation.values();
} else if (componentType == Integer.TYPE) {
value = defaultAnnotation.intValues();
} else if (componentType == Integer.class) {
value = ArrayUtils.toObject(defaultAnnotation.intValues());
} else if (componentType == Long.TYPE) {
value = defaultAnnotation.longValues();
} else if (componentType == Long.class) {
value = ArrayUtils.toObject(defaultAnnotation.longValues());
} else if (componentType == Boolean.TYPE) {
value = defaultAnnotation.booleanValues();
} else if (componentType == Boolean.class) {
value = ArrayUtils.toObject(defaultAnnotation.booleanValues());
} else if (componentType == Short.TYPE) {
value = defaultAnnotation.shortValues();
} else if (componentType == Short.class) {
value = ArrayUtils.toObject(defaultAnnotation.shortValues());
} else if (componentType == Float.TYPE) {
value = defaultAnnotation.floatValues();
} else if (componentType == Float.class) {
value = ArrayUtils.toObject(defaultAnnotation.floatValues());
} else if (componentType == Double.TYPE) {
value = defaultAnnotation.doubleValues();
} else if (componentType == Double.class) {
value = ArrayUtils.toObject(defaultAnnotation.doubleValues());
} else {
log.warn("Default values for {} are not supported", componentType);
}
} else {
if (injectedClass == String.class) {
value = defaultAnnotation.values().length == 0 ? "" : defaultAnnotation.values()[0];
} else if (injectedClass == Integer.class) {
value = defaultAnnotation.intValues().length == 0 ? 0 : defaultAnnotation.intValues()[0];
} else if (injectedClass == Long.class) {
value = defaultAnnotation.longValues().length == 0 ? 0l : defaultAnnotation.longValues()[0];
} else if (injectedClass == Boolean.class) {
value = defaultAnnotation.booleanValues().length == 0 ? false : defaultAnnotation.booleanValues()[0];
} else if (injectedClass == Short.class) {
value = defaultAnnotation.shortValues().length == 0 ? ((short) 0) : defaultAnnotation.shortValues()[0];
} else if (injectedClass == Float.class) {
value = defaultAnnotation.floatValues().length == 0 ? 0f : defaultAnnotation.floatValues()[0];
} else if (injectedClass == Double.class) {
value = defaultAnnotation.doubleValues().length == 0 ? 0d : defaultAnnotation.doubleValues()[0];
} else {
log.warn("Default values for {} are not supported", injectedClass);
}
}
} else {
log.warn("Cannot provide default for {}", type);
}
return value;
}