commit 5718fff3140aac80b1b03985816bdd509eefc0a4 Author: Struchkov Mark Date: Sun Feb 13 15:36:31 2022 +0300 InitCommit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a0d6e2d --- /dev/null +++ b/.gitignore @@ -0,0 +1,85 @@ +*.class +*.log +*.ctxt +.mtj.tmp/ +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar +hs_err_pid* +target/ +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next +release.properties +dependency-reduced-pom.xml +buildNumber.properties +.mvn/timing.properties +.mvn/wrapper/maven-wrapper.jar +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf +.idea/**/contentModel.xml +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml +.idea/**/gradle.xml +.idea/**/libraries +cmake-build-*/ +.idea/**/mongoSettings.xml +*.iws +out/ +.idea_modules/ +atlassian-ide-plugin.xml +.idea/replstate.xml +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties +.idea/httpRequests +.idea/caches/build_file_checksums.ser +*~ +.fuse_hidden* +.directory +.Trash-* +.nfs* +Thumbs.db +Thumbs.db:encryptable +ehthumbs.db +ehthumbs_vista.db +*.stackdump +[Dd]esktop.ini +$RECYCLE.BIN/ +*.cab +*.msi +*.msix +*.msm +*.msp +*.lnk +.DS_Store +.AppleDouble +.LSOverride +Icon +._* +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..6691e3b --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,17 @@ +image: maven:3.8.4-openjdk-11 +variables: + MAVEN_OPTS: "-Dmaven.repo.local=./.m2/repository" + +stages: + - deploy + +deploy: + stage: deploy + only: + - /^release-.*$/ + except: + - branches + before_script: + - gpg --pinentry-mode loopback --passphrase $GPG_PASSPHRASE --import $GPG_PRIVATE_KEY + script: + - 'mvn --settings $MAVEN_SETTINGS -U -P ossrh,release clean deploy' \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..a390257 --- /dev/null +++ b/pom.xml @@ -0,0 +1,79 @@ + + + 4.0.0 + + dev.struchkov.haiti + haiti + 0.0.4 + + + dev.struchkov.haiti.utils + haiti-utils-field-constants + 0.0.1-SNAPSHOT + + Haiti Field Name Constants Utils + Generating class field names + https://github.com/haiti-projects/haiti-utils-field-constants + + GitHub + https://github.com/haiti-projects/haiti-utils-field-constants/issues + + + + 11 + ${java.version} + ${java.version} + UTF-8 + UTF-8 + + + + + dev.struchkov.haiti + haiti-utils + + + javax.persistence + javax.persistence-api + + + com.google.auto.service + auto-service + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + + + com.google.auto.service + auto-service + 1.0.1 + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + + + org.apache.maven.plugins + maven-source-plugin + + + + + + scm:git:https://github.com/haiti-projects/haiti-utils-field-constants.git + https://github.com/haiti-projects/haiti-utils-field-constants + scm:git:https://github.com/haiti-projects/haiti-utils-field-constants.git + + + \ No newline at end of file diff --git a/src/main/java/dev/struchkov/haiti/utils/fieldconstants/ClassCreator.java b/src/main/java/dev/struchkov/haiti/utils/fieldconstants/ClassCreator.java new file mode 100644 index 0000000..cbf1669 --- /dev/null +++ b/src/main/java/dev/struchkov/haiti/utils/fieldconstants/ClassCreator.java @@ -0,0 +1,66 @@ +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 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 fields, PrintWriter out) { + for (FieldDto field : fields) { + out.println(format(" public static final String {0} = \"{1}\";", field.getFieldStringName(), field.getFieldName())); + } + } + +} diff --git a/src/main/java/dev/struchkov/haiti/utils/fieldconstants/annotation/FieldNames.java b/src/main/java/dev/struchkov/haiti/utils/fieldconstants/annotation/FieldNames.java new file mode 100644 index 0000000..32cf300 --- /dev/null +++ b/src/main/java/dev/struchkov/haiti/utils/fieldconstants/annotation/FieldNames.java @@ -0,0 +1,14 @@ +package dev.struchkov.haiti.utils.fieldconstants.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.CLASS) +public @interface FieldNames { + + String postfix() default "Fields"; + +} diff --git a/src/main/java/dev/struchkov/haiti/utils/fieldconstants/annotation/IgnoreField.java b/src/main/java/dev/struchkov/haiti/utils/fieldconstants/annotation/IgnoreField.java new file mode 100644 index 0000000..1a05f6a --- /dev/null +++ b/src/main/java/dev/struchkov/haiti/utils/fieldconstants/annotation/IgnoreField.java @@ -0,0 +1,12 @@ +package dev.struchkov.haiti.utils.fieldconstants.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.CLASS) +public @interface IgnoreField { + +} diff --git a/src/main/java/dev/struchkov/haiti/utils/fieldconstants/domain/ClassDto.java b/src/main/java/dev/struchkov/haiti/utils/fieldconstants/domain/ClassDto.java new file mode 100644 index 0000000..d60ec33 --- /dev/null +++ b/src/main/java/dev/struchkov/haiti/utils/fieldconstants/domain/ClassDto.java @@ -0,0 +1,44 @@ +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 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 getFields() { + return fields; + } + + public void setFields(Set fields) { + this.fields = fields; + } + +} diff --git a/src/main/java/dev/struchkov/haiti/utils/fieldconstants/domain/FieldDto.java b/src/main/java/dev/struchkov/haiti/utils/fieldconstants/domain/FieldDto.java new file mode 100644 index 0000000..ed87228 --- /dev/null +++ b/src/main/java/dev/struchkov/haiti/utils/fieldconstants/domain/FieldDto.java @@ -0,0 +1,30 @@ +package dev.struchkov.haiti.utils.fieldconstants.domain; + +/** + * // TODO: 07.06.2021 Добавить описание. + * + * @author upagge 07.06.2021 + */ +public class FieldDto { + + private final String fieldStringName; + private final String fieldName; + + private FieldDto(String fieldStringName, String fieldName) { + this.fieldStringName = fieldStringName; + this.fieldName = fieldName; + } + + public static FieldDto of(String fieldStringName, String fieldName) { + return new FieldDto(fieldStringName, fieldName); + } + + public String getFieldStringName() { + return fieldStringName; + } + + public String getFieldName() { + return fieldName; + } + +} diff --git a/src/main/java/dev/struchkov/haiti/utils/fieldconstants/processor/FieldNameProcessor.java b/src/main/java/dev/struchkov/haiti/utils/fieldconstants/processor/FieldNameProcessor.java new file mode 100644 index 0000000..c746fe1 --- /dev/null +++ b/src/main/java/dev/struchkov/haiti/utils/fieldconstants/processor/FieldNameProcessor.java @@ -0,0 +1,76 @@ +package dev.struchkov.haiti.utils.fieldconstants.processor; + +import com.google.auto.service.AutoService; +import dev.struchkov.haiti.utils.fieldconstants.ClassCreator; +import dev.struchkov.haiti.utils.fieldconstants.annotation.FieldNames; +import dev.struchkov.haiti.utils.fieldconstants.annotation.IgnoreField; +import dev.struchkov.haiti.utils.fieldconstants.domain.ClassDto; +import dev.struchkov.haiti.utils.fieldconstants.domain.FieldDto; + +import javax.annotation.processing.AbstractProcessor; +import javax.annotation.processing.Processor; +import javax.annotation.processing.RoundEnvironment; +import javax.annotation.processing.SupportedAnnotationTypes; +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.Column; +import javax.persistence.Table; +import java.util.Arrays; +import java.util.Set; +import java.util.stream.Collectors; + +@SupportedAnnotationTypes("dev.struchkov.haiti.utils.fieldconstants.annotation.FieldNames") +@SupportedSourceVersion(SourceVersion.RELEASE_11) +@AutoService(Processor.class) +public class FieldNameProcessor extends AbstractProcessor { + + @Override + public boolean process(Set set, RoundEnvironment roundEnvironment) { + for (TypeElement annotation : set) { + Set 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 Table anTable = annotatedElement.getAnnotation(Table.class); + final String newClassName = annotatedElementName + settings.postfix(); + + final Set fields = annotatedElement.getEnclosedElements().stream() + .filter(this::isField) + .filter(this::isNotIgnoreField) + .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; + } + + 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)); + } + +} diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java new file mode 100644 index 0000000..86de4e3 --- /dev/null +++ b/src/main/java/module-info.java @@ -0,0 +1,8 @@ +module haiti.utils.fieldconstants { + exports dev.struchkov.haiti.utils.fieldconstants.annotation; + + requires java.compiler; + requires com.google.auto.service; + requires java.persistence; + requires haiti.utils; +} \ No newline at end of file