mirror of
https://github.com/collinsmith/riiablo.git
synced 2025-02-11 11:27:16 +07:00
Added generation of table manifest
This commit is contained in:
parent
a5fd0e8b14
commit
fc09a8213c
@ -1,42 +1,54 @@
|
|||||||
package com.riiablo.table.annotation;
|
package com.riiablo.table.annotation;
|
||||||
|
|
||||||
import com.google.auto.service.AutoService;
|
import com.google.auto.service.AutoService;
|
||||||
|
import com.squareup.javapoet.ArrayTypeName;
|
||||||
|
import com.squareup.javapoet.CodeBlock;
|
||||||
|
import com.squareup.javapoet.JavaFile;
|
||||||
|
import com.squareup.javapoet.MethodSpec;
|
||||||
|
import com.squareup.javapoet.TypeSpec;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.LinkedHashSet;
|
import java.util.LinkedHashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import javax.annotation.processing.AbstractProcessor;
|
import javax.annotation.processing.AbstractProcessor;
|
||||||
import javax.annotation.processing.ProcessingEnvironment;
|
import javax.annotation.processing.ProcessingEnvironment;
|
||||||
import javax.annotation.processing.Processor;
|
import javax.annotation.processing.Processor;
|
||||||
import javax.annotation.processing.RoundEnvironment;
|
import javax.annotation.processing.RoundEnvironment;
|
||||||
import javax.annotation.processing.SupportedSourceVersion;
|
|
||||||
import javax.lang.model.SourceVersion;
|
import javax.lang.model.SourceVersion;
|
||||||
import javax.lang.model.element.Element;
|
import javax.lang.model.element.Element;
|
||||||
import javax.lang.model.element.ElementKind;
|
import javax.lang.model.element.ElementKind;
|
||||||
|
import javax.lang.model.element.Modifier;
|
||||||
import javax.lang.model.element.TypeElement;
|
import javax.lang.model.element.TypeElement;
|
||||||
import javax.lang.model.element.VariableElement;
|
import javax.lang.model.element.VariableElement;
|
||||||
|
import org.apache.commons.collections4.SetUtils;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.apache.commons.lang3.exception.ExceptionUtils;
|
import org.apache.commons.lang3.exception.ExceptionUtils;
|
||||||
|
|
||||||
@AutoService(Processor.class)
|
@AutoService(Processor.class)
|
||||||
@SupportedSourceVersion(SourceVersion.RELEASE_7)
|
|
||||||
public class SchemaProcessor extends AbstractProcessor {
|
public class SchemaProcessor extends AbstractProcessor {
|
||||||
static final Set<String> SUPPORTED_ANNOTATIONS;
|
private final Set<String> schemas = new HashSet<>();
|
||||||
static {
|
private Context context;
|
||||||
Set<String> set = new LinkedHashSet<>();
|
|
||||||
set.add(Schema.class.getCanonicalName());
|
|
||||||
set.add(PrimaryKey.class.getCanonicalName());
|
|
||||||
SUPPORTED_ANNOTATIONS = set;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void init(ProcessingEnvironment processingEnv) {
|
public synchronized void init(ProcessingEnvironment processingEnv) {
|
||||||
super.init(processingEnv);
|
super.init(processingEnv);
|
||||||
|
context = new Context(processingEnv);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean process(
|
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
|
||||||
|
if (roundEnv.processingOver()) {
|
||||||
|
generateManifest();
|
||||||
|
} else {
|
||||||
|
processAnnotations(annotations, roundEnv);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void processAnnotations(
|
||||||
Set<? extends TypeElement> annotations,
|
Set<? extends TypeElement> annotations,
|
||||||
RoundEnvironment roundEnv
|
RoundEnvironment roundEnv) {
|
||||||
) {
|
|
||||||
final Context context = new Context(processingEnv);
|
|
||||||
for (Element element : roundEnv.getElementsAnnotatedWith(PrimaryKey.class)) {
|
for (Element element : roundEnv.getElementsAnnotatedWith(PrimaryKey.class)) {
|
||||||
VariableElement variableElement = (VariableElement) element;
|
VariableElement variableElement = (VariableElement) element;
|
||||||
if (!Constants.isPrimaryKey(variableElement)) {
|
if (!Constants.isPrimaryKey(variableElement)) {
|
||||||
@ -46,11 +58,11 @@ public class SchemaProcessor extends AbstractProcessor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TableCodeGenerator tableCodeGenerator = new TableCodeGenerator(
|
TableCodeGenerator tableCodeGenerator = new TableCodeGenerator(
|
||||||
context, "com.riiablo.excel.table");
|
context, "com.riiablo.table.table");
|
||||||
SerializerCodeGenerator serializerCodeGenerator = new SerializerCodeGenerator(
|
SerializerCodeGenerator serializerCodeGenerator = new SerializerCodeGenerator(
|
||||||
context, "com.riiablo.excel.serializer");
|
context, "com.riiablo.table.serializer");
|
||||||
ParserCodeGenerator parserCodeGenerator = new ParserCodeGenerator(
|
ParserCodeGenerator parserCodeGenerator = new ParserCodeGenerator(
|
||||||
context, "com.riiablo.excel.parser");
|
context, "com.riiablo.table.parser");
|
||||||
for (Element element : roundEnv.getElementsAnnotatedWith(Schema.class)) {
|
for (Element element : roundEnv.getElementsAnnotatedWith(Schema.class)) {
|
||||||
if (element.getKind() != ElementKind.CLASS) {
|
if (element.getKind() != ElementKind.CLASS) {
|
||||||
context.error(element, "{} can only be applied to classes", Schema.class);
|
context.error(element, "{} can only be applied to classes", Schema.class);
|
||||||
@ -68,7 +80,7 @@ public class SchemaProcessor extends AbstractProcessor {
|
|||||||
t.printStackTrace(System.err);
|
t.printStackTrace(System.err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (schemaElement.parserElement.declaredType != null) {
|
if (schemaElement.parserElement.declaredType != null) {
|
||||||
try {
|
try {
|
||||||
parserCodeGenerator.generate(schemaElement)
|
parserCodeGenerator.generate(schemaElement)
|
||||||
@ -92,13 +104,47 @@ public class SchemaProcessor extends AbstractProcessor {
|
|||||||
t.printStackTrace(System.err);
|
t.printStackTrace(System.err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
schemas.add(CodeBlock.of("$S", schemaElement.element).toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void generateManifest() {
|
||||||
|
try {
|
||||||
|
JavaFile.builder("com.riiablo.table",
|
||||||
|
TypeSpec
|
||||||
|
.classBuilder("TableManifest")
|
||||||
|
.addModifiers(Modifier.PUBLIC, Modifier.FINAL)
|
||||||
|
.addMethod(MethodSpec
|
||||||
|
.constructorBuilder()
|
||||||
|
.addModifiers(Modifier.PRIVATE)
|
||||||
|
.build())
|
||||||
|
.addMethod(MethodSpec
|
||||||
|
.methodBuilder("tables")
|
||||||
|
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
|
||||||
|
.returns(ArrayTypeName.of(String.class))
|
||||||
|
.addStatement("return new String[] {\n$L\n}", StringUtils
|
||||||
|
.join(schemas, ",\n"))
|
||||||
|
.build())
|
||||||
|
.build())
|
||||||
|
.build()
|
||||||
|
.writeTo(processingEnv.getFiler());
|
||||||
|
} catch (Throwable t) {
|
||||||
|
context.error(ExceptionUtils.getRootCauseMessage(t));
|
||||||
|
t.printStackTrace(System.err);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<String> getSupportedAnnotationTypes() {
|
public Set<String> getSupportedAnnotationTypes() {
|
||||||
return SUPPORTED_ANNOTATIONS;
|
Set<String> set = new LinkedHashSet<>();
|
||||||
|
set.add(Schema.class.getCanonicalName());
|
||||||
|
set.add(PrimaryKey.class.getCanonicalName());
|
||||||
|
return SetUtils.unmodifiableSet(set);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SourceVersion getSupportedSourceVersion() {
|
||||||
|
return SourceVersion.latestSupported();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user