|
| 1 | +package tech.ydb.yoj.generator; |
| 2 | + |
| 3 | +import org.slf4j.Logger; |
| 4 | +import org.slf4j.LoggerFactory; |
| 5 | +import tech.ydb.yoj.ExperimentalApi; |
| 6 | +import tech.ydb.yoj.databind.schema.Table; |
| 7 | + |
| 8 | +import javax.annotation.processing.AbstractProcessor; |
| 9 | +import javax.annotation.processing.RoundEnvironment; |
| 10 | +import javax.annotation.processing.SupportedAnnotationTypes; |
| 11 | +import javax.annotation.processing.SupportedSourceVersion; |
| 12 | +import javax.lang.model.SourceVersion; |
| 13 | +import javax.lang.model.element.Element; |
| 14 | +import javax.lang.model.element.ElementKind; |
| 15 | +import javax.lang.model.element.PackageElement; |
| 16 | +import javax.lang.model.element.TypeElement; |
| 17 | +import javax.tools.FileObject; |
| 18 | +import java.io.IOException; |
| 19 | +import java.io.Writer; |
| 20 | +import java.util.Set; |
| 21 | + |
| 22 | +/** |
| 23 | + * Generates a "-Fields" classes for all entities in a module. |
| 24 | + * <p>This is useful for building queries like '<code>where(AuditEvent.Id.TRAIL_ID).eq(trailId)</code>'</p> |
| 25 | + * <p>Assume that we have an entity:</p> |
| 26 | + * <pre>{@code |
| 27 | + * @Table(...) |
| 28 | + * class MyTable { |
| 29 | + * String strField; |
| 30 | + * Object anyTypeField; |
| 31 | + * Id idField; |
| 32 | + * OtherClass fieldOfOtherClass; |
| 33 | + * |
| 34 | + * static class Id { |
| 35 | + * String value; |
| 36 | + * OneMoreLevel deepField1; |
| 37 | + * OneMoreLevel deepField2; |
| 38 | + * |
| 39 | + * static class OneMoreLevel { |
| 40 | + * Integer field; |
| 41 | + * } |
| 42 | + * } |
| 43 | + * } |
| 44 | + * } |
| 45 | + * A generated class will be |
| 46 | + * <pre>{@code |
| 47 | + * class MyTableFields { // Annotation processors must create a new class, so the name is different |
| 48 | + * // fields considered as 'simple' if there is no nested class of the field's type |
| 49 | + * public static final String STR_FIELD = "strField"; |
| 50 | + * public static final String ANY_TYPE_FIELD = "anyTypeField"; |
| 51 | + * // `fieldOfOtherClass` is a simple field because `OtherClass` is not inside the given class |
| 52 | + * public static final String FIELD_OF_OTHER_CLASS = "fieldOfOtherClass"; |
| 53 | + * |
| 54 | + * // idField is not simple because it has a type Id and there is a nested class Id |
| 55 | + * // Pay attention that the generated nested class uses the field's name! It's necessary because we might |
| 56 | + * // have several fields of the same type |
| 57 | + * public static class IdField { |
| 58 | + * public static final String VALUE = "idField.value"; // Mind that the value has "idField." prefix |
| 59 | + * |
| 60 | + * // The Annotation Processor works recursively |
| 61 | + * // Also it's the example of several fields with the same type |
| 62 | + * static class DeepField1 { |
| 63 | + * public static final String FIELD = "idField.deepField1.field"; |
| 64 | + * } |
| 65 | + * static class DeepField2 { |
| 66 | + * // Pay attention that ".deepField2." is used here |
| 67 | + * public static final String FIELD = "idField.deepField2.field"; |
| 68 | + * } |
| 69 | + * } |
| 70 | + * } |
| 71 | + * } |
| 72 | + * <ul> |
| 73 | + * <b>Additional info:</b> |
| 74 | + * <li>Support Records</li> |
| 75 | + * <li>Support Kotlin data classes</li> |
| 76 | + * </ul> |
| 77 | + * <ul> |
| 78 | + * <b>Known issues (should be fixed in future):</b> |
| 79 | + * <li>if entity doesn't have @Table it won't be processed even if it's implements Entity interface</li> |
| 80 | + * <li>We assume that annotation @Table is used on top-level class</li> |
| 81 | + * <li>The AP will break in case of two nested classes which refer each other (i.e. circular dependency) </li> |
| 82 | + * <li>Will generate nested classes even disregarding @Column's flatten option </li> |
| 83 | + * <li>No logs are written</li> |
| 84 | + * <li>if a field has type of a class which is not nested inside the annotated class, the field will be ignored</li> |
| 85 | + * <li>There is a rare situation when generated code won't compile. The following source class |
| 86 | + * <pre>{@code |
| 87 | + * class Name{ |
| 88 | + * Class1 nameClash; |
| 89 | + * public static class Class1 { |
| 90 | + * Class2 nameClash; |
| 91 | + * class Class2{ |
| 92 | + * String nameClash; |
| 93 | + * } |
| 94 | + * } |
| 95 | + * } |
| 96 | + * } |
| 97 | + * will produce |
| 98 | + * <pre>{@code |
| 99 | + * public class NameFields { |
| 100 | + * public class NameClash { |
| 101 | + * public class NameClash { |
| 102 | + * public static final String NAME_CLASH = "nameClash.nameClash.nameClash"; |
| 103 | + * } |
| 104 | + * } |
| 105 | + * } |
| 106 | + * } |
| 107 | + * which won't compile due to 2 NameClash classes. |
| 108 | + * </li> |
| 109 | + * </ul> |
| 110 | + * |
| 111 | + * @author pavel-lazarev |
| 112 | + */ |
| 113 | + |
| 114 | +@ExperimentalApi(issue = "https://github.com/ydb-platform/yoj-project/pull/57") |
| 115 | +@SupportedAnnotationTypes({ |
| 116 | + "tech.ydb.yoj.databind.schema.Table", |
| 117 | +}) |
| 118 | +@SupportedSourceVersion(SourceVersion.RELEASE_17) |
| 119 | +public class FieldGeneratorAnnotationProcessor extends AbstractProcessor { |
| 120 | + |
| 121 | + private static final Logger log = LoggerFactory.getLogger(FieldGeneratorAnnotationProcessor.class); |
| 122 | + |
| 123 | + private static final String TARGET_PACKAGE = "generated"; |
| 124 | + private static final String TARGET_CLASS_NAME_SUFFIX = "Fields"; |
| 125 | + |
| 126 | + @Override |
| 127 | + public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) { |
| 128 | + Set<? extends Element> elementsAnnotatedWith = roundEnvironment.getElementsAnnotatedWith(Table.class); |
| 129 | + log.info("Found {} classes to process ", elementsAnnotatedWith.size()); |
| 130 | + for (Element rootElement : elementsAnnotatedWith) { |
| 131 | + log.debug("Processing {}", rootElement.getSimpleName()); |
| 132 | + |
| 133 | + SourceClassStructure sourceClassStructure = SourceClassStructure.analyse( |
| 134 | + rootElement, |
| 135 | + processingEnv.getTypeUtils() |
| 136 | + ); |
| 137 | + TargetClassStructure targetClassStructure = TargetClassStructure.build( |
| 138 | + sourceClassStructure, |
| 139 | + rootElement.getSimpleName() + TARGET_CLASS_NAME_SUFFIX |
| 140 | + ); |
| 141 | + |
| 142 | + String packageName = Utils.concatFieldNameChain( |
| 143 | + calcPackage(rootElement), |
| 144 | + TARGET_PACKAGE |
| 145 | + ); |
| 146 | + String generatedSource = StringConstantsRenderer.render(targetClassStructure, packageName); |
| 147 | + log.debug("Generated:\n {}", generatedSource); |
| 148 | + saveFile( |
| 149 | + generatedSource, |
| 150 | + Utils.concatFieldNameChain( |
| 151 | + packageName, |
| 152 | + targetClassStructure.className() |
| 153 | + ) |
| 154 | + ); |
| 155 | + } |
| 156 | + |
| 157 | + return false; |
| 158 | + } |
| 159 | + |
| 160 | + private String calcPackage(Element element) { |
| 161 | + while (element.getKind() != ElementKind.PACKAGE) { |
| 162 | + element = element.getEnclosingElement(); |
| 163 | + } |
| 164 | + PackageElement packageElement = (PackageElement) element; |
| 165 | + return packageElement.getQualifiedName().toString(); |
| 166 | + } |
| 167 | + |
| 168 | + private void saveFile(String classContent, String fullClassName) { |
| 169 | + try { |
| 170 | + FileObject file = processingEnv.getFiler().createSourceFile(fullClassName); |
| 171 | + try (Writer writer = file.openWriter()) { |
| 172 | + writer.write(classContent); |
| 173 | + } |
| 174 | + } catch (IOException ex) { |
| 175 | + throw new RuntimeException( |
| 176 | + "Can not save file %s. Content:\n%s".formatted(fullClassName, classContent), |
| 177 | + ex |
| 178 | + ); |
| 179 | + } |
| 180 | + } |
| 181 | +} |
0 commit comments