Author: justin
Date: Thu Dec 19 20:54:06 2013
New Revision: 1552420
URL: http://svn.apache.org/r1552420
Log:
default value support
Added:
sling/whiteboard/justin/yamf/org.apache.sling.yamf.api/src/main/java/org/apache/sling/yamf/api/Default.java
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/impl/DefaultTest.java
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/testmodels/classes/DefaultStringModel.java
Modified:
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/main/java/org/apache/sling/yamf/impl/YamfAdapterFactory.java
Added:
sling/whiteboard/justin/yamf/org.apache.sling.yamf.api/src/main/java/org/apache/sling/yamf/api/Default.java
URL:
http://svn.apache.org/viewvc/sling/whiteboard/justin/yamf/org.apache.sling.yamf.api/src/main/java/org/apache/sling/yamf/api/Default.java?rev=1552420&view=auto
==============================================================================
---
sling/whiteboard/justin/yamf/org.apache.sling.yamf.api/src/main/java/org/apache/sling/yamf/api/Default.java
(added)
+++
sling/whiteboard/justin/yamf/org.apache.sling.yamf.api/src/main/java/org/apache/sling/yamf/api/Default.java
Thu Dec 19 20:54:06 2013
@@ -0,0 +1,45 @@
+/*
+ * 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.yamf.api;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Default value for an injection.
+ */
+@Target({ ElementType.FIELD, ElementType.METHOD })
+@Retention(RetentionPolicy.RUNTIME)
+public @interface Default {
+
+ boolean[] booleanValues() default false;
+
+ double[] doubleValues() default 0;
+
+ float[] floatValues() default 0;
+
+ int[] intValues() default 0;
+
+ long[] longValues() default 0L;
+
+ short[] shortValues() default 0;
+
+ String[] values() default "";
+
+}
Modified:
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/main/java/org/apache/sling/yamf/impl/YamfAdapterFactory.java
URL:
http://svn.apache.org/viewvc/sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/main/java/org/apache/sling/yamf/impl/YamfAdapterFactory.java?rev=1552420&r1=1552419&r2=1552420&view=diff
==============================================================================
---
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/main/java/org/apache/sling/yamf/impl/YamfAdapterFactory.java
(original)
+++
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/main/java/org/apache/sling/yamf/impl/YamfAdapterFactory.java
Thu Dec 19 20:54:06 2013
@@ -20,10 +20,13 @@ import java.lang.reflect.AnnotatedElemen
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
+import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Proxy;
import java.lang.reflect.Type;
import java.net.URL;
import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
import java.util.Collections;
import java.util.Dictionary;
import java.util.Enumeration;
@@ -50,6 +53,7 @@ import org.apache.felix.scr.annotations.
import org.apache.sling.api.adapter.AdapterFactory;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.apache.sling.commons.osgi.ServiceUtil;
+import org.apache.sling.yamf.api.Default;
import org.apache.sling.yamf.api.Model;
import org.apache.sling.yamf.api.Optional;
import org.apache.sling.yamf.api.Projection;
@@ -245,6 +249,19 @@ public class YamfAdapterFactory implemen
}
}
+ Iterator<Method> it = injectableMethods.iterator();
+ while (it.hasNext()) {
+ Method method = it.next();
+ Default defaultAnnotation = method.getAnnotation(Default.class);
+ if (defaultAnnotation != null) {
+ Type returnType =
mapPrimitiveClasses(method.getGenericReturnType());
+ Object value = getDefaultValue(defaultAnnotation, returnType);
+ if (setMethod(method, methods, value)) {
+ it.remove();
+ }
+ }
+ }
+
if (injectableMethods.isEmpty()) {
return new MapBackedInvocationHandler(methods);
} else {
@@ -287,6 +304,19 @@ public class YamfAdapterFactory implemen
}
}
+ Iterator<Field> it = injectableFields.iterator();
+ while (it.hasNext()) {
+ Field field = it.next();
+ Default defaultAnnotation = field.getAnnotation(Default.class);
+ if (defaultAnnotation != null) {
+ Type fieldType = mapPrimitiveClasses(field.getGenericType());
+ Object value = getDefaultValue(defaultAnnotation, fieldType);
+ if (setField(field, object, value)) {
+ it.remove();
+ }
+ }
+ }
+
if (injectableFields.isEmpty()) {
try {
invokePostConstruct(object);
@@ -319,6 +349,67 @@ public class YamfAdapterFactory implemen
}
}
+ private Object getDefaultValue(Default defaultAnnotation, Type type) {
+ if (type instanceof Class) {
+ Class<?> injectedClass = (Class<?>) type;
+ if (injectedClass.isArray()) {
+ Class<?> componentType = injectedClass.getComponentType();
+ if (componentType == String.class) {
+ return defaultAnnotation.values();
+ }
+ if (componentType == Integer.TYPE) {
+ return defaultAnnotation.intValues();
+ }
+ if (componentType == Long.TYPE) {
+ return defaultAnnotation.longValues();
+ }
+ if (componentType == Boolean.TYPE) {
+ return defaultAnnotation.booleanValues();
+ }
+ if (componentType == Short.TYPE) {
+ return defaultAnnotation.shortValues();
+ }
+ if (componentType == Float.TYPE) {
+ return defaultAnnotation.floatValues();
+ }
+ if (componentType == Double.TYPE) {
+ return defaultAnnotation.doubleValues();
+ }
+
+ log.warn("Default values for {} are not supported",
componentType);
+ return null;
+ } else {
+ if (injectedClass == String.class) {
+ return defaultAnnotation.values()[0];
+ }
+ if (injectedClass == Integer.TYPE) {
+ return defaultAnnotation.intValues()[0];
+ }
+ if (injectedClass == Long.TYPE) {
+ return defaultAnnotation.longValues()[0];
+ }
+ if (injectedClass == Boolean.TYPE) {
+ return defaultAnnotation.booleanValues()[0];
+ }
+ if (injectedClass == Short.TYPE) {
+ return defaultAnnotation.shortValues()[0];
+ }
+ if (injectedClass == Float.TYPE) {
+ return defaultAnnotation.floatValues()[0];
+ }
+ if (injectedClass == Double.TYPE) {
+ return defaultAnnotation.doubleValues()[0];
+ }
+
+ log.warn("Default values for {} are not supported",
injectedClass);
+ return null;
+ }
+ } else {
+ log.warn("Cannot provide default for {}", type);
+ return null;
+ }
+ }
+
private Object getAdaptable(Object adaptable, AnnotatedElement point) {
Projection projection = point.getAnnotation(Projection.class);
if (projection == null) {
Added:
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/impl/DefaultTest.java
URL:
http://svn.apache.org/viewvc/sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/impl/DefaultTest.java?rev=1552420&view=auto
==============================================================================
---
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/impl/DefaultTest.java
(added)
+++
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/impl/DefaultTest.java
Thu Dec 19 20:54:06 2013
@@ -0,0 +1,56 @@
+/*
+ * 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.yamf.impl;
+
+import static org.junit.Assert.*;
+import static org.mockito.Mockito.*;
+
+import java.util.Collections;
+
+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.yamf.impl.injectors.ValueMapInjector;
+import org.apache.sling.yamf.testmodels.classes.DefaultStringModel;
+import org.junit.Before;
+import org.junit.Test;
+import org.osgi.framework.Constants;
+
+public class DefaultTest {
+
+ private YamfAdapterFactory factory;
+
+ @Before
+ public void setup() {
+ factory = new YamfAdapterFactory();
+ factory.bindInjector(new ValueMapInjector(),
+ Collections.<String, Object>
singletonMap(Constants.SERVICE_ID, 0L));
+ }
+
+ @Test
+ public void testDefaultStringValue() {
+ ValueMap vm = new ValueMapDecorator(Collections.<String,
Object>emptyMap());
+
+ Resource res = mock(Resource.class);
+ when(res.adaptTo(ValueMap.class)).thenReturn(vm);
+
+ DefaultStringModel model = factory.getAdapter(res,
DefaultStringModel.class);
+ assertNotNull(model);
+ assertEquals("firstDefault", model.getFirstProperty());
+ assertEquals(2, model.getSecondProperty().length);
+ }
+}
Added:
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/testmodels/classes/DefaultStringModel.java
URL:
http://svn.apache.org/viewvc/sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/testmodels/classes/DefaultStringModel.java?rev=1552420&view=auto
==============================================================================
---
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/testmodels/classes/DefaultStringModel.java
(added)
+++
sling/whiteboard/justin/yamf/org.apache.sling.yamf.impl/src/test/java/org/apache/sling/yamf/testmodels/classes/DefaultStringModel.java
Thu Dec 19 20:54:06 2013
@@ -0,0 +1,43 @@
+/*
+ * 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.yamf.testmodels.classes;
+
+import javax.inject.Inject;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.yamf.api.Default;
+import org.apache.sling.yamf.api.Model;
+
+@Model(adaptables = Resource.class)
+public class DefaultStringModel {
+
+ @Inject
+ @Default(values = "firstDefault")
+ private String firstProperty;
+
+ @Inject
+ @Default(values = { "firstDefault", "secondDefault" })
+ private String[] secondProperty;
+
+ public String getFirstProperty() {
+ return firstProperty;
+ }
+
+ public String[] getSecondProperty() {
+ return secondProperty;
+ }
+}