Commit 5808ffc1 authored by Johannes Mey's avatar Johannes Mey
Browse files

improve uid handling.

parent 001af931
......@@ -908,8 +908,6 @@ aspect NameResolutionHelper {
generateDirectedContextDependentNameResolution(sb);
}
public void ManyRelationComponent.generateContextDependentNameResolution(StringBuilder sb) {
System.out.println("serializer: " + serializer);
System.out.println("ResolverHelper: " + resolverHelper);
if (serializer && !resolverHelper) {
sb.append(ind(1) + ofTypeDecl() + " " + toTypeDecl() + ".resolve" + nameCapitalized() + "ByToken(String id, int position) {\n");
......@@ -1063,8 +1061,8 @@ aspect Serializer {
sb.append(ind(4) + "g.writeObjectFieldStart(fieldName);\n");
sb.append(ind(3) + "}\n");
sb.append(ind(3) + "g.writeStringField(\"" + jsonTypeKey + "\", \"" + getID() + "\");\n");
sb.append(ind(3) + "if (__uid() == null) throw new SerializationException(\"The unique identifier of " + getID() + " is missing.\");\n");
sb.append(ind(3) + "g.writeStringField(\"id\", __uid());\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()) {
......@@ -1167,18 +1165,18 @@ aspect Serializer {
public void OneRelationComponent.serialize(StringBuilder sb, int indent) {
if (useJastAddNames){
sb.append(ind(indent) + "g.writeStringField(\""+getID()+"\", get" + getID() + "().__uid());\n");
sb.append(ind(indent) + "g.writeStringField(\""+getID()+"\", get" + getID() + "().unique$Id());\n");
} else {
sb.append(ind(indent) + "g.writeStringField(\""+getID()+"\", " + getID() + "().__uid());\n");
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){
sb.append(ind(indent + 1) + "g.writeStringField(\"" + getID() + "\", get" + getID() + "().__uid());\n");
sb.append(ind(indent + 1) + "g.writeStringField(\"" + getID() + "\", get" + getID() + "().unique$Id());\n");
} else {
sb.append(ind(indent + 1) + "g.writeStringField(\"" + getID() + "\", " + getID() + "().__uid());\n");
sb.append(ind(indent + 1) + "g.writeStringField(\"" + getID() + "\", " + getID() + "().unique$Id());\n");
}
sb.append(ind(indent) + "}\n");
}
......@@ -1190,7 +1188,7 @@ aspect Serializer {
} else {
sb.append(ind(indent) + "for (" + ofTypeDecl().getID() + " child : " + getID() + "()) {\n");
}
sb.append(ind(indent + 1) + "g.writeString(child.__uid());\n");
sb.append(ind(indent + 1) + "g.writeString(child.unique$Id());\n");
sb.append(ind(indent) + "}\n");
sb.append(ind(indent) + "g.writeEndArray();\n");
}
......@@ -1227,7 +1225,7 @@ aspect Serializer {
// deserialize id
sb.append(ind(2) + "if (node.has(\"id\")) {\n");
sb.append(ind(3) + "element.__uid = node.get(\"id\").asText();\n");
sb.append(ind(3) + "element.unique$Id = node.get(\"id\").asText();\n");
sb.append(ind(2) + "}\n");
// deserialize containment children
......@@ -1339,55 +1337,53 @@ aspect Serializer {
sb.append(ind(2) + "}\n");
sb.append(ind(1) + "}\n");
sb.append("\n");
sb.append(ind(1) + "protected String ASTNode.__uid = null;\n");
sb.append(ind(1) + "protected String ASTNode.unique$Id = null;\n");
sb.append("\n");
sb.append(ind(1) + "protected String ASTNode.__uid() {\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 (__uid == null) {\n");
sb.append(ind(4) + "__uid = UIDProvider.getUID();\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 __uid;\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(2) + "return null;\n");
sb.append(ind(1) + " return null;\n");
sb.append(ind(1) + "}\n");
if (serializer) {
sb.append(ind(1) + "ASTNode ASTNode.globallyResolveASTNodeByUID(String uid) {\n");
sb.append(ind(2) + "if (getParent() == null) {\n");
sb.append(ind(3) + "return __uidMap().get(uid);\n");
sb.append(ind(2) + "} else {\n");
sb.append(ind(3) + "return getParent().globallyResolveASTNodeByUID(uid);\n");
sb.append(ind(2) + "}\n");
sb.append(ind(1) + "}\n");
}
sb.append(ind(1) + "syn java.util.Map<String, ASTNode> ASTNode.__uidMap() {\n");
sb.append(ind(2) + "return(__uidMap(new java.util.HashMap()));\n");
sb.append("\n");
sb.append(ind(1) + "ASTNode ASTNode.globallyResolveASTNodeByUID(String uid) {\n");
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");
sb.append(ind(3) + "return getParent().globallyResolveASTNodeByUID(uid);\n");
sb.append(ind(2) + "}\n");
sb.append(ind(1) + "}\n");
sb.append(ind(1) + "protected java.util.Map<String, ASTNode> ASTNode.__uidMap(java.util.Map<String, ASTNode> map) {\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(__uid())) {\n");
sb.append(ind(4) + "throw new RuntimeException(new SerializationException(\"UID \" + this.__uid() + \" is assigned to both " +
"\" + this.getClass().getSimpleName() + \":\" + this.hashCode() + \" and " +
"\" + map.get(__uid()).getClass().getSimpleName() + \":\" + map.get(__uid()).hashCode()));\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.__uid, this);\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.__uidMap(map);\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");
}
......
......@@ -69,7 +69,6 @@ public class Compiler {
}
if (optionUseJastaddNames.isSet()) {
System.out.println("Using JastAdd names");
ASTNode.useJastAddNames = true;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment