paul-bjorkstrand commented on a change in pull request #17:
URL: 
https://github.com/apache/sling-org-apache-sling-models-impl/pull/17#discussion_r443759845



##########
File path: src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java
##########
@@ -495,12 +480,97 @@ public RuntimeException inject(InjectableElement element, 
Object value) {
         }
     }
 
+    @NotNull
+    private InjectableElement getElement(InjectableElement element, Type type) 
{
+        return new InjectableElement() {
+            @Override
+            public AnnotatedElement getAnnotatedElement() {
+                return element.getAnnotatedElement();
+            }
+
+            @Override
+            public Type getType() {
+                return type;
+            }
+
+            @Override
+            public boolean isPrimitive() {
+                return element.isPrimitive();
+            }
+
+            @Override
+            public String getName() {
+                return element.getName();
+            }
+
+            @Override
+            public String getSource() {
+                return element.getSource();
+            }
+
+            @Override
+            public String getVia() {
+                return element.getVia();
+            }
+
+            @Override
+            public Class<? extends ViaProviderType> getViaProviderType() {
+                return element.getViaProviderType();
+            }
+
+            @Override
+            public boolean hasDefaultValue() {
+                return element.hasDefaultValue();
+            }
+
+            @Override
+            public Object getDefaultValue() {
+                return element.getDefaultValue() == null ? Optional.empty() : 
element.getDefaultValue();
+            }
+
+            @Override
+            public boolean isOptional(InjectAnnotationProcessor 
annotationProcessor) {
+                return true;
+            }
+        };
+    }
+
     private
     @Nullable
     RuntimeException injectElement(final InjectableElement element, final 
Object adaptable,
                                    final @NotNull DisposalCallbackRegistry 
registry, final InjectCallback callback,
                                    final @NotNull Map<ValuePreparer, Object> 
preparedValues,
                                    final @Nullable BundleContext modelContext) 
{
+        if (element instanceof InjectableField) {
+            Type genericType = ((InjectableField) 
element).getFieldGenericType();
+
+            if (genericType instanceof ParameterizedType) {
+                ParameterizedType pType = (ParameterizedType) genericType;
+
+                if (pType.getRawType().equals(Optional.class)) {
+                    InjectableElement el = getElement(element, 
pType.getActualTypeArguments()[0]);
+
+                    return injectElementInternal(el, adaptable, registry, new 
InjectCallback() {
+                        @Override
+                        public RuntimeException inject(InjectableElement 
element1, Object value) {
+                            return callback.inject(element, 
value.equals(Optional.empty())
+                                    ? value
+                                    : Optional.of(value));

Review comment:
       Looks like @justinedelson beat me to the punch with the class suggestion 
🙂 




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to