All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.com.objectos.way.sql.BootProcessor Maven / Gradle / Ivy

/*
 * Copyright 2015 Objectos, Fábrica de Software LTDA.
 *
 * 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 br.com.objectos.way.sql;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import javax.annotation.Generated;
import javax.annotation.processing.Processor;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.ElementFilter;

import br.com.objectos.core.collections.ImmutableSet;
import br.com.objectos.way.code.AbstractTypeElementAnnotationProcessor;
import br.com.objectos.way.code.AnnotationInfo;
import br.com.objectos.way.code.Artifact;
import br.com.objectos.way.code.TypeInfo;
import br.com.objectos.way.metainf.Services;

import com.squareup.javapoet.AnnotationSpec;
import com.squareup.javapoet.JavaFile;

/**
 * @author [email protected] (Marcio Endo)
 */
@Services(Processor.class)
public class BootProcessor extends AbstractTypeElementAnnotationProcessor {

  static final AnnotationSpec ANNOTATION_SPEC = AnnotationSpec.builder(Generated.class)
      .addMember("value", "$S", BootProcessor.class.getName())
      .build();

  private static final String ANNOTATION_NAME = "br.com.objectos.way.sql.Boot";

  @Override
  public Set getSupportedAnnotationTypes() {
    return ImmutableSet.of(ANNOTATION_NAME);
  }

  @Override
  protected Stream generate(TypeInfo typeInfo) {
    AnnotationInfo bootAnnotationInfo = typeInfo.annotationInfo(ANNOTATION_NAME).get();
    int development = bootAnnotationInfo.intValue("development", 0);
    int level = bootAnnotationInfo.intValue("level", 0);

    if (level < 2) {
      typeInfo.compilationError("level must be > 1");
      return Stream.empty();
    }

    if (development < 1) {
      typeInfo.compilationError("development must be > 0");
      return Stream.empty();
    }

    if (development > level) {
      typeInfo.compilationError("development must not be greater than level");
      return Stream.empty();
    }

    String packageName = typeInfo.packageInfo().toString();
    List levelList = IntStream.rangeClosed(1, level)
        .mapToObj(it -> Level.of(packageName, it))
        .collect(Collectors.toList());

    Stream levelStream = levelList.stream()
        .filter(l -> l.filter(development))
        .flatMap(Level::generate);

    Stream moreStream = Stream.of(
        SqlType.of(typeInfo, levelList).generate(),
        InsertIntoType.of(levelList).generate(),
        UpdateType.of(levelList).generate());

    return Stream.concat(levelStream, moreStream).map(Artifact::of);
  }

  @Override
  protected Set roundTypeElementSet(RoundEnvironment roundEnv) {
    TypeElement annotationType = processingEnv.getElementUtils().getTypeElement(ANNOTATION_NAME);
    Set elements = roundEnv.getElementsAnnotatedWith(annotationType);
    return ElementFilter.typesIn(elements);
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy