aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cschnei...@apache.org
Subject svn commit: r1724229 - in /aries/trunk/blueprint/blueprint-maven-plugin/src: main/java/org/apache/aries/blueprint/plugin/model/ test/java/org/apache/aries/blueprint/plugin/bad/ test/java/org/apache/aries/blueprint/plugin/model/ test/java/org/apache/ari...
Date Tue, 12 Jan 2016 13:42:00 GMT
Author: cschneider
Date: Tue Jan 12 13:42:00 2016
New Revision: 1724229

URL: http://svn.apache.org/viewvc?rev=1724229&view=rev
Log:
[ARIES-1474] Fix init/destroy method inheritence.

Added:
    aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/bad/
    aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/bad/BadBean1.java
      - copied, changed from r1724022, aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/ParentBean.java
    aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/bad/BadBean2.java
      - copied, changed from r1724022, aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/ParentBean.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/test/java/org/apache/aries/blueprint/plugin/model/BeanTest.java
    aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyBean1.java
    aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/ParentBean.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=1724229&r1=1724228&r2=1724229&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
Tue Jan 12 13:42:00 2016
@@ -34,6 +34,9 @@ import javax.persistence.PersistenceUnit
 
 import org.springframework.stereotype.Component;
 
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Iterables;
+
 public class Bean extends BeanRef {
     public String initMethod;
     public String destroyMethod;
@@ -45,15 +48,16 @@ public class Bean extends BeanRef {
     public Bean(Class<?> clazz) {
         super(clazz, BeanRef.getBeanName(clazz));
 
-        for (Method method : clazz.getDeclaredMethods()) {
-            PostConstruct postConstruct = getEffectiveAnnotation(method, PostConstruct.class);
-            if (postConstruct != null) {
-                this.initMethod = method.getName();
-            }
-            PreDestroy preDestroy = getEffectiveAnnotation(method, PreDestroy.class);
-            if (preDestroy != null) {
-                this.destroyMethod = method.getName();
-            }
+        // Init method
+        Method initMethod = getMethodWithAnnotation(clazz, PostConstruct.class);
+        if (initMethod != null) {
+            this.initMethod = initMethod.getName();
+        }
+
+        // Destroy method
+        Method destroyMethod = getMethodWithAnnotation(clazz, PreDestroy.class);
+        if (destroyMethod != null) {
+            this.destroyMethod = destroyMethod.getName();
         }
         this.isPrototype = isPrototype(clazz);
         this.persistenceFields = getPersistenceFields();
@@ -81,7 +85,7 @@ public class Bean extends BeanRef {
         }
         return persistenceFields.toArray(new Field[]{});
     }
-    
+
     public void resolve(Matcher matcher) {
         Class<?> curClass = this.clazz;
         while (curClass != null && curClass != Object.class) {
@@ -89,7 +93,7 @@ public class Bean extends BeanRef {
             curClass = curClass.getSuperclass();
         }
     }
-    
+
     private void resolveProperties(Matcher matcher, Class<?> curClass) {
         for (Field field : curClass.getDeclaredFields()) {
             Property prop = Property.create(matcher, field);
@@ -99,46 +103,27 @@ public class Bean extends BeanRef {
         }
     }
 
-    private static <T extends Annotation> T getEffectiveAnnotation(Method method, Class<T>
annotationClass) {
-        final Class<?> methodClass = method.getDeclaringClass();
-        final String name = method.getName();
-        final Class<?>[] params = method.getParameterTypes();
-
-        // 1. Current class
-        final T rootAnnotation = method.getAnnotation(annotationClass);
-        if (rootAnnotation != null) {
-            return rootAnnotation;
-        }
-
-        // 2. Superclass
-        final Class<?> superclass = methodClass.getSuperclass();
-        if (superclass != null) {
-            final T annotation = getMethodAnnotation(superclass, name, params, annotationClass);
-            if (annotation != null)
-                return annotation;
-        }
-
-        // 3. Interfaces
-        for (final Class<?> intfs : methodClass.getInterfaces()) {
-            final T annotation = getMethodAnnotation(intfs, name, params, annotationClass);
-            if (annotation != null)
-                return annotation;
-        }
-
-        return null;
-    }
-
-    private static <T extends Annotation> T getMethodAnnotation(Class<?> searchClass,
String name, Class<?>[] params,
-            Class<T> annotationClass) {
-        try {
-            Method method = searchClass.getMethod(name, params);
-            return getEffectiveAnnotation(method, annotationClass);
-        } catch (NoSuchMethodException e) {
-            return null;
+    private static <T extends Annotation> Method getMethodWithAnnotation(Class<?>
classToSearch,
+                                                                         Class<T> annotationClass)
{
+        List<Method> methods = getMethodsWithAnnotation(classToSearch, annotationClass);
+        Preconditions.checkArgument(methods.size() <= 1,
+                                    "Found %d methods annotated with %s in class %s, but
only 1 allowed",
+                                    methods.size(), annotationClass.getName(), classToSearch.getName());
+        return Iterables.getOnlyElement(methods, null);
+    }
+
+    private static <T extends Annotation> List<Method> getMethodsWithAnnotation(Class<?>
classToSearch,
+                                                                                Class<T>
annotationClass) {
+        List<Method> methods = new ArrayList<>();
+        for (Method method : classToSearch.getMethods()) {
+            T annotation = method.getAnnotation(annotationClass);
+            if (annotation != null) {
+                methods.add(method);
+            }
         }
+        return methods;
     }
 
-
     @Override
     public int hashCode() {
         final int prime = 31;
@@ -158,5 +143,5 @@ public class Bean extends BeanRef {
             writer.writeProperty(property);
         }
     }
-    
+
 }

Copied: aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/bad/BadBean1.java
(from r1724022, aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/ParentBean.java)
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/bad/BadBean1.java?p2=aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/bad/BadBean1.java&p1=aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/ParentBean.java&r1=1724022&r2=1724229&rev=1724229&view=diff
==============================================================================
--- aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/ParentBean.java
(original)
+++ aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/bad/BadBean1.java
Tue Jan 12 13:42:00 2016
@@ -16,18 +16,18 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.aries.blueprint.plugin.test;
+package org.apache.aries.blueprint.plugin.bad;
 
 import javax.annotation.PostConstruct;
-import javax.annotation.PreDestroy;
+import javax.inject.Singleton;
 
-public class ParentBean {
+import org.apache.aries.blueprint.plugin.test.ParentBean;
 
+@Singleton
+public class BadBean1 extends ParentBean
+{
     @PostConstruct
-    public void init() {
-    }
-    
-    @PreDestroy
-    public void destroy() {
+    public void secondInit() {
+
     }
 }

Copied: aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/bad/BadBean2.java
(from r1724022, aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/ParentBean.java)
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/bad/BadBean2.java?p2=aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/bad/BadBean2.java&p1=aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/ParentBean.java&r1=1724022&r2=1724229&rev=1724229&view=diff
==============================================================================
--- aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/ParentBean.java
(original)
+++ aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/bad/BadBean2.java
Tue Jan 12 13:42:00 2016
@@ -16,18 +16,18 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.aries.blueprint.plugin.test;
+package org.apache.aries.blueprint.plugin.bad;
 
-import javax.annotation.PostConstruct;
 import javax.annotation.PreDestroy;
+import javax.inject.Singleton;
 
-public class ParentBean {
+import org.apache.aries.blueprint.plugin.test.ParentBean;
 
-    @PostConstruct
-    public void init() {
-    }
-    
+@Singleton
+public class BadBean2 extends ParentBean
+{
     @PreDestroy
-    public void destroy() {
+    public void secondDestroy() {
+
     }
 }

Modified: aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/model/BeanTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/model/BeanTest.java?rev=1724229&r1=1724228&r2=1724229&view=diff
==============================================================================
--- aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/model/BeanTest.java
(original)
+++ aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/model/BeanTest.java
Tue Jan 12 13:42:00 2016
@@ -25,6 +25,8 @@ import static org.junit.Assert.assertTru
 
 import javax.inject.Named;
 
+import org.apache.aries.blueprint.plugin.bad.BadBean1;
+import org.apache.aries.blueprint.plugin.bad.BadBean2;
 import org.apache.aries.blueprint.plugin.test.MyBean1;
 import org.apache.aries.blueprint.plugin.test.MyBean3;
 import org.apache.aries.blueprint.plugin.test.MyBean4;
@@ -34,7 +36,7 @@ import org.junit.Test;
 
 
 public class BeanTest {
-    
+
     @Test
     public void testParseMyBean1() {
         Bean bean = new Bean(MyBean1.class);
@@ -54,7 +56,7 @@ public class BeanTest {
         assertEquals("bean2", prop.name);
         assertEquals("serviceA", prop.ref);
     }
-    
+
     @Test
     public void testParseMyBean3() {
         Bean bean = new Bean(MyBean3.class);
@@ -69,7 +71,7 @@ public class BeanTest {
         assertEquals(5, bean.properties.size());
         assertTrue(bean.isPrototype);
     }
-    
+
     @Test
     public void testParseNamedBean() {
         Bean bean = new Bean(ServiceAImpl1.class);
@@ -84,7 +86,7 @@ public class BeanTest {
         assertEquals("There should be no properties", 0, bean.properties.size());
         assertTrue(bean.isPrototype);
     }
-    
+
     @Test
     public void testBlueprintBundleContext() {
         Bean bean = new Bean(MyBean4.class);
@@ -95,4 +97,13 @@ public class BeanTest {
         assertFalse(bean.isPrototype);
     }
 
+    @Test(expected = IllegalArgumentException.class)
+    public void testMultipleInitMethods() {
+        new Bean(BadBean1.class);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testMultipleDestroyMethods() {
+        new Bean(BadBean2.class);
+    }
 }

Modified: aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyBean1.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyBean1.java?rev=1724229&r1=1724228&r2=1724229&view=diff
==============================================================================
--- aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyBean1.java
(original)
+++ aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyBean1.java
Tue Jan 12 13:42:00 2016
@@ -18,6 +18,7 @@
  */
 package org.apache.aries.blueprint.plugin.test;
 
+import javax.annotation.PostConstruct;
 import javax.inject.Singleton;
 import javax.persistence.EntityManager;
 import javax.persistence.PersistenceContext;
@@ -33,20 +34,23 @@ public class MyBean1 extends ParentBean
 
     @Autowired
     ServiceA bean2;
-    
+
     @PersistenceContext(unitName="person")
     EntityManager em;
-    
+
     @PersistenceUnit(unitName="person")
     EntityManager emf;
-    
-    public void init() {
+
+    public void overridenInit() {
+        // By overriding the method and removing the annotation, this method has lost its
+        // @PostConstruct method because it isn't @Inherited
     }
-    
-    public void destroy() {
+
+    @PostConstruct
+    public void init() {
     }
 
     public void saveData() {
-        
+
     }
 }

Modified: aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/ParentBean.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/ParentBean.java?rev=1724229&r1=1724228&r2=1724229&view=diff
==============================================================================
--- aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/ParentBean.java
(original)
+++ aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/ParentBean.java
Tue Jan 12 13:42:00 2016
@@ -24,9 +24,9 @@ import javax.annotation.PreDestroy;
 public class ParentBean {
 
     @PostConstruct
-    public void init() {
+    public void overridenInit() {
     }
-    
+
     @PreDestroy
     public void destroy() {
     }



Mime
View raw message