public Object getValue()

in src/main/java/org/apache/sling/models/impl/injectors/ResourcePathInjector.java [59:105]


    public Object getValue(@NotNull Object adaptable, String name, @NotNull Type declaredType, @NotNull AnnotatedElement element,
            @NotNull DisposalCallbackRegistry callbackRegistry) {
        String[] resourcePaths = null;
        Path pathAnnotation = element.getAnnotation(Path.class);
        ResourcePath resourcePathAnnotation = element.getAnnotation(ResourcePath.class);
        if (pathAnnotation != null) {
            resourcePaths = getPathsFromAnnotation(pathAnnotation);
        } else if (resourcePathAnnotation != null) {
            resourcePaths = getPathsFromAnnotation(resourcePathAnnotation);
        }
        if (ArrayUtils.isEmpty(resourcePaths) && name != null) {
            // try the valuemap
            ValueMap map = getValueMap(adaptable);
            if (map != null) {
                resourcePaths = map.get(name, String[].class);
            }
        }
        if (ArrayUtils.isEmpty(resourcePaths)) {
            // could not find a path to inject
            return null;
        }

        ResourceResolver resolver = getResourceResolver(adaptable);
        if (resolver == null) {
            LOG.debug("Could not get resolver from adaptable {}", adaptable);
            return null;
        }
        List<Resource> resources = getResources(resolver, resourcePaths, name);

        if (resources == null || resources.isEmpty()) {
            return null;
        }
        // unwrap/wrap if necessary
        if (isDeclaredTypeCollection(declaredType)) {
            return resources;
        } if (declaredType instanceof Class<?> && ((Class<?>)declaredType).isArray()){
            return resources.toArray(new Resource[0]);
        }
         if (resources.size() == 1) {
            return resources.get(0);
        } else {
            // multiple resources to inject, but field is not a list
            LOG.warn("Cannot inject multiple resources into field {} since it is not declared as a list", name);
            return null;
        }

    }