001package io.ebean.querybean.generator; 002 003import javax.annotation.processing.AbstractProcessor; 004import javax.annotation.processing.ProcessingEnvironment; 005import javax.annotation.processing.RoundEnvironment; 006import javax.lang.model.SourceVersion; 007import javax.lang.model.element.Element; 008import javax.lang.model.element.TypeElement; 009import javax.persistence.Embeddable; 010import javax.persistence.Entity; 011import java.util.LinkedHashSet; 012import java.util.Set; 013 014/** 015 * Process compiled entity beans and generates 'query beans' for them. 016 */ 017public class Processor extends AbstractProcessor { 018 019 private static final String GENERATE_KOTLIN_CODE_OPTION = "generate.kotlin.code"; 020 private static final String KAPT_KOTLIN_GENERATED_OPTION = "kapt.kotlin.generated"; 021 022 public Processor() { 023 } 024 025 @Override 026 public Set<String> getSupportedOptions() { 027 028 Set<String> options = new LinkedHashSet<>(); 029 options.add(KAPT_KOTLIN_GENERATED_OPTION); 030 options.add(GENERATE_KOTLIN_CODE_OPTION); 031 return options; 032 } 033 034 @Override 035 public synchronized void init(ProcessingEnvironment processingEnv) { 036 super.init(processingEnv); 037 } 038 039 @Override 040 public Set<String> getSupportedAnnotationTypes() { 041 042 Set<String> annotations = new LinkedHashSet<>(); 043 annotations.add(Entity.class.getCanonicalName()); 044 annotations.add(Embeddable.class.getCanonicalName()); 045 return annotations; 046 } 047 048 @Override 049 public SourceVersion getSupportedSourceVersion() { 050 return SourceVersion.latest(); 051 } 052 053 @Override 054 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { 055 056 int entityCount = 0; 057 int embeddableCount = 0; 058 059 //processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "options[" + processingEnv.getOptions() + "]"); 060 061 String generatedDir = processingEnv.getOptions().get("kapt.kotlin.generated"); 062 if (generatedDir == null) { 063 generatedDir = "target/generated-sources/kapt/compile"; 064 } 065 066 ProcessingContext context = new ProcessingContext(processingEnv, generatedDir); 067 068 for (Element element : roundEnv.getElementsAnnotatedWith(Entity.class)) { 069 generateQueryBeans(context, element); 070 entityCount++; 071 } 072 073 for (Element element : roundEnv.getElementsAnnotatedWith(Embeddable.class)) { 074 generateQueryBeans(context, element); 075 embeddableCount++; 076 } 077 078 if (entityCount > 0 || embeddableCount > 0) { 079 context.logNote("Generated query beans for [" + entityCount + "] entities [" + embeddableCount + "] embeddable"); 080 } 081 082 return true; 083 } 084 085 private void generateQueryBeans(ProcessingContext context, Element element) { 086 try { 087 SimpleQueryBeanWriter beanWriter = new SimpleQueryBeanWriter((TypeElement) element, context); 088 beanWriter.writeRootBean(); 089 beanWriter.writeAssocBean(); 090 } catch (Exception e) { 091 context.logError(element, "Error generating query beans: " + e); 092 } 093 } 094}