BidirectionalAPI.jadd 14.6 KB
Newer Older
1
2
aspect BackendBidirectionalAPI {

3
  public void RelationComponent.generateBiOneOne(StringBuilder sb) {
4
    boolean resolve = resolverHelper || serializer;
5
6

    // Set
7
8
    String setMethodDecl = getTypeUse().decl() + " " + getTypeUse().decl() + ".set" + nameCapitalized() + "(" + ofTypeDecl() + " o)";
    sb.append(ind(1) + "public " + setMethodDecl + " {\n");
9
    if (!isOpt()) {
10
11
12
      sb.append(ind(2) + "assertNotNull(o);\n");
    }
    // unset the old opposite
13
    sb.append(ind(2) + "if (" + getImplAttributeField() + " != null)\n");
14
    sb.append(ind(3) + getImplAttributeField() + ".set" + opposite().getImplAttributeName() + "(null);\n");
15

16
17
    sb.append(ind(2) + "if (o != null && " + (resolve ? "!o." + isUnresolvedMethod + "() && o." : "o.") + opposite().getImplAttributeField() + " != null) {\n");
    sb.append(ind(3) + "o." + opposite().getImplAttributeField() + ".set" + getImplAttributeName() + "(null);\n");
18
19
    sb.append(ind(2) + "}\n");
    sb.append(ind(2) + "set" + getImplAttributeName() + "(o);\n");
20
21
    if (resolve) {
      sb.append(ind(2) + "if (o == null || !o." + isUnresolvedMethod + "())\n");
22
    }
23
24
25
    if (isOpt()) {
      sb.append(ind(resolve ? 3 : 2) + "if (o != null)\n");
    }
26
    sb.append(ind((isOpt() ? 1 : 0) + (resolve ? 3 : 2)) + "o.set" + opposite().getImplAttributeName() + "(this);\n");
27
28
29
30
31

    sb.append(ind(2) + "return this;\n");
    sb.append(ind(1) + "}\n");
  }

32
  public void RelationComponent.generateBiManyMany(StringBuilder sb) {
33
34
    // Get
    if (useJastAddNames) {
35
36
37
      String getMethodDecl1 = "java.util.List<" + ofTypeDecl() + "> " + getTypeUse().decl() + ".get" + nameCapitalized() + "s()";
      String getMethodDecl2 = "java.util.List<" + ofTypeDecl() + "> " + getTypeUse().decl() + ".get" + nameCapitalized() + "List()";

38
      // getXs
39
      sb.append("public " + getMethodDecl1 + " {\n");
40
41
42
43
      sb.append(ind(2) + "return get" + nameCapitalized() + "List();\n");
      sb.append(ind(1) + "}\n");

      // getXList
44
      sb.append(ind(1) + "public " + getMethodDecl2 + " {\n");
45
    } else {
46
47
      String getMethodDecl = "java.util.List<" + ofTypeDecl() + "> " + getTypeUse().decl() + "." + name() + "()";
      sb.append("public " + getMethodDecl + " {\n");
48
    }
49
    sb.append(ind(2) + ASTNode.listInterface + "<" + ofTypeDecl() + "> l = get" + getImplAttributeName() + "();\n");
50
51
52
    // resolve the entire list
    if (resolverHelper | serializer) {
      sb.append(ind(2) + "if (l != null) {\n");
53
54
55
56
57
      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." + isUnresolvedMethod + "()) {\n");
      sb.append(ind(5) + "changed = true;\n");
58
59
      sb.append(ind(5) + ofTypeDecl() + " resolvedElement = resolve" + nameCapitalized() + "" + resolvePostfix + "(element." + asUnresolvedMethod + "().get" + unresolvedTokenMethod + "(), i);\n");
      sb.append(ind(5) + "if (resolvedElement != null && element." + asUnresolvedMethod + "().get" + unresolvedResolveOppositeMethod + "()) {\n");
60
      sb.append(ind(6) + ASTNode.listInterface + "<" + getTypeUse().decl() + "> otherList = resolvedElement." + opposite().getImplAttributeField() + ";\n");
61
62
63
64
      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");
65
      sb.append(ind(6) + "resolvedElement.set" + opposite().getImplAttributeName() + "(otherList);\n");
66
67
68
69
70
71
72
      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");
73
74
      sb.append(ind(2) + "}\n");
    }
75
    sb.append(ind(2) + "return l != null ? java.util.Collections.unmodifiableList(l) : java.util.Collections.emptyList();\n");
76
77
78
    sb.append(ind(1) + "}\n");

    // Add
79
80
    String addMethodDecl1 = "void " + getTypeUse().decl() + ".add" + (useJastAddNames ? "" : "To") + nameCapitalized() + "(" + ofTypeDecl() + " o)";
    sb.append(ind(1) + "public " + addMethodDecl1 + " {\n");
81
    sb.append(ind(2) + "assertNotNull(o);\n");
82
    sb.append(ind(2) + ASTNode.listInterface + "<" + ofTypeDecl() + "> list = " + getImplAttributeField() + ";\n");
83
84
85
    sb.append(ind(2) + "if (list == null) {\n");
    sb.append(ind(3) + "list = new " + ASTNode.listClass + "<>();\n");
    sb.append(ind(2) + "}\n");
86
    sb.append(ind(2) + ASTNode.listInterface + "<" + opposite().ofTypeDecl() + "> list2 = o." + opposite().getImplAttributeField() + ";\n");
87
88
89
90
91
92
    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");
93
    sb.append(ind(2) + "o.set" + opposite().getImplAttributeName() + "(list2);\n");
94
95
96
    sb.append(ind(1) + "}\n");

    // Insert / add at specific position
97
98
    String addMethodDecl2 = "void " + getTypeUse().decl() + ".add" + (useJastAddNames ? "" : "To") + nameCapitalized() + "(int index, " + ofTypeDecl() + " o)";
    sb.append(ind(1) + "public " + addMethodDecl2 + " {\n");
99
    sb.append(ind(2) + "assertNotNull(o);\n");
100
    sb.append(ind(2) + ASTNode.listInterface + "<" + ofTypeDecl() + "> list = " + getImplAttributeField() + ";\n");
101
102
103
    sb.append(ind(2) + "if (list == null) {\n");
    sb.append(ind(3) + "list = new " + ASTNode.listClass + "<>();\n");
    sb.append(ind(2) + "}\n");
104
    sb.append(ind(2) + ASTNode.listInterface + "<" + opposite().ofTypeDecl() + "> list2 = o." + opposite().getImplAttributeField() + ";\n");
105
106
107
108
109
110
    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");
111
    sb.append(ind(2) + "o.set" + opposite().getImplAttributeName() + "(list2);\n");
112
113
114
    sb.append(ind(1) + "}\n");

    // Remove
115
116
    String removeMethodDecl = "void " + getTypeUse().decl() + ".remove" + (useJastAddNames ? "" : "From") + nameCapitalized() + "(" + ofTypeDecl() + " o)";
    sb.append(ind(1) + "public " + removeMethodDecl + " {\n");
117
    sb.append(ind(2) + "assertNotNull(o);\n");
118
    sb.append(ind(2) + ASTNode.listInterface + "<" + ofTypeDecl() + "> list = " + getImplAttributeField() + ";\n");
119
    sb.append(ind(2) + "if (list != null && list.remove(o)) {\n");
120
    sb.append(ind(3) + ASTNode.listInterface + "<" + opposite().ofTypeDecl() + "> list2 = o." + opposite().getImplAttributeField() + ";\n");
121
122
    sb.append(ind(3) + "if (list2 != null) list2.remove(this);\n");
    sb.append(ind(3) + "set" + getImplAttributeName() + "(list);\n");
123
    sb.append(ind(3) + "o.set" + opposite().getImplAttributeName() + "(list2);\n");
124
125
126
127
128
    sb.append(ind(2) + "}\n");
    sb.append(ind(1) + "}\n");
  }


129
  public void RelationComponent.generateBiManyOne(StringBuilder sb) {
130
131
    // Get
    if (useJastAddNames) {
132
133
134
      String getMethodDecl1 = "java.util.List<" + ofTypeDecl() + "> " + getTypeUse().decl() + ".get" + nameCapitalized() + "s()";
      String getMethodDecl2 = "java.util.List<" + ofTypeDecl() + "> " + getTypeUse().decl() + ".get" + nameCapitalized() + "List()";

135
      // getXs
136
      sb.append("public " + getMethodDecl1 + " {\n");
137
138
139
140
      sb.append(ind(2) + "return get" + nameCapitalized() + "List();\n");
      sb.append(ind(1) + "}\n");

      // getXList
141
      sb.append(ind(1) + "public " + getMethodDecl2 + " {\n");
142
    } else {
143
144
      String getMethodDecl = "java.util.List<" + ofTypeDecl() + "> " + getTypeUse().decl() + "." + name() + "()";
      sb.append("public " + getMethodDecl + " {\n");
145
    }
146
    sb.append(ind(2) + ASTNode.listInterface + "<" + ofTypeDecl() + "> l = get" + getImplAttributeName() + "();\n");
147
148
149
    // resolve the entire list
    if (resolverHelper | serializer) {
      sb.append(ind(2) + "if (l != null) {\n");
150
151
152
153
154
      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." + isUnresolvedMethod + "()) {\n");
      sb.append(ind(5) + "changed = true;\n");
155
156
      sb.append(ind(5) + ofTypeDecl() + " resolvedElement = resolve" + nameCapitalized() + resolvePostfix + "(element." + asUnresolvedMethod + "().get" + unresolvedTokenMethod + "(), i);\n");
      sb.append(ind(5) + "if (element." + asUnresolvedMethod + "().get" + unresolvedResolveOppositeMethod + "()) {\n");
157
      sb.append(ind(6) + getTypeUse().decl() + " oldTarget = resolvedElement." + opposite().getImplAttributeField() + ";\n");
158
159
160
161
162
163
164
      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");
165
      sb.append(ind(7) + "resolvedElement.set" + opposite().getImplAttributeName() + "(this);\n");
166
167
168
169
170
171
172
173
174
175
      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");
176
177
      sb.append(ind(2) + "}\n");
    }
178
    sb.append(ind(2) + "return l != null ? java.util.Collections.unmodifiableList(l) : java.util.Collections.emptyList();\n");
179
180
181
    sb.append(ind(1) + "}\n");

    // Add
182
183
    String addMethodDecl1 = "void " + getTypeUse().decl() + ".add" + (useJastAddNames ? "" : "To") + nameCapitalized() + "(" + ofTypeDecl() + " o)";
    sb.append(ind(1) + "public " + addMethodDecl1 + " {\n");
184
    sb.append(ind(2) + "assertNotNull(o);\n");
185
186
    sb.append(ind(2) + "if (o != null && o." + opposite().getImplAttributeField() + " != null) {\n");
    sb.append(ind(3) + ASTNode.listInterface + "<" + ofTypeDecl() + "> list2 = o." + opposite().getImplAttributeField() + "." + getImplAttributeField() + ";\n");
187
    sb.append(ind(3) + "if (list2.remove(o))\n");
188
    sb.append(ind(4) + "o." + opposite().getImplAttributeField()  + ".set" + getImplAttributeName() + "(list2);\n");
189
    sb.append(ind(2) + "}\n");
190
    sb.append(ind(2) + ASTNode.listInterface + "<" + ofTypeDecl() + "> list = " + getImplAttributeField() + ";\n");
191
192
193
194
195
    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");
196
    sb.append(ind(2) + "o.set" + opposite().getImplAttributeName() + "(this);\n");
197
198
199
    sb.append(ind(1) + "}\n");

    // Insert / add at specific position
200
201
    String addMethodDecl2 = "void " + getTypeUse().decl() + ".add" + (useJastAddNames ? "" : "To") + nameCapitalized() + "(int index, " + ofTypeDecl() + " o)";
    sb.append(ind(1) + "public " + addMethodDecl2 + " {\n");
202
    sb.append(ind(2) + "assertNotNull(o);\n");
203
204
    sb.append(ind(2) + "if (o != null && o." + opposite().getImplAttributeField() + " != null) {\n");
    sb.append(ind(3) + ASTNode.listInterface + "<" + ofTypeDecl() + "> list2 = o." + opposite().getImplAttributeField() + "." + getImplAttributeField() + ";\n");
205
    sb.append(ind(3) + "if (list2.remove(o))\n");
206
    sb.append(ind(4) + "o." + opposite().getImplAttributeField() + ".set" + getImplAttributeName() + "(list2);\n");
207
    sb.append(ind(2) + "}\n");
208
    sb.append(ind(2) + ASTNode.listInterface + "<" + ofTypeDecl() + "> list = " + getImplAttributeField() + ";\n");
209
210
211
212
213
    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");
214
    sb.append(ind(2) + "o.set" + opposite().getImplAttributeName() + "(this);\n");
215
216
217
    sb.append(ind(1) + "}\n");

    // Remove
218
219
    String removeMethodDecl = "void " + getTypeUse().decl() + ".remove" + (useJastAddNames ? "" : "From") + nameCapitalized() + "(" + ofTypeDecl() + " o)";
    sb.append(ind(1) + "public " + removeMethodDecl + " {\n");
220
    sb.append(ind(2) + "assertNotNull(o);\n");
221
    sb.append(ind(2) + ASTNode.listInterface + "<" + ofTypeDecl() + "> list = " + getImplAttributeField() + ";\n");
222
223
    sb.append(ind(2) + "if (list != null && list.remove(o)) {\n");
    sb.append(ind(3) + "set" + getImplAttributeName() + "(list);\n");
224
225
    sb.append(ind(3) + "if (o." + opposite().getImplAttributeField() + " == this) {\n");
    sb.append(ind(4) + "o.set" + opposite().getImplAttributeName() + "(null);\n");
226
227
228
229
230
    sb.append(ind(3) + "}\n");
    sb.append(ind(2) + "}\n");
    sb.append(ind(1) + "}\n");
  }

231
  public void RelationComponent.generateBiOneMany(StringBuilder sb) {
232
    // Set
233
234
    String setMethodDecl = getTypeUse().decl() + " " + getTypeUse().decl() + ".set" + nameCapitalized() + "(" + ofTypeDecl() + " o)";
    sb.append(ind(1) + "public " + setMethodDecl + " {\n");
235
    if (!isOpt()) {
236
237
238
      sb.append(ind(2) + "assertNotNull(o);\n");
    }
    sb.append(ind(2) + "if (" + getImplAttributeField() + " != null) {\n");
239
    sb.append(ind(3) + ASTNode.listInterface + "<" + getTypeUse().decl() + "> list2 = " + getImplAttributeField() + "." + opposite().getImplAttributeField() + ";\n");
240
    sb.append(ind(3) + "list2.remove(this);\n");
241
    sb.append(ind(3) + getImplAttributeField() + "." + "set" + opposite().getImplAttributeName() + "(list2);\n");
242
243
244
    sb.append(ind(2) + "}\n");
    sb.append(ind(2) + "set" + getImplAttributeName() + "(o);\n");

245
246
    int ind = isOpt() ? 3 : 2;
    if (isOpt()) {
247
248
      sb.append(ind(2) + "if (o != null) {\n");
    }
249
    sb.append(ind(ind) + ASTNode.listInterface + "<" + getTypeUse().decl() + "> list = o." + opposite().getImplAttributeField() + ";\n");
250
251
252
253
    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");
254
    sb.append(ind(ind) + "o.set" + opposite().getImplAttributeName() + "(list);\n");
255
    if (isOpt()) {
256
257
258
259
260
261
      sb.append(ind(2) + "}\n");
    }
    sb.append(ind(2) + "return this;\n");
    sb.append(ind(1) + "}\n");
  }
}