SLING-5710 - fixing issue where super interface methods weren't injected
authorJustin Edelson <justin@apache.org>
Tue, 3 May 2016 16:47:38 +0000 (16:47 +0000)
committerJustin Edelson <justin@apache.org>
Tue, 3 May 2016 16:47:38 +0000 (16:47 +0000)
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1742144 13f79535-47bb-0310-9956-ffa450edef68

src/main/java/org/apache/sling/models/impl/ReflectionUtil.java
src/test/java/org/apache/sling/models/impl/InterfaceInheritanceTest.java [new file with mode: 0644]
src/test/java/org/apache/sling/models/testmodels/interfaces/SubClassModel.java [new file with mode: 0644]
src/test/java/org/apache/sling/models/testmodels/interfaces/SuperClassModel.java [new file with mode: 0644]

index 1f4a488..c3ca90d 100644 (file)
@@ -55,9 +55,19 @@ public final class ReflectionUtil {
         while (type != null) {
             Method[] methods = type.getDeclaredMethods();
             addAnnotated(methods, result);
+            addAnnotatedMethodsFromInterfaces(type, result);
             type = type.getSuperclass();
         }
         return result;
+
+    }
+
+    private static void addAnnotatedMethodsFromInterfaces(Class<?> type, List<Method> result) {
+        for (Class<?> iface : type.getInterfaces()) {
+            Method[] methods = iface.getDeclaredMethods();
+            addAnnotated(methods, result);
+            addAnnotatedMethodsFromInterfaces(iface, result);
+        }
     }
 
     public static <T extends AnnotatedElement> void addAnnotated(T[] elements, List<T> set) {
diff --git a/src/test/java/org/apache/sling/models/impl/InterfaceInheritanceTest.java b/src/test/java/org/apache/sling/models/impl/InterfaceInheritanceTest.java
new file mode 100644 (file)
index 0000000..d59b127
--- /dev/null
@@ -0,0 +1,86 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.models.impl;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ValueMap;
+import org.apache.sling.api.wrappers.ValueMapDecorator;
+import org.apache.sling.models.impl.injectors.ChildResourceInjector;
+import org.apache.sling.models.impl.injectors.ValueMapInjector;
+import org.apache.sling.models.testmodels.classes.*;
+import org.apache.sling.models.testmodels.interfaces.SubClassModel;
+import org.apache.sling.models.testmodels.interfaces.SuperClassModel;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.service.component.ComponentContext;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.Map;
+
+import static org.junit.Assert.*;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+@RunWith(MockitoJUnitRunner.class)
+public class InterfaceInheritanceTest {
+
+    @Mock
+    private ComponentContext componentCtx;
+
+    @Mock
+    private BundleContext bundleContext;
+
+    private ModelAdapterFactory factory;
+
+    @Before
+    public void setup() {
+        when(componentCtx.getBundleContext()).thenReturn(bundleContext);
+        when(componentCtx.getProperties()).thenReturn(new Hashtable<String, Object>());
+
+        factory = new ModelAdapterFactory();
+        factory.activate(componentCtx);
+        ValueMapInjector valueMapInjector = new ValueMapInjector();
+        factory.bindInjector(valueMapInjector, new ServicePropertiesMap(1, 2));
+
+        factory.bindInjectAnnotationProcessorFactory(valueMapInjector,
+                Collections.<String, Object> singletonMap(Constants.SERVICE_ID, 2L));
+        factory.adapterImplementations.addClassesAsAdapterAndImplementation(SuperClassModel.class, SubClassModel.class);
+    }
+
+    @Test
+    public void testSimplePropertyModel() {
+        Map<String, Object> map = new HashMap<String, Object>();
+        map.put("superClassString", "first-value");
+        map.put("subClassString", "second-value");
+        ValueMap vm = new ValueMapDecorator(map);
+
+        Resource res = mock(Resource.class);
+        when(res.adaptTo(ValueMap.class)).thenReturn(vm);
+
+        SubClassModel model = factory.getAdapter(res, SubClassModel.class);
+        assertNotNull(model);
+        assertEquals("first-value", model.getSuperClassString());
+        assertEquals("second-value", model.getSubClassString());
+    }
+}
diff --git a/src/test/java/org/apache/sling/models/testmodels/interfaces/SubClassModel.java b/src/test/java/org/apache/sling/models/testmodels/interfaces/SubClassModel.java
new file mode 100644 (file)
index 0000000..947e3f7
--- /dev/null
@@ -0,0 +1,29 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.models.testmodels.interfaces;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.models.annotations.Model;
+
+import javax.inject.Inject;
+
+@Model(adaptables = Resource.class)
+public interface SubClassModel extends SuperClassModel {
+
+    @Inject
+    String getSubClassString();
+}
diff --git a/src/test/java/org/apache/sling/models/testmodels/interfaces/SuperClassModel.java b/src/test/java/org/apache/sling/models/testmodels/interfaces/SuperClassModel.java
new file mode 100644 (file)
index 0000000..ee749a1
--- /dev/null
@@ -0,0 +1,29 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.models.testmodels.interfaces;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.models.annotations.Model;
+
+import javax.inject.Inject;
+
+@Model(adaptables = Resource.class)
+public interface SuperClassModel {
+
+    @Inject
+    String getSuperClassString();
+}