|
| 1 | +/* |
| 2 | +* Copyright 2017 Basis Technology Corp. |
| 3 | +* |
| 4 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +* you may not use this file except in compliance with the License. |
| 6 | +* You may obtain a copy of the License at |
| 7 | +* |
| 8 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +* |
| 10 | +* Unless required by applicable law or agreed to in writing, software |
| 11 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +* See the License for the specific language governing permissions and |
| 14 | +* limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package com.basistech.rosette.annotations; |
| 18 | + |
| 19 | +import com.fasterxml.jackson.annotation.JsonInclude; |
| 20 | +import com.fasterxml.jackson.annotation.JsonTypeName; |
| 21 | +import com.fasterxml.jackson.databind.Module; |
| 22 | +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; |
| 23 | +import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder; |
| 24 | +import com.squareup.javapoet.AnnotationSpec; |
| 25 | +import com.squareup.javapoet.CodeBlock; |
| 26 | +import com.squareup.javapoet.JavaFile; |
| 27 | +import com.squareup.javapoet.MethodSpec; |
| 28 | +import com.squareup.javapoet.TypeSpec; |
| 29 | +import lombok.Builder; |
| 30 | + |
| 31 | +import javax.annotation.processing.AbstractProcessor; |
| 32 | +import javax.annotation.processing.ProcessingEnvironment; |
| 33 | +import javax.annotation.processing.RoundEnvironment; |
| 34 | +import javax.lang.model.SourceVersion; |
| 35 | +import javax.lang.model.element.Element; |
| 36 | +import javax.lang.model.element.ElementKind; |
| 37 | +import javax.lang.model.element.Modifier; |
| 38 | +import javax.lang.model.element.TypeElement; |
| 39 | +import javax.tools.Diagnostic; |
| 40 | +import java.io.IOException; |
| 41 | +import java.util.Arrays; |
| 42 | +import java.util.Collections; |
| 43 | +import java.util.HashMap; |
| 44 | +import java.util.HashSet; |
| 45 | +import java.util.Map; |
| 46 | +import java.util.Set; |
| 47 | + |
| 48 | +public class JacksonMixinProcessor extends AbstractProcessor { |
| 49 | + |
| 50 | + private static final String PACKAGE_NAME = "com.basistech.rosette.apimodel.jackson"; |
| 51 | + |
| 52 | + private ProcessingEnvironment processingEnvironment; |
| 53 | + |
| 54 | + @Override |
| 55 | + public synchronized void init(ProcessingEnvironment processingEnvironment) { |
| 56 | + super.init(processingEnvironment); |
| 57 | + this.processingEnvironment = processingEnvironment; |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnvironment) { |
| 62 | + if (roundEnvironment.getElementsAnnotatedWith(JacksonMixin.class).isEmpty()) { |
| 63 | + return true; |
| 64 | + } |
| 65 | + Map<String, String> addMixinCode = new HashMap<>(); |
| 66 | + for (Element element : roundEnvironment.getElementsAnnotatedWith(JacksonMixin.class)) { |
| 67 | + if (element.getKind() != ElementKind.CLASS) { |
| 68 | + processingEnvironment.getMessager().printMessage(Diagnostic.Kind.ERROR, "Annotation only available to Class"); |
| 69 | + continue; |
| 70 | + } |
| 71 | + TypeElement typeElement = (TypeElement) element; |
| 72 | + String elementQualifiedName = typeElement.getQualifiedName().toString(); |
| 73 | + String elementSimpleName = typeElement.getSimpleName().toString(); |
| 74 | + if (typeElement.getAnnotation(Builder.class) != null) { |
| 75 | + TypeSpec.Builder mixinClassBuilder = TypeSpec |
| 76 | + .classBuilder(typeElement.getSimpleName().toString() + "Mixin") |
| 77 | + .addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT) |
| 78 | + .addAnnotation(AnnotationSpec.builder(JsonTypeName.class) |
| 79 | + .addMember("value", "$S", typeElement.getSimpleName()) |
| 80 | + .build()) |
| 81 | + .addAnnotation(AnnotationSpec.builder(JsonDeserialize.class) |
| 82 | + .addMember("builder", CodeBlock.builder() |
| 83 | + .add(elementQualifiedName + "." + elementSimpleName + "Builder.class").build()) |
| 84 | + .build()) |
| 85 | + .addAnnotation(AnnotationSpec.builder(JsonInclude.class) |
| 86 | + .addMember("value", CodeBlock.builder() |
| 87 | + .add("JsonInclude.Include.NON_NULL").build()) |
| 88 | + .build()) |
| 89 | + .addType(TypeSpec |
| 90 | + .classBuilder(typeElement.getSimpleName().toString() + "MixinBuilder") |
| 91 | + .addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT) |
| 92 | + .addAnnotation(AnnotationSpec.builder(JsonPOJOBuilder.class) |
| 93 | + .addMember("withPrefix", "$S", "") |
| 94 | + .build()).build()); |
| 95 | + try { |
| 96 | + String packageName = elementQualifiedName.substring(0, elementQualifiedName.lastIndexOf(".")) |
| 97 | + + ".jackson"; |
| 98 | + JavaFile.builder(packageName, mixinClassBuilder.build()) |
| 99 | + .build() |
| 100 | + .writeTo(processingEnvironment.getFiler()); |
| 101 | + addMixinCode.put(elementQualifiedName + ".class", |
| 102 | + packageName + "." + typeElement.getSimpleName().toString() + "Mixin" + ".class"); |
| 103 | + addMixinCode.put(elementQualifiedName + "." + elementSimpleName + "Builder.class", |
| 104 | + packageName + "." + typeElement.getSimpleName().toString() |
| 105 | + + "Mixin." + typeElement.getSimpleName().toString() + "MixinBuilder.class"); |
| 106 | + } catch (IOException e) { |
| 107 | + e.printStackTrace(); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + MethodSpec.Builder methodSpecBuilder = MethodSpec.methodBuilder("addMixins") |
| 113 | + .addModifiers(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL) |
| 114 | + .addParameter(Module.SetupContext.class, "context"); |
| 115 | + for (String key : addMixinCode.keySet()) { |
| 116 | + methodSpecBuilder.addStatement(" context.setMixInAnnotations($L, $L)", key, addMixinCode.get(key)); |
| 117 | + } |
| 118 | + TypeSpec.Builder mixinUtilClassBuilder = TypeSpec |
| 119 | + .classBuilder("MixinUtil") |
| 120 | + .addModifiers(Modifier.PUBLIC, Modifier.FINAL) |
| 121 | + .addAnnotation(AnnotationSpec.builder(SuppressWarnings.class) |
| 122 | + .addMember("value", "$S", "PMD") |
| 123 | + .build()) |
| 124 | + .addMethod(methodSpecBuilder.build()); |
| 125 | + try { |
| 126 | + JavaFile.builder(PACKAGE_NAME, mixinUtilClassBuilder.build()) |
| 127 | + .build() |
| 128 | + .writeTo(processingEnvironment.getFiler()); |
| 129 | + } catch (IOException e) { |
| 130 | + e.printStackTrace(); |
| 131 | + } |
| 132 | + |
| 133 | + return true; |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public Set<String> getSupportedAnnotationTypes() { |
| 138 | + return Collections.unmodifiableSet(new HashSet<>(Arrays.asList(JacksonMixin.class.getCanonicalName()))); |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public SourceVersion getSupportedSourceVersion() { |
| 143 | + return SourceVersion.latestSupported(); |
| 144 | + } |
| 145 | +} |
0 commit comments