Android Open Source - storm-gen Main Processor






From Project

Back to project page storm-gen.

License

The source code is released under:

Apache License

If you think the Android project storm-gen listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*******************************************************************************
 * Copyright 2012 Google, Inc./* w w  w.jav  a 2  s.  c o  m*/
 *
 * 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.turbomanage.storm.apt;

import java.io.Writer;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.tools.JavaFileObject;

import com.turbomanage.storm.api.Converter;
import com.turbomanage.storm.api.Database;
import com.turbomanage.storm.api.Entity;
import com.turbomanage.storm.apt.converter.ConverterProcessor;
import com.turbomanage.storm.apt.database.DatabaseFactoryTemplate;
import com.turbomanage.storm.apt.database.DatabaseModel;
import com.turbomanage.storm.apt.database.DatabaseProcessor;
import com.turbomanage.storm.apt.entity.EntityDaoTemplate;
import com.turbomanage.storm.apt.entity.EntityProcessor;
import com.turbomanage.storm.apt.entity.TableHelperTemplate;

import freemarker.cache.ClassTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;

@SupportedAnnotationTypes({ "com.turbomanage.storm.api.Database","com.turbomanage.storm.api.Entity","javax.persistence.Entity","com.turbomanage.storm.api.Converter" })
@SupportedSourceVersion(SourceVersion.RELEASE_6)
public class MainProcessor extends AbstractProcessor {
  private ProcessorLogger logger;
  private Configuration cfg = new Configuration();
  private static String ERR_MSG = "Error processing stORM annotation";

  @Override
  public boolean process(Set<? extends TypeElement> annotations,
      RoundEnvironment roundEnv) {

    cfg.setTemplateLoader(new ClassTemplateLoader(this.getClass(), "/res"));
    this.logger = new ProcessorLogger(processingEnv.getMessager());
    logger.info("Running MainProcessor...");

    // Exit early if no annotations in this round so we don't overwrite the
    // env file
    if (annotations.size() < 1) {
      return true;
    }

    for (TypeElement annotationType : annotations) {
      logger.info("Processing elements with @" + annotationType.getQualifiedName());
    }

    StormEnvironment stormEnv = new StormEnvironment(logger);
    // Read in previously processed classes to support incremental compilation
    stormEnv.readIndex(processingEnv.getFiler());

    for (Element element : roundEnv.getElementsAnnotatedWith(Converter.class)) {
      try {
        ConverterProcessor cproc = new ConverterProcessor(element, stormEnv);
        cproc.populateModel();
      } catch (StormAnnotationException e) {
        logger.error(e.getMessage(), e.getElement());
        return true;
      } catch (Exception e) {
        logger.error(ERR_MSG, e, element);
        return true;
      }
    }

    // First pass on @Database annotations to get all db names
    for (Element element : roundEnv.getElementsAnnotatedWith(Database.class)) {
      try {
        DatabaseProcessor dbProc = new DatabaseProcessor(element, stormEnv);
        dbProc.populateModel();
        stormEnv.addDatabase(dbProc.getModel());
      } catch (StormAnnotationException e) {
        logger.error(e.getMessage(), e.getElement());
        return true;
      } catch (Exception e) {
        logger.error(ERR_MSG, e, element);
        return true;
      }
    }
    Set<TypeElement> entities = new HashSet<TypeElement>();
    try {
      entities.addAll((Collection<? extends TypeElement>) roundEnv.getElementsAnnotatedWith(Entity.class));
      entities.addAll((Collection<? extends TypeElement>) roundEnv.getElementsAnnotatedWith(javax.persistence.Entity.class));
    } catch (Exception e) {
      // swallow: apt bug in RoundEnvImpl if there are no annotations present
    }
    for (Element element : entities) {
      try {
        ClassProcessor eproc = new EntityProcessor(element, stormEnv);
        eproc.populateModel();
        // Generate EntityDao
        EntityDaoTemplate daoTemplate = new EntityDaoTemplate(eproc.getModel());
        processTemplate(processingEnv, cfg, daoTemplate);
        // Generate EntityTable
        TableHelperTemplate tableHelperTemplate = new TableHelperTemplate(eproc.getModel());
        processTemplate(processingEnv, cfg, tableHelperTemplate);
      } catch (StormAnnotationException e) {
        logger.error(e.getMessage(), e.getElement());
        return true;
      } catch (Exception e) {
        logger.error(ERR_MSG, e, element);
        return true;
      }
    }
    
    // Second pass to generate DatabaseFactory templates now that
    // all entities have been associated with a db
    for (DatabaseModel dbModel : stormEnv.getDbModels()) {
      try {
        DatabaseFactoryTemplate dbFactoryTemplate = new DatabaseFactoryTemplate(dbModel);
        processTemplate(processingEnv, cfg, dbFactoryTemplate);
      } catch (Exception e) {
        logger.error(ERR_MSG, e);
        return true;
      } 
    }

    // Write all processed dbs to index to support incremental compilation
    stormEnv.writeIndex(processingEnv.getFiler());

    return true;
  }

  void processTemplate(ProcessingEnvironment processingEnv, Configuration cfg, ClassTemplate template) {
    JavaFileObject file;
    try {
      file = processingEnv.getFiler().createSourceFile(
          template.getGeneratedClass());
      Writer out = file.openWriter();
      Template t = cfg.getTemplate(template.getTemplatePath());
      logger.info("Generating " + file.getName() + " with " + t.getName());
      t.process(template.getModel(), out);
      out.flush();
      out.close();
    } catch (Exception e) {
      logger.error("Template error", e);
    }
  }

}




Java Source Code List

com.turbomanage.storm.DatabaseHelper.java
com.turbomanage.storm.ModelBase.java
com.turbomanage.storm.SQLiteDao.java
com.turbomanage.storm.TableHelper.java
com.turbomanage.storm.api.Converter.java
com.turbomanage.storm.api.DatabaseFactory.java
com.turbomanage.storm.api.Database.java
com.turbomanage.storm.api.Entity.java
com.turbomanage.storm.api.Id.java
com.turbomanage.storm.api.Syncable.java
com.turbomanage.storm.api.package-info.java
com.turbomanage.storm.apt.BaseDaoModel.java
com.turbomanage.storm.apt.ClassModel.java
com.turbomanage.storm.apt.ClassProcessor.java
com.turbomanage.storm.apt.ClassTemplate.java
com.turbomanage.storm.apt.MainProcessor.java
com.turbomanage.storm.apt.ProcessorLogger.java
com.turbomanage.storm.apt.SqlUtil.java
com.turbomanage.storm.apt.StormAnnotationException.java
com.turbomanage.storm.apt.StormEnvironment.java
com.turbomanage.storm.apt.converter.ConverterModel.java
com.turbomanage.storm.apt.converter.ConverterProcessor.java
com.turbomanage.storm.apt.converter.ConverterTypeAnnotationValueVisitor.java
com.turbomanage.storm.apt.converter.ConverterTypeAnnotationValuesVisitor.java
com.turbomanage.storm.apt.converter.package-info.java
com.turbomanage.storm.apt.database.DatabaseFactoryTemplate.java
com.turbomanage.storm.apt.database.DatabaseModel.java
com.turbomanage.storm.apt.database.DatabaseProcessor.java
com.turbomanage.storm.apt.database.package-info.java
com.turbomanage.storm.apt.entity.EntityDaoTemplate.java
com.turbomanage.storm.apt.entity.EntityModel.java
com.turbomanage.storm.apt.entity.EntityProcessor.java
com.turbomanage.storm.apt.entity.FieldModel.java
com.turbomanage.storm.apt.entity.TableHelperTemplate.java
com.turbomanage.storm.apt.entity.package-info.java
com.turbomanage.storm.csv.CsvTableReader.java
com.turbomanage.storm.csv.CsvTableWriter.java
com.turbomanage.storm.csv.CsvUtils.java
com.turbomanage.storm.csv.package-info.java
com.turbomanage.storm.exception.TooManyResultsException.java
com.turbomanage.storm.exception.TypeNotSupportedException.java
com.turbomanage.storm.exception.package-info.java
com.turbomanage.storm.query.Query.java
com.turbomanage.storm.query.package-info.java
com.turbomanage.storm.sample.activity.ContactActivity.java
com.turbomanage.storm.sample.activity.NewContactActivity.java
com.turbomanage.storm.sample.adapter.ContactAdapter.java
com.turbomanage.storm.sample.database.SampleDatabaseHelper.java
com.turbomanage.storm.sample.loader.AbstractAsyncTaskLoader.java
com.turbomanage.storm.sample.loader.ContactLoader.java
com.turbomanage.storm.sample.model.Contact.java
com.turbomanage.storm.types.BlobConverter.java
com.turbomanage.storm.types.BooleanConverter.java
com.turbomanage.storm.types.ByteConverter.java
com.turbomanage.storm.types.CharConverter.java
com.turbomanage.storm.types.DateConverter.java
com.turbomanage.storm.types.DoubleConverter.java
com.turbomanage.storm.types.EnumConverter.java
com.turbomanage.storm.types.FloatConverter.java
com.turbomanage.storm.types.IntegerConverter.java
com.turbomanage.storm.types.LongConverter.java
com.turbomanage.storm.types.PrimitiveConverter.java
com.turbomanage.storm.types.ShortConverter.java
com.turbomanage.storm.types.StringConverter.java
com.turbomanage.storm.types.TypeConverter.java
com.turbomanage.storm.types.package-info.java
com.turbomanage.storm.package-info.java