xbean-blueprint/src/main/java/org/apache/xbean/blueprint/generator/QdoxMappingLoader.java [50:477]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
public class QdoxMappingLoader implements MappingLoader {
    public static final String XBEAN_ANNOTATION = "org.apache.xbean.XBean";
    public static final String PROPERTY_ANNOTATION = "org.apache.xbean.Property";
    public static final String INIT_METHOD_ANNOTATION = "org.apache.xbean.InitMethod";
    public static final String DESTROY_METHOD_ANNOTATION = "org.apache.xbean.DestroyMethod";
    public static final String FACTORY_METHOD_ANNOTATION = "org.apache.xbean.FactoryMethod";
    public static final String MAP_ANNOTATION = "org.apache.xbean.Map";
    public static final String FLAT_PROPERTY_ANNOTATION = "org.apache.xbean.Flat";
    public static final String FLAT_COLLECTION_ANNOTATION = "org.apache.xbean.FlatCollection";
    public static final String ELEMENT_ANNOTATION = "org.apache.xbean.Element";

    private static final Log log = LogFactory.getLog(QdoxMappingLoader.class);
    private final String defaultNamespace;
    private final File[] srcDirs;
    private final String[] excludedClasses;
    private Type collectionType;

    public QdoxMappingLoader(String defaultNamespace, File[] srcDirs, String[] excludedClasses) {
        this.defaultNamespace = defaultNamespace;
        this.srcDirs = srcDirs;
        this.excludedClasses = excludedClasses;
    }

    public String getDefaultNamespace() {
        return defaultNamespace;
    }

    public File[] getSrcDirs() {
        return srcDirs;
    }

    public Set<NamespaceMapping> loadNamespaces() throws IOException {
        JavaDocBuilder builder = new JavaDocBuilder();

        log.debug("Source directories: ");

        for (File sourceDirectory : srcDirs) {
            if (!sourceDirectory.isDirectory() && !sourceDirectory.toString().endsWith(".jar")) {
                log.warn("Specified source directory isn't a directory or a jar file: '" + sourceDirectory.getAbsolutePath() + "'.");
            }
            log.debug(" - " + sourceDirectory.getAbsolutePath());

            getSourceFiles(sourceDirectory, excludedClasses, builder);
        }

        collectionType = builder.getClassByName("java.util.Collection").asType();
        return loadNamespaces(builder);
    }

    private Set<NamespaceMapping> loadNamespaces(JavaDocBuilder builder) {
        // load all of the elements
        List<ElementMapping> elements = loadElements(builder);

        // index the elements by namespace and find the root element of each namespace
        Map<String, Set<ElementMapping>> elementsByNamespace = new HashMap<String, Set<ElementMapping>>();
        Map<String, ElementMapping> namespaceRoots = new HashMap<String, ElementMapping>();
        for (ElementMapping element : elements) {
            String namespace = element.getNamespace();
            Set<ElementMapping> namespaceElements = elementsByNamespace.get(namespace);
            if (namespaceElements == null) {
                namespaceElements = new HashSet<ElementMapping>();
                elementsByNamespace.put(namespace, namespaceElements);
            }
            namespaceElements.add(element);
            if (element.isRootElement()) {
                if (namespaceRoots.containsKey(namespace)) {
                    log.info("Multiple root elements found for namespace " + namespace);
                }
                namespaceRoots.put(namespace, element);
            }
        }

        // build the NamespaceMapping objects
        Set<NamespaceMapping> namespaces = new TreeSet<NamespaceMapping>();
        for (Map.Entry<String, Set<ElementMapping>> entry : elementsByNamespace.entrySet()) {
            String namespace = entry.getKey();
            Set namespaceElements = entry.getValue();
            ElementMapping rootElement = namespaceRoots.get(namespace);
            NamespaceMapping namespaceMapping = new NamespaceMapping(namespace, namespaceElements, rootElement);
            namespaces.add(namespaceMapping);
        }
        return Collections.unmodifiableSet(namespaces);
    }

    private List<ElementMapping> loadElements(JavaDocBuilder builder) {
        JavaSource[] javaSources = builder.getSources();
        List<ElementMapping> elements = new ArrayList<ElementMapping>();
        for (JavaSource javaSource : javaSources) {
            if (javaSource.getClasses().length == 0) {
                log.info("No Java Classes defined in: " + javaSource.getURL());
            } else {
                JavaClass[] classes = javaSource.getClasses();
                for (JavaClass javaClass : classes) {
                    ElementMapping element = loadElement(builder, javaClass);
                    if (element != null && !javaClass.isAbstract()) {
                        elements.add(element);
                    } else {
                        log.debug("No XML annotation found for type: " + javaClass.getFullyQualifiedName());
                    }
                }
            }
        }
        return elements;
    }

    private ElementMapping loadElement(JavaDocBuilder builder, JavaClass javaClass) {
        DocletTag xbeanTag = javaClass.getTagByName(XBEAN_ANNOTATION);
        if (xbeanTag == null) {
            return null;
        }

        String element = getElementName(javaClass, xbeanTag);
        String description = getProperty(xbeanTag, "description");
        if (description == null) {
            description = javaClass.getComment();

        }
        String namespace = getProperty(xbeanTag, "namespace", defaultNamespace);
        boolean root = getBooleanProperty(xbeanTag, "rootElement");
        String contentProperty = getProperty(xbeanTag, "contentProperty");
        String factoryClass = getProperty(xbeanTag, "factoryClass");

        Map<String, MapMapping> mapsByPropertyName = new HashMap<String, MapMapping>();
        List<String> flatProperties = new ArrayList<String>();
        Map<String, String> flatCollections = new HashMap<String, String>();
        Set<AttributeMapping> attributes = new HashSet<AttributeMapping>();
        Map<String, AttributeMapping> attributesByPropertyName = new HashMap<String, AttributeMapping>();

        for (JavaClass jClass = javaClass; jClass != null; jClass = jClass.getSuperJavaClass()) {
            BeanProperty[] beanProperties = jClass.getBeanProperties();
            for (BeanProperty beanProperty : beanProperties) {
                // we only care about properties with a setter
                if (beanProperty.getMutator() != null) {
                    AttributeMapping attributeMapping = loadAttribute(beanProperty, "");
                    if (attributeMapping != null) {
                        attributes.add(attributeMapping);
                        attributesByPropertyName.put(attributeMapping.getPropertyName(), attributeMapping);
                    }
                    JavaMethod acc = beanProperty.getAccessor();
                    if (acc != null) {
                        DocletTag mapTag = acc.getTagByName(MAP_ANNOTATION);
                        if (mapTag != null) {
                            MapMapping mm = new MapMapping(
                                    mapTag.getNamedParameter("entryName"),
                                    mapTag.getNamedParameter("keyName"),
                                    Boolean.valueOf(mapTag.getNamedParameter("flat")),
                                    mapTag.getNamedParameter("dups"),
                                    mapTag.getNamedParameter("defaultKey"));
                            mapsByPropertyName.put(beanProperty.getName(), mm);
                        }

                        DocletTag flatColTag = acc.getTagByName(FLAT_COLLECTION_ANNOTATION);
                        if (flatColTag != null) {
                            String childName = flatColTag.getNamedParameter("childElement");
                            if (childName == null)
                                throw new InvalidModelException("Flat collections must specify the childElement attribute.");
                            flatCollections.put(beanProperty.getName(), childName);
                        }

                        DocletTag flatPropTag = acc.getTagByName(FLAT_PROPERTY_ANNOTATION);
                        if (flatPropTag != null) {
                            flatProperties.add(beanProperty.getName());
                        }
                    }
                }
            }
        }

        String initMethod = null;
        String destroyMethod = null;
        String factoryMethod = null;
        for (JavaClass jClass = javaClass; jClass != null; jClass = jClass.getSuperJavaClass()) {
            JavaMethod[] methods = javaClass.getMethods();
            for (JavaMethod method : methods) {
                if (method.isPublic() && !method.isConstructor()) {
                    if (initMethod == null && method.getTagByName(INIT_METHOD_ANNOTATION) != null) {
                        initMethod = method.getName();
                    }
                    if (destroyMethod == null && method.getTagByName(DESTROY_METHOD_ANNOTATION) != null) {
                        destroyMethod = method.getName();
                    }
                    if (factoryMethod == null && method.getTagByName(FACTORY_METHOD_ANNOTATION) != null) {
                        factoryMethod = method.getName();
                    }

                }
            }
        }

        List<List<ParameterMapping>> constructorArgs = new ArrayList<List<ParameterMapping>>();
        JavaMethod[] methods = javaClass.getMethods();
        for (JavaMethod method : methods) {
            JavaParameter[] parameters = method.getParameters();
            if (isValidConstructor(factoryMethod, method, parameters)) {
                List<ParameterMapping> args = new ArrayList<ParameterMapping>(parameters.length);
                for (JavaParameter parameter : parameters) {
                    AttributeMapping attributeMapping = attributesByPropertyName.get(parameter.getName());
                    if (attributeMapping == null) {
                        attributeMapping = loadParameter(parameter);

                        attributes.add(attributeMapping);
                        attributesByPropertyName.put(attributeMapping.getPropertyName(), attributeMapping);
                    }
                    args.add(new ParameterMapping(attributeMapping.getPropertyName(), toMappingType(parameter.getType(), null)));
                }
                constructorArgs.add(Collections.unmodifiableList(args));
            }
        }

        HashSet<String> interfaces = new HashSet<String>();
        interfaces.addAll(getFullyQualifiedNames(javaClass.getImplementedInterfaces()));

        JavaClass actualClass = javaClass;
        if (factoryClass != null) {
            JavaClass clazz = builder.getClassByName(factoryClass);
            if (clazz != null) {
                log.info("Detected factory: using " + factoryClass + " instead of " + javaClass.getFullyQualifiedName());
                actualClass = clazz;
            } else {
                log.info("Could not load class built by factory: " + factoryClass);
            }
        }

        ArrayList<String> superClasses = new ArrayList<String>();
        JavaClass p = actualClass;
        if (actualClass != javaClass) {
            superClasses.add(actualClass.getFullyQualifiedName());
        }
        while (true) {
            JavaClass s = p.getSuperJavaClass();
            if (s == null || s.equals(p) || "java.lang.Object".equals(s.getFullyQualifiedName())) {
                break;
            }
            p = s;
            superClasses.add(p.getFullyQualifiedName());
            interfaces.addAll(getFullyQualifiedNames(p.getImplementedInterfaces()));
        }

        return new ElementMapping(namespace,
                element,
                javaClass.getFullyQualifiedName(),
                description,
                root,
                initMethod,
                destroyMethod,
                factoryMethod,
                contentProperty,
                attributes,
                constructorArgs,
                flatProperties,
                mapsByPropertyName,
                flatCollections,
                superClasses,
                interfaces);
    }

    private List<String> getFullyQualifiedNames(JavaClass[] implementedInterfaces) {
        ArrayList<String> l = new ArrayList<String>();
        for (JavaClass implementedInterface : implementedInterfaces) {
            l.add(implementedInterface.getFullyQualifiedName());
        }
        return l;
    }

    private String getElementName(JavaClass javaClass, DocletTag tag) {
        String elementName = getProperty(tag, "element");
        if (elementName == null) {
            String className = javaClass.getFullyQualifiedName();
            int index = className.lastIndexOf(".");
            if (index > 0) {
                className = className.substring(index + 1);
            }
            // strip off "Bean" from a spring factory bean
            if (className.endsWith("FactoryBean")) {
                className = className.substring(0, className.length() - 4);
            }
            elementName = Utils.decapitalise(className);
        }
        return elementName;
    }

    private AttributeMapping loadAttribute(BeanProperty beanProperty, String defaultDescription) {
        DocletTag propertyTag = getPropertyTag(beanProperty);

        if (getBooleanProperty(propertyTag, "hidden")) {
            return null;
        }

        String attribute = getProperty(propertyTag, "alias", beanProperty.getName());
        String attributeDescription = getAttributeDescription(beanProperty, propertyTag, defaultDescription);
        String defaultValue = getProperty(propertyTag, "default");
        boolean fixed = getBooleanProperty(propertyTag, "fixed");
        boolean required = getBooleanProperty(propertyTag, "required");
        String nestedType = getProperty(propertyTag, "nestedType");
        String propertyEditor = getProperty(propertyTag, "propertyEditor");

        return new AttributeMapping(attribute,
                beanProperty.getName(),
                attributeDescription,
                toMappingType(beanProperty.getType(), nestedType),
                defaultValue,
                fixed,
                required,
                propertyEditor);
    }

    private static DocletTag getPropertyTag(BeanProperty beanProperty) {
        JavaMethod accessor = beanProperty.getAccessor();
        if (accessor != null) {
            DocletTag propertyTag = accessor.getTagByName(PROPERTY_ANNOTATION);
            if (propertyTag != null) {
                return propertyTag;
            }
        }
        JavaMethod mutator = beanProperty.getMutator();
        if (mutator != null) {
            DocletTag propertyTag = mutator.getTagByName(PROPERTY_ANNOTATION);
            if (propertyTag != null) {
                return propertyTag;
            }
        }
        return null;
    }

    private String getAttributeDescription(BeanProperty beanProperty, DocletTag propertyTag, String defaultDescription) {
        String description = getProperty(propertyTag, "description");
        if (description != null && description.trim().length() > 0) {
            return description.trim();
        }

        JavaMethod accessor = beanProperty.getAccessor();
        if (accessor != null) {
            description = accessor.getComment();
            if (description != null && description.trim().length() > 0) {
                return description.trim();
            }
        }

        JavaMethod mutator = beanProperty.getMutator();
        if (mutator != null) {
            description = mutator.getComment();
            if (description != null && description.trim().length() > 0) {
                return description.trim();
            }
        }
        return defaultDescription;
    }

    private AttributeMapping loadParameter(JavaParameter parameter) {
        String parameterName = parameter.getName();
        String parameterDescription = getParameterDescription(parameter);

        // first attempt to load the attribute from the java beans accessor methods
        JavaClass javaClass = parameter.getParentMethod().getParentClass();
        BeanProperty beanProperty = javaClass.getBeanProperty(parameterName);
        if (beanProperty != null) {
            AttributeMapping attributeMapping = loadAttribute(beanProperty, parameterDescription);
            // if the attribute mapping is null, the property was tagged as hidden and this is an error
            if (attributeMapping == null) {
                throw new InvalidModelException("Hidden property usage: " +
                        "The construction method " + toMethodLocator(parameter.getParentMethod()) +
                        " can not use a hidded property " + parameterName);
            }
            return attributeMapping;
        }

        // create an attribute solely based on the parameter information
        return new AttributeMapping(parameterName,
                parameterName,
                parameterDescription,
                toMappingType(parameter.getType(), null),
                null,
                false,
                false,
                null);
    }

    private String getParameterDescription(JavaParameter parameter) {
        String parameterName = parameter.getName();
        DocletTag[] tags = parameter.getParentMethod().getTagsByName("param");
        for (DocletTag tag : tags) {
            if (tag.getParameters()[0].equals(parameterName)) {
                String parameterDescription = tag.getValue().trim();
                if (parameterDescription.startsWith(parameterName)) {
                    parameterDescription = parameterDescription.substring(parameterName.length()).trim();
                }
                return parameterDescription;
            }
        }
        return null;
    }

    private boolean isValidConstructor(String factoryMethod, JavaMethod method, JavaParameter[] parameters) {
        if (!method.isPublic() || parameters.length == 0) {
            return false;
        }

        if (factoryMethod == null) {
            return method.isConstructor();
        } else {
            return method.getName().equals(factoryMethod);
        }
    }

    private static String getProperty(DocletTag propertyTag, String propertyName) {
        return getProperty(propertyTag, propertyName, null);
    }

    private static String getProperty(DocletTag propertyTag, String propertyName, String defaultValue) {
        String value = null;
        if (propertyTag != null) {
            value = propertyTag.getNamedParameter(propertyName);
        }
        if (value == null) {
            return defaultValue;
        }
        return value;
    }

    private boolean getBooleanProperty(DocletTag propertyTag, String propertyName) {
        return toBoolean(getProperty(propertyTag, propertyName));
    }

    private static boolean toBoolean(String value) {
        if (value != null) {
            return Boolean.valueOf(value);
        }
        return false;
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



xbean-spring/src/main/java/org/apache/xbean/spring/generator/QdoxMappingLoader.java [50:477]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
public class QdoxMappingLoader implements MappingLoader {
    public static final String XBEAN_ANNOTATION = "org.apache.xbean.XBean";
    public static final String PROPERTY_ANNOTATION = "org.apache.xbean.Property";
    public static final String INIT_METHOD_ANNOTATION = "org.apache.xbean.InitMethod";
    public static final String DESTROY_METHOD_ANNOTATION = "org.apache.xbean.DestroyMethod";
    public static final String FACTORY_METHOD_ANNOTATION = "org.apache.xbean.FactoryMethod";
    public static final String MAP_ANNOTATION = "org.apache.xbean.Map";
    public static final String FLAT_PROPERTY_ANNOTATION = "org.apache.xbean.Flat";
    public static final String FLAT_COLLECTION_ANNOTATION = "org.apache.xbean.FlatCollection";
    public static final String ELEMENT_ANNOTATION = "org.apache.xbean.Element";

    private static final Log log = LogFactory.getLog(QdoxMappingLoader.class);
    private final String defaultNamespace;
    private final File[] srcDirs;
    private final String[] excludedClasses;
    private Type collectionType;

    public QdoxMappingLoader(String defaultNamespace, File[] srcDirs, String[] excludedClasses) {
        this.defaultNamespace = defaultNamespace;
        this.srcDirs = srcDirs;
        this.excludedClasses = excludedClasses;
    }

    public String getDefaultNamespace() {
        return defaultNamespace;
    }

    public File[] getSrcDirs() {
        return srcDirs;
    }

    public Set<NamespaceMapping> loadNamespaces() throws IOException {
        JavaDocBuilder builder = new JavaDocBuilder();

        log.debug("Source directories: ");

        for (File sourceDirectory : srcDirs) {
            if (!sourceDirectory.isDirectory() && !sourceDirectory.toString().endsWith(".jar")) {
                log.warn("Specified source directory isn't a directory or a jar file: '" + sourceDirectory.getAbsolutePath() + "'.");
            }
            log.debug(" - " + sourceDirectory.getAbsolutePath());

            getSourceFiles(sourceDirectory, excludedClasses, builder);
        }

        collectionType = builder.getClassByName("java.util.Collection").asType();
        return loadNamespaces(builder);
    }

    private Set<NamespaceMapping> loadNamespaces(JavaDocBuilder builder) {
        // load all of the elements
        List<ElementMapping> elements = loadElements(builder);

        // index the elements by namespace and find the root element of each namespace
        Map<String, Set<ElementMapping>> elementsByNamespace = new HashMap<String, Set<ElementMapping>>();
        Map<String, ElementMapping> namespaceRoots = new HashMap<String, ElementMapping>();
        for (ElementMapping element : elements) {
            String namespace = element.getNamespace();
            Set<ElementMapping> namespaceElements = elementsByNamespace.get(namespace);
            if (namespaceElements == null) {
                namespaceElements = new HashSet<ElementMapping>();
                elementsByNamespace.put(namespace, namespaceElements);
            }
            namespaceElements.add(element);
            if (element.isRootElement()) {
                if (namespaceRoots.containsKey(namespace)) {
                    log.info("Multiple root elements found for namespace " + namespace);
                }
                namespaceRoots.put(namespace, element);
            }
        }

        // build the NamespaceMapping objects
        Set<NamespaceMapping> namespaces = new TreeSet<NamespaceMapping>();
        for (Map.Entry<String, Set<ElementMapping>> entry : elementsByNamespace.entrySet()) {
            String namespace = entry.getKey();
            Set namespaceElements = entry.getValue();
            ElementMapping rootElement = namespaceRoots.get(namespace);
            NamespaceMapping namespaceMapping = new NamespaceMapping(namespace, namespaceElements, rootElement);
            namespaces.add(namespaceMapping);
        }
        return Collections.unmodifiableSet(namespaces);
    }

    private List<ElementMapping> loadElements(JavaDocBuilder builder) {
        JavaSource[] javaSources = builder.getSources();
        List<ElementMapping> elements = new ArrayList<ElementMapping>();
        for (JavaSource javaSource : javaSources) {
            if (javaSource.getClasses().length == 0) {
                log.info("No Java Classes defined in: " + javaSource.getURL());
            } else {
                JavaClass[] classes = javaSource.getClasses();
                for (JavaClass javaClass : classes) {
                    ElementMapping element = loadElement(builder, javaClass);
                    if (element != null && !javaClass.isAbstract()) {
                        elements.add(element);
                    } else {
                        log.debug("No XML annotation found for type: " + javaClass.getFullyQualifiedName());
                    }
                }
            }
        }
        return elements;
    }

    private ElementMapping loadElement(JavaDocBuilder builder, JavaClass javaClass) {
        DocletTag xbeanTag = javaClass.getTagByName(XBEAN_ANNOTATION);
        if (xbeanTag == null) {
            return null;
        }

        String element = getElementName(javaClass, xbeanTag);
        String description = getProperty(xbeanTag, "description");
        if (description == null) {
            description = javaClass.getComment();

        }
        String namespace = getProperty(xbeanTag, "namespace", defaultNamespace);
        boolean root = getBooleanProperty(xbeanTag, "rootElement");
        String contentProperty = getProperty(xbeanTag, "contentProperty");
        String factoryClass = getProperty(xbeanTag, "factoryClass");

        Map<String, MapMapping> mapsByPropertyName = new HashMap<String, MapMapping>();
        List<String> flatProperties = new ArrayList<String>();
        Map<String, String> flatCollections = new HashMap<String, String>();
        Set<AttributeMapping> attributes = new HashSet<AttributeMapping>();
        Map<String, AttributeMapping> attributesByPropertyName = new HashMap<String, AttributeMapping>();

        for (JavaClass jClass = javaClass; jClass != null; jClass = jClass.getSuperJavaClass()) {
            BeanProperty[] beanProperties = jClass.getBeanProperties();
            for (BeanProperty beanProperty : beanProperties) {
                // we only care about properties with a setter
                if (beanProperty.getMutator() != null) {
                    AttributeMapping attributeMapping = loadAttribute(beanProperty, "");
                    if (attributeMapping != null) {
                        attributes.add(attributeMapping);
                        attributesByPropertyName.put(attributeMapping.getPropertyName(), attributeMapping);
                    }
                    JavaMethod acc = beanProperty.getAccessor();
                    if (acc != null) {
                        DocletTag mapTag = acc.getTagByName(MAP_ANNOTATION);
                        if (mapTag != null) {
                            MapMapping mm = new MapMapping(
                                    mapTag.getNamedParameter("entryName"),
                                    mapTag.getNamedParameter("keyName"),
                                    Boolean.valueOf(mapTag.getNamedParameter("flat")),
                                    mapTag.getNamedParameter("dups"),
                                    mapTag.getNamedParameter("defaultKey"));
                            mapsByPropertyName.put(beanProperty.getName(), mm);
                        }

                        DocletTag flatColTag = acc.getTagByName(FLAT_COLLECTION_ANNOTATION);
                        if (flatColTag != null) {
                            String childName = flatColTag.getNamedParameter("childElement");
                            if (childName == null)
                                throw new InvalidModelException("Flat collections must specify the childElement attribute.");
                            flatCollections.put(beanProperty.getName(), childName);
                        }

                        DocletTag flatPropTag = acc.getTagByName(FLAT_PROPERTY_ANNOTATION);
                        if (flatPropTag != null) {
                            flatProperties.add(beanProperty.getName());
                        }
                    }
                }
            }
        }

        String initMethod = null;
        String destroyMethod = null;
        String factoryMethod = null;
        for (JavaClass jClass = javaClass; jClass != null; jClass = jClass.getSuperJavaClass()) {
            JavaMethod[] methods = javaClass.getMethods();
            for (JavaMethod method : methods) {
                if (method.isPublic() && !method.isConstructor()) {
                    if (initMethod == null && method.getTagByName(INIT_METHOD_ANNOTATION) != null) {
                        initMethod = method.getName();
                    }
                    if (destroyMethod == null && method.getTagByName(DESTROY_METHOD_ANNOTATION) != null) {
                        destroyMethod = method.getName();
                    }
                    if (factoryMethod == null && method.getTagByName(FACTORY_METHOD_ANNOTATION) != null) {
                        factoryMethod = method.getName();
                    }

                }
            }
        }

        List<List<ParameterMapping>> constructorArgs = new ArrayList<List<ParameterMapping>>();
        JavaMethod[] methods = javaClass.getMethods();
        for (JavaMethod method : methods) {
            JavaParameter[] parameters = method.getParameters();
            if (isValidConstructor(factoryMethod, method, parameters)) {
                List<ParameterMapping> args = new ArrayList<ParameterMapping>(parameters.length);
                for (JavaParameter parameter : parameters) {
                    AttributeMapping attributeMapping = attributesByPropertyName.get(parameter.getName());
                    if (attributeMapping == null) {
                        attributeMapping = loadParameter(parameter);

                        attributes.add(attributeMapping);
                        attributesByPropertyName.put(attributeMapping.getPropertyName(), attributeMapping);
                    }
                    args.add(new ParameterMapping(attributeMapping.getPropertyName(), toMappingType(parameter.getType(), null)));
                }
                constructorArgs.add(Collections.unmodifiableList(args));
            }
        }

        HashSet<String> interfaces = new HashSet<String>();
        interfaces.addAll(getFullyQualifiedNames(javaClass.getImplementedInterfaces()));

        JavaClass actualClass = javaClass;
        if (factoryClass != null) {
            JavaClass clazz = builder.getClassByName(factoryClass);
            if (clazz != null) {
                log.info("Detected factory: using " + factoryClass + " instead of " + javaClass.getFullyQualifiedName());
                actualClass = clazz;
            } else {
                log.info("Could not load class built by factory: " + factoryClass);
            }
        }

        ArrayList<String> superClasses = new ArrayList<String>();
        JavaClass p = actualClass;
        if (actualClass != javaClass) {
            superClasses.add(actualClass.getFullyQualifiedName());
        }
        while (true) {
            JavaClass s = p.getSuperJavaClass();
            if (s == null || s.equals(p) || "java.lang.Object".equals(s.getFullyQualifiedName())) {
                break;
            }
            p = s;
            superClasses.add(p.getFullyQualifiedName());
            interfaces.addAll(getFullyQualifiedNames(p.getImplementedInterfaces()));
        }

        return new ElementMapping(namespace,
                element,
                javaClass.getFullyQualifiedName(),
                description,
                root,
                initMethod,
                destroyMethod,
                factoryMethod,
                contentProperty,
                attributes,
                constructorArgs,
                flatProperties,
                mapsByPropertyName,
                flatCollections,
                superClasses,
                interfaces);
    }

    private List<String> getFullyQualifiedNames(JavaClass[] implementedInterfaces) {
        ArrayList<String> l = new ArrayList<String>();
        for (JavaClass implementedInterface : implementedInterfaces) {
            l.add(implementedInterface.getFullyQualifiedName());
        }
        return l;
    }

    private String getElementName(JavaClass javaClass, DocletTag tag) {
        String elementName = getProperty(tag, "element");
        if (elementName == null) {
            String className = javaClass.getFullyQualifiedName();
            int index = className.lastIndexOf(".");
            if (index > 0) {
                className = className.substring(index + 1);
            }
            // strip off "Bean" from a spring factory bean
            if (className.endsWith("FactoryBean")) {
                className = className.substring(0, className.length() - 4);
            }
            elementName = Utils.decapitalise(className);
        }
        return elementName;
    }

    private AttributeMapping loadAttribute(BeanProperty beanProperty, String defaultDescription) {
        DocletTag propertyTag = getPropertyTag(beanProperty);

        if (getBooleanProperty(propertyTag, "hidden")) {
            return null;
        }

        String attribute = getProperty(propertyTag, "alias", beanProperty.getName());
        String attributeDescription = getAttributeDescription(beanProperty, propertyTag, defaultDescription);
        String defaultValue = getProperty(propertyTag, "default");
        boolean fixed = getBooleanProperty(propertyTag, "fixed");
        boolean required = getBooleanProperty(propertyTag, "required");
        String nestedType = getProperty(propertyTag, "nestedType");
        String propertyEditor = getProperty(propertyTag, "propertyEditor");

        return new AttributeMapping(attribute,
                beanProperty.getName(),
                attributeDescription,
                toMappingType(beanProperty.getType(), nestedType),
                defaultValue,
                fixed,
                required,
                propertyEditor);
    }

    private static DocletTag getPropertyTag(BeanProperty beanProperty) {
        JavaMethod accessor = beanProperty.getAccessor();
        if (accessor != null) {
            DocletTag propertyTag = accessor.getTagByName(PROPERTY_ANNOTATION);
            if (propertyTag != null) {
                return propertyTag;
            }
        }
        JavaMethod mutator = beanProperty.getMutator();
        if (mutator != null) {
            DocletTag propertyTag = mutator.getTagByName(PROPERTY_ANNOTATION);
            if (propertyTag != null) {
                return propertyTag;
            }
        }
        return null;
    }

    private String getAttributeDescription(BeanProperty beanProperty, DocletTag propertyTag, String defaultDescription) {
        String description = getProperty(propertyTag, "description");
        if (description != null && description.trim().length() > 0) {
            return description.trim();
        }

        JavaMethod accessor = beanProperty.getAccessor();
        if (accessor != null) {
            description = accessor.getComment();
            if (description != null && description.trim().length() > 0) {
                return description.trim();
            }
        }

        JavaMethod mutator = beanProperty.getMutator();
        if (mutator != null) {
            description = mutator.getComment();
            if (description != null && description.trim().length() > 0) {
                return description.trim();
            }
        }
        return defaultDescription;
    }

    private AttributeMapping loadParameter(JavaParameter parameter) {
        String parameterName = parameter.getName();
        String parameterDescription = getParameterDescription(parameter);

        // first attempt to load the attribute from the java beans accessor methods
        JavaClass javaClass = parameter.getParentMethod().getParentClass();
        BeanProperty beanProperty = javaClass.getBeanProperty(parameterName);
        if (beanProperty != null) {
            AttributeMapping attributeMapping = loadAttribute(beanProperty, parameterDescription);
            // if the attribute mapping is null, the property was tagged as hidden and this is an error
            if (attributeMapping == null) {
                throw new InvalidModelException("Hidden property usage: " +
                        "The construction method " + toMethodLocator(parameter.getParentMethod()) +
                        " can not use a hidded property " + parameterName);
            }
            return attributeMapping;
        }

        // create an attribute solely based on the parameter information
        return new AttributeMapping(parameterName,
                parameterName,
                parameterDescription,
                toMappingType(parameter.getType(), null),
                null,
                false,
                false,
                null);
    }

    private String getParameterDescription(JavaParameter parameter) {
        String parameterName = parameter.getName();
        DocletTag[] tags = parameter.getParentMethod().getTagsByName("param");
        for (DocletTag tag : tags) {
            if (tag.getParameters()[0].equals(parameterName)) {
                String parameterDescription = tag.getValue().trim();
                if (parameterDescription.startsWith(parameterName)) {
                    parameterDescription = parameterDescription.substring(parameterName.length()).trim();
                }
                return parameterDescription;
            }
        }
        return null;
    }

    private boolean isValidConstructor(String factoryMethod, JavaMethod method, JavaParameter[] parameters) {
        if (!method.isPublic() || parameters.length == 0) {
            return false;
        }

        if (factoryMethod == null) {
            return method.isConstructor();
        } else {
            return method.getName().equals(factoryMethod);
        }
    }

    private static String getProperty(DocletTag propertyTag, String propertyName) {
        return getProperty(propertyTag, propertyName, null);
    }

    private static String getProperty(DocletTag propertyTag, String propertyName, String defaultValue) {
        String value = null;
        if (propertyTag != null) {
            value = propertyTag.getNamedParameter(propertyName);
        }
        if (value == null) {
            return defaultValue;
        }
        return value;
    }

    private boolean getBooleanProperty(DocletTag propertyTag, String propertyName) {
        return toBoolean(getProperty(propertyTag, propertyName));
    }

    private static boolean toBoolean(String value) {
        if (value != null) {
            return Boolean.valueOf(value);
        }
        return false;
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



