This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to annotated tag org.apache.sling.models.impl-1.1.0 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-models-impl.git
commit 025b2aee6ca7f9b11680a9fe48285854aa4ddac5 Author: Justin Edelson <jus...@apache.org> AuthorDate: Fri Aug 22 15:49:57 2014 +0000 SLING-3876 - ensuring that class-based self injection only happens on the first constructor parameter. Otherwise the @Self annotation is necessary. Also changing the ranking so that other injectors have the opportunity to inject to the first constructor parameter before. git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/models/impl@1619847 13f79535-47bb-0310-9956-ffa450edef68 --- .../sling/models/impl/ConstructorParameter.java | 2 +- .../sling/models/impl/injectors/SelfInjector.java | 15 +++++--- .../models/impl/injectors/SelfInjectorTest.java | 43 ++++++++++++++++------ 3 files changed, 41 insertions(+), 19 deletions(-) diff --git a/src/main/java/org/apache/sling/models/impl/ConstructorParameter.java b/src/main/java/org/apache/sling/models/impl/ConstructorParameter.java index eab70ae..efd4693 100644 --- a/src/main/java/org/apache/sling/models/impl/ConstructorParameter.java +++ b/src/main/java/org/apache/sling/models/impl/ConstructorParameter.java @@ -27,7 +27,7 @@ import java.lang.reflect.Type; * AnnotatedElement. This class acts as a facade to ease * compatibility with field and method injection. */ -class ConstructorParameter implements AnnotatedElement { +public class ConstructorParameter implements AnnotatedElement { private final Annotation[] annotations; private final Class<?> type; diff --git a/src/main/java/org/apache/sling/models/impl/injectors/SelfInjector.java b/src/main/java/org/apache/sling/models/impl/injectors/SelfInjector.java index 5787b46..9e06b35 100644 --- a/src/main/java/org/apache/sling/models/impl/injectors/SelfInjector.java +++ b/src/main/java/org/apache/sling/models/impl/injectors/SelfInjector.java @@ -23,6 +23,7 @@ import org.apache.felix.scr.annotations.Component; import org.apache.felix.scr.annotations.Property; import org.apache.felix.scr.annotations.Service; import org.apache.sling.models.annotations.injectorspecific.Self; +import org.apache.sling.models.impl.ConstructorParameter; import org.apache.sling.models.spi.DisposalCallbackRegistry; import org.apache.sling.models.spi.AcceptsNullName; import org.apache.sling.models.spi.Injector; @@ -36,7 +37,7 @@ import org.osgi.framework.Constants; */ @Component @Service -@Property(name = Constants.SERVICE_RANKING, intValue = 100) +@Property(name = Constants.SERVICE_RANKING, intValue = Integer.MAX_VALUE) public class SelfInjector implements Injector, InjectAnnotationProcessorFactory, AcceptsNullName { @Override @@ -49,11 +50,13 @@ public class SelfInjector implements Injector, InjectAnnotationProcessorFactory, // if the @Self annotation is present return the adaptable to be inserted directly or to be adapted from if (element.isAnnotationPresent(Self.class)) { return adaptable; - } - // otherwise apply class-based injection only if class matches or is a superclass - else if (type instanceof Class<?>) { - Class<?> requestedClass = (Class<?>)type; - if (requestedClass.isAssignableFrom(adaptable.getClass())) { + } else { + // special handling for the first constructor parameter + // apply class-based injection only if class matches or is a superclass + if (element instanceof ConstructorParameter && + ((ConstructorParameter)element).getParameterIndex() == 0 && + type instanceof Class<?> && + ((Class<?>)type).isAssignableFrom(adaptable.getClass())) { return adaptable; } } diff --git a/src/test/java/org/apache/sling/models/impl/injectors/SelfInjectorTest.java b/src/test/java/org/apache/sling/models/impl/injectors/SelfInjectorTest.java index 3e7251a..146a8b7 100644 --- a/src/test/java/org/apache/sling/models/impl/injectors/SelfInjectorTest.java +++ b/src/test/java/org/apache/sling/models/impl/injectors/SelfInjectorTest.java @@ -18,8 +18,7 @@ */ package org.apache.sling.models.impl.injectors; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertSame; +import static org.junit.Assert.*; import static org.mockito.Mockito.*; import java.lang.reflect.AnnotatedElement; @@ -29,6 +28,8 @@ import javax.servlet.http.HttpServletRequest; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.resource.ResourceResolver; import org.apache.sling.models.annotations.injectorspecific.Self; +import org.apache.sling.models.impl.ConstructorParameter; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; @@ -39,40 +40,58 @@ import org.mockito.runners.MockitoJUnitRunner; public class SelfInjectorTest { private SelfInjector injector = new SelfInjector(); - + @Mock private SlingHttpServletRequest request; + @Mock private AnnotatedElement annotatedElement; + + @Mock + private ConstructorParameter firstConstructorParameter; + + @Mock + private ConstructorParameter secondConstructorParameter; + + @Before + public void setup() { + when(firstConstructorParameter.getParameterIndex()).thenReturn(0); + when(secondConstructorParameter.getParameterIndex()).thenReturn(1); + } @Test public void testMatchingClass() { - Object result = injector.getValue(request, "notRelevant", SlingHttpServletRequest.class, annotatedElement, null); - assertSame(request, result); + assertSame(request, injector.getValue(request, "notRelevant", SlingHttpServletRequest.class, firstConstructorParameter, null)); + assertNull(injector.getValue(request, "notRelevant", SlingHttpServletRequest.class, secondConstructorParameter, null)); + assertNull(injector.getValue(request, "notRelevant", SlingHttpServletRequest.class, annotatedElement, null)); } @Test public void testMatchingSubClass() { - Object result = injector.getValue(request, "notRelevant", HttpServletRequest.class, annotatedElement, null); - assertSame(request, result); + assertSame(request, injector.getValue(request, "notRelevant", HttpServletRequest.class, firstConstructorParameter, null)); + assertNull(injector.getValue(request, "notRelevant", HttpServletRequest.class, secondConstructorParameter, null)); + assertNull(injector.getValue(request, "notRelevant", HttpServletRequest.class, annotatedElement, null)); } @Test public void testNotMatchingClass() { - Object result = injector.getValue(request, "notRelevant", ResourceResolver.class, annotatedElement, null); - assertNull(result); + assertNull(injector.getValue(request, "notRelevant", ResourceResolver.class, firstConstructorParameter, null)); + assertNull(injector.getValue(request, "notRelevant", ResourceResolver.class, secondConstructorParameter, null)); + assertNull(injector.getValue(request, "notRelevant", ResourceResolver.class, annotatedElement, null)); } @Test public void testWithNullName() { - Object result = injector.getValue(request, null, SlingHttpServletRequest.class, annotatedElement, null); - assertSame(request, result); + assertSame(request, injector.getValue(request, null, SlingHttpServletRequest.class, firstConstructorParameter, null)); + assertNull(injector.getValue(request, null, SlingHttpServletRequest.class, secondConstructorParameter, null)); + assertNull(injector.getValue(request, null, SlingHttpServletRequest.class, annotatedElement, null)); } @Test public void testMatchingClassWithSelfAnnotation() { when(annotatedElement.isAnnotationPresent(Self.class)).thenReturn(true); - Object result = injector.getValue(request, "notRelevant", SlingHttpServletRequest.class, annotatedElement, null); + Object result = injector + .getValue(request, "notRelevant", SlingHttpServletRequest.class, annotatedElement, null); assertSame(request, result); } -- To stop receiving notification emails like this one, please contact "commits@sling.apache.org" <commits@sling.apache.org>.