Search in sources :

Example 1 with ProcessingException

use of com.tickaroo.tikxml.processor.ProcessingException in project tikxml by Tickaroo.

the class TikXmlAutoValueExtension method applicable.

@Override
public boolean applicable(Context context) {
    ProcessingEnvironment environment = context.processingEnvironment();
    try {
        Xml xmlAnnotation = context.autoValueClass().getAnnotation(Xml.class);
        if (xmlAnnotation == null) {
            // Auto value class not annotated with @Xml annotation
            return false;
        }
        if (!xmlAnnotation.inheritance()) {
            throw new ProcessingException(context.autoValueClass(), "Inheritance in TikXml can't be disabled via @" + Xml.class.getSimpleName() + "(inheritance = false) in class " + context.autoValueClass().getQualifiedName());
        }
        List<AnnotatedMethod<?>> annotatedMethods = AutoValueScannerKt.extractAutoValueProperties(context.autoValueClass(), context.properties(), context.processingEnvironment().getTypeUtils(), context.processingEnvironment().getElementUtils());
        // generate code
        AutoValueAnnotatedClass annotatedClass = new AutoValueAnnotatedClass(context.packageName(), context.autoValueClass(), xmlAnnotation, annotatedMethods);
        try {
            Filer filer = context.processingEnvironment().getFiler();
            JavaFile.builder(context.packageName(), AutoValueTypeAdapterCodeGeneratorKt.generateValueHolder(annotatedClass, context.processingEnvironment().getElementUtils())).build().writeTo(filer);
            JavaFile.builder(context.packageName(), AutoValueTypeAdapterCodeGeneratorKt.generateTypeAdapter(annotatedClass)).build().writeTo(filer);
        } catch (IOException e) {
            throw new ProcessingException(annotatedClass.getAutoValueClass(), "Error while generating code for " + annotatedClass.getAutoValueClass().getQualifiedName() + ": " + e.getMessage());
        }
    } catch (ProcessingException exception) {
        environment.getMessager().printMessage(Diagnostic.Kind.ERROR, exception.getMessage(), exception.getElement());
    }
    // We don't generate code as an autovalue extension
    return false;
}
Also used : Xml(com.tickaroo.tikxml.annotation.Xml) IOException(java.io.IOException) ProcessingEnvironment(javax.annotation.processing.ProcessingEnvironment) Filer(javax.annotation.processing.Filer) ProcessingException(com.tickaroo.tikxml.processor.ProcessingException)

Aggregations

Xml (com.tickaroo.tikxml.annotation.Xml)1 ProcessingException (com.tickaroo.tikxml.processor.ProcessingException)1 IOException (java.io.IOException)1 Filer (javax.annotation.processing.Filer)1 ProcessingEnvironment (javax.annotation.processing.ProcessingEnvironment)1