You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by ge...@apache.org on 2011/05/23 13:50:36 UTC

svn commit: r1126454 - /geronimo/server/trunk/plugins/j2ee/geronimo-naming-builder/src/main/java/org/apache/geronimo/naming/deployment/AbstractNamingBuilder.java

Author: genspring
Date: Mon May 23 11:50:36 2011
New Revision: 1126454

URL: http://svn.apache.org/viewvc?rev=1126454&view=rev
Log:
consider the @resource annotation of field/method when infering the resource Type.

Modified:
    geronimo/server/trunk/plugins/j2ee/geronimo-naming-builder/src/main/java/org/apache/geronimo/naming/deployment/AbstractNamingBuilder.java

Modified: geronimo/server/trunk/plugins/j2ee/geronimo-naming-builder/src/main/java/org/apache/geronimo/naming/deployment/AbstractNamingBuilder.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/plugins/j2ee/geronimo-naming-builder/src/main/java/org/apache/geronimo/naming/deployment/AbstractNamingBuilder.java?rev=1126454&r1=1126453&r2=1126454&view=diff
==============================================================================
--- geronimo/server/trunk/plugins/j2ee/geronimo-naming-builder/src/main/java/org/apache/geronimo/naming/deployment/AbstractNamingBuilder.java (original)
+++ geronimo/server/trunk/plugins/j2ee/geronimo-naming-builder/src/main/java/org/apache/geronimo/naming/deployment/AbstractNamingBuilder.java Mon May 23 11:50:36 2011
@@ -18,6 +18,7 @@
 package org.apache.geronimo.naming.deployment;
 
 import java.beans.Introspector;
+import java.lang.annotation.Annotation;
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
 import java.util.ArrayList;
@@ -29,6 +30,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
+import javax.annotation.Resource;
 import javax.xml.namespace.QName;
 import org.apache.geronimo.common.DeploymentException;
 import org.apache.geronimo.deployment.service.EnvironmentBuilder;
@@ -360,18 +362,7 @@ public abstract class AbstractNamingBuil
                 Class<?> clazz = bundle.loadClass(className);
                 String fieldName = getStringValue(injectionTarget.getInjectionTargetName());
                 Class<?> fieldType = getField(clazz, fieldName);
-                fieldType = deprimitivize(fieldType);
-                if (type == null) {
-                    type = fieldType;
-                } else if (!fieldType.equals(type)) {
-                    if (fieldType.isAssignableFrom(type)) {
-                        //type is subclass
-                    } else if (type.isAssignableFrom(fieldType)) {
-                        type = fieldType;
-                    } else {
-                        throw new DeploymentException("Mismatched types in env-entry named: " + name + " type: " + type + " injections " + injectionTargets);
-                    }
-                }
+                type = fieldType;
             } catch (ClassNotFoundException e) {
                 throw new DeploymentException("could not load injection target class for env entry named: " + name + " in class: " + className, e);
             } catch (NoSuchFieldException e) {
@@ -383,6 +374,25 @@ public abstract class AbstractNamingBuil
         }
         return type.getName();
     }
+    
+    private static Class<?> chooseType(String name, Class<?> originalType, Class<?> alternativeType) throws DeploymentException{
+        alternativeType = deprimitivize(alternativeType);
+        if (originalType == null) {
+            return alternativeType;
+        } else if (!alternativeType.equals(originalType)) {
+            if (alternativeType.isAssignableFrom(originalType)) {
+                //originalType is subclass
+                return originalType;
+            } else if (originalType.isAssignableFrom(alternativeType)) {
+                //alternativeType is subclass
+                return alternativeType;
+            } else {
+                throw new DeploymentException("Mismatched types in named: " + name + " type: " + originalType );
+            }
+        } 
+        
+        return originalType;
+    }
 
     //duplicated in ResourceAnnotationHelper
     public static Class<?> deprimitivize(Class<?> fieldType) {
@@ -402,10 +412,20 @@ public abstract class AbstractNamingBuil
         primitives.put(short.class, Short.class);
     }
 
-    private Class<?> getField(Class<?> clazz, String fieldName) throws NoSuchFieldException {
+    private Class<?> getField(Class<?> clazz, String fieldName) throws NoSuchFieldException, DeploymentException {
+        
+        Class<?> type = null;
+        
         do {
             try {
-                return clazz.getDeclaredField(fieldName).getType();
+                Field field = clazz.getDeclaredField(fieldName);
+                Resource resource = field.getAnnotation(Resource.class);
+                if (resource != null) {
+                    type = chooseType(fieldName, field.getType(), resource.type());
+                } else {
+                    type = field.getType();
+                }
+                
             } catch (NoSuchFieldException e) {
                 //look at superclass
             }
@@ -413,15 +433,25 @@ public abstract class AbstractNamingBuil
                 if (method.getReturnType() == void.class && method.getParameterTypes().length == 1) {
                     String methodName = method.getName();
                     if (methodName.startsWith("set")) {
-                        String type = Introspector.decapitalize(methodName.substring(3));
-                        if (fieldName.equals(type)) {
-                            return method.getParameterTypes()[0];
+                        String setName = Introspector.decapitalize(methodName.substring(3));
+                        if (fieldName.equals(setName)) {
+                            Resource resource = method.getAnnotation(Resource.class);
+                            if (resource != null) {
+                                type = chooseType(fieldName, method.getParameterTypes()[0], resource.type());
+                            } else {
+                                type = method.getParameterTypes()[0];
+                            }
                         }
                     }
                 }
             }
             clazz = clazz.getSuperclass();
         } while (clazz != null);
+        
+        if (type != null) {
+            return type;
+        }
+        
         throw new NoSuchFieldException(fieldName);
     }
 }