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.0.6 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-models-impl.git
commit e95b12226206370f4666f0cca1d1ce2390f58aea Author: Justin Edelson <[email protected]> AuthorDate: Tue Jun 24 18:56:45 2014 +0000 SLING-3677 - converting arrays to lists in ValueMapInjector. Thanks to Krystian Panek for the patch! git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/models/impl@1605153 13f79535-47bb-0310-9956-ffa450edef68 --- .../models/impl/injectors/ValueMapInjector.java | 23 ++++++++++++ .../models/impl/ResourceModelClassesTest.java | 21 +++++++++++ .../sling/models/testmodels/classes/ListModel.java | 41 ++++++++++++++++++++++ 3 files changed, 85 insertions(+) diff --git a/src/main/java/org/apache/sling/models/impl/injectors/ValueMapInjector.java b/src/main/java/org/apache/sling/models/impl/injectors/ValueMapInjector.java index ee2d419..9c4a123 100644 --- a/src/main/java/org/apache/sling/models/impl/injectors/ValueMapInjector.java +++ b/src/main/java/org/apache/sling/models/impl/injectors/ValueMapInjector.java @@ -18,7 +18,11 @@ package org.apache.sling.models.impl.injectors; import java.lang.reflect.AnnotatedElement; import java.lang.reflect.Array; +import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; import org.apache.commons.lang.ClassUtils; import org.apache.commons.lang.StringUtils; @@ -83,6 +87,25 @@ public class ValueMapInjector implements Injector, InjectAnnotationProcessorFact } return null; } + } else if (type instanceof ParameterizedType) { + // list support + ParameterizedType pType = (ParameterizedType) type; + if (pType.getActualTypeArguments().length != 1) { + return null; + } + Class<?> collectionType = (Class<?>) pType.getRawType(); + if (!(collectionType.equals(Collection.class) || collectionType.equals(List.class))) { + return null; + } + + Class<?> itemType = (Class<?>) pType.getActualTypeArguments()[0]; + Object array = map.get(name, Array.newInstance(itemType, 0).getClass()); + if (array == null) { + return null; + + } + + return Arrays.asList((Object[]) array); } else { log.debug("ValueMapInjector doesn't support non-class types {}", type); return null; diff --git a/src/test/java/org/apache/sling/models/impl/ResourceModelClassesTest.java b/src/test/java/org/apache/sling/models/impl/ResourceModelClassesTest.java index 52d41f8..3f020a9 100644 --- a/src/test/java/org/apache/sling/models/impl/ResourceModelClassesTest.java +++ b/src/test/java/org/apache/sling/models/impl/ResourceModelClassesTest.java @@ -35,6 +35,7 @@ import org.apache.sling.models.testmodels.classes.ArrayWrappersModel; import org.apache.sling.models.testmodels.classes.ChildModel; import org.apache.sling.models.testmodels.classes.ChildResourceModel; import org.apache.sling.models.testmodels.classes.ChildValueMapModel; +import org.apache.sling.models.testmodels.classes.ListModel; import org.apache.sling.models.testmodels.classes.ParentModel; import org.apache.sling.models.testmodels.classes.ResourceModelWithRequiredField; import org.apache.sling.models.testmodels.classes.ResourceModelWithRequiredFieldOptionalStrategy; @@ -141,6 +142,26 @@ public class ResourceModelClassesTest { } @Test + public void testListModel() { + Map<String, Object> map = new HashMap<String, Object>(); + map.put("intList", new Integer[] {1, 2, 9, 8}); + map.put("stringList", new String[] {"hello", "world"}); + + ValueMap vm = new ValueMapDecorator(map); + Resource res = mock(Resource.class); + when(res.adaptTo(ValueMap.class)).thenReturn(vm); + + ListModel model = factory.getAdapter(res, ListModel.class); + assertNotNull(model); + + assertEquals(4, model.getIntList().size()); + assertEquals(new Integer(2), model.getIntList().get(1)); + + assertEquals(2, model.getStringList().size()); + assertEquals("hello", model.getStringList().get(0)); + } + + @Test public void testRequiredPropertyModel() { Map<String, Object> map = new HashMap<String, Object>(); map.put("first", "first-value"); diff --git a/src/test/java/org/apache/sling/models/testmodels/classes/ListModel.java b/src/test/java/org/apache/sling/models/testmodels/classes/ListModel.java new file mode 100644 index 0000000..b2ec3aa --- /dev/null +++ b/src/test/java/org/apache/sling/models/testmodels/classes/ListModel.java @@ -0,0 +1,41 @@ +/* + * 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.classes; + +import org.apache.sling.api.resource.Resource; +import org.apache.sling.models.annotations.Model; + +import javax.inject.Inject; +import java.util.List; + +@Model(adaptables = Resource.class) +public class ListModel { + + @Inject + private List<Integer> intList; + + @Inject + private List<String> stringList; + + public List<Integer> getIntList() { + return intList; + } + + public List<String> getStringList() { + return stringList; + } +} \ No newline at end of file -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
