Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,28 +53,41 @@
//
var decType = (ASTCDType) decoratorData.getAsDecorated(originalClazz);
if (MCTypeFacade.getInstance().isBooleanType(attribute.getMCType())) {
this.getterData.getOrCreateMethods(attribute).add(decorateMandatory(decType, attribute));
decorateMandatory(decType, attribute).ifPresent(this.getterData.getOrCreateMethods(
attribute)::add);
}
else if (MCCollectionSymTypeRelations.isList(attribute.getSymbol().getType())) {
this.getterData.getOrCreateMethods(attribute).add(decorateList(decType, attribute));
decorateList(decType, attribute).ifPresent(this.getterData.getOrCreateMethods(
attribute)::add);
decorateWithAssocFunctions(decType, attribute, true);
}
else if (MCCollectionSymTypeRelations.isSet(attribute.getSymbol().getType())) {
this.getterData.getOrCreateMethods(attribute).add(decorateSet(decType, attribute));
decorateSet(decType, attribute).ifPresent(this.getterData.getOrCreateMethods(
attribute)::add);
decorateWithAssocFunctions(decType, attribute, false);
}
else if (MCCollectionSymTypeRelations.isOptional(attribute.getSymbol().getType())) {
this.getterData.getOrCreateMethods(attribute).add(decorateOptional(decType, attribute));
this.getterData.getOrCreateMethods(attribute).add(decorateOptionalIsPresent(decType,
attribute));
decorateOptional(decType, attribute).ifPresent(this.getterData.getOrCreateMethods(
attribute)::add);
decorateOptionalIsPresent(decType, attribute).ifPresent(this.getterData.getOrCreateMethods(
attribute)::add);
}
else {
this.getterData.getOrCreateMethods(attribute).add(decorateMandatory(decType, attribute));
decorateMandatory(decType, attribute).ifPresent(this.getterData.getOrCreateMethods(
attribute)::add);
}
}
}

protected MethodInformation decorateMandatory(ASTCDType decoratedType, ASTCDAttribute attribute) {
protected void logAlreadyExistingWarning(ASTCDAttribute attribute) {
Log.warn("0xTODO: Unable to decorate setter of `" + attribute.getName()

Check warning on line 83 in cdlang/src/main/java/de/monticore/cd/codegen/decorators/GetterDecorator.java

View workflow job for this annotation

GitHub Actions / check-error-codes

ErrorCodeInvalid

Invalid error code 0xTODO (./cdlang/src/main/java/de/monticore/cd/codegen/decorators/GetterDecorator.java:83)
+ "` as such a method already exists.", attribute.get_SourcePositionStart(), attribute
.get_SourcePositionEnd());

}

protected Optional<MethodInformation> decorateMandatory(ASTCDType decoratedType,
ASTCDAttribute attribute) {
String name = (MCTypeFacade.getInstance().isBooleanType(attribute.getMCType()) ? "is" : "get")
+ StringTransformations.capitalize(attribute.getName());
ASTMCType type = attribute.getMCType().deepClone();
Expand All @@ -84,15 +97,19 @@
"methods.Get", attribute)));
method.getModifier().setAbstract(attribute.getModifier().isDerived());

addToClass(decoratedType, method);
if (!addToClass(decoratedType, method)) {
logAlreadyExistingWarning(attribute);
return Optional.empty();
}

this.updateModifier(attribute);

return new MethodInformation(GetterMethodKind.GET_MANDATORY_OR_OPT, method, "methods.Get",
attribute.getName());
return Optional.of(new MethodInformation(GetterMethodKind.GET_MANDATORY_OR_OPT, method,
"methods.Get", attribute.getName()));
}

protected MethodInformation decorateOptional(ASTCDType decoratedType, ASTCDAttribute attribute) {
protected Optional<MethodInformation> decorateOptional(ASTCDType decoratedType,
ASTCDAttribute attribute) {
String name = "get" + StringTransformations.capitalize(attribute.getName());
ASTMCType type = getCDGenService().getFirstTypeArgument(attribute.getMCType()).deepClone();

Expand All @@ -107,26 +124,33 @@
getMethod.getModifier().setAbstract(attribute.getModifier().isDerived());
CD4C.getInstance().addImport(decoratedType, Log.class.getName());

addToClass(decoratedType, getMethod);
return new MethodInformation(GetterMethodKind.GET_MANDATORY_OR_OPT, getMethod,
"methods.opt.Get4Opt", attribute.getName());
if (!addToClass(decoratedType, getMethod)) {
logAlreadyExistingWarning(attribute);
return Optional.empty();
}
return Optional.of(new MethodInformation(GetterMethodKind.GET_MANDATORY_OR_OPT, getMethod,
"methods.opt.Get4Opt", attribute.getName()));
}

protected MethodInformation decorateOptionalIsPresent(ASTCDType decoratedType,
protected Optional<MethodInformation> decorateOptionalIsPresent(ASTCDType decoratedType,
ASTCDAttribute attribute) {
ASTCDMethod isPresentMethod = CDMethodFacade.getInstance().createMethod(attribute.getModifier()
.deepClone(), MCTypeFacade.getInstance().createBooleanType(), "isPresent"
+ StringTransformations.capitalize(attribute.getName()));
glexOpt.ifPresent(glex -> glex.replaceTemplate(EMPTY_BODY, isPresentMethod,
new TemplateHookPoint("methods.opt.IsPresent4Opt", attribute)));
addToClass(decoratedType, isPresentMethod);
if (!addToClass(decoratedType, isPresentMethod)) {
logAlreadyExistingWarning(attribute);
return Optional.empty();
}

this.updateModifier(attribute);
return new MethodInformation(GetterMethodKind.IS_PRESENT, isPresentMethod,
"methods.opt.IsPresent4Opt", attribute.getName());
return Optional.of(new MethodInformation(GetterMethodKind.IS_PRESENT, isPresentMethod,
"methods.opt.IsPresent4Opt", attribute.getName()));
}

protected MethodInformation decorateSet(ASTCDType decoratedType, ASTCDAttribute attribute) {
protected Optional<MethodInformation> decorateSet(ASTCDType decoratedType,
ASTCDAttribute attribute) {
String name = "get" + StringTransformations.capitalize(attribute.getName());
ASTMCType type = getCDGenService().getFirstTypeArgument(attribute.getMCType()).deepClone();

Expand All @@ -135,15 +159,19 @@
glexOpt.ifPresent(glex -> glex.replaceTemplate(EMPTY_BODY, getListMethod, new TemplateHookPoint(
"methods.Get", attribute)));
getListMethod.getModifier().setAbstract(attribute.getModifier().isDerived());
addToClass(decoratedType, getListMethod);
if (!addToClass(decoratedType, getListMethod)) {
logAlreadyExistingWarning(attribute);
return Optional.empty();
}

this.updateModifier(attribute);

return new MethodInformation(GetterMethodKind.GET_COLLECTION, getListMethod, "methods.Get",
attribute.getName());
return Optional.of(new MethodInformation(GetterMethodKind.GET_COLLECTION, getListMethod,
"methods.Get", attribute.getName()));
}

protected MethodInformation decorateList(ASTCDType decoratedType, ASTCDAttribute attribute) {
protected Optional<MethodInformation> decorateList(ASTCDType decoratedType,
ASTCDAttribute attribute) {
String name = "get" + StringTransformations.capitalize(attribute.getName());
ASTMCType type = getCDGenService().getFirstTypeArgument(attribute.getMCType()).deepClone();

Expand All @@ -152,11 +180,14 @@
glexOpt.ifPresent(glex -> glex.replaceTemplate(EMPTY_BODY, getListMethod, new TemplateHookPoint(
"methods.Get", attribute)));
getListMethod.getModifier().setAbstract(attribute.getModifier().isDerived());
addToClass(decoratedType, getListMethod);
if (!addToClass(decoratedType, getListMethod)) {
logAlreadyExistingWarning(attribute);
return Optional.empty();
}

this.updateModifier(attribute);
return new MethodInformation(GetterMethodKind.GET_COLLECTION, getListMethod, "methods.Get",
attribute.getName());
return Optional.of(new MethodInformation(GetterMethodKind.GET_COLLECTION, getListMethod,
"methods.Get", attribute.getName()));
}

protected void decorateWithAssocFunctions(ASTCDType decoratedType, ASTCDAttribute attribute,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,40 +54,43 @@

switch (info.getMultiplicity()) {
case OPTIONAL:
this.setterData.getOrCreateMethods(attribute).add(decorateOptionalAbsent(decClazz,
attribute));
this.setterData.getOrCreateMethods(attribute).add(decorateOptSet(decClazz, attribute));
decorateOptionalAbsent(decClazz, attribute).ifPresent(this.setterData.getOrCreateMethods(
attribute)::add);
decorateOptSet(decClazz, attribute).ifPresent(this.setterData.getOrCreateMethods(
attribute)::add);
break;
case MANDATORY:
this.setterData.getOrCreateMethods(attribute).add(decorateMandatory(decClazz, attribute));
decorateMandatory(decClazz, attribute).ifPresent(this.setterData.getOrCreateMethods(
attribute)::add);
break;
case SET:
if (info.isOrdered()) {
this.setterData.getOrCreateMethods(attribute).add(decorateAddWithIndex(decClazz,
attribute));
this.setterData.getOrCreateMethods(attribute).add(decorateRemoveWithIndex(decClazz,
attribute));
decorateAddWithIndex(decClazz, attribute).ifPresent(this.setterData.getOrCreateMethods(
attribute)::add);
decorateRemoveWithIndex(decClazz, attribute).ifPresent(this.setterData
.getOrCreateMethods(attribute)::add);
}
else {
this.setterData.getOrCreateMethods(attribute).add(decorateAddUnordered(decClazz,
attribute));
this.setterData.getOrCreateMethods(attribute).add(decorateRemoveUnordered(decClazz,
attribute));
decorateAddUnordered(decClazz, attribute).ifPresent(this.setterData.getOrCreateMethods(
attribute)::add);
decorateRemoveUnordered(decClazz, attribute).ifPresent(this.setterData
.getOrCreateMethods(attribute)::add);
}

}
updateModifier(attribute);
}
}

protected MethodInformation decorateMandatory(ASTCDType clazz, ASTCDAttribute attribute) {
protected Optional<MethodInformation> decorateMandatory(ASTCDType clazz,
ASTCDAttribute attribute) {
String name = "set" + StringUtils.capitalize(StringTransformations.capitalize(attribute
.getName()));
return decorate(clazz, attribute, SetterMethodKind.SET_MANDATORY_OR_OPT, "methods.Set", name,
CDParameterFacade.getInstance().createParameters(attribute), attribute);
}

protected MethodInformation decorateOptSet(ASTCDType clazz, ASTCDAttribute attribute) {
protected Optional<MethodInformation> decorateOptSet(ASTCDType clazz, ASTCDAttribute attribute) {
String name = "set" + StringUtils.capitalize(StringTransformations.capitalize(attribute
.getName()));
ASTMCType type = getCDGenService().getFirstTypeArgument(attribute.getMCType()).deepClone();
Expand All @@ -96,14 +99,16 @@
attribute, "--unused--");
}

protected MethodInformation decorateOptionalAbsent(ASTCDType clazz, ASTCDAttribute attribute) {
protected Optional<MethodInformation> decorateOptionalAbsent(ASTCDType clazz,
ASTCDAttribute attribute) {
String name = "set" + StringUtils.capitalize(StringTransformations.capitalize(attribute
.getName())) + "Absent";
return decorate(clazz, attribute, SetterMethodKind.UNSET_OPTIONAL, "methods.opt.SetAbsent",
name, List.of(), attribute);
}

protected MethodInformation decorateAddWithIndex(ASTCDType clazz, ASTCDAttribute attribute) {
protected Optional<MethodInformation> decorateAddWithIndex(ASTCDType clazz,
ASTCDAttribute attribute) {
String name = "add" + StringUtils.capitalize(StringTransformations.capitalize(attribute
.getName()));
ASTMCType type = getCDGenService().getFirstTypeArgument(attribute.getMCType()).deepClone();
Expand All @@ -112,40 +117,52 @@
.getInstance().createParameter(type, attribute.getName())), attribute);
}

protected MethodInformation decorateRemoveWithIndex(ASTCDType clazz, ASTCDAttribute attribute) {
protected Optional<MethodInformation> decorateRemoveWithIndex(ASTCDType clazz,
ASTCDAttribute attribute) {
String name = "remove" + StringUtils.capitalize(StringTransformations.capitalize(attribute
.getName()));
ASTMCType type = getCDGenService().getFirstTypeArgument(attribute.getMCType()).deepClone();
var m = decorate(clazz, attribute, SetterMethodKind.REM, "methods.list.Rem", name, List.of(
CDParameterFacade.getInstance().createParameter(int.class, "index")), attribute);
m.getSetMethod().setMCReturnType(MCBasicTypesMill.mCReturnTypeBuilder().setMCType(type)
if (m.isEmpty()) {
return m;
}
m.get().getSetMethod().setMCReturnType(MCBasicTypesMill.mCReturnTypeBuilder().setMCType(type)
.build());
return m;
}

protected MethodInformation decorateAddUnordered(ASTCDType clazz, ASTCDAttribute attribute) {
protected Optional<MethodInformation> decorateAddUnordered(ASTCDType clazz,
ASTCDAttribute attribute) {
String name = "add" + StringUtils.capitalize(StringTransformations.capitalize(attribute
.getName()));
ASTMCType type = getCDGenService().getFirstTypeArgument(attribute.getMCType()).deepClone();
var m = decorate(clazz, attribute, SetterMethodKind.ADD, "methods.list.AddUnordered", name, List
.of(CDParameterFacade.getInstance().createParameter(type, attribute.getName())), attribute);
m.getSetMethod().setMCReturnType(MCBasicTypesMill.mCReturnTypeBuilder().setMCType(MCTypeFacade
.getInstance().createBooleanType()).build());
if (m.isEmpty()) {
return m;
}
m.get().getSetMethod().setMCReturnType(MCBasicTypesMill.mCReturnTypeBuilder().setMCType(
MCTypeFacade.getInstance().createBooleanType()).build());
return m;
}

protected MethodInformation decorateRemoveUnordered(ASTCDType clazz, ASTCDAttribute attribute) {
protected Optional<MethodInformation> decorateRemoveUnordered(ASTCDType clazz,
ASTCDAttribute attribute) {
String name = "remove" + StringUtils.capitalize(StringTransformations.capitalize(attribute
.getName()));
ASTMCType type = getCDGenService().getFirstTypeArgument(attribute.getMCType()).deepClone();
var m = decorate(clazz, attribute, SetterMethodKind.REM, "methods.list.RemUnordered", name, List
.of(CDParameterFacade.getInstance().createParameter(type, attribute.getName())), attribute);
m.getSetMethod().setMCReturnType(MCBasicTypesMill.mCReturnTypeBuilder().setMCType(MCTypeFacade
.getInstance().createBooleanType()).build());
if (m.isEmpty()) {
return m;
}
m.get().getSetMethod().setMCReturnType(MCBasicTypesMill.mCReturnTypeBuilder().setMCType(
MCTypeFacade.getInstance().createBooleanType()).build());
return m;
}

protected MethodInformation decorate(ASTCDType decParent, ASTCDAttribute attribute,
protected Optional<MethodInformation> decorate(ASTCDType decParent, ASTCDAttribute attribute,
SetterMethodKind kind, String templateName, String methodName, List<ASTCDParameter> params,
Object... templateParams) {

Expand All @@ -154,9 +171,14 @@
glexOpt.ifPresent(glex -> glex.replaceTemplate(EMPTY_BODY, method, new TemplateHookPoint(
templateName, templateParams)));

addToClass(decParent, method);
if (!addToClass(decParent, method)) {
Log.warn("0xTODO: Unable to decorate setter of `" + attribute.getName()

Check warning on line 175 in cdlang/src/main/java/de/monticore/cd/codegen/decorators/SetterDecorator.java

View workflow job for this annotation

GitHub Actions / check-error-codes

ErrorCodeInvalid

Invalid error code 0xTODO (./cdlang/src/main/java/de/monticore/cd/codegen/decorators/SetterDecorator.java:175)
+ "` as such a method already exists.", attribute.get_SourcePositionStart(), attribute
.get_SourcePositionEnd());
return Optional.empty();
}

return new MethodInformation(kind, method, templateName, attribute.getName());
return Optional.of(new MethodInformation(kind, method, templateName, attribute.getName()));

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import de.monticore.ast.ASTNode;
import de.monticore.cd.codegen.CDGenService;
import de.monticore.cd.codegen.decorators.IDecorator;
import de.monticore.cd4codebasis._ast.ASTCDMethod;
import de.monticore.cdbasis._ast.*;
import de.monticore.generating.templateengine.GlobalExtensionManagement;
import java.util.Optional;
Expand Down Expand Up @@ -37,9 +38,37 @@ else if (decoratedParent instanceof ASTCDCompilationUnit)
.getName());
}

protected void addToClass(ASTCDType clazz, ASTCDMember member) {
// TODO: Only add iff not yet present (#4310)
/**
* Adds a member to a class, if it does not already exist
*
* @param clazz the class
* @param member the to-be added member
* @return whether a conflict already exists
*/
protected boolean addToClass(ASTCDType clazz, ASTCDMember member) {
// add iff not yet present (#4310)
if (member instanceof ASTCDMethod method) {
for (ASTCDMember mem : clazz.getCDMemberList()) {
if (mem instanceof ASTCDMethod meth) {
if (method.getName().equals(meth.getName()) && method.getCDParameterList().size() == meth
.getCDParameterList().size()) {
// TODO: Check if params are compatible? (#4310)
return false;
}
}
}
}
else if (member instanceof ASTCDAttribute attribute) {
for (ASTCDMember mem : clazz.getCDMemberList()) {
if (mem instanceof ASTCDAttribute attr) {
if (attr.getName().equals(attribute.getName())) {
return false;
}
}
}
}
clazz.addCDMember(member);
return true;
}

public CDGenService getCDGenService() { return decoratorData.cdGenService; }
Expand Down
Loading
Loading