Errors.jrag 3.76 KB
Newer Older
René Schöne's avatar
René Schöne committed
1
2
3
4
5
import java.util.Set;
import java.util.TreeSet;
import java.util.LinkedList;

aspect Errors {
6
  coll Set<ErrorMessage> RagConnect.errors()
René Schöne's avatar
René Schöne committed
7
    [new TreeSet<ErrorMessage>()]
8
    root RagConnect;
René Schöne's avatar
René Schöne committed
9

René Schöne's avatar
René Schöne committed
10
11
    ReadFromMqttDefinition contributes error("Read definition already defined for " + getToken().getName())
      when isAlreadyDefined()
12
      to RagConnect.errors();
René Schöne's avatar
René Schöne committed
13
14
15

    ReadFromMqttDefinition contributes error("Reading target token must not be an NTA token!")
      when getToken().getNTA()
16
      to RagConnect.errors();
René Schöne's avatar
René Schöne committed
17
18
19
20
21
22
23

    // if first mapping is null, then suitableDefaultMapping() == null
    ReadFromMqttDefinition contributes error("No suitable default mapping found for type " +
      ((getMappingList().isEmpty())
        ? getToken().getJavaTypeUse().prettyPrint()
        : getMappingList().get(0).getFromType().prettyPrint()))
      when effectiveMappings().get(0) == null
24
      to RagConnect.errors();
René Schöne's avatar
René Schöne committed
25

René Schöne's avatar
René Schöne committed
26
27
28
    ReadFromMqttDefinition contributes error("to-type of last mapping must be type of the Token!")
      when getToken().getJavaTypeUse().prettyPrint().equals(
          effectiveMappings().get(effectiveMappings().size() - 1))
29
      to RagConnect.errors();
René Schöne's avatar
René Schöne committed
30
31
32

    WriteToMqttDefinition contributes error("Writing target token must be an NTA token!")
      when !getToken().getNTA()
33
      to RagConnect.errors();
René Schöne's avatar
René Schöne committed
34
35
36

    WriteToMqttDefinition contributes error("Write definition already defined for " + getToken().getName())
      when isAlreadyDefined()
37
      to RagConnect.errors();
René Schöne's avatar
René Schöne committed
38
39
40

    DependencyDefinition contributes error("Dependency definition already defined for " + getSource().containingTypeDecl().getName() + " with name " + getID())
      when isAlreadyDefined()
41
      to RagConnect.errors();
René Schöne's avatar
René Schöne committed
42
43
44

    DependencyDefinition contributes error("The name of a dependency definition must not be equal to a list-node on the source")
      when isAlreadyDefinedAsList()
45
      to RagConnect.errors();
René Schöne's avatar
René Schöne committed
46
47
48

    DependencyDefinition contributes error("There must be a write update definition targeting " + getSource().parentTypeypeAndName() + " for dependency definition " + getID())
      when targetUpdateDefinition() == null
49
      to RagConnect.errors();
René Schöne's avatar
René Schöne committed
50
51
}

René Schöne's avatar
René Schöne committed
52
53
54
55
56
57
58
59
60
61
62
aspect ErrorHelpers {
  syn boolean DependencyDefinition.isAlreadyDefinedAsList() {
    for (Component comp : getSource().containingTypeDecl().getComponentList()) {
      if (comp.isListComponent() && comp.getName().equals(this.getID())) {
        return true;
      }
    }
    return false;
  }
  syn String TokenComponent.parentTypeypeAndName() = containingTypeDecl().getName() + "." + getName();
}
René Schöne's avatar
René Schöne committed
63
64
65
66
67
68
69
70
71
72
73

aspect ErrorMessage {
  public class ErrorMessage implements Comparable<ErrorMessage> {
    private final ASTNode node;
    private final String filename;
    private final int line;
    private final int col;
    private final String message;

    public ErrorMessage(ASTNode node, String message) {
      this.node = node;
René Schöne's avatar
René Schöne committed
74
      this.filename = node.containedFileName();
René Schöne's avatar
René Schöne committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
      this.line = node.getStartLine();
      this.col = node.getStartColumn();
      this.message = message;
    }

    public ASTNode getNode() {
      return node;
    }
    public int getLine() {
      return line;
    }
    public int getCol() {
      return col;
    }
    public String getMessage() {
      return message;
    }

    public String toString() {
      return filename + " Line " + line + ", column " + col + ": " + message;
    }

    @Override
    public int compareTo(ErrorMessage err) {
      int n = filename.compareTo(err.filename);
      if (n != 0) {
        return n;
      }

      n = line - err.line;
      if (n != 0) {
        return n;
      }

      n = col-err.col;
      if (n != 0) {
        return n;
      }

      return message.compareTo(err.message);
    }
  }

  protected ErrorMessage ASTNode.error(String message) {
    return new ErrorMessage(this, message);
  }
}