Commit 8b4c52ab authored by Johannes Mey's avatar Johannes Mey
Browse files

split backend into a file for each contained aspect

parent 7e9b8432
Pipeline #5086 passed with stage
in 2 minutes and 7 seconds
aspect BackendAPI {
public void Relation.generateAPI(StringBuilder sb) {
sb.append(ind(1) + "// " + prettyPrint() + "\n");
getDirection().generateAPI(sb);
sb.append("\n");
}
public abstract void Direction.generateAPI(StringBuilder sb);
inh Relation Direction.relation();
eq Relation.getChild().relation() = this;
eq Program.getChild().relation() = null;
public String RelationComponent.nameCapitalized() {
return name().substring(0,1).toUpperCase() + name().substring(1);
}
}
aspect BackendAbstractGrammar {
public static String ASTNode.listClass = "ArrayList";
public static String ASTNode.jastAddListType = "List";
public static boolean ASTNode.resolverHelper = false;
public static boolean ASTNode.serializer = false;
public static boolean ASTNode.jsonPointer = false;
public static boolean ASTNode.manualReferences = false;
public static boolean ASTNode.useJastAddNames = false;
public String Program.generateAbstractGrammar() {
StringBuilder sb = new StringBuilder();
generateAbstractGrammar(sb);
return sb.toString();
}
public void Program.generateAbstractGrammar(StringBuilder sb) {
for (TypeDecl td: getTypeDecls()) {
td.generateAbstractGrammar(sb);
}
}
public void TypeDecl.generateUnresolvedClass(StringBuilder sb) {
if (getAbstract()) {
sb.append(ind(1) + "abstract ");
} else {
sb.append(ind(1));
}
sb.append("class " + "Unresolved$" + getID() + " extends " + getID() + " implements Unresolved$Node {\n");
sb.append(ind(2) + "private String unresolved$Token;\n");
sb.append(ind(2) + "public String getUnresolved$Token() {\n");
sb.append(ind(3) + "return unresolved$Token;\n");
sb.append(ind(2) + "}\n");
sb.append(ind(2) + "void setUnresolved$Token(String token) {\n");
sb.append(ind(3) + "this.unresolved$Token = token;\n");
sb.append(ind(2) + "}\n");
sb.append(ind(2) + "private boolean unresolved$ResolveOpposite;\n");
sb.append(ind(2) + "public boolean getUnresolved$ResolveOpposite() {\n");
sb.append(ind(3) + "return unresolved$ResolveOpposite;\n");
sb.append(ind(2) + "}\n");
sb.append(ind(2) + "void setUnresolved$ResolveOpposite(boolean resolveOpposite) {\n");
sb.append(ind(3) + "this.unresolved$ResolveOpposite = resolveOpposite;\n");
sb.append(ind(2) + "}\n");
sb.append(ind(1) + "}\n");
sb.append(ind(1) + "Unresolved$Node " + getID() + ".as$Unresolved() {\n");
sb.append(ind(2) + "return null;\n");
sb.append(ind(1) + "}\n");
sb.append(ind(1) + "Unresolved$Node Unresolved$" + getID() + ".as$Unresolved() {\n");
sb.append(ind(2) + "return this;\n");
sb.append(ind(1) + "}\n");
sb.append(ind(1) + "boolean " + getID() + ".is$Unresolved() {\n");
sb.append(ind(2) + "return false;\n");
sb.append(ind(1) + "}\n");
sb.append(ind(1) + "boolean Unresolved$" + getID() + ".is$Unresolved() {\n");
sb.append(ind(2) + "return true;\n");
sb.append(ind(1) + "}\n");
}
public void TypeDecl.generateAbstractGrammar(StringBuilder sb) {
if (getAbstract()) {
sb.append("abstract ");
}
sb.append(getID());
if (hasSuper()) {
sb.append(" : " + getSuper());
}
if (getNumComponent() > 0 || relationComponents().size() > 0) {
sb.append(" ::=");
}
for (Component c: getComponents()) {
sb.append(" ");
sb.append(c.generateAbstractGrammar());
}
for (RelationComponent c: relationComponents()) {
sb.append(" ");
sb.append(c.generateAbstractGrammar());
}
sb.append(";\n");
}
public String Component.generateAbstractGrammar() {
if (getID().equals(getTypeUse().toString())) {
return getTypeUse().toString();
} else {
return getID() + ":" + getTypeUse();
}
}
public String ListComponent.generateAbstractGrammar() {
return super.generateAbstractGrammar() + "*";
}
public String OptComponent.generateAbstractGrammar() {
return "[" + super.generateAbstractGrammar() + "]";
}
public String NTAComponent.generateAbstractGrammar() {
return "/" + super.generateAbstractGrammar() + "/";
}
public String NTAListComponent.generateAbstractGrammar() {
return "/" + super.generateAbstractGrammar() + "*/";
}
public String NTAOptComponent.generateAbstractGrammar() {
return "/[" + super.generateAbstractGrammar() + "]/";
}
public String TokenComponent.generateAbstractGrammar() {
return "<" + getID() + ":" + getTypeUse() + ">";
}
public String NTATokenComponent.generateAbstractGrammar() {
return "/<" + getID() + ":" + getTypeUse() + ">/";
}
public String RelationComponent.generateAbstractGrammar() {
return "<" + getImplAttributeName() + ":" + ofTypeDecl() + ">";
}
public String ManyRelationComponent.generateAbstractGrammar() {
return "<" + getImplAttributeName() + ":" + ASTNode.listClass + "<" + ofTypeDecl() + ">>";
}
public String RelationComponent.getImplAttributeName() {
return "_impl_" + getID();
}
public String RelationComponent.getImplAttributeField() {
// tt.bind("TypeInSignature", ASTNode.convTypeNameToSignature(type()));
return "token" + ofTypeDecl() + "__impl_" + getID();
}
public String ManyRelationComponent.getImplAttributeField() {
// tt.bind("TypeInSignature", ASTNode.convTypeNameToSignature(type()));
return "token" + listClass + "_" + ofTypeDecl() + "___impl_" + getID();
}
}
aspect BackendAspect {
public String Program.generateAspect() {
StringBuilder sb = new StringBuilder();
generateAspect(sb);
return sb.toString();
}
public void Program.generateAspect(StringBuilder sb) {
sb.append("import java.util.ArrayList;\n");
sb.append("import java.util.Collections;\n");
sb.append("import java.time.Instant;\n");
sb.append("import java.time.Period;\n");
sb.append("aspect RelAstAPI {\n");
for (TypeDecl td: getTypeDecls()) {
if (td.needsConstructor()) {
td.generateConstructor(sb);
}
}
for (Relation r: getRelations()) {
r.generateAPI(sb);
}
generateLowerBoundCheck(sb);
sb.append(ind(1) + "public static void ASTNode.assertNotNull(Object obj) {\n");
sb.append(ind(2) + "if (obj == null) {\n");
sb.append(ind(3) + "throw new NullPointerException();\n");
sb.append(ind(2) + "}\n");
sb.append(ind(1) + "}\n");
sb.append("}\n");
}
public void TypeDecl.generateConstructor(StringBuilder sb) {
sb.append(ind(1) + "public " + getID() + "." + getID() + "(");
int i = 0;
for (Component c: componentsTransitive()) {
sb.append(c.constructorParameter());
if (++i < componentsTransitive().size()) {
sb.append(", ");
}
}
sb.append(") {\n");
for (Component c: componentsTransitive()) {
sb.append(ind(2) + c.constructorSetMethod() + "(" + c.getID() + ");\n");
}
sb.append(ind(1) + "}\n");
}
public String Component.constructorParameter() {
return getTypeUse() + " " + getID();
}
public String ListComponent.constructorParameter() {
return ASTNode.jastAddListType + "<" + getTypeUse() + "> " + getID();
}
public String OptComponent.constructorParameter() {
return "Opt<" + getTypeUse() + "> " + getID();
}
public String Component.constructorSetMethod() {
return "set" + getID();
}
public String ListComponent.constructorSetMethod() {
return "set" + getID() + "List";
}
public String OptComponent.constructorSetMethod() {
return "set" + getID() + "Opt";
}
}
aspect BackendBidirectionalAPI {
public void Bidirectional.generateAPI(StringBuilder sb) {
RelationComponent l = relation().getLeft();
RelationComponent r = relation().getRight();
if (l.multiplicityOne()) {
if (r.multiplicityOne()) {
l.generateBiOneOne(sb, false);
r.generateBiOneOne(sb, false);
} else if (r.multiplicityOpt()) {
l.generateBiOneOne(sb, false);
r.generateBiOneOne(sb, true);
} else if (r.multiplicityMany()) {
l.generateBiOneMany(sb, false);
r.generateBiManyOne(sb, l);
}
} else if (l.multiplicityOpt()) {
if (r.multiplicityOne()) {
l.generateBiOneOne(sb, true);
r.generateBiOneOne(sb, false);
} else if (r.multiplicityOpt()) {
l.generateBiOneOne(sb, true);
r.generateBiOneOne(sb, true);
} else if (r.multiplicityMany()) {
l.generateBiOneMany(sb, true);
r.generateBiManyOne(sb, l);
}
} else if (l.multiplicityMany()) {
if (r.multiplicityOne()) {
l.generateBiManyOne(sb, r);
r.generateBiOneMany(sb, false);
} else if (r.multiplicityOpt()) {
l.generateBiManyOne(sb, r);
r.generateBiOneMany(sb, true);
} else if (r.multiplicityMany()) {
l.generateBiManyMany(sb, r);
r.generateBiManyMany(sb, l);
}
}
}
public void RelationComponent.generateBiOneOne(StringBuilder sb, boolean isOpt) {
// Get
generateGetOne(sb);
// Set
sb.append(ind(1) + "public " + getTypeUse().decl() + " " + getTypeUse().decl());
sb.append(".set" + nameCapitalized() + "(" + ofTypeDecl() + " o) {\n");
if (!isOpt) {
sb.append(ind(2) + "assertNotNull(o);\n");
}
// unset the old opposite
sb.append(ind(2) + "if (" + getImplAttributeField() + " != null) {\n");
sb.append(ind(3) + "" + getImplAttributeField() + ".set" + otherSide().getImplAttributeName() + "(null);\n");
sb.append(ind(2) + "}\n");
if (resolverHelper | serializer) {
sb.append(ind(2) + "if (o != null && !o.is$Unresolved() && o." + otherSide().getImplAttributeField() + " != null) {\n");
} else {
sb.append(ind(2) + "if (o != null && o." + otherSide().getImplAttributeField() + " != null) {\n");
}
sb.append(ind(3) + "o." + otherSide().getImplAttributeField() + ".set" + getImplAttributeName() + "(null);\n");
sb.append(ind(2) + "}\n");
sb.append(ind(2) + "set" + getImplAttributeName() + "(o);\n");
if (resolverHelper | serializer) {
sb.append(ind(2) + "if (o == null || !o.is$Unresolved()) {\n");
if (isOpt) {
sb.append(ind(3) + "if (o != null) {\n");
sb.append(ind(4) + "o.set" + otherSide().getImplAttributeName() + "(this);\n");
sb.append(ind(3) + "}\n");
} else {
sb.append(ind(3) + "o.set" + otherSide().getImplAttributeName() + "(this);\n");
}
sb.append(ind(2) + "}\n");
} else {
if (isOpt) {
sb.append(ind(2) + "if (o != null) {\n");
sb.append(ind(3) + "o.set" + otherSide().getImplAttributeName() + "(this);\n");
sb.append(ind(2) + "}\n");
} else {
sb.append(ind(2) + "o.set" + otherSide().getImplAttributeName() + "(this);\n");
}
}
sb.append(ind(2) + "return this;\n");
sb.append(ind(1) + "}\n");
if (isOpt) {
generateExtraOptAPI(sb);
}
}
public void RelationComponent.generateBiManyMany(StringBuilder sb, RelationComponent opposite) {
// Get
sb.append(ind(1) + "public java.util.List<" + ofTypeDecl() + "> " + getTypeUse().decl() + ".");
if (useJastAddNames) {
// getXs
sb.append("get" + nameCapitalized() + "s() {\n");
sb.append(ind(2) + "return get" + nameCapitalized() + "List();\n");
sb.append(ind(1) + "}\n");
// getXList
sb.append(ind(1) + "public java.util.List<" + ofTypeDecl() + "> " + getTypeUse().decl());
sb.append(".get" + nameCapitalized() + "List() {\n");
} else {
sb.append(name() + "() {\n");
}
sb.append(ind(2) + ASTNode.listClass + "<" + ofTypeDecl() + "> l = get" + getImplAttributeName() + "();\n");
// resolve the entire list
if (resolverHelper | serializer) {
sb.append(ind(2) + "if (l != null) {\n");
sb.append(ind(3) + "boolean changed = false;\n");
sb.append(ind(3) + "for (int i = 0; i < l.size(); i++) {\n");
sb.append(ind(4) + ofTypeDecl() + " element = l.get(i);\n");
sb.append(ind(4) + "if (element.is$Unresolved()) {\n");
sb.append(ind(5) + "changed = true;\n");
sb.append(ind(5) + ofTypeDecl() + " resolvedElement = resolve" + nameCapitalized() + "ByToken(element.as$Unresolved().getUnresolved$Token(), i);\n");
sb.append(ind(5) + "if (resolvedElement != null && element.as$Unresolved().getUnresolved$ResolveOpposite()) {\n");
sb.append(ind(6) + ASTNode.listClass + "<" + getTypeUse().decl() + "> otherList = resolvedElement." + opposite.getImplAttributeField() + ";\n");
sb.append(ind(6) + "if (otherList == null) {\n");
sb.append(ind(7) + "otherList = new " + listClass + "<>();\n");
sb.append(ind(6) + "}\n");
sb.append(ind(6) + "otherList.add(this);\n");
sb.append(ind(6) + "resolvedElement.set" + opposite.getImplAttributeName() + "(otherList);\n");
sb.append(ind(5) + "}\n");
sb.append(ind(5) + "l.set(i, resolvedElement);\n");
sb.append(ind(4) + "}\n");
sb.append(ind(3) + "}\n");
sb.append(ind(3) + "if (changed) {\n");
sb.append(ind(4) + "set" + getImplAttributeName() + "(l);\n");
sb.append(ind(3) + "}\n");
sb.append(ind(2) + "}\n");
}
sb.append(ind(2) + "return l != null ? Collections.unmodifiableList(l) : Collections.emptyList();\n");
sb.append(ind(1) + "}\n");
// Add
sb.append(ind(1) + "public void " + getTypeUse().decl() + ".add");
if (!useJastAddNames) {
sb.append("To");
}
sb.append(nameCapitalized() + "(" + ofTypeDecl() + " o) {\n");
sb.append(ind(2) + "assertNotNull(o);\n");
sb.append(ind(2) + ASTNode.listClass + "<" + ofTypeDecl() + "> list = " + getImplAttributeField() + ";\n");
sb.append(ind(2) + "if (list == null) {\n");
sb.append(ind(3) + "list = new " + ASTNode.listClass + "<>();\n");
sb.append(ind(2) + "}\n");
sb.append(ind(2) + ASTNode.listClass + "<" + otherSide().ofTypeDecl() + "> list2 = o." + otherSide().getImplAttributeField() + ";\n");
sb.append(ind(2) + "if (list2 == null) {\n");
sb.append(ind(3) + "list2 = new "+ ASTNode.listClass + "<>();\n");
sb.append(ind(2) + "}\n");
sb.append(ind(2) + "list.add(o);\n");
sb.append(ind(2) + "list2.add(this);\n");
sb.append(ind(2) + "set" + getImplAttributeName() + "(list);\n");
sb.append(ind(2) + "o.set" + otherSide().getImplAttributeName() + "(list2);\n");
sb.append(ind(1) + "}\n");
// Insert / add at specific position
sb.append(ind(1) + "public void " + getTypeUse().decl() + ".add");
if (!useJastAddNames) {
sb.append("To");
}
sb.append(nameCapitalized() + "(int index, " + ofTypeDecl() + " o) {\n");
sb.append(ind(2) + "assertNotNull(o);\n");
sb.append(ind(2) + ASTNode.listClass + "<" + ofTypeDecl() + "> list = " + getImplAttributeField() + ";\n");
sb.append(ind(2) + "if (list == null) {\n");
sb.append(ind(3) + "list = new " + ASTNode.listClass + "<>();\n");
sb.append(ind(2) + "}\n");
sb.append(ind(2) + ASTNode.listClass + "<" + otherSide().ofTypeDecl() + "> list2 = o."
+ otherSide().getImplAttributeField() + ";\n");
sb.append(ind(2) + "if (list2 == null) {\n");
sb.append(ind(3) + "list2 = new "+ ASTNode.listClass + "<>();\n");
sb.append(ind(2) + "}\n");
sb.append(ind(2) + "list.add(index, o);\n");
sb.append(ind(2) + "list2.add(this);\n");
sb.append(ind(2) + "set" + getImplAttributeName() + "(list);\n");
sb.append(ind(2) + "o.set" + otherSide().getImplAttributeName() + "(list2);\n");
sb.append(ind(1) + "}\n");
// Remove
sb.append(ind(1) + "public void " + getTypeUse().decl() + ".remove");
if (!useJastAddNames) {
sb.append("From");
}
sb.append(nameCapitalized() + "(" + ofTypeDecl() + " o) {\n");
sb.append(ind(2) + "assertNotNull(o);\n");
sb.append(ind(2) + ASTNode.listClass + "<" + ofTypeDecl() + "> list = " + getImplAttributeField() + ";\n");
sb.append(ind(2) + "if (list != null && list.remove(o)) {\n");
sb.append(ind(3) + ASTNode.listClass + "<" + otherSide().ofTypeDecl() + "> list2 = o."
+ otherSide().getImplAttributeField() + ";\n");
sb.append(ind(3) + "if (list2 != null) list2.remove(this);\n");
sb.append(ind(3) + "set" + getImplAttributeName() + "(list);\n");
sb.append(ind(3) + "o.set" + otherSide().getImplAttributeName() + "(list2);\n");
sb.append(ind(2) + "}\n");
sb.append(ind(1) + "}\n");
}
public void RelationComponent.generateBiManyOne(StringBuilder sb, RelationComponent opposite) {
// Get
sb.append(ind(1) + "public java.util.List<" + ofTypeDecl() + "> " + getTypeUse().decl() + ".");
if (useJastAddNames) {
// getXs
sb.append("get" + nameCapitalized() + "s() {\n");
sb.append(ind(2) + "return get" + nameCapitalized() + "List();\n");
sb.append(ind(1) + "}\n");
// getXList
sb.append(ind(1) + "public java.util.List<" + ofTypeDecl() + "> " + getTypeUse().decl());
sb.append(".get" + nameCapitalized() + "List() {\n");
} else {
sb.append(name() + "() {\n");
}
sb.append(ind(2) + ASTNode.listClass + "<" + ofTypeDecl() + "> l = get" + getImplAttributeName() + "();\n");
// resolve the entire list
if (resolverHelper | serializer) {
sb.append(ind(2) + "if (l != null) {\n");
sb.append(ind(3) + "boolean changed = false;\n");
sb.append(ind(3) + "for (int i = 0; i < l.size(); i++) {\n");
sb.append(ind(4) + ofTypeDecl() + " element = l.get(i);\n");
sb.append(ind(4) + "if (element.is$Unresolved()) {\n");
sb.append(ind(5) + "changed = true;\n");
sb.append(ind(5) + ofTypeDecl() + " resolvedElement = resolve" + nameCapitalized() + "ByToken(element.as$Unresolved().getUnresolved$Token(), i);\n");
sb.append(ind(5) + "if (element.as$Unresolved().getUnresolved$ResolveOpposite()) {\n");
sb.append(ind(6) + getTypeUse().decl() + " oldTarget = resolvedElement." + opposite.getImplAttributeField() + ";\n");
sb.append(ind(6) + "if (oldTarget != null && oldTarget != this) {\n");
sb.append(ind(7) + "oldTarget." + getImplAttributeField() + ".remove(resolvedElement);\n");
sb.append(ind(6) + "}\n");
sb.append(ind(6) + "if (oldTarget == this) {\n");
sb.append(ind(7) + "l.remove(i);\n");
sb.append(ind(7) + "i--;\n");
sb.append(ind(6) + "} else {\n");
sb.append(ind(7) + "resolvedElement.set" + opposite.getImplAttributeName() + "(this);\n");
sb.append(ind(7) + "l.set(i, resolvedElement);\n");
sb.append(ind(6) + "}\n");
sb.append(ind(5) + "} else {\n");
sb.append(ind(6) + "l.set(i, resolvedElement);\n");
sb.append(ind(5) + "}\n");
sb.append(ind(4) + "}\n");
sb.append(ind(3) + "}\n");
sb.append(ind(3) + "if (changed) {\n");
sb.append(ind(4) + "set" + getImplAttributeName() + "(l);\n");
sb.append(ind(3) + "}\n");
sb.append(ind(2) + "}\n");
}
sb.append(ind(2) + "return l != null ? Collections.unmodifiableList(l) : Collections.emptyList();\n");
sb.append(ind(1) + "}\n");
// Add
sb.append(ind(1) + "public void " + getTypeUse().decl() + ".add");
if (!useJastAddNames) {
sb.append("To");
}
sb.append(nameCapitalized() + "(" + ofTypeDecl() + " o) {\n");
sb.append(ind(2) + "assertNotNull(o);\n");
sb.append(ind(2) + "if (o != null && o." + otherSide().getImplAttributeField() + " != null) {\n");
sb.append(ind(3) + ASTNode.listClass + "<" + ofTypeDecl() + "> list2 = o."
+ otherSide().getImplAttributeField() + "." + getImplAttributeField() + ";\n");
sb.append(ind(3) + "if (list2.remove(o))\n");
sb.append(ind(4) + "o." + otherSide().getImplAttributeField()
+ ".set" + getImplAttributeName() + "(list2);\n");
sb.append(ind(2) + "}\n");
sb.append(ind(2) + ASTNode.listClass + "<" + ofTypeDecl() + "> list = " + getImplAttributeField() + ";\n");
sb.append(ind(2) + "if (list == null) {\n");
sb.append(ind(3) + "list = new " + ASTNode.listClass + "<>();\n");
sb.append(ind(2) + "}\n");
sb.append(ind(2) + "list.add(o);\n");
sb.append(ind(2) + "set" + getImplAttributeName() + "(list);\n");
sb.append(ind(2) + "o.set" + otherSide().getImplAttributeName() + "(this);\n");
sb.append(ind(1) + "}\n");
// Insert / add at specific position
sb.append(ind(1) + "public void " + getTypeUse().decl() + ".add");
if (!useJastAddNames) {
sb.append("To");
}
sb.append(nameCapitalized() + "(int index, " + ofTypeDecl() + " o) {\n");
sb.append(ind(2) + "assertNotNull(o);\n");
sb.append(ind(2) + "if (o != null && o." + otherSide().getImplAttributeField() + " != null) {\n");
sb.append(ind(3) + ASTNode.listClass + "<" + ofTypeDecl() + "> list2 = o." + otherSide().getImplAttributeField() + "." + getImplAttributeField() + ";\n");
sb.append(ind(3) + "if (list2.remove(o))\n");
sb.append(ind(4) + "o." + otherSide().getImplAttributeField() + ".set" + getImplAttributeName() + "(list2);\n");
sb.append(ind(2) + "}\n");
sb.append(ind(2) + ASTNode.listClass + "<" + ofTypeDecl() + "> list = " + getImplAttributeField() + ";\n");
sb.append(ind(2) + "if (list == null) {\n");
sb.append(ind(3) + "list = new " + ASTNode.listClass + "<>();\n");
sb.append(ind(2) + "}\n");
sb.append(ind(2) + "list.add(index, o);\n");
sb.append(ind(2) + "set" + getImplAttributeName() + "(list);\n");
sb.append(ind(2) + "o.set" + otherSide().getImplAttributeName() + "(this);\n");
sb.append(ind(1) + "}\n");
// Remove
sb.append(ind(1) + "public void " + getTypeUse().decl() + ".remove");
if (!useJastAddNames) {
sb.append("From");
}
sb.append(nameCapitalized() + "(" + ofTypeDecl() + " o) {\n");
sb.append(ind(2) + "assertNotNull(o);\n");
sb.append(ind(2) + ASTNode.listClass + "<" + ofTypeDecl() + "> list = " + getImplAttributeField() + ";\n");
sb.append(ind(2) + "if (list != null && list.remove(o)) {\n");
sb.append(ind(3) + "set" + getImplAttributeName() + "(list);\n");
sb.append(ind(3) + "if (o." + otherSide().getImplAttributeField() + " == this) {\n");
sb.append(ind(4) + "o.set" + otherSide().getImplAttributeName() + "(null);\n");
sb.append(ind(3) + "}\n");
sb.append(ind(2) + "}\n");
sb.append(ind(1) + "}\n");
}
public void RelationComponent.generateBiOneMany(StringBuilder sb, boolean isOpt) {
// Get
generateGetOne(sb);
// Set
sb.append(ind(1) + "public " + getTypeUse().decl() + " " + getTypeUse().decl() + ".set" + nameCapitalized()
+ "(" + ofTypeDecl() + " o) {\n");
if (!isOpt) {
sb.append(ind(2) + "assertNotNull(o);\n");
}
sb.append(ind(2) + "if (" + getImplAttributeField() + " != null) {\n");
sb.append(ind(3) + ASTNode.listClass + "<" + getTypeUse().decl() + "> list2 = " + getImplAttributeField()
+ "." + otherSide().getImplAttributeField() + ";\n");
sb.append(ind(3) + "list2.remove(this);\n");
sb.append(ind(3) + getImplAttributeField() + "." + "set"
+ otherSide().getImplAttributeName() + "(list2);\n");
sb.append(ind(2) + "}\n");
sb.append(ind(2) + "set" + getImplAttributeName() + "(o);\n");
int ind = isOpt ? 3 : 2;
if (isOpt) {
sb.append(ind(2) + "if (o != null) {\n");
}
sb.append(ind(ind) + ASTNode.listClass + "<" + getTypeUse().decl() + "> list = o."
+ otherSide().getImplAttributeField() + ";\n");
sb.append(ind(ind) + "if (list == null) {\n");
sb.append(ind(ind+1) + "list = new " + ASTNode.listClass + "<>();\n");
sb.append(ind(ind) + "}\n");
sb.append(ind(ind) + "list.add(this);\n");
sb.append(ind(ind) + "o.set" + otherSide().getImplAttributeName() + "(list);\n");
if (isOpt) {
sb.append(ind(2) + "}\n");
}
sb.append(ind(2) + "return this;\n");
sb.append(ind(1) + "}\n");
if (isOpt) {
generateExtraOptAPI(sb);
}
}
}
aspect BackendDirectedAPI {
public void RightDirection.generateAPI(StringBuilder sb) {
relation().getLeft().generateDirectedAPI(sb);
}
public void LeftDirection.generateAPI(StringBuilder sb) {
relation().getRight().generateDirectedAPI(sb);
}
public abstract void RelationComponent.generateDirectedAPI(StringBuilder sb);
public void OneRelationComponent.generateDirectedAPI(StringBuilder sb) {
generateDirectedZeroOneAPI(sb, false);
}
public void OptionalRelationComponent.generateDirectedAPI(StringBuilder sb) {
generateDirectedZeroOneAPI(sb, true);
generateExtraOptAPI(sb);
}
public void RelationComponent.generateDirectedZeroOneAPI(StringBuilder sb, boolean optional) {
// Get
generateGetOne(sb);
// Set
sb.append(ind(1) + "public " + getTypeUse().decl() + " " + getTypeUse().decl());
sb.append(".set" + nameCapitalized() + "(" + ofTypeDecl() + " o) {\n");
if (!optional) {
sb.append(ind(2) + "assertNotNull(o);\n");
}
sb.append(ind(2) + "set" + getImplAttributeName() + "(o);\n");
sb.append(ind(2) + "return this;\n");
sb.append(ind(1) + "}\n");
}
public void ManyRelationComponent.generateDirectedAPI(StringBuilder sb) {
// Get
sb.append(ind(1) + "public java.util.List<" + ofTypeDecl() + "> " + getTypeUse().decl() + ".");
if (useJastAddNames) {
// getXs
sb.append("get" + nameCapitalized() + "s() {\n");
sb.append(ind(2) + "return get" + nameCapitalized() + "List();\n");
sb.append(ind(1) + "}\n");
// getXList
sb.append(ind(1) + "public java.util.List<" + ofTypeDecl() + "> " + getTypeUse().decl());
sb.append(".get" + nameCapitalized() + "List() {\n");
} else {
sb.append(name() + "() {\n");
}
sb.append(ind(2) + ASTNode.listClass + "<" + ofTypeDecl() + "> l = get" + getImplAttributeName() + "();\n");
// resolve the entire list
if (resolverHelper | serializer) {
sb.append(ind(2) + "if (l != null) {\n");
sb.append(ind(3) + "boolean changed = false;\n");
sb.append(ind(3) + "for (int i = 0; i < l.size(); i++) {\n");
sb.append(ind(4) + ofTypeDecl() + " element = l.get(i);\n");
sb.append(ind(4) + "if (element.is$Unresolved()) {\n");