Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(98)

Unified Diff: pkg/analysis_server/tool/spec/codegen_java_types.dart

Issue 485213004: Generate a toJson method onto our generated types, int is now represented as the wrapped Integer in… (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: pkg/analysis_server/tool/spec/codegen_java_types.dart
diff --git a/pkg/analysis_server/tool/spec/codegen_java_types.dart b/pkg/analysis_server/tool/spec/codegen_java_types.dart
index 1ae8e5b3d767115e4d9f788597de703385949152..9a2e803ede70401a759365dfdb23a0da91ee5df6 100644
--- a/pkg/analysis_server/tool/spec/codegen_java_types.dart
+++ b/pkg/analysis_server/tool/spec/codegen_java_types.dart
@@ -75,6 +75,9 @@ class CodegenJavaType extends CodegenJavaVisitor {
writeln('import java.util.List;');
writeln('import java.util.Map;');
writeln('import com.google.dart.server.utilities.general.ObjectUtilities;');
+ writeln('import com.google.gson.JsonArray;');
+ writeln('import com.google.gson.JsonObject;');
+ writeln('import com.google.gson.JsonPrimitive;');
writeln('import org.apache.commons.lang3.StringUtils;');
writeln();
javadocComment(toHtmlVisitor.collectHtml(() {
@@ -116,8 +119,10 @@ class CodegenJavaType extends CodegenJavaVisitor {
//
TypeObject typeObject = typeDef.type as TypeObject;
List<TypeObjectField> fields = typeObject.fields;
- // TODO (jwren) we need to possibly remove fields such as "type" in
- // these objects: AddContentOverlay | ChangeContentOverlay | RemoveContentOverlay
+ // TODO (jwren) we need to possibly remove fields such as "type" in these
+
Paul Berry 2014/08/19 20:51:12 Unexpected whitespace
jwren 2014/08/20 20:51:21 Done.
jwren 2014/08/20 20:51:21 Done.
+
+ // objects: AddContentOverlay | ChangeContentOverlay | RemoveContentOverlay
for (TypeObjectField field in fields) {
privateField(javaName(field.name), () {
javadocComment(toHtmlVisitor.collectHtml(() {
@@ -166,7 +171,44 @@ class CodegenJavaType extends CodegenJavaVisitor {
}
//
- // equals method
+ // toJson() method, example:
+// public JsonObject toJson() {
+// JsonObject jsonObject = new JsonObject();
+// jsonObject.addProperty("x", x);
+// jsonObject.addProperty("y", y);
+// return jsonObject;
+// }
+ publicMethod('toJson', () {
+ writeln('public JsonObject toJson() {');
+ indent(() {
+ writeln('// Create a JsonObject');
+ writeln('JsonObject jsonObject = new JsonObject();');
+ for (TypeObjectField field in fields) {
+ writeln();
+ writeln('// ${javaName(field.name)}');
+ if (!isObject(field.type)) {
+ if (field.optional) {
+ writeln('if (${javaName(field.name)} != null) {');
+ indent(() {
+ _writeOutJsonObjectAddStatement(field);
+ });
+ writeln('}');
+ } else {
+ _writeOutJsonObjectAddStatement(field);
+ }
+ } else {
+ writeln('// the type Object cannot be represented in JsonObject');
+ }
+ }
+ writeln();
+ writeln('// Return the JsonObject');
+ writeln('return jsonObject;');
+ });
+ writeln('}');
+ });
+
+ //
+ // equals() method
//
publicMethod('equals', () {
writeln('@Override');
@@ -228,8 +270,7 @@ class CodegenJavaType extends CodegenJavaVisitor {
if (className == 'Element') {
_extraMethodsOnElement.forEach((String methodName, String fieldName) {
publicMethod(methodName, () {
- writeln(
- 'public boolean ${methodName}() {');
+ writeln('public boolean ${methodName}() {');
writeln(' return (flags & ${fieldName}) != 0;');
writeln('}');
});
@@ -283,6 +324,29 @@ class CodegenJavaType extends CodegenJavaVisitor {
}
}
+ void _writeOutJsonObjectAddStatement(TypeObjectField field) {
+ String name = javaName(field.name);
+ if (isDeclaredInSpec(field.type)) {
+ writeln('jsonObject.add("${name}", ${name}.toJson());');
+ } else if (field.type is TypeList) {
+ TypeDecl listItemType = (field.type as TypeList).itemType;
+ String jsonArrayName = 'jsonArray${capitalize(name)}';
+ writeln('JsonArray ${jsonArrayName} = new JsonArray();');
+ writeln('for(${javaType(listItemType)} elt : ${name}) {');
+ indent(() {
+ if (isDeclaredInSpec(listItemType)) {
+ writeln('${jsonArrayName}.add(elt.toJson());');
+ } else {
+ writeln('${jsonArrayName}.add(new JsonPrimitive(elt));');
+ }
+ });
+ writeln('}');
+ writeln('jsonObject.add("${name}", ${jsonArrayName});');
+ } else {
+ writeln('jsonObject.addProperty("${name}", ${name});');
+ }
+ }
+
/**
* Get the name of the consumer class for responses to this request.
*/

Powered by Google App Engine
This is Rietveld 408576698