geronimo-xbean-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d...@apache.org
Subject svn commit: r617547 [2/3] - in /geronimo/xbean/trunk/xbean-reflect/src: main/java/org/apache/xbean/recipe/ test/java/org/apache/xbean/recipe/
Date Fri, 01 Feb 2008 16:23:29 GMT
Modified: geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/ObjectRecipe.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/ObjectRecipe.java?rev=617547&r1=617546&r2=617547&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/ObjectRecipe.java (original)
+++ geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/ObjectRecipe.java Fri Feb  1 08:23:21 2008
@@ -16,52 +16,67 @@
  */
 package org.apache.xbean.recipe;
 
-import org.apache.xbean.propertyeditor.PropertyEditors;
-
-import java.lang.reflect.Constructor;
+import java.lang.reflect.Field;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
-import java.lang.reflect.Field;
 import java.util.ArrayList;
-import java.util.Arrays;
+import java.util.EnumSet;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
-import java.util.Iterator;
-import java.security.AccessController;
-import java.security.PrivilegedAction;
 
 /**
  * @version $Rev: 6688 $ $Date: 2005-12-29T02:08:29.200064Z $
  */
 public class ObjectRecipe extends AbstractRecipe {
-    private final String type;
-    private final String factoryMethod;
-    private final String[] constructorArgNames;
-    private final Class[] constructorArgTypes;
-    private final LinkedHashMap<Property,Object> properties;
-    private final List<Option> options = new ArrayList<Option>();
+    private String typeName;
+    private Class typeClass;
+    private ConstructionStrategy constructionStrategy;
+    private String factoryMethod;
+    private String[] constructorArgNames;
+    private Class[] constructorArgTypes;
+    private final LinkedHashMap<Property,Object> properties = new LinkedHashMap<Property,Object>();
+    private final EnumSet<Option> options = EnumSet.of(Option.FIELD_INJECTION);
     private final Map<String,Object> unsetProperties = new LinkedHashMap<String,Object>();
 
-    public ObjectRecipe(Class type) {
-        this(type.getName());
+    public ObjectRecipe(Class typeClass) {
+        this(typeClass, null, null, null, null);
+    }
+
+    public ObjectRecipe(Class typeClass, String factoryMethod) {
+        this(typeClass, factoryMethod, null, null, null);
+    }
+
+    public ObjectRecipe(Class typeClass, Map<String,Object> properties) {
+        this(typeClass, null, null, null, properties);
     }
 
-    public ObjectRecipe(Class type, String factoryMethod) {
-        this(type.getName(), factoryMethod);
+    public ObjectRecipe(Class typeClass, String[] constructorArgNames) {
+        this(typeClass, null, constructorArgNames, null, null);
     }
 
-    public ObjectRecipe(Class type, Map<String,Object> properties) {
-        this(type.getName(), properties);
+    public ObjectRecipe(Class typeClass, String[] constructorArgNames, Class[] constructorArgTypes) {
+        this(typeClass, null, constructorArgNames, constructorArgTypes, null);
     }
 
-    public ObjectRecipe(Class type, String[] constructorArgNames, Class[] constructorArgTypes) {
-        this(type.getName(), constructorArgNames, constructorArgTypes);
+    public ObjectRecipe(Class type, String factoryMethod, String[] constructorArgNames) {
+        this(type, factoryMethod, constructorArgNames, null, null);
     }
 
     public ObjectRecipe(Class type, String factoryMethod, String[] constructorArgNames, Class[] constructorArgTypes) {
-        this(type.getName(), factoryMethod, constructorArgNames, constructorArgTypes);
+        this(type, factoryMethod, constructorArgNames, constructorArgTypes, null);
+    }
+
+    public ObjectRecipe(Class typeClass, String factoryMethod, String[] constructorArgNames, Class[] constructorArgTypes, Map<String,Object> properties) {
+        this.typeClass = typeClass;
+        this.factoryMethod = factoryMethod;
+        this.constructorArgNames = constructorArgNames;
+        this.constructorArgTypes = constructorArgTypes;
+        constructionStrategy = new ExplicitConstructionStrategy();
+        if (properties != null) {
+            setAllProperties(properties);
+        }
     }
 
     public ObjectRecipe(String typeName) {
@@ -76,34 +91,30 @@
         this(typeName, null, null, null, properties);
     }
 
+    public ObjectRecipe(String typeName, String[] constructorArgNames) {
+        this(typeName, null, constructorArgNames, null, null);
+    }
+
     public ObjectRecipe(String typeName, String[] constructorArgNames, Class[] constructorArgTypes) {
         this(typeName, null, constructorArgNames, constructorArgTypes, null);
     }
 
+    public ObjectRecipe(String typeName, String factoryMethod, String[] constructorArgNames) {
+        this(typeName, factoryMethod, constructorArgNames, null, null);
+    }
+
     public ObjectRecipe(String typeName, String factoryMethod, String[] constructorArgNames, Class[] constructorArgTypes) {
         this(typeName, factoryMethod, constructorArgNames, constructorArgTypes, null);
     }
 
-    public ObjectRecipe(String type, String factoryMethod, String[] constructorArgNames, Class[] constructorArgTypes, Map<String,Object> properties) {
-        options.add(Option.FIELD_INJECTION);
-        
-        this.type = type;
+    public ObjectRecipe(String typeName, String factoryMethod, String[] constructorArgNames, Class[] constructorArgTypes, Map<String,Object> properties) {
+        this.typeName = typeName;
         this.factoryMethod = factoryMethod;
-        if (constructorArgNames != null) {
-            this.constructorArgNames = constructorArgNames;
-        } else {
-            this.constructorArgNames = new String[0];
-        }
-        if (constructorArgTypes != null) {
-            this.constructorArgTypes = constructorArgTypes;
-        } else {
-            this.constructorArgTypes = new Class[0];
-        }
+        this.constructorArgNames = constructorArgNames;
+        this.constructorArgTypes = constructorArgTypes;
+        constructionStrategy = new ExplicitConstructionStrategy();
         if (properties != null) {
-            this.properties = new LinkedHashMap<Property,Object>();
             setAllProperties(properties);
-        } else {
-            this.properties = new LinkedHashMap<Property,Object>();
         }
     }
 
@@ -115,11 +126,65 @@
         options.remove(option);
     }
 
+    public ConstructionStrategy getConstructionStrategy() {
+        return constructionStrategy;
+    }
+
+    public void setConstructionStrategy(ConstructionStrategy constructionStrategy) {
+        this.constructionStrategy = constructionStrategy;
+    }
+
+    public String[] getConstructorArgNames() {
+        return constructorArgNames;
+    }
+
+    public void setConstructorArgNames(String[] constructorArgNames) {
+        this.constructorArgNames = constructorArgNames;
+    }
+
+    public Class[] getConstructorArgTypes() {
+        return constructorArgTypes;
+    }
+
+    public void setConstructorArgTypes(Class[] constructorArgTypes) {
+        this.constructorArgTypes = constructorArgTypes;
+    }
+
+    /**
+     * Gets the name of the factory method to call on the constructed instance.
+     * This method must be of the form <code>public T factoryMethodName();</code>
+     * and specifically must NOT be static.  Static factory methods are handled using
+     * a ConstructionStrategy.
+     * @return the name of the factory method to call on the constructed instance
+     */
+    public String getFactoryMethod() {
+        return factoryMethod;
+    }
+
+    /**
+     * Sets the name of the factory method to call on the constructed instance.
+     * This method must be of the form <code>public T factoryMethodName();</code>
+     * and specifically must NOT be static.  Static factory methods are handled using
+     * a ConstructionStrategy.
+     * @param factoryMethod the name of the factory method to call on the constructed instance
+     */
+    public void setFactoryMethod(String factoryMethod) {
+        this.factoryMethod = factoryMethod;
+    }
+
     public Object getProperty(String name) {
         Object value = properties.get(new Property(name));
         return value;
     }
 
+    public Map<String, Object> getProperties() {
+        LinkedHashMap<String, Object> properties = new LinkedHashMap<String, Object>();
+        for (Map.Entry<Property, Object> entry : this.properties.entrySet()) {
+            properties.put(entry.getKey().name, entry.getValue());
+        }
+        return properties;
+    }
+
     public void setProperty(String name, Object value) {
         setProperty(new Property(name), value);
     }
@@ -141,10 +206,9 @@
     }
 
 
-    public void setAllProperties(Map map) {
+    public void setAllProperties(Map<?,?> map) {
         if (map == null) throw new NullPointerException("map is null");
-        for (Iterator iterator = map.entrySet().iterator(); iterator.hasNext();) {
-            Map.Entry entry = (Map.Entry) iterator.next();
+        for (Map.Entry<?, ?> entry : map.entrySet()) {
             String name = (String) entry.getKey();
             Object value = entry.getValue();
             setProperty(name, value);
@@ -155,15 +219,45 @@
         return unsetProperties;
     }
 
-    public boolean canCreate(Class type, ClassLoader classLoader) {
-        Class myType = getType(classLoader);
-        return type.isAssignableFrom(myType);
+    public List<Recipe> getNestedRecipes() {
+        List<Recipe> nestedRecipes = new ArrayList<Recipe>(properties.size());
+        for (Object o : properties.values()) {
+            if (o instanceof Recipe) {
+                Recipe recipe = (Recipe) o;
+                nestedRecipes.add(recipe);
+            }
+        }
+        return nestedRecipes;
+    }
+
+    public List<Recipe> getConstructorRecipes() {
+        Construction construction = constructionStrategy.getConstruction(this, Object.class);
+        if (!construction.hasInstanceFactory()) {
+            // only include recipes used in the construcor args
+            List<String> parameterNames = construction.getParameterNames();
+            List<Recipe> nestedRecipes = new ArrayList<Recipe>(parameterNames.size());
+            for (Map.Entry<Property, Object> entry : properties.entrySet()) {
+                if (parameterNames.contains(entry.getKey().name) && entry.getValue() instanceof Recipe) {
+                    Recipe recipe = (Recipe) entry.getValue();
+                    nestedRecipes.add(recipe);
+                }
+            }
+            return nestedRecipes;
+        } else {
+            // when there is an instance factory all nested recipes are used in the constructor
+            return getNestedRecipes();
+        }
+    }
+
+    public boolean canCreate(Class type) {
+        Class myType = getType();
+        return type.isAssignableFrom(myType) || type.isAssignableFrom(myType);
     }
 
-    public Object create(ClassLoader classLoader) throws ConstructionException {
+    protected Object internalCreate(Class expectedType, boolean lazyRefAllowed) throws ConstructionException {
         unsetProperties.clear();
         // load the type class
-        Class typeClass = getType(classLoader);
+        Class typeClass = getType();
 
         // verify that it is a class we can construct
         if (!Modifier.isPublic(typeClass.getModifiers())) {
@@ -179,46 +273,41 @@
         // clone the properties so they can be used again
         Map<Property,Object> propertyValues = new LinkedHashMap<Property,Object>(properties);
 
-        // find the factory method is one is declared
-        Method factoryMethod = null;
-        if (this.factoryMethod != null) {
-            factoryMethod = findFactoryMethod(typeClass, this.factoryMethod);
-        }
-
         // create the instance
-        Object result;
-        if (factoryMethod != null && Modifier.isStatic(factoryMethod.getModifiers())) {
-            result = createInstance(factoryMethod, propertyValues, classLoader);
-        } else {
-            Constructor constructor = selectConstructor(typeClass);
-            result = createInstance(constructor, propertyValues, classLoader);
-        }
-        Object instance = result;
+        Construction construction = constructionStrategy.getConstruction(this, expectedType);
+        Object[] parameters = extractConstructorArgs(propertyValues,
+                construction.getParameterNames(),
+                construction.getParameterTypes());
+        Object instance = construction.create(parameters);
+
+        // add to execution context if name is specified
+        if (getName() != null) {
+            ExecutionContext.getContext().addObject(getName(), instance);
+        }
+       
+        // set the properties
+        setProperties(propertyValues, instance, instance.getClass());
 
-        setProperties(propertyValues, instance, instance.getClass(), classLoader);
         // call instance factory method
-        if (factoryMethod != null && !Modifier.isStatic(factoryMethod.getModifiers())) {
-            try {
-                instance = factoryMethod.invoke(instance);
-            } catch (Exception e) {
-                Throwable t = e;
-                if (e instanceof InvocationTargetException) {
-                    InvocationTargetException invocationTargetException = (InvocationTargetException) e;
-                    if (invocationTargetException.getCause() != null) {
-                        t = invocationTargetException.getCause();
-                    }
-                }
-                throw new ConstructionException("Error calling factory method: " + factoryMethod, t);
-            }
-        }
+        instance = construction.callInstanceFactory(instance);
 
         return instance;
     }
-    
-    public Class setStaticProperties(ClassLoader classLoader) throws ConstructionException {
+
+    public void setProperties(Object instance) throws ConstructionException {
         unsetProperties.clear();
+
+        // clone the properties so they can be used again
+        Map<Property,Object> propertyValues = new LinkedHashMap<Property,Object>(properties);
+
+        setProperties(propertyValues, instance, instance.getClass());
+    }
+
+    public Class setStaticProperties() throws ConstructionException {
+        unsetProperties.clear();
+
         // load the type class
-        Class typeClass = getType(classLoader);
+        Class typeClass = getType();
 
         // verify that it is a class we can construct
         if (!Modifier.isPublic(typeClass.getModifiers())) {
@@ -234,69 +323,72 @@
         // clone the properties so they can be used again
         Map<Property,Object> propertyValues = new LinkedHashMap<Property,Object>(properties);
 
-        setProperties(propertyValues, null, typeClass, classLoader);
+        setProperties(propertyValues, null, typeClass);
 
         return typeClass;
     }
 
-    private void setProperties(Map<Property, Object> propertyValues, Object instance, Class clazz, ClassLoader classLoader) {
-        boolean allowPrivate = options.contains(Option.PRIVATE_PROPERTIES);
-        boolean allowStatic = options.contains(Option.STATIC_PROPERTIES);
-        boolean ignoreMissingProperties = options.contains(Option.IGNORE_MISSING_PROPERTIES);
-        boolean caseInsesnitive = options.contains(Option.CASE_INSENSITIVE_PROPERTIES);
+    public Class getType() {
+        if (typeClass != null || typeName != null) {
+            Class type = typeClass;
+            if (type == null) {
+                try {
+                    type = RecipeHelper.loadClass(typeName);
+                } catch (ClassNotFoundException e) {
+                    throw new ConstructionException("Type class could not be found: " + typeName);
+                }
+            }
+
+            return type;
+        }
+
+        return null;
+    }
+
+    private void setProperties(Map<Property, Object> propertyValues, Object instance, Class clazz) {
         // set remaining properties
         for (Map.Entry<Property, Object> entry : RecipeHelper.prioritizeProperties(propertyValues)) {
             Property propertyName = entry.getKey();
             Object propertyValue = entry.getValue();
 
-            setProperty(instance, clazz, propertyName, propertyValue, allowPrivate, allowStatic, ignoreMissingProperties, caseInsesnitive, classLoader);
+            setProperty(instance, clazz, propertyName, propertyValue);
         }
 
     }
 
-    private Class getType(ClassLoader classLoader) {
-        Class typeClass = null;
-        try {
-            typeClass = Class.forName(type, true, classLoader);
-        } catch (ClassNotFoundException e) {
-            throw new ConstructionException("Type class could not be found: " + type);
-        }
-        return typeClass;
-    }
+    private void setProperty(Object instance, Class clazz, Property propertyName, Object propertyValue) {
 
-    private void setProperty(Object instance, Class clazz, Property propertyName, Object propertyValue, boolean allowPrivate, boolean allowStatic, boolean ignoreMissingProperties, boolean caseInsesnitive, ClassLoader classLoader) {
         Member member;
         try {
             if (propertyName instanceof SetterProperty){
-                member = new MethodMember(findSetter(clazz, propertyName.name, propertyValue, allowPrivate, allowStatic, caseInsesnitive, classLoader));
+                member = new MethodMember(ReflectionUtil.findSetter(clazz, propertyName.name, propertyValue, options));
             } else if (propertyName instanceof FieldProperty){
-                member = new FieldMember(findField(clazz, propertyName.name, propertyValue, allowPrivate, allowStatic, caseInsesnitive, classLoader));
+                member = new FieldMember(ReflectionUtil.findField(clazz, propertyName.name, propertyValue, options));
             } else {
                 try {
-                    member = new MethodMember(findSetter(clazz, propertyName.name, propertyValue, allowPrivate, allowStatic, caseInsesnitive, classLoader));
+                    member = new MethodMember(ReflectionUtil.findSetter(clazz, propertyName.name, propertyValue, options));
                 } catch (MissingAccessorException noSetter) {
                     if (!options.contains(Option.FIELD_INJECTION)) {
                         throw noSetter;
                     }
 
                     try {
-                        member = new FieldMember(findField(clazz, propertyName.name, propertyValue, allowPrivate, allowStatic, caseInsesnitive, classLoader));
+                        member = new FieldMember(ReflectionUtil.findField(clazz, propertyName.name, propertyValue, options));
                     } catch (MissingAccessorException noField) {
                         throw (noField.getMatchLevel() > noSetter.getMatchLevel())? noField: noSetter;
                     }
                 }
             }
         } catch (MissingAccessorException e) {
-            if (ignoreMissingProperties){
+            if (options.contains(Option.IGNORE_MISSING_PROPERTIES)) {
                 unsetProperties.put(propertyName.name, propertyValue);
                 return;
-            } else {
-                throw e;
             }
+            throw e;
         }
 
         try {
-            propertyValue = convert(member.getType(), propertyValue, classLoader);
+            propertyValue = RecipeHelper.convert(member.getType(), propertyValue, false);
             member.setValue(instance, propertyValue);
         } catch (Exception e) {
             Throwable t = e;
@@ -310,23 +402,23 @@
         }
     }
 
-    private Object[] extractConstructorArgs(Map propertyValues, Class[] constructorArgTypes, ClassLoader classLoader) {
-        Object[] parameters = new Object[constructorArgNames.length];
-        for (int i = 0; i < constructorArgNames.length; i++) {
-            Property name = new Property(constructorArgNames[i]);
-            Class type = constructorArgTypes[i];
+    private Object[] extractConstructorArgs(Map propertyValues, List<String> argNames, List<Class> argTypes) {
+        Object[] parameters = new Object[argNames.size()];
+        for (int i = 0; i < argNames.size(); i++) {
+            Property name = new Property(argNames.get(i));
+            Class type = argTypes.get(i);
 
             Object value;
             if (propertyValues.containsKey(name)) {
                 value = propertyValues.remove(name);
-                if (!RecipeHelper.isInstance(type, value) && !RecipeHelper.isConvertable(type, value, classLoader)) {
+                if (!RecipeHelper.isInstance(type, value) && !RecipeHelper.isConvertable(type, value)) {
                     throw new ConstructionException("Invalid and non-convertable constructor parameter type: " +
                             "name=" + name + ", " +
                             "index=" + i + ", " +
                             "expected=" + type.getName() + ", " +
-                            "actual=" + getClassName(value));
+                            "actual=" + (value == null ? "null" : value.getClass().getName()));
                 }
-                value = convert(type, value, classLoader);
+                value = RecipeHelper.convert(type, value, false);
             } else {
                 value = getDefaultValue(type);
             }
@@ -337,520 +429,27 @@
         return parameters;
     }
 
-    private static String getClassName(Object value) {
-        if (value == null) return "null";
-
-        return value.getClass().getName();
-    }
-
-    private Object convert(Class type, Object value, ClassLoader classLoader) {
-        if (value instanceof Recipe) {
-            if (value instanceof SecretRecipe) {
-                SecretRecipe recipe = (SecretRecipe) value;
-                value = recipe.create(this, classLoader);
-            } else {
-                Recipe recipe = (Recipe) value;
-                value = recipe.create(classLoader);
-            }
-        }
-
-        if (value instanceof String && (type != Object.class)) {
-            String stringValue = (String) value;
-            value = PropertyEditors.getValue(type, stringValue);
-        }
-        return value;
-    }
-
     private static Object getDefaultValue(Class type) {
         if (type.equals(Boolean.TYPE)) {
             return Boolean.FALSE;
         } else if (type.equals(Character.TYPE)) {
-            return new Character((char) 0);
+            return (char) 0;
         } else if (type.equals(Byte.TYPE)) {
-            return new Byte((byte) 0);
+            return (byte) 0;
         } else if (type.equals(Short.TYPE)) {
-            return new Short((short) 0);
+            return (short) 0;
         } else if (type.equals(Integer.TYPE)) {
-            return new Integer(0);
+            return 0;
         } else if (type.equals(Long.TYPE)) {
-            return new Long(0);
+            return (long) 0;
         } else if (type.equals(Float.TYPE)) {
-            return new Float(0);
+            return (float) 0;
         } else if (type.equals(Double.TYPE)) {
-            return new Double(0);
+            return (double) 0;
         }
         return null;
     }
 
-    private Object createInstance(Constructor constructor, Map propertyValues, ClassLoader classLoader) {
-        // get the constructor parameters
-        Object[] parameters = extractConstructorArgs(propertyValues, constructor.getParameterTypes(), classLoader);
-
-        try {
-            Object object = constructor.newInstance(parameters);
-            return object;
-        } catch (Exception e) {
-            Throwable t = e;
-            if (e instanceof InvocationTargetException) {
-                InvocationTargetException invocationTargetException = (InvocationTargetException) e;
-                if (invocationTargetException.getCause() != null) {
-                    t = invocationTargetException.getCause();
-                }
-            }
-            throw new ConstructionException("Error invoking constructor: " + constructor, t);
-        }
-    }
-
-    private Object createInstance(Method method, Map propertyValues, ClassLoader classLoader) {
-        // get the constructor parameters
-        Object[] parameters = extractConstructorArgs(propertyValues, method.getParameterTypes(), classLoader);
-
-        try {
-            Object object = method.invoke(null, parameters);
-            return object;
-        } catch (Exception e) {
-            Throwable t = e;
-            if (e instanceof InvocationTargetException) {
-                InvocationTargetException invocationTargetException = (InvocationTargetException) e;
-                if (invocationTargetException.getCause() != null) {
-                    t = invocationTargetException.getCause();
-                }
-            }
-            throw new ConstructionException("Error invoking factory method: " + method, t);
-        }
-    }
-
-    private Constructor selectConstructor(Class typeClass) {
-        if (constructorArgNames.length > 0 && constructorArgTypes.length == 0) {
-            ArrayList<Constructor> matches = new ArrayList<Constructor>();
-
-            Constructor[] constructors = typeClass.getConstructors();
-            for (Constructor constructor : constructors) {
-                if (constructor.getParameterTypes().length == constructorArgNames.length) {
-                    matches.add(constructor);
-                }
-            }
-
-            if (matches.size() < 1) {
-                StringBuffer buffer = new StringBuffer("No parameter types supplied; unable to find a potentially valid constructor: ");
-                buffer.append("constructor= public ").append(typeClass.getName());
-                buffer.append(toArgumentList(constructorArgNames));
-                throw new ConstructionException(buffer.toString());
-            } else if (matches.size() > 1) {
-                StringBuffer buffer = new StringBuffer("No parameter types supplied; found too many potentially valid constructors: ");
-                buffer.append("constructor= public ").append(typeClass.getName());
-                buffer.append(toArgumentList(constructorArgNames));
-                throw new ConstructionException(buffer.toString());
-            }
-
-            return matches.get(0);
-        }
-
-        try {
-            Constructor constructor = typeClass.getConstructor(constructorArgTypes);
-
-            if (!Modifier.isPublic(constructor.getModifiers())) {
-                // this will never occur since private constructors are not returned from
-                // getConstructor, but leave this here anyway, just to be safe
-                throw new ConstructionException("Constructor is not public: " + constructor);
-            }
-
-            return constructor;
-        } catch (NoSuchMethodException e) {
-            // try to find a matching private method
-            Constructor[] constructors = typeClass.getDeclaredConstructors();
-            for (Constructor constructor : constructors) {
-                if (isAssignableFrom(constructorArgTypes, constructor.getParameterTypes())) {
-                    if (!Modifier.isPublic(constructor.getModifiers())) {
-                        throw new ConstructionException("Constructor is not public: " + constructor);
-                    }
-                }
-            }
-
-            StringBuffer buffer = new StringBuffer("Unable to find a valid constructor: ");
-            buffer.append("constructor= public ").append(typeClass.getName());
-            buffer.append(toParameterList(constructorArgTypes));
-            throw new ConstructionException(buffer.toString());
-        }
-    }
-
-    private String toParameterList(Class[] parameterTypes) {
-        StringBuffer buffer = new StringBuffer();
-        buffer.append("(");
-        for (int i = 0; i < parameterTypes.length; i++) {
-            Class type = parameterTypes[i];
-            if (i > 0) buffer.append(", ");
-            buffer.append(type.getName());
-        }
-        buffer.append(")");
-        return buffer.toString();
-    }
-
-    private String toArgumentList(String[] parameterNames) {
-        StringBuffer buffer = new StringBuffer();
-        buffer.append("(");
-        for (int i = 0; i < parameterNames.length; i++) {
-            String parameterName = parameterNames[i];
-            if (i > 0) buffer.append(", ");
-            buffer.append('<').append(parameterName).append('>');
-        }
-        buffer.append(")");
-        return buffer.toString();
-    }
-
-    public Method findFactoryMethod(Class typeClass, String factoryMethod) {
-        if (factoryMethod == null) throw new NullPointerException("name is null");
-        if (factoryMethod.length() == 0) throw new IllegalArgumentException("name is an empty string");
-
-        int matchLevel = 0;
-        MissingFactoryMethodException missException = null;
-
-        List<Method> methods = new ArrayList<Method>(Arrays.asList(typeClass.getMethods()));
-        methods.addAll(Arrays.asList(typeClass.getDeclaredMethods()));
-        for (Method method : methods) {
-            if (method.getName().equals(factoryMethod)) {
-                if (Modifier.isStatic(method.getModifiers())) {
-                    if (method.getParameterTypes().length != constructorArgNames.length) {
-                        if (matchLevel < 1) {
-                            matchLevel = 1;
-                            missException = new MissingFactoryMethodException("Static factory method has " + method.getParameterTypes().length + " arugments " +
-                                    "but expected " + constructorArgNames.length + " arguments: " + method);
-                        }
-                        continue;
-                    }
-
-                    if (constructorArgTypes.length > 0 && !isAssignableFrom(constructorArgTypes, method.getParameterTypes())) {
-                        if (matchLevel < 2) {
-                            matchLevel = 2;
-                            missException = new MissingFactoryMethodException("Static factory method has signature " +
-                                    "public static " + typeClass.getName() + "." + factoryMethod + toParameterList(method.getParameterTypes()) +
-                                    " but expected signature " +
-                                    "public static " + typeClass.getName() + "." + factoryMethod + toParameterList(constructorArgTypes));
-                        }
-                        continue;
-                    }
-                } else {
-                    if (method.getParameterTypes().length != 0) {
-                        if (matchLevel < 1) {
-                            matchLevel = 1;
-                            missException = new MissingFactoryMethodException("Instance factory method has parameters: " + method);
-                        }
-                        continue;
-                    }
-                }
-
-                if (method.getReturnType() == Void.TYPE) {
-                    if (matchLevel < 3) {
-                        matchLevel = 3;
-                        missException = new MissingFactoryMethodException("Factory method does not return a value: " + method);
-                    }
-                    continue;
-                }
-
-                if (Modifier.isAbstract(method.getModifiers())) {
-                    if (matchLevel < 4) {
-                        matchLevel = 4;
-                        missException = new MissingFactoryMethodException("Factory method is abstract: " + method);
-                    }
-                    continue;
-                }
-
-                if (!Modifier.isPublic(method.getModifiers())) {
-                    if (matchLevel < 5) {
-                        matchLevel = 5;
-                        missException = new MissingFactoryMethodException("Factory method is not public: " + method);
-                    }
-                    continue;
-                }
-
-                return method;
-            }
-        }
-
-        if (missException != null) {
-            throw missException;
-        } else {
-            StringBuffer buffer = new StringBuffer("Unable to find a valid factory method: ");
-            buffer.append("public void ").append(typeClass.getName()).append(".");
-            buffer.append(factoryMethod).append("()");
-            throw new MissingFactoryMethodException(buffer.toString());
-        }
-    }
-
-    /**
-     * @deprecated use the method with allowStatic
-     * @param typeClass
-     * @param propertyName
-     * @param propertyValue
-     * @param allowPrivate
-     * @param classLoader
-     * @return field
-     */
-    public static Method findSetter(Class typeClass, String propertyName, Object propertyValue, boolean allowPrivate, ClassLoader classLoader) {
-        return findSetter(typeClass, propertyName,  propertyValue, allowPrivate, false, false, classLoader);
-    }
-
-    public static Method findSetter(Class typeClass, String propertyName, Object propertyValue, boolean allowPrivate, boolean allowStatic, boolean caseInsesnitive, ClassLoader classLoader) {
-        if (propertyName == null) throw new NullPointerException("name is null");
-        if (propertyName.length() == 0) throw new IllegalArgumentException("name is an empty string");
-
-        if (propertyName.contains("/")){
-            String[] strings = propertyName.split("/");
-            if (strings == null || strings.length != 2) throw new IllegalArgumentException("badly formed <class>/<attribute> property name: " + propertyName);
-
-            String className = strings[0];
-            propertyName = strings[1];
-
-            boolean found = false;
-            while(!typeClass.equals(Object.class) && !found){
-                if (typeClass.getName().equals(className)){
-                    found = true;
-                    break;
-                } else {
-                    typeClass = typeClass.getSuperclass();
-                }
-            }
-
-            if (!found) throw new MissingAccessorException("Type not assignable to class: " + className, -1);
-        }
-
-        String setterName = "set" + Character.toUpperCase(propertyName.charAt(0));
-        if (propertyName.length() > 0) {
-            setterName += propertyName.substring(1);
-        }
-
-
-        int matchLevel = 0;
-        MissingAccessorException missException = null;
-
-        List<Method> methods = new ArrayList<Method>(Arrays.asList(typeClass.getMethods()));
-        methods.addAll(Arrays.asList(typeClass.getDeclaredMethods()));
-        for (Method method : methods) {
-            if (method.getName().equals(setterName) || (caseInsesnitive && method.getName().equalsIgnoreCase(setterName))) {
-                if (method.getParameterTypes().length == 0) {
-                    if (matchLevel < 1) {
-                        matchLevel = 1;
-                        missException = new MissingAccessorException("Setter takes no parameters: " + method, matchLevel);
-                    }
-                    continue;
-                }
-
-                if (method.getParameterTypes().length > 1) {
-                    if (matchLevel < 1) {
-                        matchLevel = 1;
-                        missException = new MissingAccessorException("Setter takes more then one parameter: " + method, matchLevel);
-                    }
-                    continue;
-                }
-
-                if (method.getReturnType() != Void.TYPE) {
-                    if (matchLevel < 2) {
-                        matchLevel = 2;
-                        missException = new MissingAccessorException("Setter returns a value: " + method, matchLevel);
-                    }
-                    continue;
-                }
-
-                if (Modifier.isAbstract(method.getModifiers())) {
-                    if (matchLevel < 3) {
-                        matchLevel = 3;
-                        missException = new MissingAccessorException("Setter is abstract: " + method, matchLevel);
-                    }
-                    continue;
-                }
-
-                if (!allowPrivate && !Modifier.isPublic(method.getModifiers())) {
-                    if (matchLevel < 4) {
-                        matchLevel = 4;
-                        missException = new MissingAccessorException("Setter is not public: " + method, matchLevel);
-                    }
-                    continue;
-                }
-
-                if (!allowStatic && Modifier.isStatic(method.getModifiers())) {
-                    if (matchLevel < 4) {
-                        matchLevel = 4;
-                        missException = new MissingAccessorException("Setter is static: " + method, matchLevel);
-                    }
-                    continue;
-                }
-
-                Class methodParameterType = method.getParameterTypes()[0];
-                if (methodParameterType.isPrimitive() && propertyValue == null) {
-                    if (matchLevel < 6) {
-                        matchLevel = 6;
-                        missException = new MissingAccessorException("Null can not be assigned to " +
-                                methodParameterType.getName() + ": " + method, matchLevel);
-                    }
-                    continue;
-                }
-
-
-                if (!RecipeHelper.isInstance(methodParameterType, propertyValue) && !RecipeHelper.isConvertable(methodParameterType, propertyValue, classLoader)) {
-                    if (matchLevel < 5) {
-                        matchLevel = 5;
-                        missException = new MissingAccessorException(getClassName(propertyValue) + " can not be assigned or converted to " +
-                                methodParameterType.getName() + ": " + method, matchLevel);
-                    }
-                    continue;
-                }
-
-                if (allowPrivate && !Modifier.isPublic(method.getModifiers())) {
-                    setAccessible(method);
-                }
-
-                return method;
-            }
-
-        }
-
-        if (missException != null) {
-            throw missException;
-        } else {
-            StringBuffer buffer = new StringBuffer("Unable to find a valid setter method: ");
-            buffer.append("public void ").append(typeClass.getName()).append(".");
-            buffer.append(setterName).append("(").append(getClassName(propertyValue)).append(")");
-            throw new MissingAccessorException(buffer.toString(), -1);
-        }
-    }
-
-    /**
-     * @deprecated use the method with allowStatic
-     * @param typeClass
-     * @param propertyName
-     * @param propertyValue
-     * @param allowPrivate
-     * @param classLoader
-     * @return field
-     */
-    public static Field findField(Class typeClass, String propertyName, Object propertyValue, boolean allowPrivate, ClassLoader classLoader) {
-        return findField(typeClass, propertyName,  propertyValue, allowPrivate, false, false, classLoader);
-    }
-
-    public static Field findField(Class typeClass, String propertyName, Object propertyValue, boolean allowPrivate, boolean allowStatic, boolean caseInsesnitive, ClassLoader classLoader) {
-        if (propertyName == null) throw new NullPointerException("name is null");
-        if (propertyName.length() == 0) throw new IllegalArgumentException("name is an empty string");
-
-        int matchLevel = 0;
-        MissingAccessorException missException = null;
-
-        if (propertyName.contains("/")){
-            String[] strings = propertyName.split("/");
-            if (strings == null || strings.length != 2) throw new IllegalArgumentException("badly formed <class>/<attribute> property name: " + propertyName);
-
-            String className = strings[0];
-            propertyName = strings[1];
-
-            boolean found = false;
-            while(!typeClass.equals(Object.class) && !found){
-                if (typeClass.getName().equals(className)){
-                    found = true;
-                    break;
-                } else {
-                    typeClass = typeClass.getSuperclass();
-                }
-            }
-
-            if (!found) throw new MissingAccessorException("Type not assignable to class: " + className, -1);
-        }
-        
-        List<Field> fields = new ArrayList<Field>(Arrays.asList(typeClass.getDeclaredFields()));
-        Class parent = typeClass.getSuperclass();
-        while (parent != null){
-            fields.addAll(Arrays.asList(parent.getDeclaredFields()));
-            parent = parent.getSuperclass();
-        }
-
-        for (Field field : fields) {
-            if (field.getName().equals(propertyName) || (caseInsesnitive && field.getName().equalsIgnoreCase(propertyName))) {
-
-                if (!allowPrivate && !Modifier.isPublic(field.getModifiers())) {
-                    if (matchLevel < 4) {
-                        matchLevel = 4;
-                        missException = new MissingAccessorException("Field is not public: " + field, matchLevel);
-                    }
-                    continue;
-                }
-
-                if (!allowStatic && Modifier.isStatic(field.getModifiers())) {
-                    if (matchLevel < 4) {
-                        matchLevel = 4;
-                        missException = new MissingAccessorException("Field is static: " + field, matchLevel);
-                    }
-                    continue;
-                }
-
-                Class fieldType = field.getType();
-                if (fieldType.isPrimitive() && propertyValue == null) {
-                    if (matchLevel < 6) {
-                        matchLevel = 6;
-                        missException = new MissingAccessorException("Null can not be assigned to " +
-                                fieldType.getName() + ": " + field, matchLevel);
-                    }
-                    continue;
-                }
-
-
-                if (!RecipeHelper.isInstance(fieldType, propertyValue) && !RecipeHelper.isConvertable(fieldType, propertyValue, classLoader)) {
-                    if (matchLevel < 5) {
-                        matchLevel = 5;
-                        missException = new MissingAccessorException(getClassName(propertyValue) + " can not be assigned or converted to " +
-                                fieldType.getName() + ": " + field, matchLevel);
-                    }
-                    continue;
-                }
-
-                if (allowPrivate && !Modifier.isPublic(field.getModifiers())) {
-                    setAccessible(field);
-                }
-
-                return field;
-            }
-
-        }
-
-        if (missException != null) {
-            throw missException;
-        } else {
-            StringBuffer buffer = new StringBuffer("Unable to find a valid field: ");
-            buffer.append("public ").append(" ").append(getClassName(propertyValue));
-            buffer.append(" ").append(propertyName).append(";");
-            throw new MissingAccessorException(buffer.toString(), -1);
-        }
-    }
-
-    public static boolean isAssignableFrom(Class[] expectedTypes, Class[] actualTypes) {
-        if (expectedTypes.length != actualTypes.length) {
-            return false;
-        }
-        for (int i = 0; i < expectedTypes.length; i++) {
-            Class expectedType = expectedTypes[i];
-            Class actualType = actualTypes[i];
-            if (expectedType != actualType && !RecipeHelper.isAssignableFrom(expectedType, actualType)) {
-                return false;
-            }
-        }
-        return true;
-    }
-
-    private static void setAccessible(final Method method) {
-        AccessController.doPrivileged(new PrivilegedAction<Object>() {
-            public Object run() {
-                method.setAccessible(true);
-                return null;
-            }
-        });
-    }
-
-    private static void setAccessible(final Field field) {
-        AccessController.doPrivileged(new PrivilegedAction<Object>() {
-            public Object run() {
-                field.setAccessible(true);
-                return null;
-            }
-        });
-    }
-
     public static interface Member {
         Class getType();
         void setValue(Object instance, Object value) throws Exception;
@@ -896,7 +495,7 @@
         }
     }
 
-    private static class Property {
+    public static class Property {
         private final String name;
 
         public Property(String name) {
@@ -926,7 +525,7 @@
         }
     }
 
-    private static class SetterProperty extends Property {
+    public static class SetterProperty extends Property {
         public SetterProperty(String name) {
             super(name);
         }
@@ -934,11 +533,12 @@
             return super.hashCode()+2;
         }
         public String toString() {
-            return "[setter] "+toString();
+            return "[setter] "+super.toString();
         }
 
     }
-    private static class FieldProperty extends Property {
+
+    public static class FieldProperty extends Property {
         public FieldProperty(String name) {
             super(name);
         }
@@ -947,7 +547,7 @@
             return super.hashCode()+1;
         }
         public String toString() {
-            return "[field] "+toString();
+            return "[field] "+ super.toString();
         }
     }
 }

Modified: geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/Option.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/Option.java?rev=617547&r1=617546&r2=617547&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/Option.java (original)
+++ geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/Option.java Fri Feb  1 08:23:21 2008
@@ -19,21 +19,14 @@
 /**
  * @version $Rev$ $Date$
  */
-public class Option {
-
-    public static final Option PRIVATE_PROPERTIES = new Option("PRIVATE_PROPERTIES");
-    public static final Option STATIC_PROPERTIES = new Option("STATIC_PROPERTIES");
-    public static final Option FIELD_INJECTION = new Option("FIELD_INJECTION");
-    public static final Option IGNORE_MISSING_PROPERTIES = new Option("IGNORE_MISSING_PROPERTIES"); 
-    public static final Option CASE_INSENSITIVE_PROPERTIES = new Option("CASE_INSENSITIVE_PROPERTIES"); 
-
-    private final String name;
-
-    public Option(String name) {
-        this.name = name;
-    }
-
-    public String toString() {
-        return name;
-    }
+public enum Option {
+    PRIVATE_PROPERTIES,
+    STATIC_PROPERTIES,
+    FIELD_INJECTION,
+    IGNORE_MISSING_PROPERTIES,
+    CASE_INSENSITIVE_PROPERTIES,
+    LAZY_ASSIGNMENT,
+    PRIVATE_CONSTRUCTOR,
+    PRIVATE_FACTORY,
+    CASE_INSENSITIVE_FACTORY,    
 }

Modified: geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/Recipe.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/Recipe.java?rev=617547&r1=617546&r2=617547&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/Recipe.java (original)
+++ geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/Recipe.java Fri Feb  1 08:23:21 2008
@@ -17,15 +17,23 @@
 package org.apache.xbean.recipe;
 
 import java.io.Serializable;
+import java.util.List;
 
 /**
  * @version $Rev: 6680 $ $Date: 2005-12-24T04:38:27.427468Z $
  */
 public interface Recipe extends Serializable {
+    String getName();
+
     float getPriority();
 
-    boolean canCreate(Class type, ClassLoader classLoader);
+    boolean canCreate(Class type);
 
     Object create() throws ConstructionException;
     Object create(ClassLoader classLoader) throws ConstructionException;
+    Object create(Class expectedType, boolean lazyRefAllowed) throws ConstructionException;
+
+    List<Recipe> getNestedRecipes();
+
+    List<Recipe> getConstructorRecipes();
 }

Modified: geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/RecipeHelper.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/RecipeHelper.java?rev=617547&r1=617546&r2=617547&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/RecipeHelper.java (original)
+++ geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/RecipeHelper.java Fri Feb  1 08:23:21 2008
@@ -20,11 +20,15 @@
 
 import java.lang.reflect.Modifier;
 import java.lang.reflect.Constructor;
+import java.lang.reflect.Type;
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.TypeVariable;
 import java.util.Comparator;
 import java.util.Map;
 import java.util.List;
 import java.util.Collections;
 import java.util.ArrayList;
+import java.util.LinkedList;
 
 /**
  * @version $Rev: 6687 $ $Date: 2005-12-28T21:08:56.733437Z $
@@ -33,13 +37,26 @@
     private RecipeHelper() {
     }
 
+    public static Recipe getCaller() {
+        LinkedList<Recipe> stack = ExecutionContext.getContext().getStack();
+        if (stack.size() < 2) {
+            return null;
+        }
+        return stack.get(stack.size() - 2);
+    }
+
+    public static Class loadClass(String name) throws ClassNotFoundException {
+        ClassLoader classLoader = ExecutionContext.getContext().getClassLoader();
+        Class<?> type = Class.forName(name, true, classLoader);
+        return type;
+    }
+
     public static boolean hasDefaultConstructor(Class type) {
         if (!Modifier.isPublic(type.getModifiers())) {
             return false;
         }
         Constructor[] constructors = type.getConstructors();
-        for (int i = 0; i < constructors.length; i++) {
-            Constructor constructor = constructors[i];
+        for (Constructor constructor : constructors) {
             if (Modifier.isPublic(constructor.getModifiers()) &&
                     constructor.getParameterTypes().length == 0) {
                 return true;
@@ -101,15 +118,17 @@
         return instance == null || type.isInstance(instance);
     }
 
-    public static boolean isConvertable(Class type, Object propertyValue, ClassLoader classLoader) {
+    public static boolean isConvertable(Class type, Object propertyValue) {
         if (propertyValue instanceof Recipe) {
             Recipe recipe = (Recipe) propertyValue;
-            return recipe.canCreate(type, classLoader);
+            return recipe.canCreate(type);
         }
         return (propertyValue instanceof String && PropertyEditors.canConvert(type));
     }
 
     public static boolean isAssignableFrom(Class expected, Class actual) {
+        if (expected == null) return true;
+
         if (expected.isPrimitive()) {
             // verify actual is the correct wrapper type
             if (expected.equals(boolean.class)) {
@@ -136,6 +155,33 @@
         return expected.isAssignableFrom(actual);
     }
 
+    public static Object convert(Class expectedType, Object value, boolean lazyRefAllowed) {
+        if (value instanceof Recipe) {
+            Recipe recipe = (Recipe) value;
+            value = recipe.create(expectedType, lazyRefAllowed);
+        }
+
+        if (value instanceof String && (expectedType != Object.class)) {
+            String stringValue = (String) value;
+            value = PropertyEditors.getValue(expectedType, stringValue);
+        }
+        return value;
+    }
+
+    public static boolean isAssignableFrom(Class[] expectedTypes, Class[] actualTypes) {
+        if (expectedTypes.length != actualTypes.length) {
+            return false;
+        }
+        for (int i = 0; i < expectedTypes.length; i++) {
+            Class expectedType = expectedTypes[i];
+            Class actualType = actualTypes[i];
+            if (expectedType != actualType && !isAssignableFrom(expectedType, actualType)) {
+                return false;
+            }
+        }
+        return true;
+    }
+
     public static class RecipeComparator implements Comparator<Object> {
         public int compare(Object left, Object right) {
             if (!(left instanceof Recipe) && !(right instanceof Recipe)) return 0;
@@ -149,5 +195,50 @@
             if (leftPriority < rightPriority) return -1;
             return 0;
         }
+    }
+
+    public static Type[] getTypeParameters(Class desiredType, Type type) {
+        if (type instanceof Class) {
+            Class rawClass = (Class) type;
+
+            // if this is the collection class we're done
+            if (desiredType.equals(type)) {
+                return null;
+            }
+
+            for (Type intf : rawClass.getGenericInterfaces()) {
+                Type[] collectionType = getTypeParameters(desiredType, intf);
+                if (collectionType != null) {
+                    return collectionType;
+                }
+            }
+
+            Type[] collectionType = getTypeParameters(desiredType, rawClass.getGenericSuperclass());
+            return collectionType;
+        } else if (type instanceof ParameterizedType) {
+            ParameterizedType parameterizedType = (ParameterizedType) type;
+
+            Type rawType = parameterizedType.getRawType();
+            if (desiredType.equals(rawType)) {
+                Type[] argument = parameterizedType.getActualTypeArguments();
+                return argument;
+            }
+            Type[] collectionTypes = getTypeParameters(desiredType,rawType);
+            if (collectionTypes != null) {
+                for (int i = 0; i < collectionTypes.length; i++) {
+                    if (collectionTypes[i] instanceof TypeVariable) {
+                        TypeVariable typeVariable = (TypeVariable) collectionTypes[i];
+                        TypeVariable[] rawTypeParams = ((Class) rawType).getTypeParameters();
+                        for (int j = 0; j < rawTypeParams.length; j++) {
+                            if (typeVariable.getName().equals(rawTypeParams[j].getName())) {
+                                collectionTypes[i] = parameterizedType.getActualTypeArguments()[j];
+                            }
+                        }
+                    }
+                }
+            }
+            return collectionTypes;
+        }
+        return null;
     }
 }

Added: geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/RecipeVisitor.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/RecipeVisitor.java?rev=617547&view=auto
==============================================================================
--- geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/RecipeVisitor.java (added)
+++ geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/RecipeVisitor.java Fri Feb  1 08:23:21 2008
@@ -0,0 +1,22 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file 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
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  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 KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.xbean.recipe;
+
+public interface RecipeVisitor {
+    void visit(Recipe recipe);
+}

Added: geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/Reference.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/Reference.java?rev=617547&view=auto
==============================================================================
--- geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/Reference.java (added)
+++ geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/Reference.java Fri Feb  1 08:23:21 2008
@@ -0,0 +1,85 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file 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
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  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 KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.xbean.recipe;
+
+/**
+ * Reference is a named (lazy) reference from one object to another. This data class is updated when the reference
+ * is resolved which can be immedately when the ref is created, or later when an instance with the referenced
+ * name is created.
+ * <p/>
+ * When the reference is resolved, an optional Action will be invoked which is commonly used to update a
+ * property on the source object of the reference.
+ */
+public class Reference {
+    private boolean resolved;
+    private Object instance;
+    private Action action;
+
+    /**
+     * Has this reference been resolved?
+     * @return true if the reference has been resolved; false otherwise
+     */
+    public boolean isResolved() {
+        return resolved;
+    }
+
+    /**
+     * Gets the referenced object instance or null if the reference has not been resolved yet;
+     *
+     * @return the referenced object instance or null
+     */
+    public Object get() {
+        return instance;
+    }
+
+    /**
+     * Sets the referenced object instance.  If an action is registered the onSet method is invoked.
+     *
+     * @param object the reference instance
+     */
+    public void set(Object object) {
+        if (resolved) {
+            throw new ConstructionException("Reference has already been resolved");
+        }
+        resolved = true;
+        this.instance = object;
+        if (action != null) {
+            action.onSet(this);
+        }
+    }
+
+    /**
+     * Registers an action to invoke when the instance is set.  If the instance, has already been set, the
+     * onSet method will immedately be invoked.
+     *
+     * @return the action to invoke when this refernce is resolved; not null
+     */
+    public void setAction(Action action) {
+        if (action == null) {
+            throw new NullPointerException("action is null");
+        }
+        this.action = action;
+        if (resolved) {
+            action.onSet(this);
+        }
+    }
+
+    public static interface Action {
+        void onSet(Reference ref);
+    }
+}
\ No newline at end of file

Added: geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/ReferenceRecipe.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/ReferenceRecipe.java?rev=617547&view=auto
==============================================================================
--- geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/ReferenceRecipe.java (added)
+++ geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/ReferenceRecipe.java Fri Feb  1 08:23:21 2008
@@ -0,0 +1,151 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file 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
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  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 KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.xbean.recipe;
+
+import java.util.Collections;
+import java.util.List;
+
+public class ReferenceRecipe extends AbstractRecipe {
+    private String referenceName;
+
+    public ReferenceRecipe() {
+    }
+
+    public ReferenceRecipe(String referenceName) {
+        this.referenceName = referenceName;
+    }
+
+    public String getReferenceName() {
+        return referenceName;
+    }
+
+    public void setReferenceName(String name) {
+        this.referenceName = name;
+    }
+
+    public List<Recipe> getNestedRecipes() {
+        ExecutionContext context = ExecutionContext.getContext();
+        if (!context.containsObject(referenceName)) {
+            throw new NoSuchObjectException(referenceName);
+        }
+
+        Object object = ExecutionContext.getContext().getObject(referenceName);
+        if (object instanceof Recipe) {
+            Recipe recipe = (Recipe) object;
+            return Collections.singletonList(recipe);
+        }
+        return Collections.emptyList();
+    }
+
+    public List<Recipe> getConstructorRecipes() {
+        return getNestedRecipes();
+    }
+
+    public boolean canCreate(Class type) {
+        if (referenceName == null) {
+            throw new ConstructionException("Reference name has not been set");
+        }
+
+        ExecutionContext context = ExecutionContext.getContext();
+
+        Object object = context.getObject(referenceName);
+        if (object instanceof Recipe) {
+            Recipe recipe = (Recipe) object;
+            return recipe.canCreate(type);
+        } else {
+            return RecipeHelper.isInstance(type, object);
+        }
+    }
+
+    protected Object internalCreate(Class expectedType, boolean lazyRefAllowed) throws ConstructionException {
+        if (referenceName == null) {
+            throw new ConstructionException("Reference name has not been set");
+        }
+
+        ExecutionContext context = ExecutionContext.getContext();
+
+        Object object;
+        if (!context.containsObject(referenceName)) {
+            if (!lazyRefAllowed) {
+                throw new ConstructionException("Currently no object registered with name " + referenceName +
+                        " and a lazy reference not allowed");
+            }
+
+            Reference reference = new Reference();
+            context.addReference(referenceName, reference);
+            object = reference;
+        } else {
+            object = context.getObject(referenceName);
+            if (object instanceof Recipe) {
+                if (lazyRefAllowed) {
+                    Reference reference = new Reference();
+                    context.addReference(referenceName, reference);
+                    object = reference;
+                } else {
+                    Recipe recipe = (Recipe) object;
+                    object = recipe.create(expectedType, false);
+                }
+
+            }
+        }
+
+        // add to execution context if name is specified
+        if (getName() != null) {
+            if (object instanceof Reference) {
+                object = new WrapperReference(getName(), (Reference) object);
+            } else {
+                ExecutionContext.getContext().addObject(getName(), object);
+            }
+        }
+
+        return object;
+    }
+
+    private static class WrapperReference extends Reference {
+        private final String name;
+        private final Reference delegate;
+
+        private WrapperReference(String name, Reference delegate) {
+            this.name = name;
+            this.delegate = delegate;
+        }
+
+        public boolean isResolved() {
+            return delegate.isResolved();
+        }
+
+        public Object get() {
+            return delegate.get();
+        }
+
+        public void set(Object object) {
+            if (isResolved()) {
+                throw new ConstructionException("Reference has already been resolved");
+            }
+
+            // add to execution context
+            ExecutionContext.getContext().addObject(name, object);
+
+            delegate.set(object);
+        }
+
+        public void setAction(Action action) {
+            delegate.setAction(action);
+        }
+    }
+}



Mime
View raw message