Example usage for java.util LinkedList descendingIterator

List of usage examples for java.util LinkedList descendingIterator

Introduction

In this page you can find the example usage for java.util LinkedList descendingIterator.

Prototype

public Iterator<E> descendingIterator() 

Source Link

Usage

From source file:org.staticioc.SpringStaticFactoryGenerator.java

/**
 * Load one or multiple context configuration file and generate a BeanFactory class that instanciates the declared Beans
 * /*from ww w . j av a  2  s  .  c  o  m*/
 * @param codeGenerator CodeGenerator object to use to generate the target BeanFactory source code
 * @param generatedPackageName the name of the target package (if any) to use for the BeanFactory 
 * @param generatedClassName the name of the target generated class (and file) name for the BeanFactory
 * @param configurationFiles List of Configuration files (Spring contexts) to load
 * @param namespacePlugins List<NamespaceParser> of extra namespace support plugins to use in addition to default's Spring Bean and Spring p
 * @return The BeanFactory content as StringBuilder object
 * @throws SAXException
 * @throws IOException
 * @throws ParserConfigurationException
 */
protected StringBuilder generate(final CodeGenerator codeGenerator, final String generatedPackageName,
        final String generatedClassName, final List<String> configurationFiles, List<NamespaceParser> plugins)
        throws SAXException, IOException, ParserConfigurationException {
    final StringBuilder res = new StringBuilder(INIT_BUFFER_SIZE);
    codeGenerator.setOutput(res);

    SpringConfigParser springConfigParser = new SpringConfigParser();
    springConfigParser.addNamespaceParsers(plugins);

    Map<String, Bean> beanClassMap = springConfigParser.load(configurationFiles);
    final LinkedList<Bean> orderedBean = springConfigParser.getBeanContainer().getOrderedBeans();

    // Track beans with a declared init-method and destroy-method attribute
    List<Bean> initRequiredBeans = new LinkedList<Bean>();
    List<Bean> destroyRequiredBeans = new LinkedList<Bean>();

    codeGenerator.comment(Level.HEADER, COMMENT_HEADER);

    codeGenerator.initPackage(generatedPackageName);
    codeGenerator.initClass(generatedClassName);

    // declare beans
    codeGenerator.comment(Level.CLASS, "Bean definition");
    for (final Bean bean : orderedBean) {
        // Ignore abstract beans and prototypes
        if (isHidden(bean)) {
            continue;
        }

        if (bean.getInitMethod() != null) {
            initRequiredBeans.add(bean);
        }

        if (bean.getDestroyMethod() != null) {
            destroyRequiredBeans.add(bean);
        }

        codeGenerator.declareBean(bean);
    }

    res.append("\n");
    codeGenerator.comment(Level.CLASS, "Constructor of the Factory");
    codeGenerator.initConstructor(generatedClassName);

    // Instantiate beans
    codeGenerator.comment(Level.METHOD, "Instanciating beans");

    for (final Bean bean : orderedBean) {
        // Ignore abstract beans and prototypes
        if (isHidden(bean)) {
            continue;
        }

        if (bean.getFactoryBean() != null) {
            codeGenerator.instantiateBeanWithFactory(bean);
        } else {
            codeGenerator.instantiateBean(bean);
        }
    }

    res.append("\n");

    // Set properties
    for (final Bean bean : orderedBean) {
        // Ignore abstract beans and beans with no properties
        if (isHidden(bean) || bean.getProperties().isEmpty()) {
            continue;
        }

        codeGenerator.comment(Level.METHOD, "Setting up bean " + bean.getId());
        for (Property prop : bean.getProperties()) {
            // Check that the reference exists and is not abstract
            if (prop.getRef() != null) {
                final Bean refBean = beanClassMap.get(prop.getRef());

                if (refBean == null) {
                    logger.warn("Reference {} does not exist for Bean {}", prop.getRef(), bean.getId());

                    if (isIgnoreUnresolvedRefs()) {
                        continue;
                    }

                } else if (refBean.isAbstract()) {
                    logger.warn("Reference {} is abstract for Bean {}", prop.getRef(), bean.getId());
                    continue;
                }
            }

            codeGenerator.declareProperty(bean, prop);
        }
        res.append("\n");
    }

    // Call init-methods
    if (!initRequiredBeans.isEmpty()) {
        codeGenerator.comment(Level.METHOD, "Init methods calls");
    }

    for (Bean bean : initRequiredBeans) {
        res.append("\t\t");
        codeGenerator.invokeMethod(bean, bean.getInitMethod(), EMPTY_STRING_ARRAY);
    }

    codeGenerator.closeConstructor(generatedClassName);
    res.append("\n");
    res.append("\n");

    codeGenerator.initDestructor(generatedClassName);

    // Call destroy-methods
    for (Bean bean : destroyRequiredBeans) {
        res.append("\t\t");
        codeGenerator.invokeMethod(bean, bean.getDestroyMethod(), EMPTY_STRING_ARRAY);
    }

    //Destructor : free beans in their reverse order of creation
    Iterator<Bean> itr = orderedBean.descendingIterator();

    while (itr.hasNext()) {
        final Bean bean = itr.next();

        // Ignore abstract beans and prototypes that were not instanciated
        if (isHidden(bean)) {
            continue;
        }
        codeGenerator.deleteBean(bean);
    }

    codeGenerator.closeDestructor(generatedClassName);

    codeGenerator.closeClass(generatedClassName);
    codeGenerator.closePackage(generatedPackageName);

    return res;
}