Android Open Source - storm-gen Class 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 . j  a  v a 2s.  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.util.ArrayList;
import java.util.List;

import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;

/**
 * Base class that introspects an annotated class using the Mirror API and
 * populates a model for use by a code generation template.
 *
 * @author David M. Chandler
 */
public abstract class ClassProcessor {

  protected TypeElement typeElement;
  protected StormEnvironment stormEnv;

  protected abstract ClassModel getModel();

  /**
   * Constructor intended to be overridden by subclasses.
   *
   * @param el
   * @param logger
   */
  protected ClassProcessor(Element el, StormEnvironment stormEnv) {
    this.typeElement = (TypeElement) el;
    this.stormEnv = stormEnv;
  }

  /**
   * Subclasses override to populate the model.
   * Invoked by main annotation processor.
   */
  protected void populateModel() {
    inspectClass();
  }

  protected void readFields(TypeElement type) {
    // Read fields from superclass if any
    TypeMirror superClass = type.getSuperclass();
    if (TypeKind.DECLARED.equals(superClass.getKind())) {
      DeclaredType superType = (DeclaredType) superClass;
      readFields((TypeElement) superType.asElement());
    }
    for (Element child : type.getEnclosedElements()) {
      if (child.getKind() == ElementKind.FIELD) {
        VariableElement field = (VariableElement) child;
        inspectField(field);
      }
    }
  }

  protected String getFieldType(VariableElement field) {
    TypeMirror fieldType = field.asType();
    return fieldType.toString();
  }

  protected void inspectClass() {
    this.getModel().setPackageName(getPackageName());
    this.getModel().setClassName(getClassName());
  }

  /**
   * Subclasses override to inspect each field and possibly add it
   * to the model. Invoked by main annotation processor.
   * Default impl does nothing, just serves to make implementation
   * optional for subclasses.
   *
   * @param field VariableElement that represents a class field
   */
  protected void inspectField(VariableElement field) {
  }

  public String getQualifiedClassName() {
    String pkg = getPackageName();
    if (pkg == null || pkg.length() < 1)
      throw new IllegalArgumentException("The default package is not allowed for type " + getClassName());
    return getPackageName() + "." + getClassName();
  }

  protected String getPackageName() {
    PackageElement pkgEl = (PackageElement) this.typeElement.getEnclosingElement();
    return pkgEl.getQualifiedName().toString();
  }

  protected String getClassName() {
    return this.typeElement.getSimpleName().toString();
  }

  protected List<String> inspectInterfaces() {
    // get list of interfaces
    List<String> iNames = new ArrayList<String>();
    List<? extends TypeMirror> interfaces = this.typeElement.getInterfaces();
    for (TypeMirror i : interfaces) {
      String iName = i.toString();
      iNames.add(iName);
    }
    return iNames;
  }

  protected void abort(String msg) {
    abort(msg, this.typeElement);
  }

  protected void abort(String msg, Element element) {
    throw new StormAnnotationException(msg, element);
  }

}




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