Serializer.jadd 29.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
aspect Serializer {

  protected static String ASTNode.jsonTypeKey = "type";
  protected static String ASTNode.jsonNodeType = "com.fasterxml.jackson.databind.JsonNode";
  protected static String ASTNode.jsonNodeTypeAccessor = ".get(\"" + jsonTypeKey + "\").asText()";
  public String Program.generateJacksonSerializer() {
    StringBuilder sb = new StringBuilder();
    generateFromJson(sb);
    generateToJson(sb);
    if (jsonPointer) {
      writeJsonPointer(sb);
    } else if (manualReferences) {
      // TODO
    } else {
      writeUID(sb);
    }

    return sb.toString();
  }

  public void Program.generateFromJson(StringBuilder sb) {
    sb.append("aspect JsonToModel {\n");

    sb.append(ind(1) + "public class DeserializationException extends Exception {\n");
    sb.append(ind(2) + "public DeserializationException(String message) {\n");
    sb.append(ind(3) + "super(message);\n");
    sb.append(ind(2) + "}\n");
    sb.append(ind(2) + "public DeserializationException(String message, Exception cause) {\n");
    sb.append(ind(3) + "super(message, cause);\n");
    sb.append(ind(2) + "}\n");
    sb.append(ind(1) + "}\n");

    sb.append(ind(1) + "public void ASTNode.serialize(com.fasterxml.jackson.core.JsonGenerator g) throws SerializationException {\n");
    sb.append(ind(2) + "serialize(g, null);\n");
    sb.append(ind(1) + "}\n");

    sb.append(ind(1) + "public void ASTNode.serialize(com.fasterxml.jackson.core.JsonGenerator g, String field) throws SerializationException {\n");
    sb.append(ind(2) + "throw new SerializationException(\"unable to serialize class \" + this.getClass().getSimpleName());\n");
    sb.append(ind(1) + "}\n");


    sb.append(ind(1) + "public void ASTNode.serialize(java.io.File file) throws SerializationException {\n");
    sb.append(ind(2) + "serialize(file, false);\n");
    sb.append(ind(1) + "}\n");

    sb.append(ind(1) + "public void ASTNode.serialize(java.io.File file, boolean humanReadable) throws SerializationException {\n");
    sb.append(ind(2) + "try {\n");
    sb.append(ind(3) + "com.fasterxml.jackson.core.JsonFactory factory = new com.fasterxml.jackson.core.JsonFactory();\n");
    sb.append(ind(3) + "com.fasterxml.jackson.core.JsonGenerator generator = factory.createGenerator(file, com.fasterxml.jackson.core.JsonEncoding.UTF8);\n");
    sb.append(ind(3) + "if (humanReadable) {\n");
    sb.append(ind(4) + "generator.setPrettyPrinter(new com.fasterxml.jackson.core.util.DefaultPrettyPrinter());\n");
    sb.append(ind(3) + "}\n");
    sb.append(ind(3) + "serialize(generator);\n");
    sb.append(ind(3) + "generator.close();\n");
    sb.append(ind(2) + "} catch (java.io.IOException e) {\n");
    sb.append(ind(3) + "throw new SerializationException(\"Unable to serialize file \" + file.getAbsolutePath(), e);\n");
    sb.append(ind(2) + "}\n");
    sb.append(ind(1) + "}\n");

    for (TypeDecl decl : getTypeDeclList()) {
      decl.deserialize(sb);
    }

    sb.append("}\n");
  }

  public void Program.generateToJson(StringBuilder sb) {
    sb.append("aspect ModelToJson {\n");

    sb.append(ind(1) + "public class SerializationException extends Exception {\n");
    sb.append(ind(2) + "public SerializationException(String message) {\n");
    sb.append(ind(3) + "super(message);\n");
    sb.append(ind(2) + "}\n");
    sb.append(ind(2) + "public SerializationException(String message, Exception cause) {\n");
    sb.append(ind(3) + "super(message, cause);\n");
    sb.append(ind(2) + "}\n");
    sb.append(ind(1) + "}\n");

    for (TypeDecl decl : getTypeDeclList()) {
      decl.serialize(sb);
    }

    sb.append("}\n");
  }

   public void TypeDecl.serialize(StringBuilder sb) {
    sb.append(ind(1) + "public void " + getID() + ".serialize(com.fasterxml.jackson.core.JsonGenerator g, String fieldName) throws SerializationException {\n");
    sb.append(ind(2) + "try {\n");
    sb.append(ind(3) + "if (fieldName == null) {\n");
    sb.append(ind(4) + "g.writeStartObject();\n");
    sb.append(ind(3) + "} else {\n");
    sb.append(ind(4) + "g.writeObjectFieldStart(fieldName);\n");
    sb.append(ind(3) + "}\n");
    if (!jsonPointer && !manualReferences) {
      sb.append(ind(3) + "g.writeStringField(\"" + jsonTypeKey + "\", \"" + getID() + "\");\n");
      sb.append(ind(3) + "if (unique$Id() == null) throw new SerializationException(\"The unique identifier of " + getID() + " is missing.\");\n");
      sb.append(ind(3) + "g.writeStringField(\"id\", unique$Id());\n");
    }
    if (componentsTransitive().size() > 0) {
      sb.append(ind(3) + "g.writeObjectFieldStart(\"children\");\n");
      for (Component child : componentsTransitive()) {
        if (child.isNullable()) {
          String componentAccessor = child.getID();
          if (child.isList()) {
            componentAccessor += "List";
          } else if (child.isOpt()) {
            componentAccessor += "Opt";
          }
          sb.append(ind(3) + "if (get" + componentAccessor + "() == null) throw new SerializationException(\"The component " + child.getID() + " of " + getID() + " is missing.\");\n");
        }
        child.serialize(sb, 3);
      }
      sb.append(ind(3) + "g.writeEndObject(); // children\n");
    }
    if (relationComponentsTransitive().size() > 0) {
      sb.append(ind(3) + "g.writeObjectFieldStart(\"relations\");\n");
      for (RelationComponent relation : relationComponentsTransitive()) {
        relation.serialize(sb, 3);
      }
      sb.append(ind(3) + "g.writeEndObject(); // relations\n");
    }
    sb.append(ind(3) + "g.writeEndObject();\n");
    sb.append(ind(2) + "} catch (java.io.IOException e) {\n");
    sb.append(ind(3) + "throw new SerializationException(\"unable to serialize " + getID() + "\", e);\n");
    sb.append(ind(2) + "}\n");
    sb.append(ind(1) + "}\n");

  }

  public abstract void Component.serialize(StringBuilder sb, int indent);

  public void NormalComponent.serialize(StringBuilder sb, int indent) {
    sb.append(ind(indent) + "get" + getID() + "().serialize(g, \"" + getID() + "\");\n");
  }

  public void OptComponent.serialize(StringBuilder sb, int indent) {
    sb.append(ind(indent) + "if (has" + getID() + "()) {\n");
    sb.append(ind(indent + 1) + "get" + getID() + "().serialize(g, \"" + getID() + "\");\n");
    sb.append(ind(indent) + "}\n");
  }

  public void TokenComponent.serialize(StringBuilder sb, int indent) {

    String type = getTypeUse().getID();

    switch (type) {
      case "float":
      case "Float":
      case "double":
      case "Double":
      case "int":
      case "Integer":
      case "short":
      case "Short":
      case "long":
      case "Long":
      case "byte":
      case "Byte":
        sb.append(ind(indent) + "g.writeNumberField(\"" + getID() + "\", get" + getID() + "());\n");
        break;
      case "boolean":
      case "Boolean":
        sb.append(ind(indent) + "g.writeBooleanField(\"" + getID() + "\", get" + getID() + "());\n");
        break;
      case "char":
      case "Character":
        sb.append(ind(indent) + "g.writeStringField(\"" + getID() + "\", Character.toString(get"+ getID() + "()));\n");
        break;
      case "String":
        sb.append(ind(indent) + "g.writeStringField(\"" + getID() + "\", get" + getID() + "());\n");
        break;
172
      case "java.time.Instant":
173
174
        sb.append(ind(indent) + "g.writeStringField(\"" + getID() + "\", get" + getID() + "().toString());\n");
        break;
175
      case "java.time.Period":
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
        sb.append(ind(indent) + "g.writeStringField(\"" + getID() + "\", get" + getID() + "().toString());\n");
        break;
      default:
        // assume that the type is an enum. there is no way of checking this here
        sb.append(ind(indent) + "g.writeStringField(\"" + getID() + "\", get" + getID() + "().name());\n");
        // sb.append("throw new DeserializationException(\"Unable to deserialize child node of type \"" + getTypeUse() + "\"\")")
    }
  }

  public void NTAComponent.serialize(StringBuilder sb, int indent) {
    // do not serialize NTA
  }

  public void NTAListComponent.serialize(StringBuilder sb, int indent) {
    // do not serialize NTA
  }

  public void NTAOptComponent.serialize(StringBuilder sb, int indent) {
    // do not serialize NTA
  }

  public void ListComponent.serialize(StringBuilder sb, int indent) {
    sb.append(ind(indent) + "if (getNum" + getID() + "() > 0) {\n");
    sb.append(ind(indent + 1) + "g.writeArrayFieldStart(\"" + getID() + "\");\n");
    sb.append(ind(indent + 1) + "for (" + getTypeUse().decl().getID() + " child : get" + getID() + "List()) {\n");
    sb.append(ind(indent + 2) + "child.serialize(g);\n");
    sb.append(ind(indent + 1) + "}\n");
    sb.append(ind(indent + 1) + "g.writeEndArray();\n");
    sb.append(ind(indent) + "}\n");
  }

  public void OneRelationComponent.serialize(StringBuilder sb, int indent) {
    if (useJastAddNames){
      if (jsonPointer) {
        sb.append(ind(indent) + "g.writeStringField(\"" + getID() + "\", get" + getID() + "().jsonPointer());\n");
      } else if (manualReferences) {
212
        sb.append(ind(indent) + "g.writeStringField(\"" + getID() + "\", " + createRefMethod + "To" + getID() + "(" + getID() + "()));\n");
213
214
215
216
217
218
219
      } else {
        sb.append(ind(indent) + "g.writeStringField(\"" + getID() + "\", get" + getID() + "().unique$Id());\n");
      }
    } else {
      if (jsonPointer) {
        sb.append(ind(indent) + "g.writeStringField(\"" + getID() + "\", " + getID() + "().jsonPointer());\n");
      } else if (manualReferences) {
220
        sb.append(ind(indent) + "g.writeStringField(\"" + getID() + "\", " + createRefMethod + "To" + getID() + "(" + getID() + "()));\n");
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
      } else {
        sb.append(ind(indent) + "g.writeStringField(\"" + getID() + "\", " + getID() + "().unique$Id());\n");
      }
    }
  }

  public void OptionalRelationComponent.serialize(StringBuilder sb, int indent) {
    sb.append(ind(indent) + "if (has" + nameCapitalized() + "()) {\n");
    if (useJastAddNames){
      if (jsonPointer) {
        sb.append(ind(indent + 1) + "g.writeStringField(\"" + getID() + "\", get" + getID() + "().jsonPointer());\n");
      } else if (manualReferences) {
        sb.append(ind(indent + 1) + "g.writeStringField(\"" + getID() + "\", get" + getID() + "().createReference());\n");
      } else {
        sb.append(ind(indent + 1) + "g.writeStringField(\"" + getID() + "\", get" + getID() + "().unique$Id());\n");
      }
    } else {
      if (jsonPointer) {
        sb.append(ind(indent + 1) + "g.writeStringField(\"" + getID() + "\", " + getID() + "().jsonPointer());\n");
      } else if (manualReferences) {
241
        sb.append(ind(indent + 1) + "g.writeStringField(\"" + getID() + "\", " + "" + createRefMethod + "To" + getID() + "(" + getID()  + "()));\n");
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
      } else {
        sb.append(ind(indent + 1) + "g.writeStringField(\"" + getID() + "\", " + getID() + "().unique$Id());\n");
      }
    }
    sb.append(ind(indent) + "}\n");
  }

  public void ManyRelationComponent.serialize(StringBuilder sb, int indent) {
    sb.append(ind(indent) + "g.writeArrayFieldStart(\"" + getID() + "\");\n");
    if (useJastAddNames) {
      sb.append(ind(indent) + "for (" + ofTypeDecl().getID() + " child : get" + getID() + "List()) {\n");
    } else {
      sb.append(ind(indent) + "for (" + ofTypeDecl().getID() + " child : " + getID() + "()) {\n");
    }
    if (jsonPointer) {
      sb.append(ind(indent + 1) + "g.writeString(child.jsonPointer());\n");
    }  else if (manualReferences) {
259
      sb.append(ind(indent + 1) + "g.writeString(" + createRefMethod + "To" + getID() + "(child));\n");
260
261
262
263
264
265
266
267
268
269
270
271
    }else {
      sb.append(ind(indent + 1) + "g.writeString(child.unique$Id());\n");
    }

    sb.append(ind(indent) + "}\n");
    sb.append(ind(indent) + "g.writeEndArray();\n");
  }

  public void TypeDecl.deserialize(StringBuilder sb) {

    sb.append(ind(1) + "public static " + getID() + " " + getID() + ".deserialize(java.io.File file) throws DeserializationException {\n");

272
273
274
275
276
277
278
279
280
281
282
    if (instantiable()) {

      sb.append(ind(2) + "try {\n");
      sb.append(ind(3) + "com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper();\n");
      sb.append(ind(3) + "com.fasterxml.jackson.core.JsonFactory factory = mapper.getFactory();\n");
      sb.append(ind(3) + "com.fasterxml.jackson.core.JsonParser parser = factory.createParser(file);\n");
      sb.append(ind(3) + getID() + " result = deserialize((com.fasterxml.jackson.databind.JsonNode)mapper.readTree(parser));\n");
      sb.append(ind(3) + "parser.close();\n");
      sb.append(ind(3) + "return result;\n");
      sb.append(ind(2) + "} catch (java.io.IOException e) {\n");
      sb.append(ind(3) + "throw new DeserializationException(\"unable to deserialize \" + file.getAbsolutePath(), e);\n");
283
      sb.append(ind(2) + "}\n");
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
      sb.append(ind(1) + "}\n");

      sb.append(ind(1) + "public static " + getID() + " " + getID() + ".deserialize(" + jsonNodeType + " node) throws DeserializationException {\n");
      sb.append(ind(2) + getID() + " element;\n");
      if (getAbstract()) {
        // switch case between all implementations of the abstract class
        sb.append(ind(2) + "switch (node" + jsonNodeTypeAccessor + ") {\n");
        for (TypeDecl subType : subTypeDecls()) {
          sb.append(ind(3) + "case \"" + subType.getID() + "\":\n");
          sb.append(ind(4) + "element = " + subType.getID() + ".deserialize(node);\n");
          sb.append(ind(4) + "break;\n");
        }
        sb.append(ind(3) + "default:\n");
        sb.append(ind(4) + "throw new DeserializationException(\"Unable to deserialize child of unexpected type \" + node" + jsonNodeTypeAccessor + " + \"(" + getID() + " expected)\");\n");
        sb.append(ind(2) + "}\n");
      } else {
        sb.append(ind(2) + "element = new " + getID() + "();\n");
      }
302

303
304
305
306
307
308
      if (!jsonPointer && !manualReferences) {
        // deserialize id
        sb.append(ind(2) + "if (node.has(\"id\")) {\n");
        sb.append(ind(3) + "element.unique$Id = node.get(\"id\").asText();\n");
        sb.append(ind(2) + "}\n");
      }
309

310
311
312
313
314
315
316
317
318
319
      // deserialize containment children
      if (componentsTransitive().size() > 0) {
        sb.append(ind(2) + "if (node.has(\"children\")) {\n");
        sb.append(ind(3) + jsonNodeType + " children = node.get(\"children\");\n");
        for (Component component : componentsTransitive()) {
          sb.append(ind(3) + "if (children.has(\"" + component.getID() + "\")) {\n");
          component.deserialize(sb, 4);
          sb.append(ind(3) + "}\n");
        }
        sb.append(ind(2) + "}\n");
320
      }
321
322
323
324
325
326
327
328
329
330
331
      // deserialize non-containment children
      Set<RelationComponent> relationComponents = relationComponents();
      if (relationComponents.size() > 0) {
        sb.append(ind(2) + "if (node.has(\"relations\")) {\n");
        sb.append(ind(3) + jsonNodeType + " relations = node.get(\"relations\");\n");
        for (RelationComponent component : relationComponents) {
          sb.append(ind(3) + "if (relations.has(\"" + component.getID() + "\")) {\n");
          component.deserialize(sb, 4);
          sb.append(ind(3) + "}\n");
        }
        sb.append(ind(2) + "}\n");
332
333
      }

334
335
336
337
      sb.append(ind(2) + "return element;\n");
    } else {
      sb.append(ind(2) + "throw new DeserializationException(\"Unable to deserialize type \\\"" + getID() + "\\\" because it is not instantiable.\");\n");
    }
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
    sb.append(ind(1) + "}\n");

  }

  public abstract void Component.deserialize(StringBuilder sb, int indent);

  public void NormalComponent.deserialize(StringBuilder sb, int indent) {
    sb.append(ind(indent) + "element.set" + getID() + "(" + getTypeUse().decl().getID() + ".deserialize(children.get(\"" + getID() + "\")));\n");
    sb.append(ind(indent - 1) + "} else {\n");
    sb.append(ind(indent) + "throw new DeserializationException(\"deserializer of missing mandatory child " + getID() + "\");\n");
  }

  public void OptComponent.deserialize(StringBuilder sb, int indent) {
    sb.append(ind(indent + 1) + "element.set" + getID() + "(" + getTypeUse().decl().getID() + ".deserialize(children.get(\"" + getID() + "\")));\n");
  }

  public void TokenComponent.deserialize(StringBuilder sb, int indent) {

    String type = getTypeUse().getID();

    switch (type) {
      case "float":
      case "Float":
        sb.append(ind(indent) + "element.set" + getID() + "(Float.valueOf(children.get(\"" + getID() + "\").asText()));\n");
        break;
      case "double":
      case "Double":
        sb.append(ind(indent) + "element.set" + getID() + "(children.get(\"" + getID() + "\").asDouble());\n");
        break;
      case "int":
      case "Integer":
        sb.append(ind(indent) + "element.set" + getID() + "(children.get(\"" + getID() + "\").asInt());\n");
        break;
      case "short":
      case "Short":
        sb.append(ind(indent) + "element.set" + getID() + "(Short.valueOf(children.get(\"" + getID() + "\").asText()));\n");
        break;
      case "long":
      case "Long":
        sb.append(ind(indent) + "element.set" + getID() + "(children.get(\"" + getID() + "\").asLong());\n");
        break;
      case "byte":
      case "Byte":
        sb.append(ind(indent) + "element.set" + getID() + "(Byte.valueOf(children.get(\"" + getID() + "\").asText()));\n");
        break;
      case "boolean":
      case "Boolean":
        sb.append(ind(indent) + "element.set" + getID() + "(children.get(\"" + getID() + "\").asBoolean());\n");
        break;
      case "char":
      case "Character":
        sb.append(ind(indent) + "String chars = children.get(\"" + getID() + "\").asText();\n");
        sb.append(ind(indent) + "if (chars.length() == 1) {\n");
        sb.append(ind(indent + 2) + "element.set" + getID() + "(chars.charAt(0));\n");
        sb.append(ind(indent) + "} else {\n");
        sb.append(ind(indent + 2) + "throw new DeserializationException(\"unable to deserialize char '\" + chars + \"'\");\n");
        sb.append(ind(indent) + "}\n");
        break;
      case "String":
        sb.append(ind(indent) + "element.set" + getID() + "(children.get(\"" + getID() + "\").asText());\n");
        break;
399
400
      case "java.time.Instant":
        sb.append(ind(indent) + "element.set" + getID() + "(java.time.Instant.parse(children.get(\"" + getID() + "\").asText()));\n");
401
        break;
402
403
      case "java.time.Period":
        sb.append(ind(indent) + "element.set" + getID() + "(java.time.Period.parse(children.get(\"" + getID() + "\").asText()));\n");
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
        break;
      default:
        // assume that the type is an enum. there is no way of checking this here
        sb.append(ind(indent) + "element.set" + getID() + "(Enum.valueOf(" + type + ".class, children.get(\"" + getID() + "\").asText()));\n");

        // sb.append("throw new DeserializationException(\"Unable to deserialize child node of type \"" + getTypeUse() + "\"\")")
    }
  }

  public void Component.writeJsonPointer(StringBuilder sb) {
    // do nothing for other components
  }

  public void NormalComponent.writeJsonPointer(StringBuilder sb) {
    sb.append(ind(1) + "eq " + enclosingTypeDecl().getID() + (useJastAddNames?".get":".") + getID() + "().jsonPointerInh() {\n");
    sb.append(ind(2) + "return this.jsonPointer() + \"/children/" + getID() + "\";\n");
    sb.append(ind(1) + "}\n");
  }

  public void ListComponent.writeJsonPointer(StringBuilder sb) {
    sb.append(ind(1) + "eq " + enclosingTypeDecl().getID() + (useJastAddNames?".get":".") + getID() + "(int index).jsonPointerInh() {\n");
    sb.append(ind(2) + "return this.jsonPointer() + \"/children/" + getID() + "/\" + index;\n");
    sb.append(ind(1) + "}\n");
  }

  public void OptComponent.writeJsonPointer(StringBuilder sb) {
    sb.append(ind(1) + "eq " + enclosingTypeDecl().getID() + (useJastAddNames?".get":".") + getID() + "().jsonPointerInh() {\n");
    sb.append(ind(2) + "return this.jsonPointer() + \"/children/" + getID() + "\";\n");
    sb.append(ind(1) + "}\n");
  }

  public void NTAComponent.writeJsonPointer(StringBuilder sb) {
    sb.append(ind(1) + "eq " + enclosingTypeDecl().getID() + (useJastAddNames?".get":".") + getID() + "().jsonPointerInh() {\n");
    sb.append(ind(2) + "return this.jsonPointer() + \"/children/" + getID() + "\";\n");
    sb.append(ind(1) + "}\n");
  }

  public void NTAListComponent.writeJsonPointer(StringBuilder sb) {
    sb.append(ind(1) + "eq " + enclosingTypeDecl().getID() + (useJastAddNames?".get":".") + getID() + "(int index).jsonPointerInh() {\n");
    sb.append(ind(2) + "return this.jsonPointer() + \"/children/" + getID() + "/\" + index;\n");
    sb.append(ind(1) + "}\n");
  }

  public void NTAOptComponent.writeJsonPointer(StringBuilder sb) {
    sb.append(ind(1) + "eq " + enclosingTypeDecl().getID() + (useJastAddNames?".get":".") + getID() + "().jsonPointerInh() {\n");
    sb.append(ind(2) + "return this.jsonPointer() + \"/children/" + getID() + "\";\n");
    sb.append(ind(1) + "}\n");
  }

  public void Component.resolveJsonPointer(StringBuilder sb) {
    // do nothing for other components
  }

  public void NormalComponent.resolveJsonPointer(StringBuilder sb) {
    sb.append(ind(4) + "case \"" + getID() + "\":\n");
    sb.append(ind(5) + "return get" + getID() + "().resolveJsonPointer(pointer, index + 2);\n");
  }

  public void ListComponent.resolveJsonPointer(StringBuilder sb) {
    sb.append(ind(4) + "case \"" + getID() + "\":\n");
    sb.append(ind(5) + "return get" + getID() + "(Integer.valueOf(pointer[index + 2])).resolveJsonPointer(pointer, index + 3);\n");
  }

  public void OptComponent.resolveJsonPointer(StringBuilder sb) {
    sb.append(ind(4) + "case \"" + getID() + "\":\n");
    sb.append(ind(5) + "return get" + getID() + "().resolveJsonPointer(pointer, index + 2);\n");
  }

  public void NTAComponent.resolveJsonPointer(StringBuilder sb) {
    sb.append(ind(4) + "case \"" + getID() + "\":\n");
    sb.append(ind(5) + "return get" + getID() + "().resolveJsonPointer(pointer, index + 2);\n");
  }

  public void NTAListComponent.resolveJsonPointer(StringBuilder sb) {
    sb.append(ind(4) + "case \"" + getID() + "\":\n");
    sb.append(ind(5) + "return get" + getID() + "(Integer.valueOf(pointer[index + 2])).resolveJsonPointer(pointer, index + 3);\n");
  }

  public void NTAOptComponent.resolveJsonPointer(StringBuilder sb) {
    sb.append(ind(4) + "case \"" + getID() + "\":\n");
    sb.append(ind(5) + "return get" + getID() + "().resolveJsonPointer(pointer, index + 2);\n");
  }

  public void TypeDecl.resolveJsonPointer(StringBuilder sb) {
    sb.append(ind(1) + "eq " + getID() + ".resolveJsonPointer(String[] pointer, int index) {\n");
    sb.append(ind(2) + "if (pointer.length == 0 || pointer.length == index) {\n");
    sb.append(ind(3) + "return this;\n");
    sb.append(ind(2) + "} else if (pointer.length == index + 1) {\n");
    sb.append(ind(3) + "throw new RuntimeException(\"there is only one child called \" + pointer[index]);\n");
    sb.append(ind(2) + "} else {\n");
    sb.append(ind(3) + "switch (pointer[index + 1]) {\n");

    for (Component c: getComponentList()) {
      c.resolveJsonPointer(sb);
    }

    sb.append(ind(4) + "default:\n");
    sb.append(ind(5) + "return super.resolveJsonPointer(pointer, index);\n");
    sb.append(ind(3) + "}\n");
    sb.append(ind(2) + "}\n");
    sb.append(ind(1) + "}\n");
  }

  public void Program.writeJsonPointer(StringBuilder sb) {
    sb.append("aspect JsonPointer {\n");

    sb.append(ind(1) + "syn String ASTNode.jsonPointer() {\n");
    sb.append(ind(2) + "if (getParent() == null) {\n");
    sb.append(ind(3) + "return \"\";\n");
    sb.append(ind(2) + "} else {\n");
    sb.append(ind(3) + "return jsonPointerInh();\n");
    sb.append(ind(2) + "}\n");
    sb.append(ind(1) + "}\n");
    sb.append(ind(1) + "inh String ASTNode.jsonPointerInh();\n");

    // define json pointer for each component
    for (TypeDecl td: getTypeDeclList()) {
      for (Component c: td.getComponentList()) {
        c.writeJsonPointer(sb);
      }
    }

    sb.append(ind(1) + "syn ASTNode ASTNode.resolveJsonPointer(String pointer) = root().resolveJsonPointer(pointer.split(\"/\"), 1);\n");
    sb.append(ind(1) + "ASTNode ASTNode.root() {\n");
    sb.append(ind(2) + "if (getParent() == null) return this;\n");
    sb.append(ind(2) + "else return getParent().root();\n");
    sb.append(ind(1) + "}\n");
    sb.append(ind(1) + "syn ASTNode ASTNode.resolveJsonPointer(String[] pointer, int index) {\n");
    sb.append(ind(2) + "if (index < pointer.length) {\n");
    sb.append(ind(3) + "throw new RuntimeException(\"found wrong child  \" + pointer[index + 1] + \" in class \" + this.getClass().getSimpleName());\n");
    sb.append(ind(2) + "} else {\n");
    sb.append(ind(3) + "throw new RuntimeException(\"Cannot resolve JSON pointer for class \" + this.getClass().getSimpleName());\n");
    sb.append(ind(2) + "}\n");
    sb.append(ind(1) + "}\n");

    // resolve JSON pointers in each nonterminal
    for (TypeDecl td: getTypeDeclList()) {
      td.resolveJsonPointer(sb);
    }

    sb.append("}\n");
  }

  public void Program.writeUID(StringBuilder sb) {
    sb.append("aspect UID {\n");
    sb.append(ind(1) + "class UIDProvider {\n");
    sb.append(ind(2) + "private static long nextUID = 0;\n");
    sb.append(ind(2) + "public static String getUID() {\n");
    sb.append(ind(3) + "return String.valueOf(nextUID++);\n");
    sb.append(ind(2) + "}\n");
    sb.append(ind(1) + "}\n");
    sb.append("\n");
    sb.append(ind(1) + "protected String ASTNode.unique$Id = null;\n");
    sb.append("\n");
    sb.append(ind(1) + "protected String ASTNode.unique$Id() {\n");
    sb.append(ind(2) + "String customUID = customID();\n");
    sb.append(ind(2) + "if (customUID == null) {\n");
    sb.append(ind(3) + "if (unique$Id == null) {\n");
    sb.append(ind(4) + "unique$Id = UIDProvider.getUID();\n");
    sb.append(ind(3) + "}\n");
    sb.append(ind(3) + "return unique$Id;\n");
    sb.append(ind(2) + "} else {\n");
    sb.append(ind(3) + "return customUID;\n");
    sb.append(ind(2) + "}\n");
    sb.append(ind(1) + "}\n");
    sb.append("\n");
    sb.append(ind(1) + "protected String ASTNode.customID() {\n");
    sb.append(ind(1) + "  return null;\n");
    sb.append(ind(1) + "}\n");
    sb.append("\n");
574
    sb.append(ind(1) + "ASTNode ASTNode." + globallyResolvePrefix + "ASTNodeByUID(String uid) {\n");
575
576
577
    sb.append(ind(2) + "if (getParent() == null) {\n");
    sb.append(ind(3) + "return uid$Map().get(uid).get();\n");
    sb.append(ind(2) + "} else {\n");
578
    sb.append(ind(3) + "return getParent()." + globallyResolvePrefix + "ASTNodeByUID(uid);\n");
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
    sb.append(ind(2) + "}\n");
    sb.append(ind(1) + "}\n");
    sb.append("\n");
    sb.append(ind(1) + "java.util.Map<String, java.lang.ref.WeakReference<ASTNode>> ASTNode.uid$Map;\n");
    sb.append("\n");
    sb.append(ind(1) + "java.util.Map<String, java.lang.ref.WeakReference<ASTNode>> ASTNode.uid$Map() {\n");
    sb.append(ind(2) + "if (uid$Map == null) {\n");
    sb.append(ind(3) + "uid$Map = uid$Map(new java.util.HashMap());\n");
    sb.append(ind(2) + "}\n");
    sb.append(ind(2) + "return uid$Map;\n");
    sb.append(ind(1) + "}\n");
    sb.append(ind(1) + "protected java.util.Map<String, java.lang.ref.WeakReference<ASTNode>> ASTNode.uid$Map(java.util.Map<String, java.lang.ref.WeakReference<ASTNode>> map) {\n");
    sb.append(ind(2) + "if (!(this instanceof " + jastAddListType + " || this instanceof Opt)) {\n");
    sb.append(ind(3) + "if (map.keySet().contains(unique$Id())) {\n");
    sb.append(ind(4) + "throw new RuntimeException(new SerializationException(\"UID \" + this.unique$Id() + \" is assigned to both \" + this.getClass().getSimpleName() + \":\" + this.hashCode() + \" and \" + map.get(unique$Id()).getClass().getSimpleName() + \":\" + map.get(unique$Id()).hashCode()));\n");
    sb.append(ind(3) + "} else {\n");
    sb.append(ind(4) + "map.put(this.unique$Id, new java.lang.ref.WeakReference(this));\n");
    sb.append(ind(3) + "}\n");
    sb.append(ind(2) + "}\n");
    sb.append(ind(2) + "for (ASTNode child : astChildren()) {\n");
    sb.append(ind(3) + "child.uid$Map(map);\n");
    sb.append(ind(2) + "}\n");
    sb.append(ind(2) + "return map;\n");
    sb.append(ind(1) + "}\n");
    sb.append("}\n");
  }

  public void NTAComponent.deserialize(StringBuilder sb, int indent) {
    // do not serialize NTA
  }

  public void NTAListComponent.deserialize(StringBuilder sb, int indent) {
    // do not serialize NTA
  }

  public void NTAOptComponent.deserialize(StringBuilder sb, int indent) {
    // do not serialize NTA
  }

  public void ListComponent.deserialize(StringBuilder sb, int indent) {
    sb.append(ind(indent) + "for (" + jsonNodeType + " child : children.get(\"" + getID() + "\")) {\n");
    sb.append(ind(indent + 1) + "element.add" + getID() + "(" + getTypeUse().decl().getID() + ".deserialize(child));\n");
    sb.append(ind(indent) + "}\n");
  }

  public void OneRelationComponent.deserialize(StringBuilder sb, int indent) {
625
    sb.append(ind(indent) + "element.set" + nameCapitalized() + "(" + ofTypeDecl().getID() + "." + createRefMethod + "Direction(relations.get(\"" + getID() + "\").asText()));\n");
626
627
628
629
630
    sb.append(ind(indent - 1) + "} else {\n");
    sb.append(ind(indent) + "throw new DeserializationException(\"deserializer of missing mandatory relation child " + getID() + "\");\n");
  }

  public void OptionalRelationComponent.deserialize(StringBuilder sb, int indent) {
631
    sb.append(ind(indent) + "element.set" + nameCapitalized() + "(" + ofTypeDecl().getID() + "." + createRefMethod + "Direction(relations.get(\"" + getID() + "\").asText()));\n");
632
633
634
635
  }

  public void ManyRelationComponent.deserialize(StringBuilder sb, int indent) {
    sb.append(ind(indent) + "for (" + jsonNodeType + " child : relations.get(\"" + getID() + "\")) {\n");
636
    sb.append(ind(indent + 1) + "element.add" + (useJastAddNames?"":"To") + nameCapitalized() + "(" + ofTypeDecl().getID() + "." + createRefMethod + "Direction(child.asText()));\n");
637
638
639
    sb.append(ind(indent) + "}\n");
  }
}