release-0.0.2

This commit is contained in:
Struchkov Mark 2022-02-17 12:29:28 +03:00
parent 26e0aee9d7
commit fc3d2883a9
16 changed files with 416 additions and 142 deletions

View File

@ -11,7 +11,7 @@
<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.1</version> <version>0.0.2</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>

View File

@ -1,66 +0,0 @@
package dev.struchkov.haiti.utils.fieldconstants;
import dev.struchkov.haiti.utils.fieldconstants.domain.ClassDto;
import dev.struchkov.haiti.utils.fieldconstants.domain.FieldDto;
import javax.annotation.processing.ProcessingEnvironment;
import javax.tools.JavaFileObject;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Set;
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 ClassCreator {
private static final String TABLE_NAME_DEFAULT = "TABLE_NAME";
private static final String TABLE_NAME_DB = "DB_TABLE_NAME";
private ClassCreator() {
utilityClass();
}
public static void record(ClassDto 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.print("public class " + classDto.getClassName() + " {");
out.println();
out.println();
generateTableName(classDto.getTableName(), classDto.getFields(), out);
generateNames(classDto.getFields(), out);
out.println();
out.println("}");
out.println();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void generateTableName(String tableName, Set<FieldDto> fields, PrintWriter out) {
if (tableName != null) {
final boolean isTableNameField = fields.stream().anyMatch(fieldDto -> fieldDto.getFieldStringName().equalsIgnoreCase(TABLE_NAME_DEFAULT));
out.println(format(" public static final String {0} = \"{1}\";", isTableNameField ? TABLE_NAME_DB : TABLE_NAME_DEFAULT, tableName));
}
}
private static void generateNames(Set<FieldDto> fields, PrintWriter out) {
for (FieldDto field : fields) {
out.println(format(" public static final String {0} = \"{1}\";", field.getFieldStringName(), field.getFieldName()));
}
}
}

View File

@ -0,0 +1,107 @@
package dev.struchkov.haiti.utils.fieldconstants;
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 javax.annotation.processing.ProcessingEnvironment;
import javax.tools.JavaFileObject;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.List;
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 CreatorClassTableMode {
private static final String TABLE_NAME_DEFAULT = "TABLE_NAME";
private static final String TABLE_NAME_DB = "DB_TABLE_NAME";
private CreatorClassTableMode() {
utilityClass();
}
public static void record(ClassTableDto 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();
generateImports(classDto.getJoinFields(), classDto.getJoinElemCollections(), out);
out.println();
out.print(format("public class {0} '{'", classDto.getClassName()));
out.println();
out.println();
generateTableName(classDto.getTableName(), classDto.getSimpleFields(), out);
generateSimpleNames(classDto.getSimpleFields(), out);
generateJoinNames(classDto.getJoinFields(), out);
generateElementCollectionNames(classDto.getJoinElemCollections(), out);
out.println("}");
out.println();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void generateImports(List<JoinFieldDto> joinFields, List<JoinElemCollectionDto> joinElemCollections, PrintWriter out) {
if ((joinFields != null && !joinFields.isEmpty()) || (joinElemCollections != null && !joinElemCollections.isEmpty())) {
out.println("import dev.struchkov.haiti.filter.jooq.join.JoinTable;");
}
}
private static void generateTableName(String tableName, Collection<SimpleFieldTableDto> 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();
}
private static void generateSimpleNames(Collection<SimpleFieldTableDto> fields, PrintWriter out) {
for (SimpleFieldTableDto field : fields) {
out.println(format(" public static final String {0} = \"{1}\";", field.getFieldStringName(), field.getFieldName()));
}
out.println();
}
private static void generateJoinNames(List<JoinFieldDto> joinFields, PrintWriter out) {
for (JoinFieldDto joinField : joinFields) {
final JoinTableContainer container = joinField.getContainer();
out.println(
format(
" public static final JoinTable {0} = JoinTable.ofLeft(\"{1}\", \"{2}\", \"{3}\");",
joinField.getFieldName(), container.getTable(), container.getBaseId(), container.getReference()
)
);
}
out.println();
}
private static void generateElementCollectionNames(List<JoinElemCollectionDto> joinElemCollections, PrintWriter out) {
for (JoinElemCollectionDto element : joinElemCollections) {
final String fieldName = element.getFieldName();
final JoinTableContainer firstContainer = element.getFirstContainer();
final JoinTableContainer secondContainer = element.getSecondContainer();
out.println(format(" public static final JoinTable[] {0} = '{'", fieldName));
out.println(format(" JoinTable.ofLeft(\"{0}\", \"{1}\", \"{2}\"),", firstContainer.getTable(), firstContainer.getBaseId(), firstContainer.getReference()));
out.println(format(" JoinTable.ofLeft(\"{0}\", \"{1}\", \"{2}\")", secondContainer.getTable(), secondContainer.getBaseId(), secondContainer.getReference()));
out.println(" };");
}
out.println();
}
}

View File

@ -1,5 +1,7 @@
package dev.struchkov.haiti.utils.fieldconstants.annotation; package dev.struchkov.haiti.utils.fieldconstants.annotation;
import dev.struchkov.haiti.utils.fieldconstants.domain.Mode;
import java.lang.annotation.ElementType; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
@ -9,6 +11,6 @@ import java.lang.annotation.Target;
@Retention(RetentionPolicy.CLASS) @Retention(RetentionPolicy.CLASS)
public @interface FieldNames { public @interface FieldNames {
String postfix() default "Fields"; Mode[] mode() default {Mode.SIMPLE};
} }

View File

@ -0,0 +1,16 @@
package dev.struchkov.haiti.utils.fieldconstants.annotation.field;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.CLASS)
public @interface ElementCollectionField {
String parentId();
String childTable();
String childReference();
}

View File

@ -1,11 +1,11 @@
package dev.struchkov.haiti.utils.fieldconstants.annotation; package dev.struchkov.haiti.utils.fieldconstants.annotation.field;
import java.lang.annotation.ElementType; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
@Target(ElementType.TYPE) @Target(ElementType.FIELD)
@Retention(RetentionPolicy.CLASS) @Retention(RetentionPolicy.CLASS)
public @interface IgnoreField { public @interface IgnoreField {

View File

@ -0,0 +1,16 @@
package dev.struchkov.haiti.utils.fieldconstants.annotation.field;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.CLASS)
public @interface JoinField {
String table();
String baseId();
String reference();
}

View File

@ -1,44 +0,0 @@
package dev.struchkov.haiti.utils.fieldconstants.domain;
import java.util.Set;
public class ClassDto {
private String classPackage;
private String className;
private String tableName;
private Set<FieldDto> fields;
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public String getClassPackage() {
return classPackage;
}
public void setClassPackage(String classPackage) {
this.classPackage = classPackage;
}
public String getTableName() {
return tableName;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
public Set<FieldDto> getFields() {
return fields;
}
public void setFields(Set<FieldDto> fields) {
this.fields = fields;
}
}

View File

@ -0,0 +1,17 @@
package dev.struchkov.haiti.utils.fieldconstants.domain;
public enum Mode {
SIMPLE("Fields"), TABLE("Columns");
private final String defaultPostfix;
Mode(String defaultPostfix) {
this.defaultPostfix = defaultPostfix;
}
public String getDefaultPostfix() {
return defaultPostfix;
}
}

View File

@ -0,0 +1,62 @@
package dev.struchkov.haiti.utils.fieldconstants.domain.mode.table;
import java.util.List;
public class ClassTableDto {
private String classPackage;
private String className;
private String tableName;
private List<SimpleFieldTableDto> simpleFields;
private List<JoinFieldDto> joinFields;
private List<JoinElemCollectionDto> joinElemCollections;
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<SimpleFieldTableDto> getSimpleFields() {
return simpleFields;
}
public void setSimpleFields(List<SimpleFieldTableDto> simpleFields) {
this.simpleFields = simpleFields;
}
public List<JoinFieldDto> getJoinFields() {
return joinFields;
}
public void setJoinFields(List<JoinFieldDto> joinFields) {
this.joinFields = joinFields;
}
public List<JoinElemCollectionDto> getJoinElemCollections() {
return joinElemCollections;
}
public void setJoinElemCollections(List<JoinElemCollectionDto> joinElemCollections) {
this.joinElemCollections = joinElemCollections;
}
}

View File

@ -0,0 +1,34 @@
package dev.struchkov.haiti.utils.fieldconstants.domain.mode.table;
import dev.struchkov.haiti.utils.Assert;
public class JoinElemCollectionDto {
private String fieldName;
private JoinTableContainer firstContainer;
private JoinTableContainer secondContainer;
private JoinElemCollectionDto(String fieldName, JoinTableContainer firstContainer, JoinTableContainer secondContainer) {
this.fieldName = fieldName;
this.firstContainer = firstContainer;
this.secondContainer = secondContainer;
}
public static JoinElemCollectionDto of(String fieldName, JoinTableContainer firstContainer, JoinTableContainer secondContainer) {
Assert.isNotNull(fieldName, firstContainer, secondContainer);
return new JoinElemCollectionDto(fieldName, firstContainer, secondContainer);
}
public String getFieldName() {
return fieldName;
}
public JoinTableContainer getFirstContainer() {
return firstContainer;
}
public JoinTableContainer getSecondContainer() {
return secondContainer;
}
}

View File

@ -0,0 +1,28 @@
package dev.struchkov.haiti.utils.fieldconstants.domain.mode.table;
import dev.struchkov.haiti.utils.Assert;
public class JoinFieldDto {
private String fieldName;
private JoinTableContainer container;
private JoinFieldDto(String fieldName, JoinTableContainer container) {
this.fieldName = fieldName;
this.container = container;
}
public static JoinFieldDto of(String fieldName, JoinTableContainer container) {
Assert.isNotNull(fieldName, container);
return new JoinFieldDto(fieldName, container);
}
public String getFieldName() {
return fieldName;
}
public JoinTableContainer getContainer() {
return container;
}
}

View File

@ -0,0 +1,34 @@
package dev.struchkov.haiti.utils.fieldconstants.domain.mode.table;
import dev.struchkov.haiti.utils.Assert;
public class JoinTableContainer {
private String table;
private String baseId;
private String reference;
private JoinTableContainer(String table, String baseId, String reference) {
this.table = table;
this.baseId = baseId;
this.reference = reference;
}
public static JoinTableContainer of(String table, String baseId, String reference) {
Assert.isNotNull(table, baseId, reference);
return new JoinTableContainer(table, baseId, reference);
}
public String getTable() {
return table;
}
public String getBaseId() {
return baseId;
}
public String getReference() {
return reference;
}
}

View File

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

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.ClassCreator; 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.IgnoreField; import dev.struchkov.haiti.utils.fieldconstants.annotation.field.ElementCollectionField;
import dev.struchkov.haiti.utils.fieldconstants.domain.ClassDto; import dev.struchkov.haiti.utils.fieldconstants.annotation.field.IgnoreField;
import dev.struchkov.haiti.utils.fieldconstants.domain.FieldDto; 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.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 javax.annotation.processing.AbstractProcessor; import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Processor; import javax.annotation.processing.Processor;
@ -16,9 +22,12 @@ 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.lang.model.type.TypeMirror;
import javax.persistence.CollectionTable;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Table; import javax.persistence.Table;
import java.util.Arrays; import java.util.Arrays;
import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -35,31 +44,88 @@ public class FieldNameProcessor extends AbstractProcessor {
final TypeMirror mirror = annotatedElement.asType(); final TypeMirror mirror = annotatedElement.asType();
final String annotatedElementName = annotatedElement.getSimpleName().toString(); final String annotatedElementName = annotatedElement.getSimpleName().toString();
final FieldNames settings = annotatedElement.getAnnotation(FieldNames.class); final FieldNames settings = annotatedElement.getAnnotation(FieldNames.class);
final Table anTable = annotatedElement.getAnnotation(Table.class);
final String newClassName = annotatedElementName + settings.postfix();
final Set<FieldDto> fields = annotatedElement.getEnclosedElements().stream() final Set<Mode> modes = Arrays.stream(settings.mode()).collect(Collectors.toUnmodifiableSet());
.filter(this::isField) if (modes.contains(Mode.TABLE)) {
.filter(this::isNotIgnoreField) generateTableMode(annotatedElement, mirror, annotatedElementName);
.map(
element -> {
final String fieldName = element.getSimpleName().toString();
final String columnName = element.getAnnotation(Column.class).name();
return FieldDto.of(fieldName, columnName);
} }
).collect(Collectors.toSet());
final ClassDto newClass = new ClassDto();
newClass.setClassName(newClassName);
newClass.setFields(fields);
newClass.setClassPackage(getPackage(mirror));
newClass.setTableName(anTable.name());
ClassCreator.record(newClass, processingEnv);
} }
} }
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) { private boolean isNotIgnoreField(Element element) {
return element.getAnnotation(IgnoreField.class) == null; return element.getAnnotation(IgnoreField.class) == null;
} }

View File

@ -1,5 +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.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;