DirectedAPI.jadd 4.49 KB
Newer Older
1
2
aspect BackendDirectedAPI {

3
  public void RelationComponent.generateDirectedZeroOneAPI(StringBuilder sb) {
4
5
    String setMethodDecl = getTypeUse().decl() + " " + getTypeUse().decl() + ".set" + nameCapitalized() + "(" + ofTypeDecl() + " o)";

6
    // Set
7
    sb.append(ind(1) + "public " + setMethodDecl + " {\n");
8
    if (!isOpt()) {
9
10
11
12
13
14
15
      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");
  }

16
  public void RelationComponent.generateDirectedManyAPI(StringBuilder sb) {
17

18
19
    // Get
    if (useJastAddNames) {
20
21
      String getMethodDecl1 = "java.util.List<" + ofTypeDecl() + "> " + getTypeUse().decl() + ".get" + nameCapitalized() + "s()";
      String getMethodDecl2 = "java.util.List<" + ofTypeDecl() + "> " + getTypeUse().decl() + ".get" + nameCapitalized() + "List()";
22
      // getXs
23
      sb.append(ind(1) + "public " + getMethodDecl1 + " {\n");
24
25
26
27
      sb.append(ind(2) + "return get" + nameCapitalized() + "List();\n");
      sb.append(ind(1) + "}\n");

      // getXList
28
      sb.append(ind(1) + "public " + getMethodDecl2 + " {\n");
29
    } else {
30
31
      String getMethodDecl = "java.util.List<" + ofTypeDecl() + "> " + getTypeUse().decl() + "." + name() + "()";
      sb.append("public " + getMethodDecl + " {\n");
32
    }
33
    sb.append(ind(2) + ASTNode.listInterface + "<" + ofTypeDecl() + "> l = get" + getImplAttributeName() + "();\n");
34
35
36
    // resolve the entire list
    if (resolverHelper | serializer) {
      sb.append(ind(2) + "if (l != null) {\n");
37
38
39
40
41
      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");
42
      sb.append(ind(5) + ofTypeDecl() + " resolvedElement = " + resolvePrefix + "" + nameCapitalized() + "" + resolvePostfix + "(element." + asUnresolvedMethod + "().get" + unresolvedTokenMethod + "(), i);\n");
43
44
45
46
47
48
      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");
49
50
      sb.append(ind(2) + "}\n");
    }
51
    sb.append(ind(2) + "return l != null ? java.util.Collections.unmodifiableList(l) : java.util.Collections.emptyList();\n");
52
53
54
    sb.append(ind(1) + "}\n");

    // Add
55
56
    String addMethodDecl1 = "void " + getTypeUse().decl() + ".add" + (useJastAddNames ? "" : "To") + nameCapitalized() + "(" + ofTypeDecl() + " o)";
    sb.append(ind(1) + "public " + addMethodDecl1 + " {\n");
57
    sb.append(ind(2) + "assertNotNull(o);\n");
58
    sb.append(ind(2) + ASTNode.listInterface + "<" + ofTypeDecl() + "> list = " + getImplAttributeField() + ";\n");
59
60
61
62
63
64
65
66
    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(1) + "}\n");

    // Insert / add at specific position
67
68
    String addMethodDecl2 = "void " + getTypeUse().decl() + ".add" + (useJastAddNames ? "" : "To") + nameCapitalized() + "(int index, " + ofTypeDecl() + " o)";
    sb.append(ind(1) + "public " + addMethodDecl2 + " {\n");
69
    sb.append(ind(2) + "assertNotNull(o);\n");
70
    sb.append(ind(2) + ASTNode.listInterface + "<" + ofTypeDecl() + "> list = " + getImplAttributeField() + ";\n");
71
72
73
74
75
76
77
78
    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(1) + "}\n");

    // Remove
79
80
    String removeMethodDecl = "void " + getTypeUse().decl() + ".remove" + (useJastAddNames ? "" : "From") + nameCapitalized() + "(" + ofTypeDecl() + " o)";
    sb.append(ind(1) + "public " + removeMethodDecl + " {\n");
81
    sb.append(ind(2) + "assertNotNull(o);\n");
82
    sb.append(ind(2) + ASTNode.listInterface + "<" + ofTypeDecl() + "> list = " + getImplAttributeField() + ";\n");
83
84
85
86
87
88
    sb.append(ind(2) + "if (list != null && list.remove(o)) {\n");
    sb.append(ind(3) + "set" + getImplAttributeName() + "(list);\n");
    sb.append(ind(2) + "}\n");
    sb.append(ind(1) + "}\n");
  }
}