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

net.beanstandard.compiler.BeanStandardProcessor Maven / Gradle / Ivy

There is a newer version: 1.5
Show newest version
package net.beanstandard.compiler;

import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.ParameterizedTypeName;
import com.squareup.javapoet.TypeName;
import com.squareup.javapoet.TypeSpec;
import net.beanstandard.BeanStandard;

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.TypeElement;
import javax.tools.JavaFileObject;
import java.io.IOException;
import java.io.Writer;
import java.util.HashSet;
import java.util.Set;

import static javax.lang.model.util.ElementFilter.typesIn;
import static javax.tools.Diagnostic.Kind.ERROR;

public final class BeanStandardProcessor extends AbstractProcessor {

  private final Set done = new HashSet<>();

  @Override
  public Set getSupportedAnnotationTypes() {
    Set strings = new HashSet<>();
    strings.add(BeanStandard.class.getCanonicalName());
    return strings;
  }

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

  @Override
  public boolean process(Set annotations, RoundEnvironment env) {
    Set typeElements =
        typesIn(env.getElementsAnnotatedWith(BeanStandard.class));
    for (TypeElement sourceClassElement : typeElements) {
      TypeName sourceClass = TypeName.get(sourceClassElement.asType());
      try {
        if (!done.add(sourceClass)) {
          continue;
        }
        Model model = Model.create(sourceClassElement);
        TypeSpec typeSpec = Analyser.create(model).analyse();
        write(rawType(model.generatedClass), typeSpec);
      } catch (ValidationException e) {
        processingEnv.getMessager().printMessage(ERROR, e.getMessage(), e.about);
      } catch (Exception e) {
        handleException(sourceClassElement, e);
        return false;
      }
    }
    return false;
  }

  private void handleException(TypeElement typeElement, Exception e) {
    e.printStackTrace();
    String message = "Error processing " +
        ClassName.get(typeElement) +
        ": " + e.getMessage();
    processingEnv.getMessager().printMessage(ERROR, message, typeElement);
  }

  private void write(ClassName generatedType, TypeSpec typeSpec) throws IOException {
    JavaFile javaFile = JavaFile.builder(generatedType.packageName(), typeSpec)
        .skipJavaLangImports(true)
        .build();
    JavaFileObject sourceFile = processingEnv.getFiler()
        .createSourceFile(generatedType.toString(),
            javaFile.typeSpec.originatingElements.toArray(new Element[0]));
    try (Writer writer = sourceFile.openWriter()) {
      writer.write(javaFile.toString());
    }
  }

  static ClassName rawType(TypeName typeName) {
    if (typeName instanceof ParameterizedTypeName) {
      return ((ParameterizedTypeName) typeName).rawType;
    }
    return ((ClassName) typeName);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy