aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cschnei...@apache.org
Subject svn commit: r1652086 - in /aries/trunk/blueprint/blueprint-maven-plugin/src: main/java/org/apache/aries/blueprint/plugin/model/ test/java/org/apache/aries/blueprint/plugin/test/
Date Thu, 15 Jan 2015 13:10:56 GMT
Author: cschneider
Date: Thu Jan 15 13:10:56 2015
New Revision: 1652086

URL: http://svn.apache.org/r1652086
Log:
ARIES-1284 applied path with thanks to Andrei

Added:
    aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/ParentBean.java
      - copied, changed from r1652028, aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyBean1.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/Property.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/MyBean3.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=1652086&r1=1652085&r2=1652086&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
Thu Jan 15 13:10:56 2015
@@ -18,6 +18,7 @@
  */
 package org.apache.aries.blueprint.plugin.model;
 
+import java.lang.annotation.Annotation;
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
 import java.util.SortedSet;
@@ -43,11 +44,11 @@ public class Bean implements Comparable<
         this.clazz = clazz;
         this.id = getBeanName(clazz);
         for (Method method : clazz.getDeclaredMethods()) {
-            PostConstruct postConstruct = method.getAnnotation(PostConstruct.class);
+            PostConstruct postConstruct = getEffectiveAnnotation(method, PostConstruct.class);
             if (postConstruct != null) {
                 this.initMethod = method.getName();
             }
-            PreDestroy preDestroy = method.getAnnotation(PreDestroy.class);
+            PreDestroy preDestroy = getEffectiveAnnotation(method, PreDestroy.class);
             if (preDestroy != null) {
                 this.destroyMethod = method.getName();
             }
@@ -104,6 +105,46 @@ public class Bean implements Comparable<
     private static String getBeanNameFromSimpleName(String name) {
         return name.substring(0, 1).toLowerCase() + name.substring(1, name.length());
     }
+    
+    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;
+        }
+    }
+    
 
     public boolean matches(Class<?> destType, String destId) {
         boolean assignable = destType.isAssignableFrom(this.clazz);

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=1652086&r1=1652085&r2=1652086&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
Thu Jan 15 13:10:56 2015
@@ -27,6 +27,7 @@ import java.util.TreeSet;
 import javax.inject.Named;
 
 import org.ops4j.pax.cdi.api.OsgiService;
+import org.springframework.beans.factory.annotation.Qualifier;
 
 public class Context implements Matcher {
 
@@ -67,8 +68,7 @@ public class Context implements Matcher
     }
     
     public Bean getMatching(Field field) {
-        Named named = field.getAnnotation(Named.class);
-        String destId = (named == null) ? null : named.value();
+        String destId = getDestinationId(field);
         // TODO Replace loop by lookup
         for (Bean bean : beans) {
             if (bean.matches(field.getType(), destId)) {
@@ -83,6 +83,18 @@ public class Context implements Matcher
         return null;
     }
 
+	private String getDestinationId(Field field) {
+		Named named = field.getAnnotation(Named.class);
+		if (named != null) {
+			return named.value();
+		}
+		Qualifier qualifier = field.getAnnotation(Qualifier.class);
+        if (qualifier != null) {
+        	return qualifier.value();
+        }
+		return null;
+	}
+
     public SortedSet<Bean> getBeans() {
         return beans;
     }

Modified: aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Property.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Property.java?rev=1652086&r1=1652085&r2=1652086&view=diff
==============================================================================
--- aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Property.java
(original)
+++ aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Property.java
Thu Jan 15 13:10:56 2015
@@ -24,6 +24,7 @@ import javax.inject.Inject;
 import javax.inject.Named;
 
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
 import org.springframework.beans.factory.annotation.Value;
 
 public class Property implements Comparable<Property> {
@@ -58,7 +59,14 @@ public class Property implements Compara
      */
     private static String getRefName(Field field) {
         Named named = field.getAnnotation(Named.class);
-        return (named != null) ? named.value() : Bean.getBeanName(field.getType());
+        if (named != null) {
+        	return named.value();
+        }
+    	Qualifier qualifier = field.getAnnotation(Qualifier.class);
+        if (qualifier != null) {
+        	return qualifier.value();
+        }
+        return Bean.getBeanName(field.getType());
     }
 
     private static boolean needsInject(Field field) {

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=1652086&r1=1652085&r2=1652086&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
Thu Jan 15 13:10:56 2015
@@ -18,8 +18,6 @@
  */
 package org.apache.aries.blueprint.plugin.test;
 
-import javax.annotation.PostConstruct;
-import javax.annotation.PreDestroy;
 import javax.inject.Singleton;
 import javax.persistence.EntityManager;
 import javax.persistence.PersistenceUnit;
@@ -30,7 +28,7 @@ import org.springframework.beans.factory
 
 @Singleton
 @Transactional(value=TxType.REQUIRED)
-public class MyBean1 {
+public class MyBean1 extends ParentBean {
 
     @Autowired
     ServiceA bean2;
@@ -38,14 +36,10 @@ public class MyBean1 {
     @PersistenceUnit(unitName="person")
     EntityManager em;
     
-    @PostConstruct
     public void init() {
-        
     }
     
-    @PreDestroy
     public void destroy() {
-        
     }
 
     public void saveData() {

Modified: aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyBean3.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyBean3.java?rev=1652086&r1=1652085&r2=1652086&view=diff
==============================================================================
--- aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyBean3.java
(original)
+++ aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyBean3.java
Thu Jan 15 13:10:56 2015
@@ -22,6 +22,7 @@ import javax.inject.Inject;
 import javax.inject.Named;
 import javax.inject.Singleton;
 
+import org.springframework.beans.factory.annotation.Qualifier;
 import org.springframework.transaction.annotation.Propagation;
 import org.springframework.transaction.annotation.Transactional;
 
@@ -34,7 +35,7 @@ public class MyBean3 {
     ServiceA serviceA1;
 
     @Inject
-    @Named("my2")
+    @Qualifier("my2")
     ServiceA serviceA2;
 
     @Inject

Copied: aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/ParentBean.java
(from r1652028, 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/ParentBean.java?p2=aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/ParentBean.java&p1=aries/trunk/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyBean1.java&r1=1652028&r2=1652086&rev=1652086&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/ParentBean.java
Thu Jan 15 13:10:56 2015
@@ -20,35 +20,14 @@ package org.apache.aries.blueprint.plugi
 
 import javax.annotation.PostConstruct;
 import javax.annotation.PreDestroy;
-import javax.inject.Singleton;
-import javax.persistence.EntityManager;
-import javax.persistence.PersistenceUnit;
-import javax.transaction.Transactional;
-import javax.transaction.Transactional.TxType;
 
-import org.springframework.beans.factory.annotation.Autowired;
+public class ParentBean {
 
-@Singleton
-@Transactional(value=TxType.REQUIRED)
-public class MyBean1 {
-
-    @Autowired
-    ServiceA bean2;
-    
-    @PersistenceUnit(unitName="person")
-    EntityManager em;
-    
     @PostConstruct
     public void init() {
-        
     }
     
     @PreDestroy
     public void destroy() {
-        
-    }
-
-    public void saveData() {
-        
     }
 }



Mime
View raw message