Package ru.yojo.codegen.util
Class MapperUtil
java.lang.Object
ru.yojo.codegen.util.MapperUtil
Utility class for common mapping, string, and code-generation operations.
Includes utilities for:
- YAML-to-Java type conversion and field name normalization
- Lombok annotation generation
- JavaDoc construction
- String utilities (camel/kebab/snake case, capitalization, empty checks)
- Class/interface/enum code template assembly
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidbuildLombokAnnotations(LombokProperties lombokProperties, Set<String> requiredImports, StringBuilder lombokAnnotationBuilder) Appends Lombok annotations (e.g.,@NoArgsConstructor,@Accessors) to the builder and adds corresponding imports to the set.static Stringcapitalize(String str) Converts the first character to title case (e.g.,"userName"→"UserName").castObjectToList(Object obj) Casts an object toArrayList<String>, returning an empty list ifnull.Casts an object toArrayList<Object>, returning an empty list ifnull.castObjectToMap(Object obj) Casts an object toMap<String, Object>, returning an empty map ifnull.static StringfinishBuild(StringBuilder sb, Set<String> requiredImports, String packageName) Finalizes Java source by prepending package and imports, and appending closing brace.static voidgenerateClassJavaDoc(StringBuilder sb, String description) Prepends a class-level JavaDoc comment to the givenStringBuilder.static StringgenerateEnumConstructor(String enumClassName, String type, String variableName) Generates an enum constructor call (used internally for enum with description).static StringgenerateGetter(String type, String variableName) Generates a getter method declaration for a field.static voidgenerateJavaDoc(StringBuilder sb, String description, String example) Appends a field-level JavaDoc comment (with optional example) to the givenStringBuilder.static StringGenerates@Max(...)annotation.static StringGenerates@Min(...)annotation.static StringgenerateSetter(String type, String variableName) Generates a setter method declaration for a field.static StringgenerateSizeAnnotation(String minLength, String maxLength) Generates@Sizeannotation based on min/max bounds.static StringBuildergetClassBuilder(String schemaName) Creates aStringBuilderinitialized with:static StringBuildergetEnumClassBuilder(String schemaName) Creates aStringBuilderinitialized with:static StringBuildergetExtendsClassBuilder(String schemaName, String extendsClass) Creates aStringBuilderinitialized with:static StringBuildergetExtendsWithImplementationClassBuilder(String schemaName, String extendsClass, Set<String> implementsFrom) Creates aStringBuilderinitialized with:static StringBuildergetImplementationClassBuilder(String schemaName, Set<String> implementsFrom) Creates aStringBuilderinitialized with:static StringBuildergetInterfaceBuilder(String schemaName) Creates aStringBuilderinitialized with:static intReturns length of an array or0ifnull.static StringgetPackage(String packageLocation, String outputDirectoryName, String messagePackageImport) Joins package parts with dots.getSchemaByName(Map<String, Object> content, String schemaName) Retrieves schema definition by name from the globalcomponents.schemassection.getSetValueIfExistsOrElseEmptySet(String key, Map<String, Object> schemaMap) Safely extracts aSet<String>value from a map for the given key, or returns an empty set.static StringgetStringValueIfExistOrElseNull(String key, Map<String, Object> map) Safely extracts a string value from a map for the given key, or returnsnull.static booleanisAnyEmpty(CharSequence... css) Checks if any of the input strings is null or empty.static booleanisBlank(CharSequence cs) Checks if aCharSequenceis null, empty, or whitespace only.static booleanisEmpty(CharSequence cs) Checks if aCharSequenceisnullor zero-length.static booleanChecks if an object array is empty.static booleanisNoneEmpty(CharSequence... css) Checks if none of the input strings is null or empty.static booleanChecks if aCharSequenceis not blank.static booleanChecks if aBooleanistrue.static intlength(CharSequence cs) Returns length of aCharSequence, or 0 ifnull.static StringBuilderprepareStringBuilder(Set<String> requiredImports, Set<String> implementsFrom, String extendsFrom, String schemaName, Set<String> importSet, FillParameters fillParameters) Prepares a class builder with correct inheritance/implementation clause and appends field declarations.static StringrefReplace(String ref) Extracts and capitalizes the schema name from an AsyncAPI$refpath.static StringsubstringBefore(String str, String separator) Gets substring before the first occurrence of a separator.static StringtoValidJavaEnumConstantName(String rawName) Converts raw enum constant name (e.g.,"success", "error-code", "\r\n") to valid Java enum identifier (UPPER_SNAKE_CASE), handling control chars and keywords.static StringtoValidJavaFieldName(String rawName) Converts a raw field name (e.g.,"user-name", "user_name") to valid Java field identifier (camelCase).static Stringuncapitalize(String str) Converts the first character to lower case (e.g.,"UserName"→"userName").
-
Constructor Details
-
MapperUtil
public MapperUtil()
-
-
Method Details
-
getSetValueIfExistsOrElseEmptySet
public static Set<String> getSetValueIfExistsOrElseEmptySet(String key, Map<String, Object> schemaMap) Safely extracts aSet<String>value from a map for the given key, or returns an empty set.- Parameters:
key- key to lookupschemaMap- map to extract from- Returns:
- non-null set of strings
-
getStringValueIfExistOrElseNull
Safely extracts a string value from a map for the given key, or returnsnull.- Parameters:
key- map keymap- source map- Returns:
- string value or
null
-
castObjectToMap
Casts an object toMap<String, Object>, returning an empty map ifnull.- Parameters:
obj- object to cast- Returns:
- non-null map
-
castObjectToList
Casts an object toArrayList<String>, returning an empty list ifnull.- Parameters:
obj- object to cast- Returns:
- non-null list
-
castObjectToListObjects
Casts an object toArrayList<Object>, returning an empty list ifnull.- Parameters:
obj- object to cast- Returns:
- non-null list
-
refReplace
Extracts and capitalizes the schema name from an AsyncAPI$refpath.Examples:
"#/components/schemas/User"→"User""./file.yaml#/components/schemas/user"→"User"
- Parameters:
ref-$refstring- Returns:
- capitalized schema name
-
generateSetter
Generates a setter method declaration for a field.- Parameters:
type- field typevariableName- field name (camelCase)- Returns:
- Java setter source, e.g.,
" public void setField(Type field) { ... }"
-
generateGetter
Generates a getter method declaration for a field.- Parameters:
type- field typevariableName- field name (camelCase)- Returns:
- Java getter source, e.g.,
" public Type getField() { ... }"
-
generateEnumConstructor
public static String generateEnumConstructor(String enumClassName, String type, String variableName) Generates an enum constructor call (used internally for enum with description).- Parameters:
enumClassName- enum class nametype- enum constant type (typicallyString)variableName- field holding description- Returns:
- constructor call string
-
getInterfaceBuilder
Creates aStringBuilderinitialized with:public interface X { }- Parameters:
schemaName- interface name- Returns:
- builder with interface header
-
getClassBuilder
Creates aStringBuilderinitialized with:public class X { }- Parameters:
schemaName- class name- Returns:
- builder with class header
-
getEnumClassBuilder
Creates aStringBuilderinitialized with:public enum X { }- Parameters:
schemaName- enum name- Returns:
- builder with enum header
-
getImplementationClassBuilder
public static StringBuilder getImplementationClassBuilder(String schemaName, Set<String> implementsFrom) Creates aStringBuilderinitialized with:public class X implements I1, I2 { }- Parameters:
schemaName- class nameimplementsFrom- set of interface simple names- Returns:
- builder with class+implements header
-
getExtendsClassBuilder
Creates aStringBuilderinitialized with:public class X extends Y { }- Parameters:
schemaName- class nameextendsClass- superclass name- Returns:
- builder with class+extends header
-
getExtendsWithImplementationClassBuilder
public static StringBuilder getExtendsWithImplementationClassBuilder(String schemaName, String extendsClass, Set<String> implementsFrom) Creates aStringBuilderinitialized with:public class X extends Y implements I1, I2 { }- Parameters:
schemaName- class nameextendsClass- superclass nameimplementsFrom- set of interface names- Returns:
- builder with full inheritance header
-
generateJavaDoc
Appends a field-level JavaDoc comment (with optional example) to the givenStringBuilder.- Parameters:
sb- builder to append todescription- field description (fromdescriptionin schema)example- field example (fromexamplein schema)
-
generateClassJavaDoc
Prepends a class-level JavaDoc comment to the givenStringBuilder. Inserted beforepublic class X { ...}.- Parameters:
sb- builder to modify (viainsert(0, ...))description- class description (fromdescriptionin schema)
-
getPackage
public static String getPackage(String packageLocation, String outputDirectoryName, String messagePackageImport) Joins package parts with dots.- Parameters:
packageLocation- base package (e.g.,com.example)outputDirectoryName- subdirectory name (e.g.,test)messagePackageImport- suffix (e.g.,messages;)- Returns:
- full package string (e.g.,
com.example.test.messages;)
-
generateSizeAnnotation
Generates@Sizeannotation based on min/max bounds.- Parameters:
minLength- minimum lengthmaxLength- maximum length- Returns:
- annotation string
-
generateMinAnnotation
Generates@Min(...)annotation.- Parameters:
min- minimum value- Returns:
- annotation string
-
generateMaxAnnotation
Generates@Max(...)annotation.- Parameters:
max- maximum value- Returns:
- annotation string
-
buildLombokAnnotations
public static void buildLombokAnnotations(LombokProperties lombokProperties, Set<String> requiredImports, StringBuilder lombokAnnotationBuilder) Appends Lombok annotations (e.g.,@NoArgsConstructor,@Accessors) to the builder and adds corresponding imports to the set.- Parameters:
lombokProperties- Lombok configrequiredImports- target import setlombokAnnotationBuilder- target builder for annotations
-
finishBuild
Finalizes Java source by prepending package and imports, and appending closing brace.- Parameters:
sb- builder with class bodyrequiredImports- set of import stringspackageName- package name (with semicolon)- Returns:
- complete Java source
-
prepareStringBuilder
public static StringBuilder prepareStringBuilder(Set<String> requiredImports, Set<String> implementsFrom, String extendsFrom, String schemaName, Set<String> importSet, FillParameters fillParameters) Prepares a class builder with correct inheritance/implementation clause and appends field declarations.- Parameters:
requiredImports- accumulated imports (updated in-place)implementsFrom- interfaces to implementextendsFrom- superclass to extendschemaName- class nameimportSet- extra imports (e.g., fromextends/implements)fillParameters- field container- Returns:
- initialized builder with fields appended
-
capitalize
Converts the first character to title case (e.g.,"userName"→"UserName").- Parameters:
str- input string- Returns:
- capitalized string
-
uncapitalize
Converts the first character to lower case (e.g.,"UserName"→"userName").- Parameters:
str- input string- Returns:
- uncapitalized string
-
isBlank
Checks if aCharSequenceis null, empty, or whitespace only.- Parameters:
cs- char sequence- Returns:
trueif blank
-
isNotBlank
Checks if aCharSequenceis not blank.- Parameters:
cs- char sequence- Returns:
trueif non-blank
-
isAnyEmpty
Checks if any of the input strings is null or empty.- Parameters:
css- vararg strings- Returns:
trueif at least one is empty
-
isNoneEmpty
Checks if none of the input strings is null or empty.- Parameters:
css- vararg strings- Returns:
trueif all are non-empty
-
isEmpty
Checks if aCharSequenceisnullor zero-length.- Parameters:
cs- char sequence- Returns:
trueif empty
-
length
Returns length of aCharSequence, or 0 ifnull.- Parameters:
cs- char sequence- Returns:
- length
-
isEmpty
Checks if an object array is empty.- Parameters:
array- array- Returns:
trueif empty ornull
-
getLength
Returns length of an array or0ifnull.- Parameters:
array- array- Returns:
- length
-
substringBefore
Gets substring before the first occurrence of a separator.- Parameters:
str- source stringseparator- separator- Returns:
- substring before separator, or original if not found
-
isTrue
Checks if aBooleanistrue.- Parameters:
bool- boolean wrapper- Returns:
trueif non-null andtrue
-
getSchemaByName
Retrieves schema definition by name from the globalcomponents.schemassection.- Parameters:
content- full AsyncAPI documentschemaName- schema name- Returns:
- schema map or empty map
-
toValidJavaFieldName
Converts a raw field name (e.g.,"user-name", "user_name") to valid Java field identifier (camelCase). Respects keyword safety.- Parameters:
rawName- raw name from YAML- Returns:
- safe camelCase name
-
toValidJavaEnumConstantName
Converts raw enum constant name (e.g.,"success", "error-code", "\r\n") to valid Java enum identifier (UPPER_SNAKE_CASE), handling control chars and keywords.- Parameters:
rawName- raw enum name- Returns:
- valid UPPER_SNAKE_CASE enum constant name
-