API.jadd 4.07 KB
Newer Older
1
2
aspect BackendAPI {
  public void Relation.generateAPI(StringBuilder sb) {
3
4
5
6
7
8
9
10
11
    sb.append(ind(1) + "// api for " + prettyPrint() + "\n");
    if (getLeft().isNavigable()) {
      sb.append(ind(1) + "// left direction for " + prettyPrint() + "\n");
      getLeft().generateAPI(sb);
    }
    if (getRight().isNavigable()) {
      sb.append(ind(1) + "// right direction for " + prettyPrint() + "\n");
      getRight().generateAPI(sb);
    }
12
13
14
    sb.append("\n");
  }

15
  public void RelationComponent.generateAPI(StringBuilder sb) {
16
    if (opposite().isNavigable()) {
17
      if (multiplicityOne() || multiplicityOpt()) {
18
        generateGetOne(sb);
19
        if (opposite().multiplicityOne() || opposite().multiplicityOpt()) {
20
          generateBiOneOne(sb);
21
        } else if (opposite().multiplicityMany()) {
22
23
          generateBiOneMany(sb);
        }
24
25
26
        if (isOpt()) {
          generateExtraOptAPI(sb);
        }
27
      } else if (multiplicityMany()) {
28
        if (opposite().multiplicityOne() || opposite().multiplicityOpt()) {
29
          generateBiManyOne(sb);
30
        } else if (opposite().multiplicityMany()) {
31
32
33
34
35
          generateBiManyMany(sb);
        }
      }
    } else {
      if (multiplicityOne() || multiplicityOpt()) {
36
        generateGetOne(sb);
37
        generateDirectedZeroOneAPI(sb);
38
39
40
        if (isOpt()) {
          generateExtraOptAPI(sb);
        }
41
42
43
44
45
46
      } else if (multiplicityMany()) {
        generateDirectedManyAPI(sb);
      }
    }
  }

47
48
49
50
51
52
53
54
55
  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) {
56
57
58
      sb.append(ind(2) + "if (" + getImplAttributeField() + " != null && " + getImplAttributeField() + "." + isUnresolvedMethod + "()) {\n");
        sb.append(ind(3) + "if (" + getImplAttributeField() + "." + asUnresolvedMethod + "().get" + unresolvedPrefix + "ResolveOpposite()) {\n");
          sb.append(ind(4) + "set" + nameCapitalized() + "(resolve" + nameCapitalized() + resolvePostfix + "(" + getImplAttributeField() + "." + asUnresolvedMethod + "().get" + unresolvedPrefix + "Token()));\n");
59
        sb.append(ind(3) + "} else {\n");
60
          sb.append(ind(4) + "set" + getImplAttributeName() + "(resolve" + nameCapitalized() + resolvePostfix + "(" + getImplAttributeField() + "." + asUnresolvedMethod + "().get" + unresolvedPrefix + "Token()));\n");
61
62
63
64
65
66
67
        sb.append(ind(3) + "}\n");
      sb.append(ind(2) + "}\n");
    }
    sb.append(ind(2) + "return get" + getImplAttributeName() + "();\n");
    sb.append(ind(1) + "}\n");
  }

68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
  public void RelationComponent.generateExtraOptAPI(StringBuilder sb) {
    // has
    sb.append(ind(1) + "public boolean " + getTypeUse().decl());
    sb.append(".has" + nameCapitalized() + "() {\n");
    sb.append(ind(2) + "return ");
    if (useJastAddNames) {
      sb.append("get" + nameCapitalized());
    } else {
      sb.append(name());
    }
    sb.append("() != null;\n");
    sb.append(ind(1) + "}\n");

    // clear
    sb.append(ind(1) + "public void " + getTypeUse().decl());
    sb.append(".clear" + nameCapitalized() + "() {\n");
    sb.append(ind(2) + "set" + nameCapitalized() + "(null);\n");
    sb.append(ind(1) + "}\n");
  }
87
88

  inh Relation Direction.relation();
89
  inh Relation RelationComponent.relation();
90
91
92
  eq Relation.getChild().relation() = this;
  eq Program.getChild().relation() = null;

93
94
95
  inh boolean RelationComponent.isNavigable();
  eq Relation.getLeft().isNavigable() = getDirection().isNavigableLeftToRight();
  eq Relation.getRight().isNavigable() = getDirection().isNavigableRightToLeft();
Johannes Mey's avatar
Johannes Mey committed
96
  eq Program.getChild().isNavigable() = false;
97
98
99
100
101
102

  syn boolean Direction.isNavigableRightToLeft() = true;
  eq RightDirection.isNavigableRightToLeft() = false;
  syn boolean Direction.isNavigableLeftToRight() = true;
  eq LeftDirection.isNavigableLeftToRight() = false;

103
104
105
106
  public String RelationComponent.nameCapitalized() {
    return name().substring(0,1).toUpperCase() + name().substring(1);
  }
}