Skip to content

Commit

Permalink
Merge pull request #668 from bci-oss/538-llow-customizing-the-class-n…
Browse files Browse the repository at this point in the history
…ame-generating-POJO

Add Prefix and Postfix to Java Generation
  • Loading branch information
Yauhenikapl authored Nov 4, 2024
2 parents 797c7d1 + 86449e5 commit bf0f521
Show file tree
Hide file tree
Showing 19 changed files with 269 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public static String determinePropertyType( final Optional<Characteristic> optio
return String.format( "Either<%s,%s>", left, right );
}

return getDataType( dataType, codeGenerationConfig.importTracker() );
return getDataType( dataType, codeGenerationConfig.importTracker(), codeGenerationConfig );
}

public static String determineCollectionAspectClassDefinition( final StructureElement element,
Expand All @@ -186,7 +186,7 @@ public static String determineCollectionAspectClassDefinition( final StructureEl
final Characteristic characteristic = property.getEffectiveCharacteristic().orElseThrow( error );
if ( characteristic instanceof Collection ) {
final String collectionType = determineCollectionType( (Collection) characteristic, false, codeGenerationConfig );
final String dataType = getDataType( characteristic.getDataType(), codeGenerationConfig.importTracker() );
final String dataType = getDataType( characteristic.getDataType(), codeGenerationConfig.importTracker(), codeGenerationConfig );
return String.format( "public class %s implements CollectionAspect<%s,%s>", element.getName(), collectionType, dataType );
}
}
Expand All @@ -199,7 +199,7 @@ public static String determineComplexTypeClassDefinition( final ComplexType elem
if ( element.isAbstractEntity() ) {
classDefinitionBuilder.append( "abstract " );
}
classDefinitionBuilder.append( "class " ).append( element.getName() );
classDefinitionBuilder.append( "class " ).append( generateClassName( element, codeGenerationConfig ) );
classDefinitionBuilder.append( genericClassSignature( element ) );
if ( element.getExtends().isPresent() ) {
final ComplexType extendedComplexType = element.getExtends().get();
Expand Down Expand Up @@ -265,20 +265,24 @@ private static String determineCollectionType( final Collection collection, fina

if ( collection.isAllowDuplicates() && collection.isOrdered() ) {
codeGenerationConfig.importTracker().importExplicit( List.class );
return containerType( List.class, getDataType( dataType, codeGenerationConfig.importTracker() ), elementConstraint );
return containerType( List.class, getDataType( dataType, codeGenerationConfig.importTracker(), codeGenerationConfig ),
elementConstraint );
}
if ( !collection.isAllowDuplicates() && collection.isOrdered() ) {
codeGenerationConfig.importTracker().importExplicit( LinkedHashSet.class );
return containerType( LinkedHashSet.class, getDataType( dataType, codeGenerationConfig.importTracker() ), elementConstraint );
return containerType( LinkedHashSet.class, getDataType( dataType, codeGenerationConfig.importTracker(), codeGenerationConfig ),
elementConstraint );
}
if ( collection.isAllowDuplicates() && !collection.isOrdered() ) {
codeGenerationConfig.importTracker().importExplicit( java.util.Collection.class );
return containerType( java.util.Collection.class, getDataType( dataType, codeGenerationConfig.importTracker() ),
return containerType( java.util.Collection.class,
getDataType( dataType, codeGenerationConfig.importTracker(), codeGenerationConfig ),
elementConstraint );
}
if ( !collection.isAllowDuplicates() && !collection.isOrdered() ) {
codeGenerationConfig.importTracker().importExplicit( Set.class );
return containerType( Set.class, getDataType( dataType, codeGenerationConfig.importTracker() ), elementConstraint );
return containerType( Set.class, getDataType( dataType, codeGenerationConfig.importTracker(), codeGenerationConfig ),
elementConstraint );
}
throw new CodeGenerationException( "Could not determine Java collection type for " + collection.getName() );
}
Expand All @@ -304,11 +308,16 @@ public static String containerType( final Class<?> containerClass, final String
* @param importTracker the import tracker
* @return a {@link String} containing the definition of the Java Data Type
*/
public static String getDataType( final Optional<Type> dataType, final ImportTracker importTracker ) {
public static String getDataType( final Optional<Type> dataType, final ImportTracker importTracker,
final JavaCodeGenerationConfig codeGenerationConfig ) {
return dataType.map( type -> {
final Type actualDataType = dataType.get();
if ( actualDataType instanceof ComplexType ) {
return ((ComplexType) actualDataType).getName();
final String complexDataType = ((ComplexType) actualDataType).getName();
if ( (!codeGenerationConfig.namePrefix().isBlank() || !codeGenerationConfig.namePostfix().isBlank()) ) {
return codeGenerationConfig.namePrefix() + complexDataType + codeGenerationConfig.namePostfix();
}
return complexDataType;
}

if ( actualDataType instanceof Scalar ) {
Expand Down Expand Up @@ -353,7 +362,7 @@ public static String toConstant( final String upperOrLowerCamel ) {
}
return TO_CONSTANT.convert( StringUtils.capitalize( upperOrLowerCamel ) );
}

public static boolean isAllUppercaseWithUnderscore( final String upperOrLowerCamel ) {
return upperOrLowerCamel != null && upperOrLowerCamel.matches( "[A-Z0-9_]+" );
}
Expand Down Expand Up @@ -487,7 +496,8 @@ public static String generateFilterCompare( final Optional<Type> optionalDataTyp
public static String getCharacteristicJavaType( final Property property, final JavaCodeGenerationConfig codeGenerationConfig ) {
final Supplier<RuntimeException> error = () -> new CodeGenerationException( "No data type found for Property " + property.getName() );
if ( hasContainerType( property ) ) {
return getDataType( property.getCharacteristic().orElseThrow( error ).getDataType(), codeGenerationConfig.importTracker() );
return getDataType( property.getCharacteristic().orElseThrow( error ).getDataType(), codeGenerationConfig.importTracker(),
codeGenerationConfig );
}

return property.getEffectiveCharacteristic().flatMap( Characteristic::getDataType ).map( type -> {
Expand Down Expand Up @@ -559,6 +569,13 @@ public static String genericClassSignature( final StructureElement element ) {
return generics.isEmpty() ? "" : "<" + generics + ">";
}

public static String generateClassName( final StructureElement element, final JavaCodeGenerationConfig config ) {
if ( (!config.namePrefix().isBlank() || !config.namePostfix().isBlank()) && element.is( StructureElement.class ) ) {
return config.namePrefix() + element.getName() + config.namePostfix();
}
return element.getName();
}

/**
* Generates the comma-separated list of arguments in a constructor call
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ public record JavaCodeGenerationConfig(
String packageName,
ImportTracker importTracker,
boolean executeLibraryMacros,
File templateLibFile
File templateLibFile,
String namePrefix,
String namePostfix

) implements GenerationConfig {
public JavaCodeGenerationConfig {
if ( packageName == null ) {
Expand All @@ -44,5 +47,11 @@ public record JavaCodeGenerationConfig(
if ( executeLibraryMacros && !templateLibFile.exists() ) {
throw new CodeGenerationException( "Incorrect configuration. Please provide a valid path to the velocity template library file." );
}
if ( namePrefix == null ) {
namePrefix = "";
}
if ( namePostfix == null ) {
namePostfix = "";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public JavaArtifact apply( final E element, final JavaCodeGenerationConfig confi
.put( "className", element.getName() )
.put( "codeGenerationConfig", config )
.put( "currentYear", Year.now() )
.put( "dataType", AspectModelJavaUtil.getDataType( element.getDataType(), config.importTracker() ) )
.put( "dataType", AspectModelJavaUtil.getDataType( element.getDataType(), config.importTracker(), config ) )
.put( "Entity", Entity.class )
.put( "enumeration", element )
.put( "importTracker", importTracker )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public JavaArtifact apply( final E element, final JavaCodeGenerationConfig confi

final String generatedSource = new TemplateEngine( context, engineConfiguration ).apply( "java-pojo" );
try {
return new JavaArtifact( Roaster.format( generatedSource ), element.getName(),
return new JavaArtifact( Roaster.format( generatedSource ), AspectModelJavaUtil.generateClassName( element, config ),
config.packageName() );
} catch ( final Exception exception ) {
throw new CodeGenerationException( generatedSource, exception );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ ${util.determineCollectionAspectClassDefinition( $element, $codeGenerationConfig
${util.generateAbstractEntityClassAnnotations( $complexElement, $codeGenerationConfig, $extendingEntities )}
${util.determineComplexTypeClassDefinition( $complexElement, $codeGenerationConfig )}
#else
public class $element.getName()${util.genericClassSignature( $element )} {
public class ${util.generateClassName( $element, $codeGenerationConfig )}${util.genericClassSignature( $element )} {
#end
#foreach( $property in $element.properties )
#javaPojoProperty( $property )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
$codeGenerationConfig.importTracker().importExplicit( $JsonCreator )
@JsonCreator
#end
public ${element.name}(
public ${util.generateClassName( $element, $codeGenerationConfig )}(
$util.constructorArguments( $allProperties, $codeGenerationConfig, $enableJacksonAnnotations )
) #if( $needInitializer ) throws DatatypeConfigurationException #end {
super(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ if (o == null || getClass() != o.getClass()) {
#if( $element.getProperties().isEmpty() ) return true
#else
#set( $objectEqualsExpression = $util.objectEqualsExpression( $element ) )
#set( $className = ${util.generateClassName( $element, $codeGenerationConfig )} )
#if( $objectEqualsExpression && !$objectEqualsExpression.empty )
final $element.getName() that = ($element.getName())o;
final $className that = ($className)o;
return $objectEqualsExpression
#else
return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
#parse( "java-static-class-property-lib.vm" )

#macro( javaStaticClassBody )
$codeGenerationConfig.importTracker().importExplicit( "${codeGenerationConfig.packageName()}.${element.getName()}" )
#set( $className = ${util.generateClassName( $element, $codeGenerationConfig )} )
$codeGenerationConfig.importTracker().importExplicit( "${codeGenerationConfig.packageName()}.${className}" )
/**
* Generated class {@link Meta${element.getName()}}.
* Generated class {@link Meta${className}}.
*/
public class Meta${element.getName()} implements StaticMetaClass<${element.getName()}>, PropertyContainer {
public class Meta${className} implements StaticMetaClass<${className}>, PropertyContainer {
public static final String NAMESPACE = "${modelUrnPrefix}";
public static final String MODEL_ELEMENT_URN = NAMESPACE + "${element.getName()}";

Expand Down Expand Up @@ -49,8 +50,8 @@ static {
#end
#end

public Class<${element.getName()}> getModelClass() {
return ${element.getName()}.class;
public Class<${className}> getModelClass() {
return ${className}.class;
}

@Override
Expand All @@ -65,13 +66,13 @@ static {

@Override
public String getName() {
return "${element.getName()}";
return "${className}";
}

#if( $element.getExtends().isPresent() )
#set( $propertyTypeParameter = "? super ${element.getName()}" )
#set( $propertyTypeParameter = "? super ${className}" )
#else
#set( $propertyTypeParameter = ${element.getName()} )
#set( $propertyTypeParameter = ${className} )
#end
@Override
public List<StaticProperty<$propertyTypeParameter, ?>> getProperties() {
Expand Down
Loading

0 comments on commit bf0f521

Please sign in to comment.