Skip to content

Commit

Permalink
Fix #682 (#688)
Browse files Browse the repository at this point in the history
* Fixed

* Cleaned

* Fixed
  • Loading branch information
SimonCockx authored Nov 20, 2023
1 parent a1e767f commit 14c42d5
Show file tree
Hide file tree
Showing 17 changed files with 275 additions and 230 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class RosettaRequestManager extends RequestManager {
.build());

/*
* TODO: contribute to Xtext
* The code that uses this list fixes a memory leak in the RequestManager and should be contributed
* back to the Xtext project then removed from here
*/
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ import com.rosetta.util.types.JavaType
import java.math.BigInteger
import java.math.BigDecimal
import com.regnosys.rosetta.generator.java.JavaScope
import java.util.List
import java.util.Optional
import java.util.Collections
import java.util.stream.Collectors
import com.rosetta.model.lib.mapper.MapperS
import com.rosetta.model.lib.mapper.MapperC
import com.rosetta.model.lib.expression.ComparisonResult
import com.rosetta.model.lib.mapper.MapperListOfLists
import java.util.function.Function
import com.regnosys.rosetta.generator.java.statement.builder.JavaStatementBuilder
import com.regnosys.rosetta.generator.java.statement.builder.JavaExpression
Expand Down Expand Up @@ -242,7 +240,7 @@ class TypeCoercionService {
if (actual.isComparisonResult) {
if (expected.isMapperS) {
// Case ComparisonResult to MapperS
[JavaExpression.from('''«it».asMapper()''', MapperS.wrap(BOOLEAN))]
[JavaExpression.from('''«it».asMapper()''', MAPPER_S.wrap(BOOLEAN))]
} else if (expected.isMapperC) {
// Case ComparisonResult to MapperC
// Not handled
Expand All @@ -251,7 +249,7 @@ class TypeCoercionService {
// Not handled
} else if (expected.isList) {
// Case ComparisonResult to List
[JavaExpression.from('''«it».getMulti()''', List.wrap(BOOLEAN))]
[JavaExpression.from('''«it».getMulti()''', LIST.wrap(BOOLEAN))]
}
} else if (actual.extendsMapper) {
if (expected.isComparisonResult) {
Expand All @@ -260,26 +258,26 @@ class TypeCoercionService {
} else if (expected.extendsMapper) {
if (actual.isMapperS && actual.hasWildcardArgument && expected.isMapperS && !expected.hasWildcardArgument) {
// Case immutable MapperS<? extends T> to mutable MapperS<T>
[JavaExpression.from('''«it».map("Make mutable", «Function».identity())''', MapperS.wrap(expected.itemType))]
[JavaExpression.from('''«it».map("Make mutable", «Function».identity())''', MAPPER_S.wrap(expected.itemType))]
} else if (actual.isMapperC && actual.hasWildcardArgument && expected.isMapperC && !expected.hasWildcardArgument) {
// Case immutable MapperC<? extends T> to mutable MapperC<T>
[JavaExpression.from('''«it».map("Make mutable", «Function».identity())''', MapperC.wrap(expected.itemType))]
[JavaExpression.from('''«it».map("Make mutable", «Function».identity())''', MAPPER_C.wrap(expected.itemType))]
} else if (actual.isMapperS && expected.isMapperC) {
// Case MapperS to MapperC
[JavaExpression.from('''«MapperC».of(«it»)''', MapperC.wrap(expected.itemType))]
[JavaExpression.from('''«MapperC».of(«it»)''', MAPPER_C.wrap(expected.itemType))]
} else if (actual.isMapperC && expected.isMapperS) {
// Case MapperC to MapperS
[JavaExpression.from('''«MapperS».of(«it».get())''', MapperS.wrap(expected.itemType))]
[JavaExpression.from('''«MapperS».of(«it».get())''', MAPPER_S.wrap(expected.itemType))]
}
} else if (expected.isMapperListOfLists) {
// Case Mapper to MapperListOfLists
// Not handled
} else if (expected.isList) {
// Case Mapper to List
if (actual.hasWildcardArgument && !expected.hasWildcardArgument) {
[JavaExpression.from('''new «ArrayList»<>(«it».getMulti())''', List.wrap(expected.itemType))]
[JavaExpression.from('''new «ArrayList»<>(«it».getMulti())''', LIST.wrap(expected.itemType))]
} else {
[JavaExpression.from('''«it».getMulti()''', List.wrap(expected.itemType))]
[JavaExpression.from('''«it».getMulti()''', LIST.wrap(expected.itemType))]
}
}
} else if (actual.isMapperListOfLists) {
Expand All @@ -290,16 +288,16 @@ class TypeCoercionService {
[JavaExpression.from('''«ComparisonResult».of(«MapperC».of(«it»))''', COMPARISON_RESULT)]
} else if (expected.isMapperS) {
// Case List to MapperS
[JavaExpression.from('''«MapperS».of(«it».get(0))''', MapperS.wrap(expected.itemType))]
[JavaExpression.from('''«MapperS».of(«it».get(0))''', MAPPER_S.wrap(expected.itemType))]
} else if (expected.isMapperC || expected.isMapper) {
// Case List to MapperC/Mapper
[JavaExpression.from('''«MapperC».<«expected.itemType»>of(«it»)''', MapperC.wrap(expected.itemType))]
[JavaExpression.from('''«MapperC».<«expected.itemType»>of(«it»)''', MAPPER_C.wrap(expected.itemType))]
} else if (expected.isMapperListOfLists) {
// Case List to MapperListOfLists
// Not handled
} else if (expected.isList && actual.hasWildcardArgument && !expected.hasWildcardArgument) {
// Case immutable List<? extends T> to mutable List<T>
[JavaExpression.from('''new «ArrayList»(«it»)''', List.wrap(expected.itemType))]
[JavaExpression.from('''new «ArrayList»(«it»)''', LIST.wrap(expected.itemType))]
}
}
)
Expand All @@ -322,11 +320,11 @@ class TypeCoercionService {
private def JavaExpression empty(JavaType expected) {
val itemType = expected.itemType
if (expected.isList) {
JavaExpression.from('''«Collections».<«itemType»>emptyList()''', List.wrap(itemType))
JavaExpression.from('''«Collections».<«itemType»>emptyList()''', LIST.wrap(itemType))
} else if (expected.isMapperS || expected.isMapper) {
JavaExpression.from('''«MapperS».<«itemType»>ofNull()''', MapperS.wrap(itemType))
JavaExpression.from('''«MapperS».<«itemType»>ofNull()''', MAPPER_S.wrap(itemType))
} else if (expected.isMapperC) {
JavaExpression.from('''«MapperC».<«itemType»>ofNull()''', MapperC.wrap(itemType))
JavaExpression.from('''«MapperC».<«itemType»>ofNull()''', MAPPER_C.wrap(itemType))
} else if (expected.isComparisonResult) {
JavaExpression.from('''«ComparisonResult».successEmptyOperand("")''', COMPARISON_RESULT)
} else if (expected == JavaPrimitiveType.BOOLEAN) {
Expand Down Expand Up @@ -408,13 +406,13 @@ class TypeCoercionService {
}
}
private def JavaExpression getItemToListConversionExpression(JavaExpression expression) {
JavaExpression.from('''«Collections».singletonList(«expression»)''', List.wrap(expression.expressionType))
JavaExpression.from('''«Collections».singletonList(«expression»)''', LIST.wrap(expression.expressionType))
}
private def JavaExpression getItemToMapperSConversionExpression(JavaExpression expression) {
JavaExpression.from('''«MapperS».of(«expression»)''', MapperS.wrap(expression.expressionType))
JavaExpression.from('''«MapperS».of(«expression»)''', MAPPER_S.wrap(expression.expressionType))
}
private def JavaExpression getItemToMapperCConversionExpression(JavaExpression expression) {
JavaExpression.from('''«MapperC».of(«Collections».singletonList(«expression»))''', MapperC.wrap(expression.expressionType))
JavaExpression.from('''«MapperC».of(«Collections».singletonList(«expression»))''', MAPPER_C.wrap(expression.expressionType))
}
private def JavaExpression getItemToComparisonResultConversionExpression(JavaExpression expression) {
JavaExpression.from('''«ComparisonResult».of(«MapperS».of(«expression»))''', COMPARISON_RESULT)
Expand All @@ -436,15 +434,15 @@ class TypeCoercionService {
.<«resultItem.expressionType»>map(«lambdaParam» -> «resultItem»)
.collect(«Collectors».toList())
''',
List.wrap(resultItem.expressionType)
LIST.wrap(resultItem.expressionType)
)
}
private def JavaExpression getMapperSItemConversionExpression(JavaExpression expression, Function<JavaExpression, JavaExpression> itemConversion, JavaScope scope) {
val actualItemType = expression.expressionType.itemType
val lambdaScope = scope.lambdaScope
val lambdaParam = lambdaScope.createUniqueIdentifier(actualItemType.simpleName.toFirstLower)
val resultItem = itemConversion.apply(new JavaVariable(lambdaParam, actualItemType))
val resultType = MapperS.wrap(resultItem.expressionType)
val resultType = MAPPER_S.wrap(resultItem.expressionType)
JavaExpression.from(
'''«expression».<«resultType.itemType»>map("Type coercion", «lambdaParam» -> «lambdaParam» == null ? null : «resultItem»)''',
resultType
Expand All @@ -455,7 +453,7 @@ class TypeCoercionService {
val lambdaScope = scope.lambdaScope
val lambdaParam = lambdaScope.createUniqueIdentifier(actualItemType.simpleName.toFirstLower)
val resultItem = itemConversion.apply(new JavaVariable(lambdaParam, actualItemType))
val resultType = MapperC.wrap(resultItem.expressionType)
val resultType = MAPPER_C.wrap(resultItem.expressionType)
JavaExpression.from(
'''«expression».<«resultType.itemType»>map("Type coercion", «lambdaParam» -> «resultItem»)''',
resultType
Expand All @@ -465,8 +463,8 @@ class TypeCoercionService {
val actualItemType = expression.expressionType.itemType
val listToListLambdaScope = scope.lambdaScope
val mapperCParam = listToListLambdaScope.createUniqueIdentifier("mapperC")
val resultMapperC = getMapperCItemConversionExpression(new JavaVariable(mapperCParam, MapperC.wrap(actualItemType)), itemConversion, listToListLambdaScope)
val resultType = MapperListOfLists.wrap(resultMapperC.expressionType.itemType)
val resultMapperC = getMapperCItemConversionExpression(new JavaVariable(mapperCParam, MAPPER_C.wrap(actualItemType)), itemConversion, listToListLambdaScope)
val resultType = MAPPER_LIST_OF_LISTS.wrap(resultMapperC.expressionType.itemType)
JavaExpression.from(
'''«expression».<«resultType.itemType»>mapListToList(«mapperCParam» -> «resultMapperC»)''',
resultType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ import com.rosetta.model.lib.ModelSymbolId
import com.rosetta.util.types.JavaReferenceType
import com.regnosys.rosetta.generator.java.statement.builder.JavaExpression
import com.regnosys.rosetta.generator.java.statement.builder.JavaStatementBuilder
import com.rosetta.model.lib.mapper.MapperC
import com.regnosys.rosetta.generator.java.statement.JavaStatement
import com.regnosys.rosetta.generator.java.types.JavaTypeUtil
import com.rosetta.model.lib.mapper.MapperS
import com.rosetta.util.types.generated.GeneratedJavaClass
import com.regnosys.rosetta.generator.java.expression.TypeCoercionService
import java.util.Collections
import com.fasterxml.jackson.core.type.TypeReference
import com.rosetta.util.types.JavaGenericTypeDeclaration

class FunctionGenerator {

Expand Down Expand Up @@ -119,7 +119,7 @@ class FunctionGenerator {

private def getQualifyingFunctionInterface(List<RAttribute> inputs) {
val parameterVariable = inputs.head.RType.toListOrSingleJavaType(inputs.head.multi)
JavaParameterizedType.from(IQualifyFunctionExtension, parameterVariable)
JavaParameterizedType.from(new TypeReference<IQualifyFunctionExtension<?>>() {}, parameterVariable)
}

private def collectFunctionDependencies(Function func) {
Expand Down Expand Up @@ -277,7 +277,7 @@ class FunctionGenerator {
protected abstract «IF multi»«List»<«returnType»>«ELSE»«returnType»«ENDIF» «classScope.getIdentifierOrThrow(alias)»(«output.toBuilderType» «aliasScope.getIdentifierOrThrow(output)», «IF !inputs.empty»«inputs.inputsAsParameters(aliasScope)»«ENDIF»);
«ELSE»
«val multi = cardinality.isMulti(alias.expression)»
«val returnType = (multi ? MapperC as Class<?> : MapperS).wrapExtendsIfNotFinal(alias.expression)»
«val returnType = (multi ? MAPPER_C as JavaGenericTypeDeclaration<?> : MAPPER_S).wrapExtendsIfNotFinal(alias.expression)»
protected abstract «returnType» «classScope.getIdentifierOrThrow(alias)»(«inputs.inputsAsParameters(aliasScope)»);
«ENDIF»
Expand Down Expand Up @@ -309,7 +309,7 @@ class FunctionGenerator {
«IF aliasOut.get(alias)»
«val multi = cardinality.isMulti(alias.expression)»
«val itemReturnType = shortcutJavaType(alias)»
«val returnType = multi ? List.wrap(itemReturnType) : itemReturnType»
«val returnType = multi ? LIST.wrap(itemReturnType) : itemReturnType»
«val body = expressionGenerator.javaCode(alias.expression, alias.shortcutExpressionJavaType, aliasScope)
.mapExpressionIfNotNull[JavaExpression.from('''toBuilder(«it»)''', returnType)]
»
Expand All @@ -318,7 +318,7 @@ class FunctionGenerator {
protected «returnType» «classScope.getIdentifierOrThrow(alias)»(«output.toBuilderType» «aliasScope.getIdentifierOrThrow(output)», «IF !inputs.empty»«inputs.inputsAsParameters(aliasScope)»«ENDIF») «body.completeAsReturn.toBlock»
«ELSE»
«val multi = cardinality.isMulti(alias.expression)»
«val returnType = (multi ? MapperC : MapperS).wrapExtendsIfNotFinal(alias.expression)»
«val returnType = (multi ? MAPPER_C as JavaGenericTypeDeclaration<?> : MAPPER_S).wrapExtendsIfNotFinal(alias.expression)»
@Override
protected «returnType» «classScope.getIdentifierOrThrow(alias)»(«inputs.inputsAsParameters(aliasScope)») «expressionGenerator.javaCode(alias.expression, returnType, aliasScope).completeAsReturn.toBlock»
Expand Down Expand Up @@ -413,12 +413,12 @@ class FunctionGenerator {
if (needsToCopy) {
javaExpr =
javaExpr
.mapExpressionIfNotNull[JavaExpression.from('''new «ArrayList»<>(«it»)''', List.wrap(effectiveExprType.itemType))]
.mapExpressionIfNotNull[JavaExpression.from('''new «ArrayList»<>(«it»)''', LIST.wrap(effectiveExprType.itemType))]
}
}
switch(op.ROperationType) {
case ADD: {
javaExpr = coercionService.addCoercions(javaExpr, attribute.isMulti ? List.wrapExtends(attribute.toBuilderItemType) : attribute.toBuilderItemType, scope)
javaExpr = coercionService.addCoercions(javaExpr, attribute.isMulti ? LIST.wrapExtends(attribute.toBuilderItemType) : attribute.toBuilderItemType, scope)
javaExpr
.mapExpression[
JavaExpression.from(
Expand Down Expand Up @@ -463,7 +463,7 @@ class FunctionGenerator {
if (cardinality.isMulti(op.expression)) {
val lambdaScope = scope.lambdaScope
val item = lambdaScope.createUniqueIdentifier("item")
expressionGenerator.javaCode(op.expression, MapperC.wrap(op.expression), scope)
expressionGenerator.javaCode(op.expression, MAPPER_C.wrap(op.expression), scope)
.collapseToSingleExpression(scope)
.mapExpression[
JavaExpression.from(
Expand All @@ -476,7 +476,7 @@ class FunctionGenerator {
.build())
.collect(«Collectors».toList())
''',
List.wrap(metaClass)
LIST.wrap(metaClass)
)
]
} else {
Expand Down Expand Up @@ -621,7 +621,7 @@ class FunctionGenerator {
private def JavaType toBuilderType(RAttribute rAttribute) {
val javaType = rAttribute.toBuilderItemType
if (rAttribute.multi) {
return List.wrap(javaType)
return LIST.wrap(javaType)
} else {
return javaType
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import com.rosetta.util.types.JavaClass
import com.rosetta.util.types.JavaParameterizedType
import javax.inject.Inject
import com.rosetta.util.types.generated.GeneratedJavaClass
import com.fasterxml.jackson.core.type.TypeReference

class MetaFieldGenerator {
@Inject extension ImportManagerExtension
Expand Down Expand Up @@ -231,7 +232,7 @@ class MetaFieldGenerator {
valueAttribute, metaAttribute
])

val FWMType = JavaParameterizedType.from(FieldWithMeta, typeCall.typeCallToRType.toJavaReferenceType)
val FWMType = JavaParameterizedType.from(new TypeReference<FieldWithMeta<?>>() {}, typeCall.typeCallToRType.toJavaReferenceType)

val scope = new JavaScope(metaJavaType.packageName)

Expand Down Expand Up @@ -280,7 +281,7 @@ class MetaFieldGenerator {
d.model = RosettaFactory.eINSTANCE.createRosettaModel
d.model.name = metaJavaType.packageName.withDots
d.attributes.addAll(referenceAttributes(typeCall))
val refInterface = JavaParameterizedType.from(ReferenceWithMeta, typeCall.typeCallToRType.toJavaReferenceType)
val refInterface = JavaParameterizedType.from(new TypeReference<ReferenceWithMeta<?>>() {}, typeCall.typeCallToRType.toJavaReferenceType)

val scope = new JavaScope(root.metaField)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.rosetta.util.types.JavaParameterizedType
import javax.inject.Inject
import org.eclipse.xtext.generator.IFileSystemAccess2
import com.regnosys.rosetta.rosetta.RosettaReport
import com.fasterxml.jackson.core.type.TypeReference

class ReportGenerator {
@Inject extension RObjectFactory
Expand All @@ -23,7 +24,7 @@ class ReportGenerator {
val rFunction = buildRFunction(report)
val clazz = rFunction.toFunctionJavaClass
val topScope = new JavaScope(clazz.packageName)
val baseInterface = JavaParameterizedType.from(ReportFunction, rFunction.inputs.head.attributeToJavaType, rFunction.output.attributeToJavaType)
val baseInterface = JavaParameterizedType.from(new TypeReference<ReportFunction<?, ?>>() {}, rFunction.inputs.head.attributeToJavaType, rFunction.output.attributeToJavaType)
val reportAnnotationArguments = '''namespace="«report.model.name»", body="«report.regulatoryBody.body.name»", corpusList={«FOR corpus: report.regulatoryBody.corpusList SEPARATOR ", "»"«corpus.name»"«ENDFOR»}'''
val classBody = functionGenerator.rBuildClass(rFunction, false, #[baseInterface], #{com.rosetta.model.lib.annotations.RosettaReport -> reportAnnotationArguments}, true, topScope);
val content = buildClass(clazz.packageName, classBody, topScope)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.rosetta.model.lib.reports.ReportFunction
import com.rosetta.util.types.JavaParameterizedType
import com.regnosys.rosetta.generator.java.function.FunctionGenerator
import com.regnosys.rosetta.rosetta.RosettaRule
import com.fasterxml.jackson.core.type.TypeReference

class RuleGenerator {
@Inject extension JavaTypeTranslator
Expand All @@ -22,7 +23,7 @@ class RuleGenerator {
def generate(RootPackage root, IFileSystemAccess2 fsa, RosettaRule rule, String version) {
val rFunctionRule = buildRFunction(rule)
val clazz = rFunctionRule.toFunctionJavaClass
val baseInterface = JavaParameterizedType.from(ReportFunction, rFunctionRule.inputs.head.attributeToJavaType, rFunctionRule.output.attributeToJavaType)
val baseInterface = JavaParameterizedType.from(new TypeReference<ReportFunction<?, ?>>() {}, rFunctionRule.inputs.head.attributeToJavaType, rFunctionRule.output.attributeToJavaType)
val topScope = new JavaScope(clazz.packageName)
val classBody = functionGenerator.rBuildClass(rFunctionRule, false, #[baseInterface], emptyMap, true, topScope)

Expand Down
Loading

0 comments on commit 14c42d5

Please sign in to comment.