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

br.com.objectos.code.AbstractExecutableElementAnnotationProcessor Maven / Gradle / Ivy

There is a newer version: 0.15.0
Show newest version
/*
 * 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.code;

import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic;

/**
 * @author [email protected] (Marcio Endo)
 */
@Deprecated
public abstract class AbstractExecutableElementAnnotationProcessor extends AbstractProcessor {

  @Override
  public abstract Set getSupportedAnnotationTypes();

  @Override
  public SourceVersion getSupportedSourceVersion() {
    return SourceVersion.latestSupported();
  }

  @Override
  public final boolean process(Set typeElements, RoundEnvironment roundEnv) {
    try {
      onStart(typeElements, roundEnv);
      ProcessingEnvironmentWrapper wrapper = ProcessingEnvironmentWrapper.wrapperOf(processingEnv);

      Set roundSet = roundTypeElementSet(roundEnv);
      roundSet.stream()
          .map(el -> wrap(wrapper, el))
          .filter(Optional::isPresent)
          .map(Optional::get)
          .filter(this::filter)
          .flatMap(this::generateIfPossible)
          .peek(this::peek)
          .forEach(this::action);

      return onSuccess(typeElements, roundEnv);

    } catch (Throwable e) {
      String msg = Throwables.getStackTraceAsString(e);
      processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, name() + " processor threw an exception: " + msg);
      return onError(typeElements, roundEnv, e);

    } finally {
      onFinish(typeElements, roundEnv);

    }
  }

  protected void action(Artifact artifact) {
    artifact.execute(processingEnv);
  }

  protected boolean filter(RoundElement element) {
    return true;
  }

  protected abstract Stream generate(TypeInfo typeInfo, MethodInfo methodInfo);

  protected void info(String msg) {
    processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING, msg);
  }

  protected String name() {
    return getClass().getSimpleName();
  }

  protected boolean onError(Set typeElements, RoundEnvironment roundEnv, Throwable e) {
    return false;
  }

  protected void onFinish(Set typeElements, RoundEnvironment roundEnv) {
  }

  protected void onStart(Set typeElements, RoundEnvironment roundEnv) {
    processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Starting");
  }

  protected boolean onSuccess(Set typeElements, RoundEnvironment roundEnv) {
    return false;
  }

  protected void peek(Artifact artifact) {
  }

  protected abstract Set roundTypeElementSet(RoundEnvironment roundEnv);

  private Stream generateIfPossible(RoundElement element) {
    try {
      Stream res = generate(element.typeInfo, element.methodInfo);
      return res;
    } catch (CodeGenerationIncompleteException e) {
      return Stream.of();
    } catch (Exception e) {
      String msg = Throwables.getStackTraceAsString(e);
      processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, name() + " processor threw an exception: " + msg);
      return Stream.of();
    }
  }

  private Optional wrap(ProcessingEnvironmentWrapper wrapper, ExecutableElement element) {
    Optional res = Optional.empty();

    Element enclosingElement = element.getEnclosingElement();
    if (Apt.isType(enclosingElement)) {
      TypeInfo typeInfo = TypeInfoTypeElement.wrap(wrapper, (TypeElement) enclosingElement);
      TypeParameterInfoMap typeParameterInfoMap = typeInfo.typeParameterInfoMap();

      MethodInfo methodInfo;
      methodInfo = MethodInfoExecutableElement.wrap(wrapper, element, typeParameterInfoMap);

      RoundElement round = new RoundElement(typeInfo, methodInfo);
      res = Optional.of(round);
    }

    return res;
  }

  protected static class RoundElement {

    protected final TypeInfo typeInfo;
    protected final MethodInfo methodInfo;

    private RoundElement(TypeInfo typeInfo, MethodInfo methodInfo) {
      this.typeInfo = typeInfo;
      this.methodInfo = methodInfo;
    }

  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy