diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index c5efd5c..289aaf6 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -1,4 +1,4 @@ -name: Java CI +name: CI Master on: push: diff --git a/README.md b/README.md index 64cfa1d..0ba2c18 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # DummyMapper 🗎 -![Java CI](https://github.com/GoodforGod/DummyMapper/workflows/Java%20CI/badge.svg) +![Java CI](https://github.com/GoodforGod/DummyMapper/workflows/CI%20Master/badge.svg) [![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=GoodforGod_DummyMapper&metric=alert_status)](https://sonarcloud.io/dashboard?id=GoodforGod_DummyMapper) [![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=GoodforGod_DummyMapper&metric=sqale_rating)](https://sonarcloud.io/dashboard?id=GoodforGod_micronaut-arangodb) [![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=GoodforGod_DummyMapper&metric=reliability_rating)](https://sonarcloud.io/dashboard?id=GoodforGod_DummyMapper) diff --git a/build.gradle b/build.gradle index 6314725..77e23b4 100644 --- a/build.gradle +++ b/build.gradle @@ -3,9 +3,9 @@ plugins { id "java-library" id "maven-publish" - id "org.jetbrains.intellij" version "1.11.0" - id "org.sonarqube" version "3.5.0.2730" - id "com.diffplug.spotless" version "6.12.0" + id "org.jetbrains.intellij" version "1.15.0" + id "org.sonarqube" version "4.2.0.3129" + id "com.diffplug.spotless" version "6.19.0" } repositories { @@ -13,6 +13,8 @@ repositories { mavenCentral() } +prepareKotlinBuildScriptModel.enabled = false + group = groupId version = artifactVersion @@ -21,7 +23,7 @@ targetCompatibility = JavaVersion.VERSION_17 intellij { plugins = ["java", "Kotlin"] - version = "2023.1" + version = "2023.2" downloadSources = false } @@ -38,7 +40,7 @@ spotless { encoding("UTF-8") importOrder() removeUnusedImports() - eclipse("4.21.0").configFile("${rootDir}/config/codestyle.xml") + eclipse("4.21").configFile("${rootDir}/config/codestyle.xml") } } @@ -66,16 +68,17 @@ configurations { } dependencies { - implementation "com.github.goodforgod:dummymaker:3.3.1" + implementation "com.github.goodforgod:dummymaker:4.0.0" implementation "org.javassist:javassist:3.29.2-GA" - implementation "io.goodforgod:jackson-datetime-configuration:1.0.0" + implementation "io.goodforgod:jackson-datetime-configuration:2.0.0" implementation "com.fasterxml.jackson.dataformat:jackson-dataformat-avro:$jacksonVersion" implementation "com.github.victools:jsonschema-generator:4.28.0" - implementation "org.apache.avro:avro-compiler:1.9.2" - implementation "io.leangen.graphql:spqr:0.11.2" + implementation "org.apache.avro:avro-compiler:1.11.2" + implementation "io.leangen.graphql:spqr:0.12.3" } + test { useJUnitPlatform() testLogging { @@ -85,8 +88,8 @@ test { } reports { - html.enabled(false) - junitXml.enabled(false) + html.required = false + junitXml.required = false } } @@ -99,7 +102,7 @@ tasks.withType(JavaCompile) { check.dependsOn jacocoTestReport jacocoTestReport { reports { - xml.enabled true + xml.required = true html.destination file("${buildDir}/jacocoHtml") } } diff --git a/gradle.properties b/gradle.properties index 5798e2b..8876f11 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ groupId=com.github.goodforgod artifactId=dummymapper -artifactVersion=2.1.1 +artifactVersion=2.2.0 ##### GRADLE ##### @@ -18,4 +18,4 @@ org.gradle.jvmargs=-Dfile.encoding=UTF-8 \ ##### FRAMEWORKS ##### -jacksonVersion=2.12.0 +jacksonVersion=2.15.2 diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar index 7454180..943f0cb 100644 Binary files a/gradle/wrapper/gradle-wrapper.jar and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 070cb70..a363877 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,6 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip +networkTimeout=10000 zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew index 1b6c787..65dcd68 100755 --- a/gradlew +++ b/gradlew @@ -55,7 +55,7 @@ # Darwin, MinGW, and NonStop. # # (3) This script is generated from the Groovy template -# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt # within the Gradle project. # # You can find Gradle at https://github.com/gradle/gradle/. @@ -80,10 +80,10 @@ do esac done -APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit - -APP_NAME="Gradle" +# This is normally unused +# shellcheck disable=SC2034 APP_BASE_NAME=${0##*/} +APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' @@ -143,12 +143,16 @@ fi if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then case $MAX_FD in #( max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC3045 MAX_FD=$( ulimit -H -n ) || warn "Could not query maximum file descriptor limit" esac case $MAX_FD in #( '' | soft) :;; #( *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC3045 ulimit -n "$MAX_FD" || warn "Could not set maximum file descriptor limit to $MAX_FD" esac @@ -205,6 +209,12 @@ set -- \ org.gradle.wrapper.GradleWrapperMain \ "$@" +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + # Use "xargs" to parse quoted args. # # With -n1 it outputs one arg per line, with the quotes and backslashes removed. diff --git a/gradlew.bat b/gradlew.bat index 107acd3..93e3f59 100644 --- a/gradlew.bat +++ b/gradlew.bat @@ -14,7 +14,7 @@ @rem limitations under the License. @rem -@if "%DEBUG%" == "" @echo off +@if "%DEBUG%"=="" @echo off @rem ########################################################################## @rem @rem Gradle startup script for Windows @@ -25,7 +25,8 @@ if "%OS%"=="Windows_NT" setlocal set DIRNAME=%~dp0 -if "%DIRNAME%" == "" set DIRNAME=. +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused set APP_BASE_NAME=%~n0 set APP_HOME=%DIRNAME% @@ -40,7 +41,7 @@ if defined JAVA_HOME goto findJavaFromJavaHome set JAVA_EXE=java.exe %JAVA_EXE% -version >NUL 2>&1 -if "%ERRORLEVEL%" == "0" goto execute +if %ERRORLEVEL% equ 0 goto execute echo. echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. @@ -75,13 +76,15 @@ set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar :end @rem End local scope for the variables with windows NT shell -if "%ERRORLEVEL%"=="0" goto mainEnd +if %ERRORLEVEL% equ 0 goto mainEnd :fail rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of rem the _cmd.exe /c_ return code! -if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 -exit /b 1 +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% :mainEnd if "%OS%"=="Windows_NT" endlocal diff --git a/src/main/java/io/goodforgod/dummymapper/MapperAction.java b/src/main/java/io/goodforgod/dummymapper/MapperAction.java index 8eb1add..4a7d93f 100644 --- a/src/main/java/io/goodforgod/dummymapper/MapperAction.java +++ b/src/main/java/io/goodforgod/dummymapper/MapperAction.java @@ -9,7 +9,7 @@ import com.intellij.openapi.ui.MessageType; import com.intellij.openapi.ui.popup.util.PopupUtil; import com.intellij.psi.PsiClass; -import io.dummymaker.util.StringUtils; +import io.goodforgod.dummymaker.util.StringUtils; import io.goodforgod.dummymapper.error.MapperException; import io.goodforgod.dummymapper.error.PsiKindException; import io.goodforgod.dummymapper.error.UnsupportedPsiFileException; diff --git a/src/main/java/io/goodforgod/dummymapper/marker/AnnotationMarker.java b/src/main/java/io/goodforgod/dummymapper/marker/AnnotationMarker.java index df18a70..2b627de 100644 --- a/src/main/java/io/goodforgod/dummymapper/marker/AnnotationMarker.java +++ b/src/main/java/io/goodforgod/dummymapper/marker/AnnotationMarker.java @@ -1,9 +1,6 @@ package io.goodforgod.dummymapper.marker; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; -import java.util.Objects; +import java.util.*; import org.jetbrains.annotations.NotNull; /** @@ -118,7 +115,7 @@ public Builder ofInternal() { public Builder withAttribute(@NotNull String name, Object value) { if (this.attributes.isEmpty()) - this.attributes = new HashMap<>(2); + this.attributes = new LinkedHashMap<>(2); this.attributes.put(name, value); return this; } diff --git a/src/main/java/io/goodforgod/dummymapper/marker/EnumMarker.java b/src/main/java/io/goodforgod/dummymapper/marker/EnumMarker.java index 8b88238..8acf005 100644 --- a/src/main/java/io/goodforgod/dummymapper/marker/EnumMarker.java +++ b/src/main/java/io/goodforgod/dummymapper/marker/EnumMarker.java @@ -1,6 +1,6 @@ package io.goodforgod.dummymapper.marker; -import io.dummymaker.util.CollectionUtils; +import io.goodforgod.dummymaker.util.CollectionUtils; import java.util.*; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; diff --git a/src/main/java/io/goodforgod/dummymapper/marker/Marker.java b/src/main/java/io/goodforgod/dummymapper/marker/Marker.java index 1955b97..be65654 100644 --- a/src/main/java/io/goodforgod/dummymapper/marker/Marker.java +++ b/src/main/java/io/goodforgod/dummymapper/marker/Marker.java @@ -1,7 +1,7 @@ package io.goodforgod.dummymapper.marker; -import io.dummymaker.util.CollectionUtils; -import io.dummymaker.util.StringUtils; +import io.goodforgod.dummymaker.util.CollectionUtils; +import io.goodforgod.dummymaker.util.StringUtils; import java.util.*; import java.util.stream.Collectors; import org.jetbrains.annotations.NotNull; diff --git a/src/main/java/io/goodforgod/dummymapper/marker/RawMarker.java b/src/main/java/io/goodforgod/dummymapper/marker/RawMarker.java index e79095b..8e90d71 100644 --- a/src/main/java/io/goodforgod/dummymapper/marker/RawMarker.java +++ b/src/main/java/io/goodforgod/dummymapper/marker/RawMarker.java @@ -1,9 +1,9 @@ package io.goodforgod.dummymapper.marker; import java.util.Collections; +import java.util.LinkedHashMap; import java.util.Map; import java.util.Objects; -import java.util.concurrent.ConcurrentHashMap; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -25,7 +25,7 @@ public RawMarker(@NotNull String root, super(root, source); this.structure = (structure == null || structure.isEmpty()) ? Collections.emptyMap() - : new ConcurrentHashMap<>(structure); + : new LinkedHashMap<>(structure); } @Override diff --git a/src/main/java/io/goodforgod/dummymapper/marker/filter/AvroFilter.java b/src/main/java/io/goodforgod/dummymapper/marker/filter/AvroFilter.java index f2669f7..477f4ba 100644 --- a/src/main/java/io/goodforgod/dummymapper/marker/filter/AvroFilter.java +++ b/src/main/java/io/goodforgod/dummymapper/marker/filter/AvroFilter.java @@ -4,7 +4,7 @@ import io.goodforgod.dummymapper.marker.AnnotationMarker; import io.goodforgod.dummymapper.marker.RawMarker; import java.util.Collections; -import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.Map; import java.util.function.Predicate; import org.apache.avro.reflect.AvroIgnore; @@ -37,7 +37,7 @@ public RawMarker filter(@NotNull RawMarker marker) { .findFirst() .orElseGet(Collections::emptyMap); - final Map attrs = new HashMap<>(annotationAttrs); + final Map attrs = new LinkedHashMap<>(annotationAttrs); attrs.put(REQUIRED_PROPERTY, false); v.addAnnotation(AnnotationMarker.builder() diff --git a/src/main/java/io/goodforgod/dummymapper/marker/filter/GenEnumAnnotationFilter.java b/src/main/java/io/goodforgod/dummymapper/marker/filter/GenEnumAnnotationFilter.java index 22c5fcb..9d40645 100644 --- a/src/main/java/io/goodforgod/dummymapper/marker/filter/GenEnumAnnotationFilter.java +++ b/src/main/java/io/goodforgod/dummymapper/marker/filter/GenEnumAnnotationFilter.java @@ -1,6 +1,6 @@ package io.goodforgod.dummymapper.marker.filter; -import io.dummymaker.annotation.complex.GenEnum; +import io.goodforgod.dummymaker.annotation.parameterized.GenEnum; import io.goodforgod.dummymapper.marker.AnnotationMarker; import java.util.function.Predicate; diff --git a/src/main/java/io/goodforgod/dummymapper/marker/filter/JacksonPropertyFilter.java b/src/main/java/io/goodforgod/dummymapper/marker/filter/JacksonPropertyFilter.java index 2224020..b8a79b1 100644 --- a/src/main/java/io/goodforgod/dummymapper/marker/filter/JacksonPropertyFilter.java +++ b/src/main/java/io/goodforgod/dummymapper/marker/filter/JacksonPropertyFilter.java @@ -4,7 +4,7 @@ import io.goodforgod.dummymapper.marker.AnnotationMarker; import io.goodforgod.dummymapper.marker.RawMarker; import java.util.Collections; -import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.Map; import org.jetbrains.annotations.NotNull; @@ -28,7 +28,7 @@ public RawMarker filter(@NotNull RawMarker marker) { .findFirst() .orElseGet(Collections::emptyMap); - final Map attrs = new HashMap<>(annotationAttrs); + final Map attrs = new LinkedHashMap<>(annotationAttrs); attrs.put(REQUIRED_PROPERTY, true); v.addAnnotation(AnnotationMarker.builder() diff --git a/src/main/java/io/goodforgod/dummymapper/marker/mapper/JsonArrayMapper.java b/src/main/java/io/goodforgod/dummymapper/marker/mapper/JsonArrayMapper.java index fde31cf..409081d 100644 --- a/src/main/java/io/goodforgod/dummymapper/marker/mapper/JsonArrayMapper.java +++ b/src/main/java/io/goodforgod/dummymapper/marker/mapper/JsonArrayMapper.java @@ -3,7 +3,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.intellij.psi.PsiJavaFile; -import io.dummymaker.factory.impl.GenFactory; +import io.goodforgod.dummymaker.GenFactory; import io.goodforgod.dummymapper.error.ParseException; import io.goodforgod.dummymapper.marker.MarkerFilter; import io.goodforgod.dummymapper.marker.MarkerMapper; diff --git a/src/main/java/io/goodforgod/dummymapper/marker/mapper/JsonMapper.java b/src/main/java/io/goodforgod/dummymapper/marker/mapper/JsonMapper.java index 12e985f..4dcc58e 100644 --- a/src/main/java/io/goodforgod/dummymapper/marker/mapper/JsonMapper.java +++ b/src/main/java/io/goodforgod/dummymapper/marker/mapper/JsonMapper.java @@ -3,7 +3,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.intellij.psi.PsiJavaFile; -import io.dummymaker.factory.impl.GenFactory; +import io.goodforgod.dummymaker.GenFactory; import io.goodforgod.dummymapper.error.ParseException; import io.goodforgod.dummymapper.marker.MarkerFilter; import io.goodforgod.dummymapper.marker.MarkerMapper; diff --git a/src/main/java/io/goodforgod/dummymapper/marker/mapper/ObjectMapperUtils.java b/src/main/java/io/goodforgod/dummymapper/marker/mapper/ObjectMapperUtils.java index 83f5603..23d4d39 100644 --- a/src/main/java/io/goodforgod/dummymapper/marker/mapper/ObjectMapperUtils.java +++ b/src/main/java/io/goodforgod/dummymapper/marker/mapper/ObjectMapperUtils.java @@ -1,13 +1,11 @@ package io.goodforgod.dummymapper.marker.mapper; import com.fasterxml.jackson.databind.DeserializationFeature; -import com.fasterxml.jackson.databind.MapperFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; import com.fasterxml.jackson.module.paramnames.ParameterNamesModule; import com.intellij.psi.PsiJavaFile; -import io.goodforgod.jackson.module.datetime.configuration.DateTimeFormatters; import io.goodforgod.jackson.module.datetime.configuration.JavaTimeModule; import io.goodforgod.jackson.module.datetime.configuration.JavaTimeModuleConfiguration; import java.text.SimpleDateFormat; @@ -27,20 +25,18 @@ static ObjectMapper getConfigured() { } static ObjectMapper configure(ObjectMapper mapper) { - final JavaTimeModule javaTimeModule = JavaTimeModuleConfiguration.ofISO().getModule(); + final JavaTimeModule javaTimeModule = new JavaTimeModuleConfiguration().getModule(); - mapper.setDateFormat(new SimpleDateFormat(DateTimeFormatters.ISO_DATE)); + mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX")); mapper.registerModule(new ParameterNamesModule()) .registerModule(new Jdk8Module()) .registerModule(javaTimeModule); mapper.setConfig(mapper.getDeserializationConfig() - .with(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY) .with(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE)); mapper.setConfig(mapper.getSerializationConfig() .with(SerializationFeature.INDENT_OUTPUT) - .with(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY) .with(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS) .without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)); diff --git a/src/main/java/io/goodforgod/dummymapper/service/AssistClassFactory.java b/src/main/java/io/goodforgod/dummymapper/service/AssistClassFactory.java index 755122e..69175c6 100644 --- a/src/main/java/io/goodforgod/dummymapper/service/AssistClassFactory.java +++ b/src/main/java/io/goodforgod/dummymapper/service/AssistClassFactory.java @@ -1,7 +1,8 @@ package io.goodforgod.dummymapper.service; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; import com.fasterxml.jackson.databind.ObjectMapper; -import io.dummymaker.util.CollectionUtils; +import io.goodforgod.dummymaker.util.CollectionUtils; import io.goodforgod.dummymapper.error.ClassBuildException; import io.goodforgod.dummymapper.error.ClassEmptyException; import io.goodforgod.dummymapper.marker.*; @@ -12,10 +13,7 @@ import java.util.stream.Collectors; import java.util.stream.IntStream; import javassist.*; -import javassist.bytecode.AnnotationsAttribute; -import javassist.bytecode.ConstPool; -import javassist.bytecode.FieldInfo; -import javassist.bytecode.SignatureAttribute; +import javassist.bytecode.*; import javassist.bytecode.annotation.*; import org.jetbrains.annotations.NotNull; @@ -30,7 +28,7 @@ public class AssistClassFactory { private static final ClassPool CLASS_POOL = ClassPool.getDefault(); - private static final Map CLASS_SUFFIX_COUNTER = new HashMap<>(); + private static final Map CLASS_SUFFIX_COUNTER = new LinkedHashMap<>(); private static final String MAPPED_VISITED = "_class_mapped_visited"; private static final Predicate IS_VISITED = m -> m.getAnnotations().stream() @@ -42,18 +40,9 @@ private AssistClassFactory() {} /** * Contains class cache via canonicalName and its value with marker and class */ - private static final Map CLASS_CACHE = new HashMap<>(); + private static final Map CLASS_CACHE = new LinkedHashMap<>(); - private static class ClassCache { - - final RawMarker marker; - final Class clazz; - - ClassCache(RawMarker marker, Class clazz) { - this.marker = marker; - this.clazz = clazz; - } - } + private record ClassCache(RawMarker marker, Class clazz) {} public static Map getMappedClasses(@NotNull RawMarker marker) { if (IS_VISITED.test(marker)) @@ -61,7 +50,7 @@ public static Map getMappedClasses(@NotNull RawMarker marker) { marker.addAnnotation(AnnotationMarker.builder().ofInternal().withName(MAPPED_VISITED).build()); - final Map mapped = new HashMap<>(); + final Map mapped = new LinkedHashMap<>(); final Map structure = marker.getStructure(); MarkerUtils.streamRawMarkers(structure) @@ -84,7 +73,7 @@ public static Map getMappedClasses(@NotNull RawMarker marker) { .map(m -> { final Map mapped1 = m.getKeyErasure() instanceof RawMarker ? getMappedClasses(((RawMarker) m.getKeyErasure())) - : new HashMap<>(1); + : new LinkedHashMap<>(1); final Map mapped2 = m.getKeyErasure() instanceof RawMarker ? getMappedClasses(((RawMarker) m.getKeyErasure())) @@ -118,7 +107,7 @@ public static Class build(@NotNull RawMarker rawMarker) { return classCache.clazz; } - final CtClass ctClass = buildInternal(rawMarker, new HashMap<>()); + final CtClass ctClass = buildInternal(rawMarker, new LinkedHashMap<>()); final Class clazz = Class.forName(ctClass.getName()); CLASS_CACHE.put(rawMarker.getSource(), new ClassCache(rawMarker, clazz)); return clazz; @@ -183,6 +172,7 @@ private static CtClass buildInternal(@NotNull RawMarker classMarker, CLASS_SUFFIX_COUNTER.computeIfPresent(originClassName, (k, v) -> v + 1); return ownClass; } catch (Exception e) { + addJsonPropertyOrder(ownClass, structure); ownClass.toClass(ObjectMapper.class.getClassLoader(), null); CLASS_SUFFIX_COUNTER.computeIfPresent(originClassName, (k, v) -> v + 1); return ownClass; @@ -347,12 +337,40 @@ private static CtField addAnnotationInfo(@NotNull CtField field, } } - if (CollectionUtils.isNotEmpty(attribute.getAnnotations())) + if (CollectionUtils.isNotEmpty(attribute.getAnnotations())) { fieldInfo.addAttribute(attribute); + } return field; } + private static CtClass addJsonPropertyOrder(@NotNull CtClass clazz, @NotNull Map structure) { + final ClassFile classFile = clazz.getClassFile(); + final ConstPool constPool = classFile.getConstPool(); + + final Annotation a = new Annotation(JsonPropertyOrder.class.getCanonicalName(), constPool); + final ArrayMemberValue arrayMemberValue = new ArrayMemberValue(constPool); + + final StringMemberValue[] members = structure.keySet().stream() + .map(f -> new StringMemberValue(f, constPool)) + .toArray(StringMemberValue[]::new); + arrayMemberValue.setValue(members); + a.addMemberValue("value", arrayMemberValue); + + final AnnotationsAttribute attribute = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag); + try { + attribute.addAnnotation(a); + } catch (Exception e) { + // nothing we can do if javaassist fail + } + + if (CollectionUtils.isNotEmpty(attribute.getAnnotations())) { + classFile.addAttribute(attribute); + } + + return clazz; + } + private static Optional getMember(Object v, ConstPool constPool) { if (v instanceof Boolean) { return Optional.of(new BooleanMemberValue((Boolean) v, constPool)); diff --git a/src/main/java/io/goodforgod/dummymapper/service/GenFactoryProvider.java b/src/main/java/io/goodforgod/dummymapper/service/GenFactoryProvider.java index 3ba2ba7..ffade93 100644 --- a/src/main/java/io/goodforgod/dummymapper/service/GenFactoryProvider.java +++ b/src/main/java/io/goodforgod/dummymapper/service/GenFactoryProvider.java @@ -1,11 +1,11 @@ package io.goodforgod.dummymapper.service; -import io.dummymaker.factory.impl.GenFactory; -import io.dummymaker.generator.IGenerator; -import io.dummymaker.model.GenRule; -import io.dummymaker.model.GenRules; -import io.dummymaker.util.CollectionUtils; -import io.dummymaker.util.StringUtils; +import io.goodforgod.dummymaker.GenFactory; +import io.goodforgod.dummymaker.GenRule; +import io.goodforgod.dummymaker.generator.Generator; +import io.goodforgod.dummymaker.util.CollectionUtils; +import io.goodforgod.dummymaker.util.RandomUtils; +import io.goodforgod.dummymaker.util.StringUtils; import io.goodforgod.dummymapper.marker.*; import io.goodforgod.dummymapper.marker.AnnotationMarker; import io.goodforgod.dummymapper.util.MarkerUtils; @@ -42,7 +42,9 @@ private GenFactoryProvider() {} public static GenFactory get(@NotNull RawMarker rawMarker) { final Map mappedClasses = AssistClassFactory.getMappedClasses(rawMarker); final List rules = getRules(rawMarker, mappedClasses); - return new GenFactory(GenRules.of(rules)); + GenFactory.Builder builder = GenFactory.builder(); + rules.forEach(builder::addRule); + return builder.build(); } private static List getRules(@NotNull RawMarker marker, @@ -61,31 +63,32 @@ private static List getRules(@NotNull RawMarker marker, .filter(StringUtils::isNotEmpty) .findFirst(); - if (!mapped.isPresent()) + if (mapped.isEmpty()) { return Collections.emptyList(); + } try { - final GenRule rule = GenRule.auto(Class.forName(mapped.get()), FACTORY_DEPTH); + final GenRule rule = GenRule.ofClass(Class.forName(mapped.get()), true, FACTORY_DEPTH); structure.forEach((k, v) -> { if (v instanceof EnumMarker) { - final IGenerator generator = () -> CollectionUtils.random(((EnumMarker) v).getValues()); - rule.add(generator, k); + final Generator generator = () -> CollectionUtils.random(((EnumMarker) v).getValues()); + rule.generateForNames(k, () -> generator); } else if (v instanceof CollectionMarker && ((CollectionMarker) v).getErasure() instanceof EnumMarker) { final EnumMarker erasure = (EnumMarker) ((CollectionMarker) v).getErasure(); - final int total = CollectionUtils.random(1, erasure.getValues().size()); - final IGenerator> generator = () -> IntStream.range(0, total) + final int total = RandomUtils.random(1, erasure.getValues().size()); + final Generator> generator = () -> IntStream.range(0, total) .mapToObj(i -> erasure.getValues().get(i)) .collect(Collectors.toCollection(() -> Set.class.isAssignableFrom(((CollectionMarker) v).getType()) ? new HashSet<>() : new ArrayList<>())); - rule.add(generator, k); + rule.generateForNames(k, () -> generator); } else if (v instanceof ArrayMarker && ((ArrayMarker) v).getErasure() instanceof EnumMarker) { final EnumMarker erasure = (EnumMarker) ((ArrayMarker) v).getErasure(); - final int total = CollectionUtils.random(1, erasure.getValues().size()); - final IGenerator generator = () -> IntStream.range(0, total) + final int total = RandomUtils.random(1, erasure.getValues().size()); + final Generator generator = () -> IntStream.range(0, total) .mapToObj(i -> erasure.getValues().get(i)) .toArray(String[]::new); - rule.add(generator, k); + rule.generateForNames(k, () -> generator); } }); diff --git a/src/main/java/io/goodforgod/dummymapper/service/PsiClassScanner.java b/src/main/java/io/goodforgod/dummymapper/service/PsiClassScanner.java index 312030b..1b16222 100644 --- a/src/main/java/io/goodforgod/dummymapper/service/PsiClassScanner.java +++ b/src/main/java/io/goodforgod/dummymapper/service/PsiClassScanner.java @@ -12,7 +12,7 @@ import com.intellij.psi.*; import com.intellij.psi.impl.source.PsiClassReferenceType; import com.intellij.psi.search.GlobalSearchScope; -import io.dummymaker.util.StringUtils; +import io.goodforgod.dummymaker.util.StringUtils; import io.goodforgod.dummymapper.error.PsiKindException; import io.goodforgod.dummymapper.error.ScanException; import io.goodforgod.dummymapper.marker.*; @@ -29,7 +29,8 @@ * Scan PsiJavaFile structure of fields and their annotations (also getters and setters annotations * for such fields) *

- * https://intellij-support.jetbrains.com/hc/en-us/community/posts/360002746839-How-to-add-an-annotation-and-import-to-a-Java-class + * JetBrains * Scans class {@link PsiFile} and build tree structure of such class file * * @author GoodforGod @@ -38,41 +39,9 @@ @SuppressWarnings("UnstableApiUsage") public class PsiClassScanner { - private static class Target { + private record Target(String root, String source) {} - private final String root; - private final String source; - - public Target(String root, String source) { - this.root = root; - this.source = source; - } - - public String getRoot() { - return root; - } - - public String getSource() { - return source; - } - - @Override - public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; - Target target = (Target) o; - return Objects.equals(root, target.root) && Objects.equals(source, target.source); - } - - @Override - public int hashCode() { - return Objects.hash(root, source); - } - } - - private final Map> scanned = new HashMap<>(); + private final Map> scanned = new LinkedHashMap<>(); public @NotNull RawMarker scan(@Nullable PsiClass target) { if (target == null) @@ -84,8 +53,9 @@ public int hashCode() { throw new PsiKindException(target.getClassKind()); final Map scannedFile = scanPsiClass(target); - if (scannedFile.isEmpty()) + if (scannedFile.isEmpty()) { return RawMarker.EMPTY; + } final String source = getFileFullName(target); final String root = getFileFullName(target); @@ -236,7 +206,7 @@ private Optional scanJavaInnerClass(@NotNull PsiClass rootClass, final String source = getFileFullName(sourceClass); final Map cached = scanned.get(new Target(root, source)); final Map structure = (cached == null) - ? scanPsiClass(sourceClass, sourceClass, new HashMap<>()) + ? scanPsiClass(sourceClass, sourceClass, new LinkedHashMap<>()) : cached; final Marker marker = structure.get(type.getPresentableText()); @@ -356,7 +326,7 @@ private Stream scanAnnotations(PsiAnnotation[] psiAnno return Arrays.stream(psiAnnotations) .filter(a -> StringUtils.isNotBlank(a.getQualifiedName())) .map(a -> { - final Map attrs = new HashMap<>(a.getAttributes().size()); + final Map attrs = new LinkedHashMap<>(a.getAttributes().size()); a.getAttributes().forEach(attr -> { final Object value = getAttributeValue(attr.getAttributeValue()); if (value != null) diff --git a/src/main/java/io/goodforgod/dummymapper/util/PsiClassUtils.java b/src/main/java/io/goodforgod/dummymapper/util/PsiClassUtils.java index 1b89deb..9000666 100644 --- a/src/main/java/io/goodforgod/dummymapper/util/PsiClassUtils.java +++ b/src/main/java/io/goodforgod/dummymapper/util/PsiClassUtils.java @@ -7,7 +7,7 @@ import com.intellij.psi.PsiType; import com.intellij.psi.impl.source.PsiClassReferenceType; import com.intellij.psi.impl.source.PsiImmediateClassType; -import io.dummymaker.util.StringUtils; +import io.goodforgod.dummymaker.util.StringUtils; import io.goodforgod.dummymapper.service.PsiClassScanner; import java.math.BigDecimal; import java.math.BigInteger; @@ -180,8 +180,9 @@ public static boolean isFieldValid(@NotNull PsiField field) { } public static @Nullable Class getSimpleTypeByName(@Nullable String name) { - if (StringUtils.isEmpty(name)) + if (StringUtils.isEmpty(name)) { return null; + } return "?".equals(name) ? String.class