AEM Sling custom injector @Source issue.
I have written my own custom annotation 'Jeroen':
import com.asadventure.platform.component.text.JeroenInjector;
import org.apache.sling.models.annotations.Source;
import org.apache.sling.models.spi.injectorspecific.InjectAnnotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@InjectAnnotation
@Source(JeroenInjector.NAME)
public @interface Jeroen {
String property();
}
And a custom injector:
import com.asadventure.core.constant.PropertyConstants;
import com.day.cq.commons.inherit.HierarchyNodeInheritanceValueMap;
import com.day.cq.commons.inherit.InheritanceValueMap;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.models.spi.DisposalCallbackRegistry;
import org.apache.sling.models.spi.Injector;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Type;
@Component(service = {Injector.class}, property = {
Constants.SERVICE_RANKING + ":Integer=" + 4300
})
public class JeroenInjector implements Injector {
public static final String NAME = "jeroen-injector";
@Override
public String getName() {
return NAME;
}
@Override
public Object getValue(final Object adaptable, final String name, final Type type, final AnnotatedElement element,
final DisposalCallbackRegistry callbackRegistry) {
// Only class types are supported
if (!(type instanceof Class<?>)) {
return null;
}
Class<?> requestedClass = (Class<?>) type;
Resource resource = getResource(adaptable);
Page page = getResourcePage(adaptable);
if (requestedClass.equals(String.class)) {
return getValueFromInheritanceMap("as:hrefLang", resource, page);
}
return null;
}
//... more logic here
}
It seems that the getValue in the jeroenInjector class also gets called for properties that are NOT annotated with my custom '@Jeroen' annotation. I tought the @Source on my annotation would prevent this from happening:

@Inject
@Optional
@Named("configPageImage/fileReference")
String pageImageReference;
I only expect fields annotated with @Jeroen to call my injectors getValue method as here:
@Jeroen(property = "as:hrefLang")
private String jeroenTest;
What am I doing wrong? Thanks in advance ![]()
