Compare commits
10 Commits
bedeb1a51e
...
4563ce2c4e
Author | SHA1 | Date | |
---|---|---|---|
4563ce2c4e | |||
e759449bc6 | |||
|
9f41dde9ec | ||
|
6d707cf8c8 | ||
|
e7420705b8 | ||
|
ed5d5bd91f | ||
|
b06535eb2e | ||
|
feba6d12a6 | ||
|
2fa7cf5066 | ||
|
e55eae854e |
8
pom.xml
8
pom.xml
@ -5,13 +5,13 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>dev.struchkov.haiti</groupId>
|
||||
<artifactId>haiti</artifactId>
|
||||
<version>0.0.4</version>
|
||||
<artifactId>haiti-dependencies</artifactId>
|
||||
<version>2.0.0</version>
|
||||
</parent>
|
||||
|
||||
<groupId>dev.struchkov.haiti.utils</groupId>
|
||||
<artifactId>haiti-utils-field-constants</artifactId>
|
||||
<version>0.0.3-SNAPSHOT</version>
|
||||
<version>0.0.6</version>
|
||||
|
||||
<name>Haiti Field Name Constants Utils</name>
|
||||
<description>Generating class field names</description>
|
||||
@ -22,7 +22,7 @@
|
||||
</issueManagement>
|
||||
|
||||
<properties>
|
||||
<java.version>11</java.version>
|
||||
<java.version>17</java.version>
|
||||
<maven.compiler.source>${java.version}</maven.compiler.source>
|
||||
<maven.compiler.target>${java.version}</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
|
@ -1,5 +1,6 @@
|
||||
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 java.lang.annotation.ElementType;
|
||||
@ -13,4 +14,6 @@ public @interface FieldNames {
|
||||
|
||||
Mode[] mode() default {Mode.SIMPLE};
|
||||
|
||||
TableModeSettings tableSettings() default @TableModeSettings(prefixTableForColumn = false);
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,7 @@
|
||||
package dev.struchkov.haiti.utils.fieldconstants.annotation.setting;
|
||||
|
||||
public @interface TableModeSettings {
|
||||
|
||||
boolean prefixTableForColumn();
|
||||
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
@ -1,10 +1,11 @@
|
||||
package dev.struchkov.haiti.utils.fieldconstants;
|
||||
package dev.struchkov.haiti.utils.fieldconstants.creator;
|
||||
|
||||
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.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 dev.struchkov.haiti.utils.fieldconstants.domain.mode.table.SimpleTableFieldDto;
|
||||
|
||||
import javax.annotation.processing.ProcessingEnvironment;
|
||||
import javax.tools.JavaFileObject;
|
||||
@ -13,6 +14,7 @@ import java.io.PrintWriter;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import static dev.struchkov.haiti.utils.Checker.checkNotEmpty;
|
||||
import static dev.struchkov.haiti.utils.Exceptions.utilityClass;
|
||||
import static java.text.MessageFormat.format;
|
||||
|
||||
@ -33,7 +35,7 @@ public final class CreatorClassTableMode {
|
||||
public static void record(ClassTableDto classDto, ProcessingEnvironment environment) {
|
||||
JavaFileObject builderFile = null;
|
||||
try {
|
||||
builderFile = environment.getFiler().createSourceFile(classDto.getClassName());
|
||||
builderFile = environment.getFiler().createSourceFile(classDto.getClassPackage() +"." + classDto.getClassName());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@ -63,19 +65,21 @@ public final class CreatorClassTableMode {
|
||||
}
|
||||
}
|
||||
|
||||
private static void generateTableName(String tableName, Collection<SimpleFieldTableDto> fields, PrintWriter out) {
|
||||
private static void generateTableName(String tableName, Collection<SimpleTableFieldDto> fields, PrintWriter out) {
|
||||
if (tableName != null) {
|
||||
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();
|
||||
}
|
||||
out.println();
|
||||
}
|
||||
|
||||
private static void generateSimpleNames(Collection<SimpleFieldTableDto> fields, PrintWriter out) {
|
||||
for (SimpleFieldTableDto field : fields) {
|
||||
private static void generateSimpleNames(Collection<SimpleTableFieldDto> fields, PrintWriter out) {
|
||||
for (SimpleTableFieldDto field : fields) {
|
||||
out.println(format(" public static final String {0} = \"{1}\";", field.getFieldStringName(), field.getFieldName()));
|
||||
}
|
||||
out.println();
|
||||
if (checkNotEmpty(fields)) {
|
||||
out.println();
|
||||
}
|
||||
}
|
||||
|
||||
private static void generateJoinNames(List<JoinFieldDto> joinFields, PrintWriter out) {
|
||||
@ -88,7 +92,9 @@ public final class CreatorClassTableMode {
|
||||
)
|
||||
);
|
||||
}
|
||||
out.println();
|
||||
if (checkNotEmpty(joinFields)) {
|
||||
out.println();
|
||||
}
|
||||
}
|
||||
|
||||
private static void generateElementCollectionNames(List<JoinElemCollectionDto> joinElemCollections, PrintWriter out) {
|
||||
@ -101,7 +107,9 @@ public final class CreatorClassTableMode {
|
||||
out.println(format(" JoinTable.ofLeft(\"{0}\", \"{1}\", \"{2}\")", secondContainer.getTable(), secondContainer.getBaseId(), secondContainer.getReference()));
|
||||
out.println(" };");
|
||||
}
|
||||
out.println();
|
||||
if (checkNotEmpty(joinElemCollections)) {
|
||||
out.println();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
@ -7,7 +7,7 @@ public class ClassTableDto {
|
||||
private String classPackage;
|
||||
private String className;
|
||||
private String tableName;
|
||||
private List<SimpleFieldTableDto> simpleFields;
|
||||
private List<SimpleTableFieldDto> simpleFields;
|
||||
private List<JoinFieldDto> joinFields;
|
||||
private List<JoinElemCollectionDto> joinElemCollections;
|
||||
|
||||
@ -35,11 +35,11 @@ public class ClassTableDto {
|
||||
this.tableName = tableName;
|
||||
}
|
||||
|
||||
public List<SimpleFieldTableDto> getSimpleFields() {
|
||||
public List<SimpleTableFieldDto> getSimpleFields() {
|
||||
return simpleFields;
|
||||
}
|
||||
|
||||
public void setSimpleFields(List<SimpleFieldTableDto> simpleFields) {
|
||||
public void setSimpleFields(List<SimpleTableFieldDto> simpleFields) {
|
||||
this.simpleFields = simpleFields;
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
package dev.struchkov.haiti.utils.fieldconstants.domain.mode.table;
|
||||
|
||||
import dev.struchkov.haiti.utils.Assert;
|
||||
import dev.struchkov.haiti.utils.Inspector;
|
||||
|
||||
public class JoinElemCollectionDto {
|
||||
|
||||
@ -15,7 +15,7 @@ public class JoinElemCollectionDto {
|
||||
}
|
||||
|
||||
public static JoinElemCollectionDto of(String fieldName, JoinTableContainer firstContainer, JoinTableContainer secondContainer) {
|
||||
Assert.isNotNull(fieldName, firstContainer, secondContainer);
|
||||
Inspector.isNotNull(fieldName, firstContainer, secondContainer);
|
||||
return new JoinElemCollectionDto(fieldName, firstContainer, secondContainer);
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
package dev.struchkov.haiti.utils.fieldconstants.domain.mode.table;
|
||||
|
||||
import dev.struchkov.haiti.utils.Assert;
|
||||
import dev.struchkov.haiti.utils.Inspector;
|
||||
|
||||
public class JoinFieldDto {
|
||||
|
||||
@ -13,7 +13,7 @@ public class JoinFieldDto {
|
||||
}
|
||||
|
||||
public static JoinFieldDto of(String fieldName, JoinTableContainer container) {
|
||||
Assert.isNotNull(fieldName, container);
|
||||
Inspector.isNotNull(fieldName, container);
|
||||
return new JoinFieldDto(fieldName, container);
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
package dev.struchkov.haiti.utils.fieldconstants.domain.mode.table;
|
||||
|
||||
import dev.struchkov.haiti.utils.Assert;
|
||||
import dev.struchkov.haiti.utils.Inspector;
|
||||
|
||||
public class JoinTableContainer {
|
||||
|
||||
@ -15,7 +15,7 @@ public class JoinTableContainer {
|
||||
}
|
||||
|
||||
public static JoinTableContainer of(String table, String baseId, String reference) {
|
||||
Assert.isNotNull(table, baseId, reference);
|
||||
Inspector.isNotNull(table, baseId, reference);
|
||||
return new JoinTableContainer(table, baseId, reference);
|
||||
}
|
||||
|
||||
|
@ -5,18 +5,18 @@ package dev.struchkov.haiti.utils.fieldconstants.domain.mode.table;
|
||||
*
|
||||
* @author upagge 07.06.2021
|
||||
*/
|
||||
public class SimpleFieldTableDto {
|
||||
public class SimpleTableFieldDto {
|
||||
|
||||
private final String fieldStringName;
|
||||
private final String fieldName;
|
||||
|
||||
private SimpleFieldTableDto(String fieldStringName, String fieldName) {
|
||||
private SimpleTableFieldDto(String fieldStringName, String fieldName) {
|
||||
this.fieldStringName = fieldStringName;
|
||||
this.fieldName = fieldName;
|
||||
}
|
||||
|
||||
public static SimpleFieldTableDto of(String fieldStringName, String fieldName) {
|
||||
return new SimpleFieldTableDto(fieldStringName, fieldName);
|
||||
public static SimpleTableFieldDto of(String fieldStringName, String fieldName) {
|
||||
return new SimpleTableFieldDto(fieldStringName, fieldName);
|
||||
}
|
||||
|
||||
public String getFieldStringName() {
|
@ -0,0 +1,45 @@
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,114 @@
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
@ -1,17 +1,11 @@
|
||||
package dev.struchkov.haiti.utils.fieldconstants.processor;
|
||||
|
||||
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.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.annotation.setting.TableModeSettings;
|
||||
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.SimpleFieldTableDto;
|
||||
import dev.struchkov.haiti.utils.fieldconstants.generator.GeneratorSimpleMode;
|
||||
import dev.struchkov.haiti.utils.fieldconstants.generator.GeneratorTableMode;
|
||||
|
||||
import javax.annotation.processing.AbstractProcessor;
|
||||
import javax.annotation.processing.Processor;
|
||||
@ -21,18 +15,10 @@ import javax.annotation.processing.SupportedSourceVersion;
|
||||
import javax.lang.model.SourceVersion;
|
||||
import javax.lang.model.element.Element;
|
||||
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.stream.Collectors;
|
||||
|
||||
@SupportedAnnotationTypes("dev.struchkov.haiti.utils.fieldconstants.annotation.FieldNames")
|
||||
@SupportedSourceVersion(SourceVersion.RELEASE_11)
|
||||
@SupportedSourceVersion(SourceVersion.RELEASE_17)
|
||||
@AutoService(Processor.class)
|
||||
public class FieldNameProcessor extends AbstractProcessor {
|
||||
|
||||
@ -41,102 +27,20 @@ public class FieldNameProcessor extends AbstractProcessor {
|
||||
for (TypeElement annotation : set) {
|
||||
Set<? extends Element> annotatedElements = roundEnvironment.getElementsAnnotatedWith(annotation);
|
||||
for (Element annotatedElement : annotatedElements) {
|
||||
final TypeMirror mirror = annotatedElement.asType();
|
||||
final String annotatedElementName = annotatedElement.getSimpleName().toString();
|
||||
final FieldNames settings = annotatedElement.getAnnotation(FieldNames.class);
|
||||
|
||||
final Set<Mode> modes = Arrays.stream(settings.mode()).collect(Collectors.toUnmodifiableSet());
|
||||
final Set<Mode> modes = Set.of(settings.mode());
|
||||
if (modes.contains(Mode.TABLE)) {
|
||||
generateTableMode(annotatedElement, mirror, annotatedElementName);
|
||||
final TableModeSettings tableSettings = settings.tableSettings();
|
||||
GeneratorTableMode.generate(processingEnv, tableSettings, annotatedElement);
|
||||
}
|
||||
if (modes.contains(Mode.SIMPLE)) {
|
||||
GeneratorSimpleMode.generate(processingEnv, annotatedElement);
|
||||
}
|
||||
}
|
||||
}
|
||||
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));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,26 @@
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
@ -1,7 +1,8 @@
|
||||
module haiti.utils.fieldconstants {
|
||||
exports dev.struchkov.haiti.utils.fieldconstants.annotation;
|
||||
exports dev.struchkov.haiti.utils.fieldconstants.domain;
|
||||
exports dev.struchkov.haiti.utils.fieldconstants.annotation.field;
|
||||
exports dev.struchkov.haiti.utils.fieldconstants.annotation.setting;
|
||||
exports dev.struchkov.haiti.utils.fieldconstants.domain;
|
||||
|
||||
requires java.compiler;
|
||||
requires com.google.auto.service;
|
||||
|
Loading…
Reference in New Issue
Block a user