protected void processBean()

in jelly-tags/bean/src/main/java/org/apache/commons/jelly/tags/bean/BeanTag.java [87:163]


    protected void processBean(String var, Object bean) throws JellyTagException {
        if (var != null) {
            context.setVariable(var, bean);
        }

        // now lets try set the parent property via calling the adder or the setter method
        if (bean != null) {
            Tag parent = this;

            while (true) {
                parent = parent.getParent();
                if (parent == null) {
                    break;
                }

                if (parent instanceof BeanSource) {
                    BeanSource source = (BeanSource) parent;
                    Object parentObject = source.getBean();
                    if (parentObject != null) {
                        if (parentObject instanceof Collection) {
                            Collection collection = (Collection) parentObject;
                            collection.add(bean);
                        }
                        else {
                            // lets see if there's a setter method...
                            Method method = findAddMethod(parentObject.getClass(), bean.getClass());
                            if (method != null) {
                                Object[] args = { bean };
                                try {
                                    method.invoke(parentObject, args);
                                }
                                catch (Exception e) {
                                    throw new JellyTagException( "failed to invoke method: " + method + " on bean: " + parentObject + " reason: " + e, e );
                                }
                            }
                            else {
                                try {
                                  BeanUtils.setProperty(parentObject, tagName, bean);
                                } catch (IllegalAccessException e) {
                                    throw new JellyTagException(e);
                                } catch (InvocationTargetException e) {
                                    throw new JellyTagException(e);
                                }
                            }
                        }
                    }
                    else {
                        log.warn("Cannot process null bean for tag: " + parent);
                    }
                }
                else if (parent instanceof CollectionTag) {
                    CollectionTag tag = (CollectionTag) parent;
                    tag.addItem(bean);
                }
                else {
                    continue;
                }
                break;
            }

            if (invokeMethod != null) {
                Object[] args = { bean };
                try {
                    invokeMethod.invoke(bean, EMPTY_ARGUMENTS);
                }
                catch (Exception e) {
                    throw new JellyTagException( "failed to invoke method: " + invokeMethod + " on bean: " + bean + " reason: " + e, e );
                }
            }
            else {
                if (parent == null && var == null) {
                    //warn if the bean gets lost in space
                    log.warn( "Could not add bean to parent for bean: " + bean );
                }
            }
        }
    }