InitCommit

This commit is contained in:
Struchkov Mark 2022-02-13 15:36:31 +03:00
commit 5718fff314
10 changed files with 431 additions and 0 deletions

85
.gitignore vendored Normal file
View File

@ -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

17
.gitlab-ci.yml Normal file
View File

@ -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'

79
pom.xml Normal file
View File

@ -0,0 +1,79 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>dev.struchkov.haiti</groupId>
<artifactId>haiti</artifactId>
<version>0.0.4</version>
</parent>
<groupId>dev.struchkov.haiti.utils</groupId>
<artifactId>haiti-utils-field-constants</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Haiti Field Name Constants Utils</name>
<description>Generating class field names</description>
<url>https://github.com/haiti-projects/haiti-utils-field-constants</url>
<issueManagement>
<system>GitHub</system>
<url>https://github.com/haiti-projects/haiti-utils-field-constants/issues</url>
</issueManagement>
<properties>
<java.version>11</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>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<dependency>
<groupId>dev.struchkov.haiti</groupId>
<artifactId>haiti-utils</artifactId>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
</dependency>
<dependency>
<groupId>com.google.auto.service</groupId>
<artifactId>auto-service</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>com.google.auto.service</groupId>
<artifactId>auto-service</artifactId>
<version>1.0.1</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
</plugin>
</plugins>
</build>
<scm>
<connection>scm:git:https://github.com/haiti-projects/haiti-utils-field-constants.git</connection>
<url>https://github.com/haiti-projects/haiti-utils-field-constants</url>
<developerConnection>scm:git:https://github.com/haiti-projects/haiti-utils-field-constants.git</developerConnection>
</scm>
</project>

View File

@ -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<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,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";
}

View File

@ -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 {
}

View File

@ -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<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,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;
}
}

View File

@ -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<? extends TypeElement> set, RoundEnvironment roundEnvironment) {
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 Table anTable = annotatedElement.getAnnotation(Table.class);
final String newClassName = annotatedElementName + settings.postfix();
final Set<FieldDto> 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));
}
}

View File

@ -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;
}