Commit 90551c73 authored by Johannes Mey's avatar Johannes Mey
Browse files

extract strings and some optimizations

parent 8098b81c
...@@ -15,11 +15,15 @@ aspect BackendAPI { ...@@ -15,11 +15,15 @@ aspect BackendAPI {
public void RelationComponent.generateAPI(StringBuilder sb) { public void RelationComponent.generateAPI(StringBuilder sb) {
if (otherSide().isNavigable()) { if (otherSide().isNavigable()) {
if (multiplicityOne() || multiplicityOpt()) { if (multiplicityOne() || multiplicityOpt()) {
generateGetOne(sb);
if (otherSide().multiplicityOne() || otherSide().multiplicityOpt()) { if (otherSide().multiplicityOne() || otherSide().multiplicityOpt()) {
generateBiOneOne(sb); generateBiOneOne(sb);
} else if (otherSide().multiplicityMany()) { } else if (otherSide().multiplicityMany()) {
generateBiOneMany(sb); generateBiOneMany(sb);
} }
if (isOpt()) {
generateExtraOptAPI(sb);
}
} else if (multiplicityMany()) { } else if (multiplicityMany()) {
if (otherSide().multiplicityOne() || otherSide().multiplicityOpt()) { if (otherSide().multiplicityOne() || otherSide().multiplicityOpt()) {
generateBiManyOne(sb); generateBiManyOne(sb);
...@@ -29,13 +33,38 @@ aspect BackendAPI { ...@@ -29,13 +33,38 @@ aspect BackendAPI {
} }
} else { } else {
if (multiplicityOne() || multiplicityOpt()) { if (multiplicityOne() || multiplicityOpt()) {
generateGetOne(sb);
generateDirectedZeroOneAPI(sb); generateDirectedZeroOneAPI(sb);
if (isOpt()) {
generateExtraOptAPI(sb);
}
} else if (multiplicityMany()) { } else if (multiplicityMany()) {
generateDirectedManyAPI(sb); generateDirectedManyAPI(sb);
} }
} }
} }
public void RelationComponent.generateGetOne(StringBuilder sb) {
sb.append(ind(1) + "public " + ofTypeDecl() + " " + getTypeUse().decl() + ".");
if (useJastAddNames) {
sb.append("get" + nameCapitalized());
} else {
sb.append(name());
}
sb.append("() {\n");
if (resolverHelper | serializer) {
sb.append(ind(2) + "if (" + getImplAttributeField() + " != null && " + getImplAttributeField() + ".is$Unresolved()) {\n");
sb.append(ind(3) + "if (" + getImplAttributeField() + ".as$Unresolved().getUnresolved$ResolveOpposite()) {\n");
sb.append(ind(4) + "set" + nameCapitalized() + "(resolve" + nameCapitalized() + "ByToken(" + getImplAttributeField() + ".as$Unresolved().getUnresolved$Token()));\n");
sb.append(ind(3) + "} else {\n");
sb.append(ind(4) + "set" + getImplAttributeName() + "(resolve" + nameCapitalized() + "ByToken(" + getImplAttributeField() + ".as$Unresolved().getUnresolved$Token()));\n");
sb.append(ind(3) + "}\n");
sb.append(ind(2) + "}\n");
}
sb.append(ind(2) + "return get" + getImplAttributeName() + "();\n");
sb.append(ind(1) + "}\n");
}
public void RelationComponent.generateExtraOptAPI(StringBuilder sb) { public void RelationComponent.generateExtraOptAPI(StringBuilder sb) {
// has // has
sb.append(ind(1) + "public boolean " + getTypeUse().decl()); sb.append(ind(1) + "public boolean " + getTypeUse().decl());
......
aspect BackendBidirectionalAPI { aspect BackendBidirectionalAPI {
public void RelationComponent.generateBiOneOne(StringBuilder sb) { public void RelationComponent.generateBiOneOne(StringBuilder sb) {
// Get boolean resolve = resolverHelper || serializer;
generateGetOne(sb);
// Set // Set
sb.append(ind(1) + "public " + getTypeUse().decl() + " " + getTypeUse().decl()); sb.append(ind(1) + "public " + getTypeUse().decl() + " " + getTypeUse().decl() + ".set" + nameCapitalized() + "(" + ofTypeDecl() + " o) {\n");
sb.append(".set" + nameCapitalized() + "(" + ofTypeDecl() + " o) {\n");
if (!isOpt()) { if (!isOpt()) {
sb.append(ind(2) + "assertNotNull(o);\n"); sb.append(ind(2) + "assertNotNull(o);\n");
} }
// unset the old opposite // unset the old opposite
sb.append(ind(2) + "if (" + getImplAttributeField() + " != null) {\n"); sb.append(ind(2) + "if (" + getImplAttributeField() + " != null)\n");
sb.append(ind(3) + "" + getImplAttributeField() + ".set" + otherSide().getImplAttributeName() + "(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 && " + (resolve ? "!o." + isUnresolvedMethod + "() && o." : "o.") + otherSide().getImplAttributeField() + " != null) {\n");
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(3) + "o." + otherSide().getImplAttributeField() + ".set" + getImplAttributeName() + "(null);\n");
sb.append(ind(2) + "}\n"); sb.append(ind(2) + "}\n");
sb.append(ind(2) + "set" + getImplAttributeName() + "(o);\n"); sb.append(ind(2) + "set" + getImplAttributeName() + "(o);\n");
if (resolverHelper | serializer) { if (resolve) {
sb.append(ind(2) + "if (o == null || !o.is$Unresolved()) {\n"); sb.append(ind(2) + "if (o == null || !o." + isUnresolvedMethod + "())\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");
}
} }
if (isOpt()) {
sb.append(ind(resolve ? 3 : 2) + "if (o != null)\n");
}
sb.append(ind((isOpt() ? 1 : 0) + (resolve ? 3 : 2)) + "o.set" + otherSide().getImplAttributeName() + "(this);\n");
sb.append(ind(2) + "return this;\n"); sb.append(ind(2) + "return this;\n");
sb.append(ind(1) + "}\n"); sb.append(ind(1) + "}\n");
if (isOpt()) {
generateExtraOptAPI(sb);
}
} }
public void RelationComponent.generateBiManyMany(StringBuilder sb) { public void RelationComponent.generateBiManyMany(StringBuilder sb) {
...@@ -69,37 +47,33 @@ aspect BackendBidirectionalAPI { ...@@ -69,37 +47,33 @@ aspect BackendBidirectionalAPI {
// resolve the entire list // resolve the entire list
if (resolverHelper | serializer) { if (resolverHelper | serializer) {
sb.append(ind(2) + "if (l != null) {\n"); sb.append(ind(2) + "if (l != null) {\n");
sb.append(ind(3) + "boolean changed = false;\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(3) + "for (int i = 0; i < l.size(); i++) {\n");
sb.append(ind(4) + ofTypeDecl() + " element = l.get(i);\n"); sb.append(ind(4) + ofTypeDecl() + " element = l.get(i);\n");
sb.append(ind(4) + "if (element.is$Unresolved()) {\n"); sb.append(ind(4) + "if (element." + isUnresolvedMethod + "()) {\n");
sb.append(ind(5) + "changed = true;\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) + ofTypeDecl() + " resolvedElement = resolve" + nameCapitalized() + "ByToken(element." + asUnresolvedMethod + "()." + getUnresolvedTokenMethod + "(), i);\n");
sb.append(ind(5) + "if (resolvedElement != null && element.as$Unresolved().getUnresolved$ResolveOpposite()) {\n"); sb.append(ind(5) + "if (resolvedElement != null && element." + asUnresolvedMethod + "()." + getUnresolvedResolveOppositeMethod + "()) {\n");
sb.append(ind(6) + ASTNode.listClass + "<" + getTypeUse().decl() + "> otherList = resolvedElement." + otherSide().getImplAttributeField() + ";\n"); sb.append(ind(6) + ASTNode.listClass + "<" + getTypeUse().decl() + "> otherList = resolvedElement." + otherSide().getImplAttributeField() + ";\n");
sb.append(ind(6) + "if (otherList == null) {\n"); sb.append(ind(6) + "if (otherList == null) {\n");
sb.append(ind(7) + "otherList = new " + listClass + "<>();\n"); sb.append(ind(7) + "otherList = new " + listClass + "<>();\n");
sb.append(ind(6) + "}\n"); sb.append(ind(6) + "}\n");
sb.append(ind(6) + "otherList.add(this);\n"); sb.append(ind(6) + "otherList.add(this);\n");
sb.append(ind(6) + "resolvedElement.set" + otherSide().getImplAttributeName() + "(otherList);\n"); sb.append(ind(6) + "resolvedElement.set" + otherSide().getImplAttributeName() + "(otherList);\n");
sb.append(ind(5) + "}\n"); sb.append(ind(5) + "}\n");
sb.append(ind(5) + "l.set(i, resolvedElement);\n"); sb.append(ind(5) + "l.set(i, resolvedElement);\n");
sb.append(ind(4) + "}\n"); sb.append(ind(4) + "}\n");
sb.append(ind(3) + "}\n"); sb.append(ind(3) + "}\n");
sb.append(ind(3) + "if (changed) {\n"); sb.append(ind(3) + "if (changed) {\n");
sb.append(ind(4) + "set" + getImplAttributeName() + "(l);\n"); sb.append(ind(4) + "set" + getImplAttributeName() + "(l);\n");
sb.append(ind(3) + "}\n"); sb.append(ind(3) + "}\n");
sb.append(ind(2) + "}\n"); sb.append(ind(2) + "}\n");
} }
sb.append(ind(2) + "return l != null ? Collections.unmodifiableList(l) : Collections.emptyList();\n"); sb.append(ind(2) + "return l != null ? Collections.unmodifiableList(l) : Collections.emptyList();\n");
sb.append(ind(1) + "}\n"); sb.append(ind(1) + "}\n");
// Add // Add
sb.append(ind(1) + "public void " + getTypeUse().decl() + ".add"); sb.append(ind(1) + "public void " + getTypeUse().decl() + ".add" + (useJastAddNames ? "" : "To") + nameCapitalized() + "(" + ofTypeDecl() + " o) {\n");
if (!useJastAddNames) {
sb.append("To");
}
sb.append(nameCapitalized() + "(" + ofTypeDecl() + " o) {\n");
sb.append(ind(2) + "assertNotNull(o);\n"); sb.append(ind(2) + "assertNotNull(o);\n");
sb.append(ind(2) + ASTNode.listClass + "<" + ofTypeDecl() + "> list = " + getImplAttributeField() + ";\n"); sb.append(ind(2) + ASTNode.listClass + "<" + ofTypeDecl() + "> list = " + getImplAttributeField() + ";\n");
sb.append(ind(2) + "if (list == null) {\n"); sb.append(ind(2) + "if (list == null) {\n");
...@@ -116,18 +90,13 @@ aspect BackendBidirectionalAPI { ...@@ -116,18 +90,13 @@ aspect BackendBidirectionalAPI {
sb.append(ind(1) + "}\n"); sb.append(ind(1) + "}\n");
// Insert / add at specific position // Insert / add at specific position
sb.append(ind(1) + "public void " + getTypeUse().decl() + ".add"); sb.append(ind(1) + "public void " + getTypeUse().decl() + ".add" + (useJastAddNames ? "" : "To") + nameCapitalized() + "(int index, " + ofTypeDecl() + " o) {\n");
if (!useJastAddNames) {
sb.append("To");
}
sb.append(nameCapitalized() + "(int index, " + ofTypeDecl() + " o) {\n");
sb.append(ind(2) + "assertNotNull(o);\n"); sb.append(ind(2) + "assertNotNull(o);\n");
sb.append(ind(2) + ASTNode.listClass + "<" + ofTypeDecl() + "> list = " + getImplAttributeField() + ";\n"); sb.append(ind(2) + ASTNode.listClass + "<" + ofTypeDecl() + "> list = " + getImplAttributeField() + ";\n");
sb.append(ind(2) + "if (list == null) {\n"); sb.append(ind(2) + "if (list == null) {\n");
sb.append(ind(3) + "list = new " + ASTNode.listClass + "<>();\n"); sb.append(ind(3) + "list = new " + ASTNode.listClass + "<>();\n");
sb.append(ind(2) + "}\n"); sb.append(ind(2) + "}\n");
sb.append(ind(2) + ASTNode.listClass + "<" + otherSide().ofTypeDecl() + "> list2 = o." sb.append(ind(2) + ASTNode.listClass + "<" + otherSide().ofTypeDecl() + "> list2 = o." + otherSide().getImplAttributeField() + ";\n");
+ otherSide().getImplAttributeField() + ";\n");
sb.append(ind(2) + "if (list2 == null) {\n"); sb.append(ind(2) + "if (list2 == null) {\n");
sb.append(ind(3) + "list2 = new "+ ASTNode.listClass + "<>();\n"); sb.append(ind(3) + "list2 = new "+ ASTNode.listClass + "<>();\n");
sb.append(ind(2) + "}\n"); sb.append(ind(2) + "}\n");
...@@ -138,16 +107,11 @@ aspect BackendBidirectionalAPI { ...@@ -138,16 +107,11 @@ aspect BackendBidirectionalAPI {
sb.append(ind(1) + "}\n"); sb.append(ind(1) + "}\n");
// Remove // Remove
sb.append(ind(1) + "public void " + getTypeUse().decl() + ".remove"); sb.append(ind(1) + "public void " + getTypeUse().decl() + ".remove" + (useJastAddNames ? "" : "From") + nameCapitalized() + "(" + ofTypeDecl() + " o) {\n");
if (!useJastAddNames) {
sb.append("From");
}
sb.append(nameCapitalized() + "(" + ofTypeDecl() + " o) {\n");
sb.append(ind(2) + "assertNotNull(o);\n"); sb.append(ind(2) + "assertNotNull(o);\n");
sb.append(ind(2) + ASTNode.listClass + "<" + ofTypeDecl() + "> list = " + getImplAttributeField() + ";\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(2) + "if (list != null && list.remove(o)) {\n");
sb.append(ind(3) + ASTNode.listClass + "<" + otherSide().ofTypeDecl() + "> list2 = o." sb.append(ind(3) + ASTNode.listClass + "<" + otherSide().ofTypeDecl() + "> list2 = o." + otherSide().getImplAttributeField() + ";\n");
+ otherSide().getImplAttributeField() + ";\n");
sb.append(ind(3) + "if (list2 != null) list2.remove(this);\n"); sb.append(ind(3) + "if (list2 != null) list2.remove(this);\n");
sb.append(ind(3) + "set" + getImplAttributeName() + "(list);\n"); sb.append(ind(3) + "set" + getImplAttributeName() + "(list);\n");
sb.append(ind(3) + "o.set" + otherSide().getImplAttributeName() + "(list2);\n"); sb.append(ind(3) + "o.set" + otherSide().getImplAttributeName() + "(list2);\n");
...@@ -175,50 +139,44 @@ aspect BackendBidirectionalAPI { ...@@ -175,50 +139,44 @@ aspect BackendBidirectionalAPI {
// resolve the entire list // resolve the entire list
if (resolverHelper | serializer) { if (resolverHelper | serializer) {
sb.append(ind(2) + "if (l != null) {\n"); sb.append(ind(2) + "if (l != null) {\n");
sb.append(ind(3) + "boolean changed = false;\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(3) + "for (int i = 0; i < l.size(); i++) {\n");
sb.append(ind(4) + ofTypeDecl() + " element = l.get(i);\n"); sb.append(ind(4) + ofTypeDecl() + " element = l.get(i);\n");
sb.append(ind(4) + "if (element.is$Unresolved()) {\n"); sb.append(ind(4) + "if (element." + isUnresolvedMethod + "()) {\n");
sb.append(ind(5) + "changed = true;\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) + ofTypeDecl() + " resolvedElement = resolve" + nameCapitalized() + globallyResolvePostfix + "(element." + asUnresolvedMethod + "()." + getUnresolvedTokenMethod + "(), i);\n");
sb.append(ind(5) + "if (element.as$Unresolved().getUnresolved$ResolveOpposite()) {\n"); sb.append(ind(5) + "if (element." + asUnresolvedMethod + "()." + getUnresolvedResolveOppositeMethod + "()) {\n");
sb.append(ind(6) + getTypeUse().decl() + " oldTarget = resolvedElement." + otherSide().getImplAttributeField() + ";\n"); sb.append(ind(6) + getTypeUse().decl() + " oldTarget = resolvedElement." + otherSide().getImplAttributeField() + ";\n");
sb.append(ind(6) + "if (oldTarget != null && oldTarget != this) {\n"); sb.append(ind(6) + "if (oldTarget != null && oldTarget != this) {\n");
sb.append(ind(7) + "oldTarget." + getImplAttributeField() + ".remove(resolvedElement);\n"); sb.append(ind(7) + "oldTarget." + getImplAttributeField() + ".remove(resolvedElement);\n");
sb.append(ind(6) + "}\n"); sb.append(ind(6) + "}\n");
sb.append(ind(6) + "if (oldTarget == this) {\n"); sb.append(ind(6) + "if (oldTarget == this) {\n");
sb.append(ind(7) + "l.remove(i);\n"); sb.append(ind(7) + "l.remove(i);\n");
sb.append(ind(7) + "i--;\n"); sb.append(ind(7) + "i--;\n");
sb.append(ind(6) + "} else {\n"); sb.append(ind(6) + "} else {\n");
sb.append(ind(7) + "resolvedElement.set" + otherSide().getImplAttributeName() + "(this);\n"); sb.append(ind(7) + "resolvedElement.set" + otherSide().getImplAttributeName() + "(this);\n");
sb.append(ind(7) + "l.set(i, resolvedElement);\n"); sb.append(ind(7) + "l.set(i, resolvedElement);\n");
sb.append(ind(6) + "}\n"); sb.append(ind(6) + "}\n");
sb.append(ind(5) + "} else {\n"); sb.append(ind(5) + "} else {\n");
sb.append(ind(6) + "l.set(i, resolvedElement);\n"); sb.append(ind(6) + "l.set(i, resolvedElement);\n");
sb.append(ind(5) + "}\n"); sb.append(ind(5) + "}\n");
sb.append(ind(4) + "}\n"); sb.append(ind(4) + "}\n");
sb.append(ind(3) + "}\n"); sb.append(ind(3) + "}\n");
sb.append(ind(3) + "if (changed) {\n"); sb.append(ind(3) + "if (changed) {\n");
sb.append(ind(4) + "set" + getImplAttributeName() + "(l);\n"); sb.append(ind(4) + "set" + getImplAttributeName() + "(l);\n");
sb.append(ind(3) + "}\n"); sb.append(ind(3) + "}\n");
sb.append(ind(2) + "}\n"); sb.append(ind(2) + "}\n");
} }
sb.append(ind(2) + "return l != null ? Collections.unmodifiableList(l) : Collections.emptyList();\n"); sb.append(ind(2) + "return l != null ? Collections.unmodifiableList(l) : Collections.emptyList();\n");
sb.append(ind(1) + "}\n"); sb.append(ind(1) + "}\n");
// Add // Add
sb.append(ind(1) + "public void " + getTypeUse().decl() + ".add"); sb.append(ind(1) + "public void " + getTypeUse().decl() + ".add" + (useJastAddNames ? "" : "To") + nameCapitalized() + "(" + ofTypeDecl() + " o) {\n");
if (!useJastAddNames) {
sb.append("To");
}
sb.append(nameCapitalized() + "(" + ofTypeDecl() + " o) {\n");
sb.append(ind(2) + "assertNotNull(o);\n"); sb.append(ind(2) + "assertNotNull(o);\n");
sb.append(ind(2) + "if (o != null && o." + otherSide().getImplAttributeField() + " != null) {\n"); sb.append(ind(2) + "if (o != null && o." + otherSide().getImplAttributeField() + " != null) {\n");
sb.append(ind(3) + ASTNode.listClass + "<" + ofTypeDecl() + "> list2 = o." sb.append(ind(3) + ASTNode.listClass + "<" + ofTypeDecl() + "> list2 = o." + otherSide().getImplAttributeField() + "." + getImplAttributeField() + ";\n");
+ otherSide().getImplAttributeField() + "." + getImplAttributeField() + ";\n");
sb.append(ind(3) + "if (list2.remove(o))\n"); sb.append(ind(3) + "if (list2.remove(o))\n");
sb.append(ind(4) + "o." + otherSide().getImplAttributeField() sb.append(ind(4) + "o." + otherSide().getImplAttributeField() + ".set" + getImplAttributeName() + "(list2);\n");
+ ".set" + getImplAttributeName() + "(list2);\n");
sb.append(ind(2) + "}\n"); sb.append(ind(2) + "}\n");
sb.append(ind(2) + ASTNode.listClass + "<" + ofTypeDecl() + "> list = " + getImplAttributeField() + ";\n"); sb.append(ind(2) + ASTNode.listClass + "<" + ofTypeDecl() + "> list = " + getImplAttributeField() + ";\n");
sb.append(ind(2) + "if (list == null) {\n"); sb.append(ind(2) + "if (list == null) {\n");
...@@ -230,32 +188,24 @@ aspect BackendBidirectionalAPI { ...@@ -230,32 +188,24 @@ aspect BackendBidirectionalAPI {
sb.append(ind(1) + "}\n"); sb.append(ind(1) + "}\n");
// Insert / add at specific position // Insert / add at specific position
sb.append(ind(1) + "public void " + getTypeUse().decl() + ".add"); sb.append(ind(1) + "public void " + getTypeUse().decl() + ".add" + (useJastAddNames ? "" : "To") + nameCapitalized() + "(int index, " + ofTypeDecl() + " o) {\n");
if (!useJastAddNames) { sb.append(ind(2) + "assertNotNull(o);\n");
sb.append("To"); 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(nameCapitalized() + "(int index, " + ofTypeDecl() + " o) {\n"); sb.append(ind(3) + "if (list2.remove(o))\n");
sb.append(ind(2) + "assertNotNull(o);\n"); sb.append(ind(4) + "o." + otherSide().getImplAttributeField() + ".set" + getImplAttributeName() + "(list2);\n");
sb.append(ind(2) + "if (o != null && o." + otherSide().getImplAttributeField() + " != null) {\n"); sb.append(ind(2) + "}\n");
sb.append(ind(3) + ASTNode.listClass + "<" + ofTypeDecl() + "> list2 = o." + otherSide().getImplAttributeField() + "." + getImplAttributeField() + ";\n"); sb.append(ind(2) + ASTNode.listClass + "<" + ofTypeDecl() + "> list = " + getImplAttributeField() + ";\n");
sb.append(ind(3) + "if (list2.remove(o))\n"); sb.append(ind(2) + "if (list == null) {\n");
sb.append(ind(4) + "o." + otherSide().getImplAttributeField() + ".set" + getImplAttributeName() + "(list2);\n"); sb.append(ind(3) + "list = new " + ASTNode.listClass + "<>();\n");
sb.append(ind(2) + "}\n"); sb.append(ind(2) + "}\n");
sb.append(ind(2) + ASTNode.listClass + "<" + ofTypeDecl() + "> list = " + getImplAttributeField() + ";\n"); sb.append(ind(2) + "list.add(index, o);\n");
sb.append(ind(2) + "if (list == null) {\n"); sb.append(ind(2) + "set" + getImplAttributeName() + "(list);\n");
sb.append(ind(3) + "list = new " + ASTNode.listClass + "<>();\n"); sb.append(ind(2) + "o.set" + otherSide().getImplAttributeName() + "(this);\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"); sb.append(ind(1) + "}\n");
// Remove // Remove
sb.append(ind(1) + "public void " + getTypeUse().decl() + ".remove"); sb.append(ind(1) + "public void " + getTypeUse().decl() + ".remove" + (useJastAddNames ? "" : "From") + nameCapitalized() + "(" + ofTypeDecl() + " o) {\n");
if (!useJastAddNames) {
sb.append("From");
}
sb.append(nameCapitalized() + "(" + ofTypeDecl() + " o) {\n");
sb.append(ind(2) + "assertNotNull(o);\n"); sb.append(ind(2) + "assertNotNull(o);\n");
sb.append(ind(2) + ASTNode.listClass + "<" + ofTypeDecl() + "> list = " + getImplAttributeField() + ";\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(2) + "if (list != null && list.remove(o)) {\n");
...@@ -268,9 +218,6 @@ aspect BackendBidirectionalAPI { ...@@ -268,9 +218,6 @@ aspect BackendBidirectionalAPI {
} }
public void RelationComponent.generateBiOneMany(StringBuilder sb) { public void RelationComponent.generateBiOneMany(StringBuilder sb) {
// Get
generateGetOne(sb);
// Set // Set
sb.append(ind(1) + "public " + getTypeUse().decl() + " " + getTypeUse().decl() + ".set" + nameCapitalized() sb.append(ind(1) + "public " + getTypeUse().decl() + " " + getTypeUse().decl() + ".set" + nameCapitalized()
+ "(" + ofTypeDecl() + " o) {\n"); + "(" + ofTypeDecl() + " o) {\n");
...@@ -278,11 +225,9 @@ aspect BackendBidirectionalAPI { ...@@ -278,11 +225,9 @@ aspect BackendBidirectionalAPI {
sb.append(ind(2) + "assertNotNull(o);\n"); sb.append(ind(2) + "assertNotNull(o);\n");
} }
sb.append(ind(2) + "if (" + getImplAttributeField() + " != null) {\n"); sb.append(ind(2) + "if (" + getImplAttributeField() + " != null) {\n");
sb.append(ind(3) + ASTNode.listClass + "<" + getTypeUse().decl() + "> list2 = " + getImplAttributeField() sb.append(ind(3) + ASTNode.listClass + "<" + getTypeUse().decl() + "> list2 = " + getImplAttributeField() + "." + otherSide().getImplAttributeField() + ";\n");
+ "." + otherSide().getImplAttributeField() + ";\n");
sb.append(ind(3) + "list2.remove(this);\n"); sb.append(ind(3) + "list2.remove(this);\n");
sb.append(ind(3) + getImplAttributeField() + "." + "set" sb.append(ind(3) + getImplAttributeField() + "." + "set" + otherSide().getImplAttributeName() + "(list2);\n");
+ otherSide().getImplAttributeName() + "(list2);\n");
sb.append(ind(2) + "}\n"); sb.append(ind(2) + "}\n");
sb.append(ind(2) + "set" + getImplAttributeName() + "(o);\n"); sb.append(ind(2) + "set" + getImplAttributeName() + "(o);\n");
...@@ -290,8 +235,7 @@ aspect BackendBidirectionalAPI { ...@@ -290,8 +235,7 @@ aspect BackendBidirectionalAPI {
if (isOpt()) { if (isOpt()) {
sb.append(ind(2) + "if (o != null) {\n"); sb.append(ind(2) + "if (o != null) {\n");
} }
sb.append(ind(ind) + ASTNode.listClass + "<" + getTypeUse().decl() + "> list = o." sb.append(ind(ind) + ASTNode.listClass + "<" + getTypeUse().decl() + "> list = o." + otherSide().getImplAttributeField() + ";\n");
+ otherSide().getImplAttributeField() + ";\n");
sb.append(ind(ind) + "if (list == null) {\n"); sb.append(ind(ind) + "if (list == null) {\n");
sb.append(ind(ind+1) + "list = new " + ASTNode.listClass + "<>();\n"); sb.append(ind(ind+1) + "list = new " + ASTNode.listClass + "<>();\n");
sb.append(ind(ind) + "}\n"); sb.append(ind(ind) + "}\n");
...@@ -302,9 +246,5 @@ aspect BackendBidirectionalAPI { ...@@ -302,9 +246,5 @@ aspect BackendBidirectionalAPI {
} }
sb.append(ind(2) + "return this;\n"); sb.append(ind(2) + "return this;\n");
sb.append(ind(1) + "}\n"); sb.append(ind(1) + "}\n");
if (isOpt()) {
generateExtraOptAPI(sb);
}
} }
} }
aspect BackendDirectedAPI { aspect BackendDirectedAPI {
public void RelationComponent.generateDirectedZeroOneAPI(StringBuilder sb) { public void RelationComponent.generateDirectedZeroOneAPI(StringBuilder sb) {
// Get
generateGetOne(sb);
// Set // Set
sb.append(ind(1) + "public " + getTypeUse().decl() + " " + getTypeUse().decl()); sb.append(ind(1) + "public " + getTypeUse().decl() + " " + getTypeUse().decl());
sb.append(".set" + nameCapitalized() + "(" + ofTypeDecl() + " o) {\n"); sb.append(".set" + nameCapitalized() + "(" + ofTypeDecl() + " o) {\n");
...@@ -13,10 +10,6 @@ aspect BackendDirectedAPI { ...@@ -13,10 +10,6 @@ aspect BackendDirectedAPI {
sb.append(ind(2) + "set" + getImplAttributeName() + "(o);\n"); sb.append(ind(2) + "set" + getImplAttributeName() + "(o);\n");
sb.append(ind(2) + "return this;\n"); sb.append(ind(2) + "return this;\n");
sb.append(ind(1) + "}\n"); sb.append(ind(1) + "}\n");
if (isOpt()) {
generateExtraOptAPI(sb);
}
} }
public void RelationComponent.generateDirectedManyAPI(StringBuilder sb) { public void RelationComponent.generateDirectedManyAPI(StringBuilder sb) {
...@@ -38,29 +31,25 @@ aspect BackendDirectedAPI { ...@@ -38,29 +31,25 @@ aspect BackendDirectedAPI {
// resolve the entire list // resolve the entire list
if (resolverHelper | serializer) { if (resolverHelper | serializer) {
sb.append(ind(2) + "if (l != null) {\n"); sb.append(ind(2) + "if (l != null) {\n");
sb.append(ind(3) + "boolean changed = false;\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(3) + "for (int i = 0; i < l.size(); i++) {\n");
sb.append(ind(4) + ofTypeDecl() + " element = l.get(i);\n"); sb.append(ind(4) + ofTypeDecl() + " element = l.get(i);\n");
sb.append(ind(4) + "if (element.is$Unresolved()) {\n"); sb.append(ind(4) + "if (element." + isUnresolvedMethod + "()) {\n");
sb.append(ind(5) + "changed = true;\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) + ofTypeDecl() + " resolvedElement = resolve" + nameCapitalized() + "ByToken(element." + asUnresolvedMethod + "()." + getUnresolvedTokenMethod + "(), i);\n");
sb.append(ind(5) + "l.set(i, resolvedElement);\n"); sb.append(ind(5) + "l.set(i, resolvedElement);\n");
sb.append(ind(4) + "}\n"); sb.append(ind(4) + "}\n");
sb.append(ind(3) + "}\n"); sb.append(ind(3) + "}\n");
sb.append(ind(3) + "if (changed) {\n"); sb.append(ind(3) + "if (changed) {\n");
sb.append(ind(4) + "set" + getImplAttributeName() + "(l);\n"); sb.append(ind(4) + "set" + getImplAttributeName() + "(l);\n");
sb.append(ind(3) + "}\n"); sb.append(ind(3) + "}\n");
sb.append(ind(2) + "}\n"); sb.append(ind(2) + "}\n");
} }
sb.append(ind(2) + "return l != null ? Collections.unmodifiableList(l) : Collections.emptyList();\n"); sb.append(ind(2) + "return l != null ? Collections.unmodifiableList(l) : Collections.emptyList();\n");
sb.append(ind(1) + "}\n"); sb.append(ind(1) + "}\n");
// Add // Add
sb.append(ind(1) + "public void " + getTypeUse().decl() + ".add"); sb.append(ind(1) + "public void " + getTypeUse().decl() + ".add" + (useJastAddNames ? "" : "To") + nameCapitalized() + "(" + ofTypeDecl() + " o) {\n");
if (!useJastAddNames) {
sb.append("To");
}
sb.append(nameCapitalized() + "(" + ofTypeDecl() + " o) {\n");
sb.append(ind(2) + "assertNotNull(o);\n"); sb.append(ind(2) + "assertNotNull(o);\n");
sb.append(ind(2) + ASTNode.listClass + "<" + ofTypeDecl() + "> list = " + getImplAttributeField() + ";\n"); sb.append(ind(2) + ASTNode.listClass + "<" + ofTypeDecl() + "> list = " + getImplAttributeField() + ";\n");
sb.append(ind(2) + "if (list == null) {\n"); sb.append(ind(2) + "if (list == null) {\n");
...@@ -71,11 +60,7 @@ aspect BackendDirectedAPI { ...@@ -71,11 +60,7 @@ aspect BackendDirectedAPI {
sb.append(ind(1) + "}\n"); sb.append(ind(1) + "}\n");
// Insert / add at specific position // Insert / add at specific position
sb.append(ind(1) + "public void " + getTypeUse().decl() + ".add"); sb.append(ind(1) + "public void " + getTypeUse().decl() + ".add" + (useJastAddNames ? "" : "To") + nameCapitalized() + "(int index, " + ofTypeDecl() + " o) {\n");
if (!useJastAddNames) {
sb.append("To");
}
sb.append(nameCapitalized() + "(int index, " + ofTypeDecl() + " o) {\n");
sb.append(ind(2) + "assertNotNull(o);\n"); sb.append(ind(2) + "assertNotNull(o);\n");
sb.append(ind(2) + ASTNode.listClass + "<" + ofTypeDecl() + "> list = " + getImplAttributeField() + ";\n"); sb.append(ind(2) + ASTNode.listClass + "<" + ofTypeDecl() + "> list = " + getImplAttributeField() + ";\n");
sb.append(ind(2) + "if (list == null) {\n"); sb.append(ind(2) + "if (list == null) {\n");
...@@ -86,11 +71,7 @@ aspect BackendDirectedAPI { ...@@ -86,11 +71,7 @@ aspect BackendDirectedAPI {
sb.append(ind(1) + "}\n"); sb.append(ind(1) + "}\n");
// Remove // Remove
sb.append(ind(1) + "public void " + getTypeUse().decl() + ".remove"); sb.append(ind(1) + "public void " + getTypeUse().decl() + ".remove" + (useJastAddNames ? "" : "From") + nameCapitalized() + "(" + ofTypeDecl() + " o) {\n");
if (!useJastAddNames) {
sb.append("From");
}
sb.append(nameCapitalized() + "(" + ofTypeDecl() + " o) {\n");
sb.append(ind(2) + "assertNotNull(o);\n"); sb.append(ind(2) + "assertNotNull(o);\n");
sb.append(ind(2) + ASTNode.listClass + "<" + ofTypeDecl() + "> list = " + getImplAttributeField() + ";\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(2) + "if (list != null && list.remove(o)) {\n");
...@@ -98,25 +79,4 @@ aspect BackendDirectedAPI { ...@@ -98,25 +79,4 @@ aspect BackendDirectedAPI {
sb.append(ind(2) + "}\n"); sb.append(ind(2) + "}\n");
sb.append(ind(1) + "}\n"); sb.append(ind(1) + "}\n");
} }
public void RelationComponent.generateGetOne(StringBuilder sb) {
sb.append(ind(1) + "public " + ofTypeDecl() + " " + getTypeUse().decl() + ".");
if (useJastAddNames) {
sb.append("get" + nameCapitalized());
} else {
sb.append(name());
}
sb.append("() {\n");
if (resolverHelper | serializer) {
sb.append(ind(2) + "if (" + getImplAttributeField() + " != null && " + getImplAttributeField() + ".is$Unresolved()) {\n");
sb.append(ind(3) + "if (" + getImplAttributeField() + ".as$Unresolved().getUnresolved$ResolveOpposite()) {\n");
sb.append(ind(4) + "set" + nameCapitalized() + "(resolve" + nameCapitalized() + "ByToken(" + getImplAttributeField() + ".as$Unresolved().getUnresolved$Token()));\n");
sb.append(ind(3) + "} else {\n");
sb.append(ind(4) + "set" + getImplAttributeName() + "(resolve" + nameCapitalized() + "ByToken(" + getImplAttributeField() + ".as$Unresolved().getUnresolved$Token()));\n");
sb.append(ind(3) + "}\n");
sb.append(ind(2) + "}\n");
}
sb.append(ind(2) + "return get" + getImplAttributeName() + "();\n");
sb.append(ind(1) + "}\n");
}
} }
aspect NameResolutionHelper { aspect NameResolutionHelper {
protected static final String ASTNode.unresolvedPrefix = "Unresolved$";
protected static final String ASTNode.isUnresolvedMethod = "is$Unresolved";
protected static final String ASTNode.asUnresolvedMethod = "as$Unresolved";
protected static final String ASTNode.getUnresolvedTokenMethod = "get" + unresolvedPrefix + "Token";
protected static final String ASTNode.getUnresolvedResolveOppositeMethod = "get" + unresolvedPrefix + "ResolveOpposite";
protected static final String ASTNode.createReferenceMethod = "createReference";
protected static final String ASTNode.createRefMethod = "createRef";
protected static final String ASTNode.createRefDirectionMethod = "createRefDirection";
protected static final String ASTNode.globallyResolvePrefix = "globallyResolve";
protected static final String ASTNode.globallyResolvePostfix = "ByToken";
protected static final String ASTNode.resolveAllMethod = "resolveAll";
protected static final String ASTNode.treeResolveAllMethod = "treeResolveAll";
public String Program.generateRewriteToSuperTypeStub() {