Skip to content

Commit

Permalink
Merge pull request #516 from bci-oss/515-extend-java-static-meta-mode…
Browse files Browse the repository at this point in the history
…l-accessors

Extend java static meta model accessors
  • Loading branch information
atextor authored Jan 29, 2024
2 parents 126edff + 50c4d5a commit 3ad1a49
Show file tree
Hide file tree
Showing 13 changed files with 512 additions and 228 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ static {
#end

#foreach( $property in $deconstructor.getAllProperties() )
#javaStaticClassProperty( $property $codeGenerationConfig )
#javaStaticClassProperty( $property $codeGenerationConfig $element )
#end
#foreach( $property in $element.getProperties() )
#if ( !$property.isAbstract() )
#javaStaticClassProperty( $property $codeGenerationConfig )
#javaStaticClassProperty( $property $codeGenerationConfig $element )
#end
#end

Expand All @@ -68,15 +68,20 @@ static {
return "${element.getName()}";
}

#if( $element.getExtends().isPresent() )
#set( $propertyTypeParameter = "? super ${element.getName()}" )
#else
#set( $propertyTypeParameter = ${element.getName()} )
#end
@Override
public List<StaticProperty<?>> getProperties() {
public List<StaticProperty<$propertyTypeParameter, ?>> getProperties() {
return Arrays.asList(
${util.staticPropertiesExpression( $element )}
);
}

@Override
public List<StaticProperty<?>> getAllProperties() {
public List<StaticProperty<$propertyTypeParameter, ?>> getAllProperties() {
#if( $element.getExtends().isPresent() )
#set( $extendedElement = $element.getExtends().get() )
$codeGenerationConfig.importTracker().importExplicit( "java.util.stream.Stream" )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
$characteristic.accept($modelVisitor, $context)
#end

#macro( javaStaticClassProperty $property $codeGenerationConfig )
#macro( javaStaticClassProperty $property $codeGenerationConfig $element )
#set( $propertyType = $util.getPropertyType( $property, $codeGenerationConfig ) )

## public static final $type $property = (definition)
Expand All @@ -67,30 +67,30 @@
#if( $util.hasContainerType( $property ) )
$codeGenerationConfig.importTracker().importExplicit( $StaticConstraintContainerProperty )
#set( $containedType = $util.getCharacteristicJavaType( $property, $codeGenerationConfig ) )
#propertyDeclaration() StaticConstraintContainerProperty<$containedType, $propertyType, #getCharacteristicClassName()>
#propertyDeclaration() StaticConstraintContainerProperty<${element.getName()}, $containedType, $propertyType, #getCharacteristicClassName()>
$util.toConstant( $property.getName() ) = #staticProperty( $property $codeGenerationConfig );
#elseif( $util.hasUnit( $property.getCharacteristic().get() ) )
$codeGenerationConfig.importTracker().importExplicit( $StaticConstraintUnitProperty )
$codeGenerationConfig.importTracker().importExplicit( $Unit )
#propertyDeclaration() StaticConstraintUnitProperty<$propertyType, #getCharacteristicClassName()>
#propertyDeclaration() StaticConstraintUnitProperty<${element.getName()}, $propertyType, #getCharacteristicClassName()>
$util.toConstant( $property.getName() ) = #staticProperty( $property $codeGenerationConfig );
#else
#propertyDeclaration() StaticConstraintProperty<$propertyType, #getCharacteristicClassName()>
#propertyDeclaration() StaticConstraintProperty<${element.getName()}, $propertyType, #getCharacteristicClassName()>
$util.toConstant( $property.getName() ) = #staticProperty( $property $codeGenerationConfig );
#end
#else
#if( $util.hasContainerType( $property ) && !$propertyType.startsWith( "Map" ) )
$codeGenerationConfig.importTracker().importExplicit( $StaticContainerProperty )
#set( $containedType = $util.getCharacteristicJavaType( $property, $codeGenerationConfig ) )
#propertyDeclaration() StaticContainerProperty<$containedType, java.util.$propertyType> $util.toConstant( $property.getName() ) =
#propertyDeclaration() StaticContainerProperty<${element.getName()}, $containedType, java.util.$propertyType> $util.toConstant( $property.getName() ) =
#staticProperty( $property $codeGenerationConfig );
#elseif( $util.hasUnit( $property.getCharacteristic().get() ) )
$codeGenerationConfig.importTracker().importExplicit( $StaticUnitProperty )
$codeGenerationConfig.importTracker().importExplicit( $Unit )
#propertyDeclaration() StaticUnitProperty<$propertyType> $util.toConstant( $property.getName() ) =
#propertyDeclaration() StaticUnitProperty<${element.getName()}, $propertyType> $util.toConstant( $property.getName() ) =
#staticProperty( $property $codeGenerationConfig );
#else
#propertyDeclaration() StaticProperty<$propertyType> $util.toConstant( $property.getName() ) =
#propertyDeclaration() StaticProperty<${element.getName()}, $propertyType> $util.toConstant( $property.getName() ) =
#staticProperty( $property $codeGenerationConfig );
#end
#end
Expand All @@ -108,25 +108,25 @@
#if( $util.hasContainerType( $property ) )
$codeGenerationConfig.importTracker().importExplicit( $StaticConstraintContainerProperty )
#set( $containedType = $util.getCharacteristicJavaType( $property, $codeGenerationConfig ) )
new StaticConstraintContainerProperty<$containedType, $propertyType, #getCharacteristicClassName()>(
new StaticConstraintContainerProperty<${element.getName()}, $containedType, $propertyType, #getCharacteristicClassName()>(
#elseif( $util.hasUnit( $property.getCharacteristic().get() ) )
$codeGenerationConfig.importTracker().importExplicit( $StaticConstraintUnitProperty )
$codeGenerationConfig.importTracker().importExplicit( $Unit )
new StaticConstraintUnitProperty<$propertyType, #getCharacteristicClassName()>(
new StaticConstraintUnitProperty<${element.getName()}, $propertyType, #getCharacteristicClassName()>(
#else
new StaticConstraintProperty<$propertyType, #getCharacteristicClassName()>(
new StaticConstraintProperty<${element.getName()}, $propertyType, #getCharacteristicClassName()>(
#end
#else
#if( $util.hasContainerType( $property ) && !$propertyType.startsWith( "Map" ) )
$codeGenerationConfig.importTracker().importExplicit( $StaticContainerProperty )
#set( $containedType = $util.getCharacteristicJavaType( $property, $codeGenerationConfig ) )
new StaticContainerProperty<$containedType, $propertyType> (
new StaticContainerProperty<${element.getName()}, $containedType, $propertyType> (
#elseif( $util.hasUnit( $property.getCharacteristic().get() ) )
$codeGenerationConfig.importTracker().importExplicit( $StaticUnitProperty )
$codeGenerationConfig.importTracker().importExplicit( $Unit )
new StaticUnitProperty<$propertyType>(
new StaticUnitProperty<${element.getName()}, $propertyType>(
#else
new StaticProperty<$propertyType>(
new StaticProperty<${element.getName()}, $propertyType>(
#end
#end

Expand Down Expand Up @@ -182,6 +182,12 @@ public Class<$propertyType> getPropertyType() {
}
#end

#set( $getterName = $util.getterName( $property ) )
@Override
public $propertyType getValue(${element.getName()} object) {
return object.$getterName();
}

#if( $util.hasUnit( $property.getCharacteristic().get() ) )
$codeGenerationConfig.importTracker().importExplicit( $Unit )
$codeGenerationConfig.importTracker().importExplicit( $Units )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

package org.eclipse.esmf.aspectmodel.java;

import static org.assertj.core.api.Assertions.*;
import static org.assertj.core.api.Assertions.anyOf;
import static org.assertj.core.api.Assertions.assertThat;

import java.io.File;
import java.io.IOException;
Expand All @@ -26,8 +27,6 @@
import java.util.function.Function;
import java.util.stream.Collectors;

import org.assertj.core.api.Condition;

import com.github.javaparser.ParseResult;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.Problem;
Expand Down Expand Up @@ -58,6 +57,7 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.reflect.TypeToken;
import com.google.inject.util.Types;
import org.assertj.core.api.Condition;

class GenerationResult {

Expand Down Expand Up @@ -113,12 +113,14 @@ void assertNumberOfFiles( final int expectedNumberOfFiles ) {
* @param fieldTypesOrTypeNames the expected fields and their types or type names
* @param annotations the expected annotations concatenated as one string, indexed by field name
*/
void assertFields( final String className, final ImmutableMap<String, Object> fieldTypesOrTypeNames, final Map<String, String> annotations ) {
void assertFields( final String className, final ImmutableMap<String, Object> fieldTypesOrTypeNames,
final Map<String, String> annotations ) {
assertThat( compilationUnits ).containsKey( className );

final List<FieldDeclaration> fields = compilationUnits.get( className ).findAll( FieldDeclaration.class );
assertThat( fields ).hasSize( fieldTypesOrTypeNames.size() );
assertThat( fields ).extracting( field -> field.resolve().getName() ).containsExactlyInAnyOrderElementsOf( fieldTypesOrTypeNames.keySet() );
assertThat( fields ).extracting( field -> field.resolve().getName() )
.containsExactlyInAnyOrderElementsOf( fieldTypesOrTypeNames.keySet() );
assertThat( fields )
.allSatisfy(
field -> {
Expand All @@ -142,7 +144,8 @@ void assertFields( final String className, final ImmutableMap<String, Object> fi
}
}

void assertCollectionElementValidationAnnotations( final String className, final String fieldName, final String expectedFieldDeclaration ) {
void assertCollectionElementValidationAnnotations( final String className, final String fieldName,
final String expectedFieldDeclaration ) {
final List<FieldDeclaration> fields = compilationUnits.get( className ).findAll( FieldDeclaration.class );
fields.stream().filter( fieldDeclaration -> fieldDeclaration.resolve().getName().equals( fieldName ) )
.forEach( fieldDeclaration -> {
Expand Down Expand Up @@ -184,7 +187,8 @@ void assertConstructorBody( final String className, final Set<String> expectedbo
bodyStatements.stream().map( Node::toString ).collect( Collectors.joining() ) );
}

void assertMethodBody( final String className, final String methodName, final boolean override, final Optional<PrimitiveType> expectedReturnType,
void assertMethodBody( final String className, final String methodName, final boolean override,
final Optional<PrimitiveType> expectedReturnType,
final int expectedNumberOfParameters, final List<String> expectedMethodBody ) {
final Optional<MethodDeclaration> possibleMethodDeclaration = compilationUnits.get( className )
.findAll( MethodDeclaration.class ).stream()
Expand Down Expand Up @@ -256,10 +260,11 @@ private TypeResolution resolveRecursive( final TypeResolution toResolve ) {
* @param className the name of the {@code Enumeration} to be tested
* @param expectedConstants a list of the expected constants
* @param expectedConstantArguments a {@code Map} containing the {@code String} representation of the argument
* for a constant. The key is the name of the constant. If this map is given, the number of entries should
* match the number of expected constants.
* for a constant. The key is the name of the constant. If this map is given, the number of entries should
* match the number of expected constants.
*/
void assertEnumConstants( final String className, final Collection<String> expectedConstants, final Map<String, String> expectedConstantArguments ) {
void assertEnumConstants( final String className, final Collection<String> expectedConstants,
final Map<String, String> expectedConstantArguments ) {
assertThat( compilationUnits ).containsKey( className );
assertThat( compilationUnits.get( className ).getPrimaryType() ).hasValueSatisfying( type -> {
assertThat( type.isEnumDeclaration() ).isTrue();
Expand Down Expand Up @@ -336,24 +341,16 @@ String getClassNameToResolve() {
if ( toResolve.asArrayType().getComponentType().isReferenceType() ) {
return "[L" + toResolve.asArrayType().getComponentType().asReferenceType().getQualifiedName() + ";";
}
switch ( toResolve.asArrayType().getComponentType().asPrimitive() ) {
case BOOLEAN:
return "[Z";
case BYTE:
return "[B";
case CHAR:
return "[C";
case DOUBLE:
return "[D";
case FLOAT:
return "[F";
case INT:
return "[I";
case LONG:
return "[J";
case SHORT:
return "[S";
}
return switch ( toResolve.asArrayType().getComponentType().asPrimitive() ) {
case BOOLEAN -> "[Z";
case BYTE -> "[B";
case CHAR -> "[C";
case DOUBLE -> "[D";
case FLOAT -> "[F";
case INT -> "[I";
case LONG -> "[J";
case SHORT -> "[S";
};
}
final String fullName = toResolve.describe();
return fullName.contains( "<" ) ? fullName.substring( 0, fullName.indexOf( "<" ) ) : fullName;
Expand All @@ -374,4 +371,8 @@ void assertCopyright( final String className, final String expectedCopyright ) {
final Comment copyrightComment = compilationUnit.getComment().get();
assertThat( copyrightComment.toString() ).contains( expectedCopyright );
}

Class<?> getGeneratedClass( final QualifiedName qualifiedName ) {
return generatedClasses.get( qualifiedName );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@
import java.io.IOException;
import java.util.Set;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.eclipse.esmf.samm.KnownVersion;
import org.eclipse.esmf.test.TestAspect;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;

import org.eclipse.esmf.samm.KnownVersion;
import org.eclipse.esmf.test.TestAspect;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

public class StaticMetaModelBaseAttributesTest extends StaticMetaModelGeneratorTest {

Expand Down Expand Up @@ -142,6 +141,7 @@ public void testGeneratedMetaModelContainsRequiredMethods( final KnownVersion me
.put( "getProperties", "return Arrays.asList(TEST_BOOLEAN);" )
.put( "getAllProperties", "return getProperties();" )
.put( "getPropertyType", "return Boolean.class;" )
.put( "getValue", "return object.isTestBoolean();" )
.build();

result.assertMethods( "MetaAspectWithBoolean", expectedMethodBodies );
Expand Down Expand Up @@ -182,6 +182,7 @@ public void testGeneratedMetaModelContainsOptionalMethods( final KnownVersion me
.put( "getPreferredNames", getPreferredNamesBody )
.put( "getDescriptions", getDescriptionsBody )
.put( "getPropertyType", "return Boolean.class;" )
.put( "getValue", "return object.isTestBoolean();" )
.build();

result.assertMethods( "MetaAspectWithAllBaseAttributes", expectedMethodBodies );
Expand Down Expand Up @@ -212,6 +213,7 @@ public void testGeneratedMetaModelContainsGetPreferredNamesMethod( final KnownVe
.put( "getAllProperties", "return getProperties();" )
.put( "getPreferredNames", getPreferredNamesBody )
.put( "getPropertyType", "return Boolean.class;" )
.put( "getValue", "return object.isTestBoolean();" )
.build();

result.assertMethods( "MetaAspectWithPreferredNames", expectedMethodBodies );
Expand Down Expand Up @@ -242,6 +244,7 @@ public void testGeneratedMetaModelContainsGetDescriptionsMethod( final KnownVers
.put( "getAllProperties", "return getProperties();" )
.put( "getDescriptions", getDescriptionsBody )
.put( "getPropertyType", "return Boolean.class;" )
.put( "getValue", "return object.isTestBoolean();" )
.build();

result.assertMethods( "MetaAspectWithDescriptions", expectedMethodBodies );
Expand All @@ -263,6 +266,7 @@ public void testGeneratedMetaModelContainsGetSeeMethod( final KnownVersion metaM
.put( "getProperties", "return Arrays.asList(TEST_BOOLEAN);" )
.put( "getAllProperties", "return getProperties();" )
.put( "getPropertyType", "return Boolean.class;" )
.put( "getValue", "return object.isTestBoolean();" )
.build();

result.assertMethods( "MetaAspectWithPropertyWithSee", expectedMethodBodies );
Expand Down
Loading

0 comments on commit 3ad1a49

Please sign in to comment.