aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cschnei...@apache.org
Subject svn commit: r1741938 - in /aries/trunk/blueprint/blueprint-maven-plugin/src: main/java/org/apache/aries/blueprint/plugin/model/ test/java/org/apache/aries/blueprint/plugin/ test/java/org/apache/aries/blueprint/plugin/test/
Date Mon, 02 May 2016 08:54:36 GMT
Author: cschneider
Date: Mon May  2 08:54:36 2016
New Revision: 1741938

URL: http://svn.apache.org/viewvc?rev=1741938&view=rev
Log:
[ARIES-1531] Add arguments to produced bean based on factory method parameters

Added:
    aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyProducedWithConstructor.java
      - copied, changed from r1741936, aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyFactoryBean.java
Modified:
    aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Bean.java
    aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Context.java
    aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/ProducedBean.java
    aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/GeneratorTest.java
    aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyFactoryBean.java

Modified: aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Bean.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Bean.java?rev=1741938&r1=1741937&r2=1741938&view=diff
==============================================================================
--- aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Bean.java
(original)
+++ aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Bean.java
Mon May  2 08:54:36 2016
@@ -89,7 +89,7 @@ public class Bean extends BeanRef {
         }
     }
 
-    private void resolveConstructorArguments(Matcher matcher) {
+    protected void resolveConstructorArguments(Matcher matcher) {
         Constructor<?>[] declaredConstructors = clazz.getDeclaredConstructors();
         for (Constructor constructor : declaredConstructors) {
             Annotation inject = constructor.getAnnotation(Inject.class);
@@ -97,44 +97,48 @@ public class Bean extends BeanRef {
             if (inject != null || autowired != null || declaredConstructors.length == 1)
{
                 Class[] parameterTypes = constructor.getParameterTypes();
                 Annotation[][] parameterAnnotations = constructor.getParameterAnnotations();
-                for (int i = 0; i < parameterTypes.length; ++i) {
-                    Annotation[] annotations = parameterAnnotations[i];
-                    String ref = null;
-                    String value = null;
-                    Value valueAnnotation = findAnnotation(annotations, Value.class);
-                    OsgiService osgiServiceAnnotation = findAnnotation(annotations, OsgiService.class);
+                resolveParametersForConstructor(matcher, parameterTypes, parameterAnnotations);
+                break;
+            }
+        }
+    }
 
-                    if (valueAnnotation != null) {
-                        value = valueAnnotation.value();
-                    }
+    protected void resolveParametersForConstructor(Matcher matcher, Class[] parameterTypes,
Annotation[][] parameterAnnotations) {
+        for (int i = 0; i < parameterTypes.length; ++i) {
+            Annotation[] annotations = parameterAnnotations[i];
+            String ref = null;
+            String value = null;
+            Value valueAnnotation = findAnnotation(annotations, Value.class);
+            OsgiService osgiServiceAnnotation = findAnnotation(annotations, OsgiService.class);
 
-                    if (osgiServiceAnnotation != null) {
-                        Named namedAnnotation = findAnnotation(annotations, Named.class);
-                        ref = namedAnnotation != null ? namedAnnotation.value() : getBeanNameFromSimpleName(parameterTypes[i].getSimpleName());
-                        OsgiServiceRef osgiServiceRef = new OsgiServiceRef(parameterTypes[i],
osgiServiceAnnotation, ref);
-                        serviceRefs.add(osgiServiceRef);
-                    }
+            if (valueAnnotation != null) {
+                value = valueAnnotation.value();
+            }
 
-                    if (ref == null && value == null && osgiServiceAnnotation
== null) {
-                        BeanRef template = new BeanRef(parameterTypes[i]);
-                        template.setQualifiersFromAnnotations(annotations);
-                        BeanRef bean = matcher.getMatching(template);
-                        if (bean != null) {
-                            ref = bean.id;
-                        } else {
-                            Named namedAnnotation = findAnnotation(annotations, Named.class);
-                            if (namedAnnotation != null) {
-                                ref = namedAnnotation.value();
-                            } else {
-                                ref = getBeanName(parameterTypes[i]);
-                            }
-                        }
-                    }
+            if (osgiServiceAnnotation != null) {
+                Named namedAnnotation = findAnnotation(annotations, Named.class);
+                ref = namedAnnotation != null ? namedAnnotation.value() : getBeanNameFromSimpleName(parameterTypes[i].getSimpleName());
+                OsgiServiceRef osgiServiceRef = new OsgiServiceRef(parameterTypes[i], osgiServiceAnnotation,
ref);
+                serviceRefs.add(osgiServiceRef);
+            }
 
-                    constructorArguments.add(new Argument(ref, value));
+            if (ref == null && value == null && osgiServiceAnnotation ==
null) {
+                BeanRef template = new BeanRef(parameterTypes[i]);
+                template.setQualifiersFromAnnotations(annotations);
+                BeanRef bean = matcher.getMatching(template);
+                if (bean != null) {
+                    ref = bean.id;
+                } else {
+                    Named namedAnnotation = findAnnotation(annotations, Named.class);
+                    if (namedAnnotation != null) {
+                        ref = namedAnnotation.value();
+                    } else {
+                        ref = getBeanName(parameterTypes[i]);
+                    }
                 }
-                break;
             }
+
+            constructorArguments.add(new Argument(ref, value));
         }
     }
 

Modified: aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Context.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Context.java?rev=1741938&r1=1741937&r2=1741938&view=diff
==============================================================================
--- aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Context.java
(original)
+++ aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Context.java
Mon May  2 08:54:36 2016
@@ -77,9 +77,9 @@ public class Context implements Matcher
                 Class<?> producedClass = method.getReturnType();
                 ProducedBean producedBean;
                 if (named != null) {
-                    producedBean = new ProducedBean(producedClass, named.value(), factoryBean,
method.getName());
+                    producedBean = new ProducedBean(producedClass, named.value(), factoryBean,
method);
                 } else {
-                    producedBean = new ProducedBean(producedClass, factoryBean, method.getName());
+                    producedBean = new ProducedBean(producedClass, factoryBean, method);
                 }
                 if (singleton != null) {
                     producedBean.setSingleton();

Modified: aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/ProducedBean.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/ProducedBean.java?rev=1741938&r1=1741937&r2=1741938&view=diff
==============================================================================
--- aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/ProducedBean.java
(original)
+++ aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/ProducedBean.java
Mon May  2 08:54:36 2016
@@ -6,9 +6,9 @@
  * 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
- * <p>
+ * <p/>
  * http://www.apache.org/licenses/LICENSE-2.0
- * <p>
+ * <p/>
  * 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
@@ -19,24 +19,34 @@
 package org.apache.aries.blueprint.plugin.model;
 
 
+import java.lang.reflect.Method;
+
 public class ProducedBean extends Bean {
     public String factoryMethod;
     public BeanRef factoryBean;
+    private Method producingMethod;
 
-    public ProducedBean(Class<?> clazz, BeanRef factoryBean, String factoryMethod)
{
+    public ProducedBean(Class<?> clazz, BeanRef factoryBean, Method factoryMethod)
{
         super(clazz);
         this.factoryBean = factoryBean;
-        this.factoryMethod = factoryMethod;
+        this.factoryMethod = factoryMethod.getName();
+        this.producingMethod = factoryMethod;
     }
 
-    public ProducedBean(Class<?> clazz, String id, BeanRef factoryBean, String factoryMethod)
{
+    public ProducedBean(Class<?> clazz, String id, BeanRef factoryBean, Method factoryMethod)
{
         super(clazz);
         this.id = id;
         this.factoryBean = factoryBean;
-        this.factoryMethod = factoryMethod;
+        this.factoryMethod = factoryMethod.getName();
+        this.producingMethod = factoryMethod;
     }
 
-    public void setSingleton(){
+    public void setSingleton() {
         this.isPrototype = false;
     }
+
+    @Override
+    protected void resolveConstructorArguments(Matcher matcher) {
+        resolveParametersForConstructor(matcher, producingMethod.getParameterTypes(), producingMethod.getParameterAnnotations());
+    }
 }

Modified: aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/GeneratorTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/GeneratorTest.java?rev=1741938&r1=1741937&r2=1741938&view=diff
==============================================================================
--- aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/GeneratorTest.java
(original)
+++ aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/GeneratorTest.java
Mon May  2 08:54:36 2016
@@ -210,6 +210,17 @@ public class GeneratorTest {
         assertEquals("produced2", xpath.evaluate("argument[8]/@ref", myBean5));
     }
 
+    @Test
+    public void testProducesBeanUsingParametersNotConstructor() throws Exception {
+        Node bean1 = getBeanById("myProducedWithConstructor");
+        assertEquals("org.apache.aries.blueprint.plugin.test.MyProducedWithConstructor",
xpath.evaluate("@class", bean1));
+        assertEquals("myFactoryBean", xpath.evaluate("@factory-ref", bean1));
+        assertEquals("createBeanWithParameters", xpath.evaluate("@factory-method", bean1));
+        assertEquals("myBean1", xpath.evaluate("argument[1]/@ref", bean1));
+        assertEquals("100", xpath.evaluate("argument[2]/@value", bean1));
+        assertEquals("ser1", xpath.evaluate("argument[3]/@ref", bean1));
+    }
+
     private static Document readToDocument(ByteArrayOutputStream os) throws ParserConfigurationException,
             SAXException, IOException {
         InputStream is = new ByteArrayInputStream(os.toByteArray());

Modified: aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyFactoryBean.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyFactoryBean.java?rev=1741938&r1=1741937&r2=1741938&view=diff
==============================================================================
--- aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyFactoryBean.java
(original)
+++ aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyFactoryBean.java
Mon May  2 08:54:36 2016
@@ -18,8 +18,12 @@
  */
 package org.apache.aries.blueprint.plugin.test;
 
+import org.ops4j.pax.cdi.api.OsgiService;
+import org.springframework.beans.factory.annotation.Value;
+
 import javax.enterprise.inject.Produces;
 import javax.inject.Inject;
+import javax.inject.Named;
 import javax.inject.Singleton;
 
 @Singleton
@@ -32,4 +36,9 @@ public class MyFactoryBean {
     public MyProduced create() {
         return new MyProduced("My message");
     }
+
+    @Produces
+    public MyProducedWithConstructor createBeanWithParameters(MyBean1 myBean1, @Value("100")
int bla, @OsgiService(filter = "myRef") @Named("ser1") ServiceC myReference) {
+        return new MyProducedWithConstructor(null);
+    }
 }

Copied: aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyProducedWithConstructor.java
(from r1741936, aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyFactoryBean.java)
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyProducedWithConstructor.java?p2=aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyProducedWithConstructor.java&p1=aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyFactoryBean.java&r1=1741936&r2=1741938&rev=1741938&view=diff
==============================================================================
--- aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyFactoryBean.java
(original)
+++ aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyProducedWithConstructor.java
Mon May  2 08:54:36 2016
@@ -6,9 +6,9 @@
  * 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
- * <p>
+ * <p/>
  * http://www.apache.org/licenses/LICENSE-2.0
- * <p>
+ * <p/>
  * 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
@@ -18,18 +18,14 @@
  */
 package org.apache.aries.blueprint.plugin.test;
 
-import javax.enterprise.inject.Produces;
+import org.ops4j.pax.cdi.api.OsgiService;
+
 import javax.inject.Inject;
-import javax.inject.Singleton;
+import javax.inject.Named;
 
-@Singleton
-public class MyFactoryBean {
-    
-    @Inject
-    ServiceB serviceB;
+public class MyProducedWithConstructor {
 
-    @Produces
-    public MyProduced create() {
-        return new MyProduced("My message");
+    @Inject
+    public MyProducedWithConstructor(@OsgiService @Named("unknown") ServiceA serviceA) {
     }
 }



Mime
View raw message