Package com.asakusafw.compiler.operator.flow

Source Code of com.asakusafw.compiler.operator.flow.FlowClassEmitter

/**
* Copyright 2011-2014 Asakusa Framework Team.
*
* Licensed 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 com.asakusafw.compiler.operator.flow;

import java.io.IOException;
import java.text.MessageFormat;
import java.util.Collections;
import java.util.List;

import javax.annotation.processing.FilerException;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.asakusafw.compiler.common.Precondition;
import com.asakusafw.compiler.operator.OperatorCompilingEnvironment;
import com.asakusafw.utils.java.jsr269.bridge.Jsr269;
import com.asakusafw.utils.java.model.syntax.Comment;
import com.asakusafw.utils.java.model.syntax.CompilationUnit;
import com.asakusafw.utils.java.model.syntax.ImportDeclaration;
import com.asakusafw.utils.java.model.syntax.ModelFactory;
import com.asakusafw.utils.java.model.syntax.PackageDeclaration;
import com.asakusafw.utils.java.model.syntax.TypeDeclaration;
import com.asakusafw.utils.java.model.util.ImportBuilder;
import com.asakusafw.utils.java.model.util.ImportBuilder.Strategy;

/**
* {@link FlowPartClass}をファイルに出力する。
* @since 0.1.0
* @version 0.7.0
*/
public class FlowClassEmitter {

    static final Logger LOG = LoggerFactory.getLogger(FlowClassEmitter.class);

    private static final String KEY_SUFFIX_FACTORY = "FACTORY";

    private final OperatorCompilingEnvironment environment;

    /**
     * インスタンスを生成する。
     * @param environment 環境オブジェクト
     * @throws IllegalArgumentException 引数に{@code null}が含まれる場合
     */
    public FlowClassEmitter(OperatorCompilingEnvironment environment) {
        Precondition.checkMustNotBeNull(environment, "environment"); //$NON-NLS-1$
        this.environment = environment;
    }

    /**
     * 指定のフロー部品クラスから演算子ファクトリークラスを生成して出力する。
     * @param aClass フロー部品クラス
     * @throws IllegalArgumentException 引数に{@code null}が含まれる場合
     */
    public void emit(FlowPartClass aClass) {
        assert aClass != null;
        String key = getResourceKey(aClass, KEY_SUFFIX_FACTORY);
        if (key != null && environment.isResourceGenerated(key)) {
            return;
        }
        ModelFactory f = environment.getFactory();
        PackageDeclaration packageDecl = getPackage(f, aClass);
        ImportBuilder imports = getImportBuilder(f, packageDecl);
        FlowFactoryClassGenerator generator = new FlowFactoryClassGenerator(
                environment,
                f,
                imports,
                aClass);
        TypeDeclaration type = generator.generate();
        List<ImportDeclaration> decls = imports.toImportDeclarations();

        try {
            emit(key, f, packageDecl, decls, type, aClass.getElement());
        } catch (IOException e) {
            LOG.debug(e.getMessage(), e);
            environment.getMessager().printMessage(Diagnostic.Kind.ERROR,
                    MessageFormat.format(
                            "{0}に対する演算子ファクトリークラスの作成に失敗しました ({1})",
                            aClass.getElement().getQualifiedName().toString(),
                            e.getMessage()));
        }
    }

    private void emit(
            String key,
            ModelFactory factory,
            PackageDeclaration packageDecl,
            List<ImportDeclaration> importDecls,
            TypeDeclaration typeDecl,
            TypeElement originating) throws IOException {

        CompilationUnit unit = factory.newCompilationUnit(
                packageDecl,
                importDecls,
                Collections.singletonList(typeDecl),
                Collections.<Comment>emptyList());

        try {
            environment.emit(unit, originating);
            if (key != null) {
                environment.setResourceGenerated(key);
            }
        } catch (FilerException e) {
            LOG.debug(MessageFormat.format(
                    "{0} has been already created in this session",
                    typeDecl.getName().toNameString()), e);
        }
    }

    private String getResourceKey(FlowPartClass aClass, String suffix) {
        assert aClass != null;
        return String.format("%s::%s", aClass.getElement().getQualifiedName(), suffix);
    }

    private PackageDeclaration getPackage(
            ModelFactory factory,
            FlowPartClass aClass) {
        PackageElement parent = (PackageElement) aClass.getElement().getEnclosingElement();
        return new Jsr269(factory).convert(parent);
    }

    private ImportBuilder getImportBuilder(
            ModelFactory factory,
            PackageDeclaration packageDecl) {
        assert factory != null;
        assert packageDecl != null;
        return new ImportBuilder(factory, packageDecl, Strategy.TOP_LEVEL);
    }
}
TOP

Related Classes of com.asakusafw.compiler.operator.flow.FlowClassEmitter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.