Compare commits

..

No commits in common. "4563ce2c4ede00705634ce6a755d4f54ddcb5ed7" and "bedeb1a51ed3d9eeccf7fb53842f201ba1b25200" have entirely different histories.

17 changed files with 134 additions and 368 deletions

View File

@ -5,13 +5,13 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<parent> <parent>
<groupId>dev.struchkov.haiti</groupId> <groupId>dev.struchkov.haiti</groupId>
<artifactId>haiti-dependencies</artifactId> <artifactId>haiti</artifactId>
<version>2.0.0</version> <version>0.0.4</version>
</parent> </parent>
<groupId>dev.struchkov.haiti.utils</groupId> <groupId>dev.struchkov.haiti.utils</groupId>
<artifactId>haiti-utils-field-constants</artifactId> <artifactId>haiti-utils-field-constants</artifactId>
<version>0.0.6</version> <version>0.0.3-SNAPSHOT</version>
<name>Haiti Field Name Constants Utils</name> <name>Haiti Field Name Constants Utils</name>
<description>Generating class field names</description> <description>Generating class field names</description>
@ -22,7 +22,7 @@
</issueManagement> </issueManagement>
<properties> <properties>
<java.version>17</java.version> <java.version>11</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source> <maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target> <maven.compiler.target>${java.version}</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

View File

@ -1,11 +1,10 @@
package dev.struchkov.haiti.utils.fieldconstants.creator; package dev.struchkov.haiti.utils.fieldconstants;
import dev.struchkov.haiti.utils.Checker;
import dev.struchkov.haiti.utils.fieldconstants.domain.mode.table.ClassTableDto; import dev.struchkov.haiti.utils.fieldconstants.domain.mode.table.ClassTableDto;
import dev.struchkov.haiti.utils.fieldconstants.domain.mode.table.JoinElemCollectionDto; import dev.struchkov.haiti.utils.fieldconstants.domain.mode.table.JoinElemCollectionDto;
import dev.struchkov.haiti.utils.fieldconstants.domain.mode.table.JoinFieldDto; import dev.struchkov.haiti.utils.fieldconstants.domain.mode.table.JoinFieldDto;
import dev.struchkov.haiti.utils.fieldconstants.domain.mode.table.JoinTableContainer; import dev.struchkov.haiti.utils.fieldconstants.domain.mode.table.JoinTableContainer;
import dev.struchkov.haiti.utils.fieldconstants.domain.mode.table.SimpleTableFieldDto; import dev.struchkov.haiti.utils.fieldconstants.domain.mode.table.SimpleFieldTableDto;
import javax.annotation.processing.ProcessingEnvironment; import javax.annotation.processing.ProcessingEnvironment;
import javax.tools.JavaFileObject; import javax.tools.JavaFileObject;
@ -14,7 +13,6 @@ import java.io.PrintWriter;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import static dev.struchkov.haiti.utils.Checker.checkNotEmpty;
import static dev.struchkov.haiti.utils.Exceptions.utilityClass; import static dev.struchkov.haiti.utils.Exceptions.utilityClass;
import static java.text.MessageFormat.format; import static java.text.MessageFormat.format;
@ -35,7 +33,7 @@ public final class CreatorClassTableMode {
public static void record(ClassTableDto classDto, ProcessingEnvironment environment) { public static void record(ClassTableDto classDto, ProcessingEnvironment environment) {
JavaFileObject builderFile = null; JavaFileObject builderFile = null;
try { try {
builderFile = environment.getFiler().createSourceFile(classDto.getClassPackage() +"." + classDto.getClassName()); builderFile = environment.getFiler().createSourceFile(classDto.getClassName());
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -65,22 +63,20 @@ public final class CreatorClassTableMode {
} }
} }
private static void generateTableName(String tableName, Collection<SimpleTableFieldDto> fields, PrintWriter out) { private static void generateTableName(String tableName, Collection<SimpleFieldTableDto> fields, PrintWriter out) {
if (tableName != null) { if (tableName != null) {
final boolean isTableNameField = fields.stream().anyMatch(simpleFieldTableDto -> simpleFieldTableDto.getFieldStringName().equalsIgnoreCase(TABLE_NAME_DEFAULT)); final boolean isTableNameField = fields.stream().anyMatch(simpleFieldTableDto -> simpleFieldTableDto.getFieldStringName().equalsIgnoreCase(TABLE_NAME_DEFAULT));
out.println(format(" public static final String {0} = \"{1}\";", isTableNameField ? TABLE_NAME_DB : TABLE_NAME_DEFAULT, tableName)); out.println(format(" public static final String {0} = \"{1}\";", isTableNameField ? TABLE_NAME_DB : TABLE_NAME_DEFAULT, tableName));
out.println();
} }
out.println();
} }
private static void generateSimpleNames(Collection<SimpleTableFieldDto> fields, PrintWriter out) { private static void generateSimpleNames(Collection<SimpleFieldTableDto> fields, PrintWriter out) {
for (SimpleTableFieldDto field : fields) { for (SimpleFieldTableDto field : fields) {
out.println(format(" public static final String {0} = \"{1}\";", field.getFieldStringName(), field.getFieldName())); out.println(format(" public static final String {0} = \"{1}\";", field.getFieldStringName(), field.getFieldName()));
} }
if (checkNotEmpty(fields)) {
out.println(); out.println();
} }
}
private static void generateJoinNames(List<JoinFieldDto> joinFields, PrintWriter out) { private static void generateJoinNames(List<JoinFieldDto> joinFields, PrintWriter out) {
for (JoinFieldDto joinField : joinFields) { for (JoinFieldDto joinField : joinFields) {
@ -92,10 +88,8 @@ public final class CreatorClassTableMode {
) )
); );
} }
if (checkNotEmpty(joinFields)) {
out.println(); out.println();
} }
}
private static void generateElementCollectionNames(List<JoinElemCollectionDto> joinElemCollections, PrintWriter out) { private static void generateElementCollectionNames(List<JoinElemCollectionDto> joinElemCollections, PrintWriter out) {
for (JoinElemCollectionDto element : joinElemCollections) { for (JoinElemCollectionDto element : joinElemCollections) {
@ -107,9 +101,7 @@ public final class CreatorClassTableMode {
out.println(format(" JoinTable.ofLeft(\"{0}\", \"{1}\", \"{2}\")", secondContainer.getTable(), secondContainer.getBaseId(), secondContainer.getReference())); out.println(format(" JoinTable.ofLeft(\"{0}\", \"{1}\", \"{2}\")", secondContainer.getTable(), secondContainer.getBaseId(), secondContainer.getReference()));
out.println(" };"); out.println(" };");
} }
if (checkNotEmpty(joinElemCollections)) {
out.println(); out.println();
} }
}
} }

View File

@ -1,6 +1,5 @@
package dev.struchkov.haiti.utils.fieldconstants.annotation; package dev.struchkov.haiti.utils.fieldconstants.annotation;
import dev.struchkov.haiti.utils.fieldconstants.annotation.setting.TableModeSettings;
import dev.struchkov.haiti.utils.fieldconstants.domain.Mode; import dev.struchkov.haiti.utils.fieldconstants.domain.Mode;
import java.lang.annotation.ElementType; import java.lang.annotation.ElementType;
@ -14,6 +13,4 @@ public @interface FieldNames {
Mode[] mode() default {Mode.SIMPLE}; Mode[] mode() default {Mode.SIMPLE};
TableModeSettings tableSettings() default @TableModeSettings(prefixTableForColumn = false);
} }

View File

@ -1,7 +0,0 @@
package dev.struchkov.haiti.utils.fieldconstants.annotation.setting;
public @interface TableModeSettings {
boolean prefixTableForColumn();
}

View File

@ -1,58 +0,0 @@
package dev.struchkov.haiti.utils.fieldconstants.creator;
import dev.struchkov.haiti.utils.fieldconstants.domain.mode.simple.SimpleFieldDto;
import dev.struchkov.haiti.utils.fieldconstants.domain.mode.table.SimpleTableFieldDto;
import dev.struchkov.haiti.utils.fieldconstants.domain.mode.simple.ClassSimpleDto;
import javax.annotation.processing.ProcessingEnvironment;
import javax.tools.JavaFileObject;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import static dev.struchkov.haiti.utils.Exceptions.utilityClass;
import static java.text.MessageFormat.format;
/**
* // TODO: 20.06.2021 Добавить описание.
*
* @author upagge 20.06.2021
*/
public final class CreatorClassSimpleMode {
private CreatorClassSimpleMode() {
utilityClass();
}
public static void record(ClassSimpleDto classDto, ProcessingEnvironment environment) {
JavaFileObject builderFile = null;
try {
builderFile = environment.getFiler().createSourceFile(classDto.getClassName());
} catch (IOException e) {
e.printStackTrace();
}
try (PrintWriter out = new PrintWriter(builderFile.openWriter())) {
out.println("package " + classDto.getClassPackage() + ";");
out.println();
out.println();
out.print(format("public class {0} '{'", classDto.getClassName()));
out.println();
out.println();
generateSimpleNames(classDto.getSimpleFields(), out);
out.println("}");
out.println();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void generateSimpleNames(Collection<SimpleFieldDto> fields, PrintWriter out) {
for (SimpleFieldDto field : fields) {
out.println(format(" public static final String {0} = \"{1}\";", field.getFieldName(), field.getFieldName()));
}
out.println();
}
}

View File

@ -1,44 +0,0 @@
package dev.struchkov.haiti.utils.fieldconstants.domain.mode.simple;
import java.util.List;
public class ClassSimpleDto {
private String classPackage;
private String className;
private String tableName;
private List<SimpleFieldDto> simpleFields;
public String getClassPackage() {
return classPackage;
}
public void setClassPackage(String classPackage) {
this.classPackage = classPackage;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public String getTableName() {
return tableName;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
public List<SimpleFieldDto> getSimpleFields() {
return simpleFields;
}
public void setSimpleFields(List<SimpleFieldDto> simpleFields) {
this.simpleFields = simpleFields;
}
}

View File

@ -1,24 +0,0 @@
package dev.struchkov.haiti.utils.fieldconstants.domain.mode.simple;
/**
* // TODO: 07.06.2021 Добавить описание.
*
* @author upagge 07.06.2021
*/
public class SimpleFieldDto {
private final String fieldName;
private SimpleFieldDto(String fieldName) {
this.fieldName = fieldName;
}
public static SimpleFieldDto of(String fieldName) {
return new SimpleFieldDto(fieldName);
}
public String getFieldName() {
return fieldName;
}
}

View File

@ -7,7 +7,7 @@ public class ClassTableDto {
private String classPackage; private String classPackage;
private String className; private String className;
private String tableName; private String tableName;
private List<SimpleTableFieldDto> simpleFields; private List<SimpleFieldTableDto> simpleFields;
private List<JoinFieldDto> joinFields; private List<JoinFieldDto> joinFields;
private List<JoinElemCollectionDto> joinElemCollections; private List<JoinElemCollectionDto> joinElemCollections;
@ -35,11 +35,11 @@ public class ClassTableDto {
this.tableName = tableName; this.tableName = tableName;
} }
public List<SimpleTableFieldDto> getSimpleFields() { public List<SimpleFieldTableDto> getSimpleFields() {
return simpleFields; return simpleFields;
} }
public void setSimpleFields(List<SimpleTableFieldDto> simpleFields) { public void setSimpleFields(List<SimpleFieldTableDto> simpleFields) {
this.simpleFields = simpleFields; this.simpleFields = simpleFields;
} }

View File

@ -1,6 +1,6 @@
package dev.struchkov.haiti.utils.fieldconstants.domain.mode.table; package dev.struchkov.haiti.utils.fieldconstants.domain.mode.table;
import dev.struchkov.haiti.utils.Inspector; import dev.struchkov.haiti.utils.Assert;
public class JoinElemCollectionDto { public class JoinElemCollectionDto {
@ -15,7 +15,7 @@ public class JoinElemCollectionDto {
} }
public static JoinElemCollectionDto of(String fieldName, JoinTableContainer firstContainer, JoinTableContainer secondContainer) { public static JoinElemCollectionDto of(String fieldName, JoinTableContainer firstContainer, JoinTableContainer secondContainer) {
Inspector.isNotNull(fieldName, firstContainer, secondContainer); Assert.isNotNull(fieldName, firstContainer, secondContainer);
return new JoinElemCollectionDto(fieldName, firstContainer, secondContainer); return new JoinElemCollectionDto(fieldName, firstContainer, secondContainer);
} }

View File

@ -1,6 +1,6 @@
package dev.struchkov.haiti.utils.fieldconstants.domain.mode.table; package dev.struchkov.haiti.utils.fieldconstants.domain.mode.table;
import dev.struchkov.haiti.utils.Inspector; import dev.struchkov.haiti.utils.Assert;
public class JoinFieldDto { public class JoinFieldDto {
@ -13,7 +13,7 @@ public class JoinFieldDto {
} }
public static JoinFieldDto of(String fieldName, JoinTableContainer container) { public static JoinFieldDto of(String fieldName, JoinTableContainer container) {
Inspector.isNotNull(fieldName, container); Assert.isNotNull(fieldName, container);
return new JoinFieldDto(fieldName, container); return new JoinFieldDto(fieldName, container);
} }

View File

@ -1,6 +1,6 @@
package dev.struchkov.haiti.utils.fieldconstants.domain.mode.table; package dev.struchkov.haiti.utils.fieldconstants.domain.mode.table;
import dev.struchkov.haiti.utils.Inspector; import dev.struchkov.haiti.utils.Assert;
public class JoinTableContainer { public class JoinTableContainer {
@ -15,7 +15,7 @@ public class JoinTableContainer {
} }
public static JoinTableContainer of(String table, String baseId, String reference) { public static JoinTableContainer of(String table, String baseId, String reference) {
Inspector.isNotNull(table, baseId, reference); Assert.isNotNull(table, baseId, reference);
return new JoinTableContainer(table, baseId, reference); return new JoinTableContainer(table, baseId, reference);
} }

View File

@ -5,18 +5,18 @@ package dev.struchkov.haiti.utils.fieldconstants.domain.mode.table;
* *
* @author upagge 07.06.2021 * @author upagge 07.06.2021
*/ */
public class SimpleTableFieldDto { public class SimpleFieldTableDto {
private final String fieldStringName; private final String fieldStringName;
private final String fieldName; private final String fieldName;
private SimpleTableFieldDto(String fieldStringName, String fieldName) { private SimpleFieldTableDto(String fieldStringName, String fieldName) {
this.fieldStringName = fieldStringName; this.fieldStringName = fieldStringName;
this.fieldName = fieldName; this.fieldName = fieldName;
} }
public static SimpleTableFieldDto of(String fieldStringName, String fieldName) { public static SimpleFieldTableDto of(String fieldStringName, String fieldName) {
return new SimpleTableFieldDto(fieldStringName, fieldName); return new SimpleFieldTableDto(fieldStringName, fieldName);
} }
public String getFieldStringName() { public String getFieldStringName() {

View File

@ -1,45 +0,0 @@
package dev.struchkov.haiti.utils.fieldconstants.generator;
import dev.struchkov.haiti.utils.fieldconstants.creator.CreatorClassSimpleMode;
import dev.struchkov.haiti.utils.fieldconstants.domain.Mode;
import dev.struchkov.haiti.utils.fieldconstants.domain.mode.simple.ClassSimpleDto;
import dev.struchkov.haiti.utils.fieldconstants.domain.mode.simple.SimpleFieldDto;
import dev.struchkov.haiti.utils.fieldconstants.util.Generator;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Element;
import java.util.ArrayList;
import java.util.List;
public class GeneratorSimpleMode {
public static void generate(ProcessingEnvironment processingEnv, Element annotatedElement) {
final String annotatedElementName = annotatedElement.getSimpleName().toString();
final String newClassName = annotatedElementName + Mode.SIMPLE.getDefaultPostfix();
final List<? extends Element> allFields = annotatedElement.getEnclosedElements().stream()
.filter(Generator::isField)
.filter(Generator::isNotIgnoreField)
.toList();
final List<SimpleFieldDto> simpleFields = getSimpleFields(allFields);
final ClassSimpleDto newClass = new ClassSimpleDto();
newClass.setClassName(newClassName);
newClass.setSimpleFields(simpleFields);
newClass.setClassPackage(Generator.getPackage(annotatedElement));
CreatorClassSimpleMode.record(newClass, processingEnv);
}
private static List<SimpleFieldDto> getSimpleFields(List<? extends Element> allFields) {
final List<SimpleFieldDto> resultList = new ArrayList<>();
for (Element field : allFields) {
final String fieldName = field.getSimpleName().toString();
resultList.add(SimpleFieldDto.of(fieldName));
}
return resultList;
}
}

View File

@ -1,114 +0,0 @@
package dev.struchkov.haiti.utils.fieldconstants.generator;
import dev.struchkov.haiti.utils.fieldconstants.annotation.field.ElementCollectionField;
import dev.struchkov.haiti.utils.fieldconstants.annotation.field.JoinField;
import dev.struchkov.haiti.utils.fieldconstants.annotation.setting.TableModeSettings;
import dev.struchkov.haiti.utils.fieldconstants.creator.CreatorClassTableMode;
import dev.struchkov.haiti.utils.fieldconstants.domain.Mode;
import dev.struchkov.haiti.utils.fieldconstants.domain.mode.table.ClassTableDto;
import dev.struchkov.haiti.utils.fieldconstants.domain.mode.table.JoinElemCollectionDto;
import dev.struchkov.haiti.utils.fieldconstants.domain.mode.table.JoinFieldDto;
import dev.struchkov.haiti.utils.fieldconstants.domain.mode.table.JoinTableContainer;
import dev.struchkov.haiti.utils.fieldconstants.domain.mode.table.SimpleTableFieldDto;
import dev.struchkov.haiti.utils.fieldconstants.util.Generator;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Element;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Table;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import static dev.struchkov.haiti.utils.Checker.checkNotNull;
public class GeneratorTableMode {
public static void generate(ProcessingEnvironment processingEnv, TableModeSettings tableSettings, Element annotatedElement) {
final String annotatedElementName = annotatedElement.getSimpleName().toString();
final Table anTable = annotatedElement.getAnnotation(Table.class);
final String newClassName = annotatedElementName + Mode.TABLE.getDefaultPostfix();
final List<? extends Element> allFields = annotatedElement.getEnclosedElements().stream()
.filter(Generator::isField)
.filter(Generator::isNotIgnoreField)
.collect(Collectors.toList());
final List<SimpleTableFieldDto> simpleFields = getSimpleFields(allFields, anTable, tableSettings);
final List<JoinFieldDto> joinFields = getJoinFields(allFields);
final List<JoinElemCollectionDto> elementCollectionFields = getElementCollectionsFields(anTable, allFields);
final ClassTableDto newClass = new ClassTableDto();
newClass.setClassName(newClassName);
newClass.setSimpleFields(simpleFields);
newClass.setJoinFields(joinFields);
newClass.setJoinElemCollections(elementCollectionFields);
newClass.setClassPackage(Generator.getPackage(annotatedElement));
newClass.setTableName(checkNotNull(anTable) ? anTable.name() : null);
CreatorClassTableMode.record(newClass, processingEnv);
}
private static List<JoinElemCollectionDto> getElementCollectionsFields(Table tableName, List<? extends Element> allFields) {
if (checkNotNull(tableName)) {
return allFields.stream()
.filter(
field -> field.getAnnotation(ElementCollectionField.class) != null &&
field.getAnnotation(CollectionTable.class) != null &&
field.getAnnotation(Column.class) != null
)
.map(field -> {
final String fieldName = field.getSimpleName().toString();
final ElementCollectionField elementCollectionField = field.getAnnotation(ElementCollectionField.class);
final CollectionTable collectionTable = field.getAnnotation(CollectionTable.class);
final Column column = field.getAnnotation(Column.class);
final JoinTableContainer firstContainer = JoinTableContainer.of(collectionTable.name(), tableName + "." + elementCollectionField.parentId(), collectionTable.joinColumns()[0].name());
final JoinTableContainer secondContainer = JoinTableContainer.of(elementCollectionField.childTable(), column.name(), elementCollectionField.childReference());
return JoinElemCollectionDto.of(fieldName, firstContainer, secondContainer);
}).collect(Collectors.toList());
}
return Collections.emptyList();
}
private static List<JoinFieldDto> getJoinFields(List<? extends Element> allFields) {
return allFields.stream()
.filter(
field -> field.getAnnotation(JoinField.class) != null &&
field.getAnnotation(ElementCollection.class) == null
)
.map(field -> {
final String fieldName = field.getSimpleName().toString();
final JoinField joinField = field.getAnnotation(JoinField.class);
final JoinTableContainer joinContainer = JoinTableContainer.of(joinField.table(), joinField.baseId(), joinField.reference());
return JoinFieldDto.of(fieldName, joinContainer);
})
.collect(Collectors.toList());
}
private static List<SimpleTableFieldDto> getSimpleFields(List<? extends Element> allFields, Table anTable, TableModeSettings tableSettings) {
final boolean prefixTableForColumn = tableSettings.prefixTableForColumn();
final List<SimpleTableFieldDto> resultList = new ArrayList<>();
allFields.stream()
.filter(
field -> field.getAnnotation(Column.class) != null &&
field.getAnnotation(ElementCollection.class) == null
)
.forEach(
field -> {
final String fieldName = field.getSimpleName().toString();
final String columnName = field.getAnnotation(Column.class).name();
if (prefixTableForColumn && checkNotNull(anTable)) {
final String tableNameAndColumnName = anTable.name() + "." + columnName;
resultList.add(SimpleTableFieldDto.of("t_" + fieldName, tableNameAndColumnName));
}
resultList.add(SimpleTableFieldDto.of(fieldName, columnName));
}
);
return resultList;
}
}

View File

@ -1,11 +1,17 @@
package dev.struchkov.haiti.utils.fieldconstants.processor; package dev.struchkov.haiti.utils.fieldconstants.processor;
import com.google.auto.service.AutoService; import com.google.auto.service.AutoService;
import dev.struchkov.haiti.utils.fieldconstants.CreatorClassTableMode;
import dev.struchkov.haiti.utils.fieldconstants.annotation.FieldNames; import dev.struchkov.haiti.utils.fieldconstants.annotation.FieldNames;
import dev.struchkov.haiti.utils.fieldconstants.annotation.setting.TableModeSettings; import dev.struchkov.haiti.utils.fieldconstants.annotation.field.ElementCollectionField;
import dev.struchkov.haiti.utils.fieldconstants.annotation.field.IgnoreField;
import dev.struchkov.haiti.utils.fieldconstants.annotation.field.JoinField;
import dev.struchkov.haiti.utils.fieldconstants.domain.Mode; import dev.struchkov.haiti.utils.fieldconstants.domain.Mode;
import dev.struchkov.haiti.utils.fieldconstants.generator.GeneratorSimpleMode; import dev.struchkov.haiti.utils.fieldconstants.domain.mode.table.ClassTableDto;
import dev.struchkov.haiti.utils.fieldconstants.generator.GeneratorTableMode; import dev.struchkov.haiti.utils.fieldconstants.domain.mode.table.JoinElemCollectionDto;
import dev.struchkov.haiti.utils.fieldconstants.domain.mode.table.JoinFieldDto;
import dev.struchkov.haiti.utils.fieldconstants.domain.mode.table.JoinTableContainer;
import dev.struchkov.haiti.utils.fieldconstants.domain.mode.table.SimpleFieldTableDto;
import javax.annotation.processing.AbstractProcessor; import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Processor; import javax.annotation.processing.Processor;
@ -15,10 +21,18 @@ 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.TypeElement; import javax.lang.model.element.TypeElement;
import javax.lang.model.type.TypeMirror;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Table;
import java.util.Arrays;
import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors;
@SupportedAnnotationTypes("dev.struchkov.haiti.utils.fieldconstants.annotation.FieldNames") @SupportedAnnotationTypes("dev.struchkov.haiti.utils.fieldconstants.annotation.FieldNames")
@SupportedSourceVersion(SourceVersion.RELEASE_17) @SupportedSourceVersion(SourceVersion.RELEASE_11)
@AutoService(Processor.class) @AutoService(Processor.class)
public class FieldNameProcessor extends AbstractProcessor { public class FieldNameProcessor extends AbstractProcessor {
@ -27,20 +41,102 @@ public class FieldNameProcessor extends AbstractProcessor {
for (TypeElement annotation : set) { for (TypeElement annotation : set) {
Set<? extends Element> annotatedElements = roundEnvironment.getElementsAnnotatedWith(annotation); Set<? extends Element> annotatedElements = roundEnvironment.getElementsAnnotatedWith(annotation);
for (Element annotatedElement : annotatedElements) { for (Element annotatedElement : annotatedElements) {
final TypeMirror mirror = annotatedElement.asType();
final String annotatedElementName = annotatedElement.getSimpleName().toString();
final FieldNames settings = annotatedElement.getAnnotation(FieldNames.class); final FieldNames settings = annotatedElement.getAnnotation(FieldNames.class);
final Set<Mode> modes = Set.of(settings.mode()); final Set<Mode> modes = Arrays.stream(settings.mode()).collect(Collectors.toUnmodifiableSet());
if (modes.contains(Mode.TABLE)) { if (modes.contains(Mode.TABLE)) {
final TableModeSettings tableSettings = settings.tableSettings(); generateTableMode(annotatedElement, mirror, annotatedElementName);
GeneratorTableMode.generate(processingEnv, tableSettings, annotatedElement);
}
if (modes.contains(Mode.SIMPLE)) {
GeneratorSimpleMode.generate(processingEnv, annotatedElement);
} }
} }
} }
return true; return true;
} }
private void generateTableMode(Element annotatedElement, TypeMirror mirror, String annotatedElementName) {
final Table anTable = annotatedElement.getAnnotation(Table.class);
final String newClassName = annotatedElementName + Mode.TABLE.getDefaultPostfix();
final List<? extends Element> allFields = annotatedElement.getEnclosedElements().stream()
.filter(this::isField)
.filter(this::isNotIgnoreField)
.collect(Collectors.toList());
final List<SimpleFieldTableDto> simpleFields = getSimpleFields(allFields);
final List<JoinFieldDto> joinFields = getJoinFields(allFields);
final List<JoinElemCollectionDto> elementCollectionFields = getElementCollectionsFields(allFields);
final ClassTableDto newClass = new ClassTableDto();
newClass.setClassName(newClassName);
newClass.setSimpleFields(simpleFields);
newClass.setJoinFields(joinFields);
newClass.setJoinElemCollections(elementCollectionFields);
newClass.setClassPackage(getPackage(mirror));
newClass.setTableName(anTable.name());
CreatorClassTableMode.record(newClass, processingEnv);
}
private List<JoinElemCollectionDto> getElementCollectionsFields(List<? extends Element> allFields) {
return allFields.stream()
.filter(
field -> field.getAnnotation(ElementCollectionField.class) != null &&
field.getAnnotation(CollectionTable.class) != null &&
field.getAnnotation(Column.class) != null
)
.map(field -> {
final String fieldName = field.getSimpleName().toString();
final ElementCollectionField elementCollectionField = field.getAnnotation(ElementCollectionField.class);
final CollectionTable collectionTable = field.getAnnotation(CollectionTable.class);
final Column column = field.getAnnotation(Column.class);
final JoinTableContainer firstContainer = JoinTableContainer.of(collectionTable.name(), elementCollectionField.parentId(), collectionTable.joinColumns()[0].name());
final JoinTableContainer secondContainer = JoinTableContainer.of(elementCollectionField.childTable(), column.name(), elementCollectionField.childReference());
return JoinElemCollectionDto.of(fieldName, firstContainer, secondContainer);
}).collect(Collectors.toList());
}
private List<JoinFieldDto> getJoinFields(List<? extends Element> allFields) {
return allFields.stream()
.filter(
field -> field.getAnnotation(JoinField.class) != null &&
field.getAnnotation(ElementCollection.class) == null
)
.map(field -> {
final String fieldName = field.getSimpleName().toString();
final JoinField joinField = field.getAnnotation(JoinField.class);
final JoinTableContainer joinContainer = JoinTableContainer.of(joinField.table(), joinField.baseId(), joinField.reference());
return JoinFieldDto.of(fieldName, joinContainer);
})
.collect(Collectors.toList());
}
private List<SimpleFieldTableDto> getSimpleFields(List<? extends Element> allFields) {
return allFields.stream()
.filter(
field -> field.getAnnotation(Column.class) != null &&
field.getAnnotation(ElementCollection.class) == null
)
.map(
field -> {
final String fieldName = field.getSimpleName().toString();
final String columnName = field.getAnnotation(Column.class).name();
return SimpleFieldTableDto.of(fieldName, columnName);
}
).collect(Collectors.toList());
}
private boolean isNotIgnoreField(Element element) {
return element.getAnnotation(IgnoreField.class) == null;
}
public boolean isField(Element element) {
return element != null && element.getKind().isField();
}
public static String getPackage(TypeMirror typeMirror) {
final String[] split = typeMirror.toString().split("\\.");
return String.join(".", Arrays.copyOf(split, split.length - 1));
}
} }

View File

@ -1,26 +0,0 @@
package dev.struchkov.haiti.utils.fieldconstants.util;
import dev.struchkov.haiti.utils.fieldconstants.annotation.field.IgnoreField;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
public class Generator {
public static boolean isNotIgnoreField(Element element) {
return element.getAnnotation(IgnoreField.class) == null;
}
public static boolean isField(Element element) {
return element != null && element.getKind().isField();
}
public static String getPackage(Element element) {
Element packageElem = element.getEnclosingElement();
while (!ElementKind.PACKAGE.equals(packageElem.getKind())) {
packageElem = packageElem.getEnclosingElement();
}
return packageElem.asType().toString();
}
}

View File

@ -1,8 +1,7 @@
module haiti.utils.fieldconstants { module haiti.utils.fieldconstants {
exports dev.struchkov.haiti.utils.fieldconstants.annotation; exports dev.struchkov.haiti.utils.fieldconstants.annotation;
exports dev.struchkov.haiti.utils.fieldconstants.annotation.field;
exports dev.struchkov.haiti.utils.fieldconstants.annotation.setting;
exports dev.struchkov.haiti.utils.fieldconstants.domain; exports dev.struchkov.haiti.utils.fieldconstants.domain;
exports dev.struchkov.haiti.utils.fieldconstants.annotation.field;
requires java.compiler; requires java.compiler;
requires com.google.auto.service; requires com.google.auto.service;