aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lin...@apache.org
Subject svn commit: r941909 - in /incubator/aries/sandbox/linsun/blueprint: blueprint-annotation-impl/src/main/java/org/apache/aries/blueprint/annotation/impl/ blueprint-annotation-impl/src/test/java/org/apache/aries/blueprint/annotation/ blueprint-annotation-...
Date Thu, 06 May 2010 20:44:36 GMT
Author: linsun
Date: Thu May  6 20:44:35 2010
New Revision: 941909

URL: http://svn.apache.org/viewvc?rev=941909&view=rev
Log:
[blueprint annotation]allow field inject name to be different than field name, add support
for method injection in bean, add a bit more tests

Modified:
    incubator/aries/sandbox/linsun/blueprint/blueprint-annotation-impl/src/main/java/org/apache/aries/blueprint/annotation/impl/BlueprintAnnotationScannerImpl.java
    incubator/aries/sandbox/linsun/blueprint/blueprint-annotation-impl/src/test/java/org/apache/aries/blueprint/annotation/AnnotationTest.java
    incubator/aries/sandbox/linsun/blueprint/blueprint-annotation-itest/src/test/java/org/apache/aries/blueprint/itests/BlueprintAnnotationTest.java
    incubator/aries/sandbox/linsun/blueprint/blueprint-sample-annotation/src/main/java/org/apache/aries/blueprint/sample/Bar.java
    incubator/aries/sandbox/linsun/blueprint/blueprint-sample-annotation/src/main/java/org/apache/aries/blueprint/sample/DateTypeConverter.java
    incubator/aries/sandbox/linsun/blueprint/blueprint-sample-annotation/src/main/java/org/apache/aries/blueprint/sample/Foo.java

Modified: incubator/aries/sandbox/linsun/blueprint/blueprint-annotation-impl/src/main/java/org/apache/aries/blueprint/annotation/impl/BlueprintAnnotationScannerImpl.java
URL: http://svn.apache.org/viewvc/incubator/aries/sandbox/linsun/blueprint/blueprint-annotation-impl/src/main/java/org/apache/aries/blueprint/annotation/impl/BlueprintAnnotationScannerImpl.java?rev=941909&r1=941908&r2=941909&view=diff
==============================================================================
--- incubator/aries/sandbox/linsun/blueprint/blueprint-annotation-impl/src/main/java/org/apache/aries/blueprint/annotation/impl/BlueprintAnnotationScannerImpl.java
(original)
+++ incubator/aries/sandbox/linsun/blueprint/blueprint-annotation-impl/src/main/java/org/apache/aries/blueprint/annotation/impl/BlueprintAnnotationScannerImpl.java
Thu May  6 20:44:35 2010
@@ -238,36 +238,8 @@ public class BlueprintAnnotationScannerI
 
             Field[] fields = clazz.getDeclaredFields();
             for (int i = 0; i < fields.length; i++) {
-                if (fields[i].isAnnotationPresent(Inject.class)) {
-                    Inject inj = fields[i].getAnnotation(Inject.class);
-                    String value = inj.value();
-                    String ref = inj.ref();
-                    String name = inj.name();
-                    String desp = inj.description();
-                    
-                    
-                    Tproperty tp = new Tproperty();
-                    tp.setName(fields[i].getName());
-                    if (value.length() > 0) {
-                        Tvalue tvalue = new Tvalue();
-                        tvalue.setContent(value);
-                        tp.setValue(tvalue);
-                    }
-                    
-                    if (ref.length() > 0) {
-                        tp.setRefAttribute(ref);
-                    }
-                    
-                    if (name.length() > 0) {
-                        tp.setName(name);
-                    }
-                    
-                    if (desp.length() > 0) {
-                        Tdescription tdesp = new Tdescription();
-                        tdesp.getContent().add(desp);
-                        tp.setDescription(tdesp);
-                        
-                    }
+                if (fields[i].isAnnotationPresent(Inject.class)) {          
+                    Tproperty tp = createTproperty(fields[i].getName(), fields[i].getAnnotation(Inject.class));
                     props.add(tp);
                 }
             }
@@ -279,7 +251,12 @@ public class BlueprintAnnotationScannerI
                     tbean.setInitMethod(methods[i].getName());
                 } else if (methods[i].isAnnotationPresent(Destroy.class)) {
                     tbean.setDestroyMethod(methods[i].getName());
-                } 
+                } else if (methods[i].isAnnotationPresent(Inject.class)) {
+                    String propertyName = convertFromMethodName(methods[i].getName());
+                    Tproperty tp = createTproperty(propertyName, methods[i].getAnnotation(Inject.class));
+                    props.add(tp);
+                }
+                
             }
             
             // check if the bean also declares service
@@ -312,6 +289,59 @@ public class BlueprintAnnotationScannerI
         return tblueprint;
     }
 
+    private String convertFromMethodName(String name) {
+        if (name.length() > 3) {
+            name = name.substring(3);
+        } else {
+            throw new BlueprintAnnotationException("The annotated method name " + name +
" is invalid");
+        }
+        String firstChar = name.substring(0, 1).toLowerCase();
+        
+        if (name.length() == 1) {
+            return firstChar;
+        } else {
+            return firstChar + name.substring(1);
+        }
+    }
+
+    /**
+     * @param nm    method or field name
+     * @param inj   inject annotation
+     * @return
+     */
+    private Tproperty createTproperty(String nm, Inject inj) {
+        String value = inj.value();
+        String ref = inj.ref();
+        String name = inj.name();
+        String desp = inj.description();
+                         
+        Tproperty tp = new Tproperty();
+        if (value.length() > 0) {
+            Tvalue tvalue = new Tvalue();
+            tvalue.setContent(value);
+            tp.setValue(tvalue);
+        }
+        
+        if (ref.length() > 0) {
+            tp.setRefAttribute(ref);
+        }
+        
+        if (name.length() > 0) {
+            tp.setName(name);
+        } else {
+            tp.setName(nm);
+        }
+        
+        if (desp.length() > 0) {
+            Tdescription tdesp = new Tdescription();
+            tdesp.getContent().add(desp);
+            tp.setDescription(tdesp);
+            
+        }
+        
+        return tp;
+    }
+
     private boolean isConverter(Class clazz) {
         Class[] classes = clazz.getInterfaces();
         for (int i = 0; i < classes.length; i++) {

Modified: incubator/aries/sandbox/linsun/blueprint/blueprint-annotation-impl/src/test/java/org/apache/aries/blueprint/annotation/AnnotationTest.java
URL: http://svn.apache.org/viewvc/incubator/aries/sandbox/linsun/blueprint/blueprint-annotation-impl/src/test/java/org/apache/aries/blueprint/annotation/AnnotationTest.java?rev=941909&r1=941908&r2=941909&view=diff
==============================================================================
--- incubator/aries/sandbox/linsun/blueprint/blueprint-annotation-impl/src/test/java/org/apache/aries/blueprint/annotation/AnnotationTest.java
(original)
+++ incubator/aries/sandbox/linsun/blueprint/blueprint-annotation-impl/src/test/java/org/apache/aries/blueprint/annotation/AnnotationTest.java
Thu May  6 20:44:35 2010
@@ -27,6 +27,8 @@ import org.apache.aries.blueprint.jaxb.T
 import org.apache.aries.blueprint.jaxb.Tblueprint;
 import org.apache.aries.blueprint.jaxb.Tproperty;
 import org.apache.aries.blueprint.jaxb.Tvalue;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.Version;
 
 import junit.framework.TestCase;
 

Modified: incubator/aries/sandbox/linsun/blueprint/blueprint-annotation-itest/src/test/java/org/apache/aries/blueprint/itests/BlueprintAnnotationTest.java
URL: http://svn.apache.org/viewvc/incubator/aries/sandbox/linsun/blueprint/blueprint-annotation-itest/src/test/java/org/apache/aries/blueprint/itests/BlueprintAnnotationTest.java?rev=941909&r1=941908&r2=941909&view=diff
==============================================================================
--- incubator/aries/sandbox/linsun/blueprint/blueprint-annotation-itest/src/test/java/org/apache/aries/blueprint/itests/BlueprintAnnotationTest.java
(original)
+++ incubator/aries/sandbox/linsun/blueprint/blueprint-annotation-itest/src/test/java/org/apache/aries/blueprint/itests/BlueprintAnnotationTest.java
Thu May  6 20:44:35 2010
@@ -55,12 +55,29 @@ public class BlueprintAnnotationTest ext
 
         assertNotNull(blueprintContainer);
 
-        Set<String> ids = blueprintContainer.getComponentIds();
         Object obj = blueprintContainer.getComponentInstance("bar");
         assertNotNull(obj);
         assertEquals(Bar.class, obj.getClass());
         Bar bar = (Bar) obj;
-        assertEquals("Hello Bar", bar.getValue());
+        assertEquals("Hello FooBar", bar.getValue());
+        
+        obj = blueprintContainer.getComponentInstance("foo");
+        assertNotNull(obj);
+        assertEquals(Foo.class, obj.getClass());
+        Foo foo = (Foo) obj;
+        assertEquals(5, foo.getA());
+       // assertEquals(10, foo.getB());
+        assertSame(bar, foo.getBar());
+        assertEquals(Currency.getInstance("PLN"), foo.getCurrency());
+        assertEquals(new SimpleDateFormat("yyyy.MM.dd").parse("2009.04.17"),
+                foo.getDate());
+
+        assertTrue(foo.isInitialized());
+        assertFalse(foo.isDestroyed());
+
+       obj = getOsgiService(bundleContext, Foo.class, null, 5000);
+        assertNotNull(obj);
+        assertSame(foo, obj);
     }
 
     @org.ops4j.pax.exam.junit.Configuration

Modified: incubator/aries/sandbox/linsun/blueprint/blueprint-sample-annotation/src/main/java/org/apache/aries/blueprint/sample/Bar.java
URL: http://svn.apache.org/viewvc/incubator/aries/sandbox/linsun/blueprint/blueprint-sample-annotation/src/main/java/org/apache/aries/blueprint/sample/Bar.java?rev=941909&r1=941908&r2=941909&view=diff
==============================================================================
--- incubator/aries/sandbox/linsun/blueprint/blueprint-sample-annotation/src/main/java/org/apache/aries/blueprint/sample/Bar.java
(original)
+++ incubator/aries/sandbox/linsun/blueprint/blueprint-sample-annotation/src/main/java/org/apache/aries/blueprint/sample/Bar.java
Thu May  6 20:44:35 2010
@@ -29,7 +29,7 @@ import org.apache.aries.blueprint.annota
 @Bean(id="bar")
 public class Bar {
     
-    @Inject(value="Hello Bar")
+    @Inject(value="Hello FooBar")
     private String value;
 
     @Inject(ref="blueprintBundleContext")

Modified: incubator/aries/sandbox/linsun/blueprint/blueprint-sample-annotation/src/main/java/org/apache/aries/blueprint/sample/DateTypeConverter.java
URL: http://svn.apache.org/viewvc/incubator/aries/sandbox/linsun/blueprint/blueprint-sample-annotation/src/main/java/org/apache/aries/blueprint/sample/DateTypeConverter.java?rev=941909&r1=941908&r2=941909&view=diff
==============================================================================
--- incubator/aries/sandbox/linsun/blueprint/blueprint-sample-annotation/src/main/java/org/apache/aries/blueprint/sample/DateTypeConverter.java
(original)
+++ incubator/aries/sandbox/linsun/blueprint/blueprint-sample-annotation/src/main/java/org/apache/aries/blueprint/sample/DateTypeConverter.java
Thu May  6 20:44:35 2010
@@ -29,15 +29,15 @@ import org.osgi.service.blueprint.contai
 @Bean(id="converter1")
 public class DateTypeConverter implements Converter {
 
-    @Inject(value="yyyy.MM.dd")
-    DateFormat format;
+    @Inject(name="format", value="yyyy.MM.dd")
+    DateFormat dateFormat;
     
     public void setFormat(String format) {
-        this.format = new SimpleDateFormat(format);
+        this.dateFormat = new SimpleDateFormat(format);
     }
     
     public Object convert(Object source, ReifiedType toType) throws Exception {
-        return format.parse(source.toString());
+        return this.dateFormat.parse(source.toString());
     }
 
     public boolean canConvert(Object fromValue, ReifiedType toType) {

Modified: incubator/aries/sandbox/linsun/blueprint/blueprint-sample-annotation/src/main/java/org/apache/aries/blueprint/sample/Foo.java
URL: http://svn.apache.org/viewvc/incubator/aries/sandbox/linsun/blueprint/blueprint-sample-annotation/src/main/java/org/apache/aries/blueprint/sample/Foo.java?rev=941909&r1=941908&r2=941909&view=diff
==============================================================================
--- incubator/aries/sandbox/linsun/blueprint/blueprint-sample-annotation/src/main/java/org/apache/aries/blueprint/sample/Foo.java
(original)
+++ incubator/aries/sandbox/linsun/blueprint/blueprint-sample-annotation/src/main/java/org/apache/aries/blueprint/sample/Foo.java
Thu May  6 20:44:35 2010
@@ -40,7 +40,7 @@ public class Foo implements Serializable
     //@Inject(value="${key.b}")
     private int b;
     
-    @Inject(ref="bar")
+    //@Inject(ref="bar")
     private Bar bar;
     
     @Inject(value="PLN")
@@ -73,6 +73,7 @@ public class Foo implements Serializable
         return bar;
     }
 
+    @Inject(ref="bar")
     public void setBar(Bar b) {
         bar = b;
     }



Mime
View raw message