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

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

Issue 462253005: Initial cut at having the enum objects from the spec generated as classes with public static string… (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: rebase with bleeding_edge 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
« no previous file with comments | « pkg/analysis_server/tool/spec/codegen_java.dart ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 c6a459f31247810c14328916883a7b263a2f2896..07d2dd5178f4c82d40cff6f58d973c44a63532f8 100644
--- a/pkg/analysis_server/tool/spec/codegen_java_types.dart
+++ b/pkg/analysis_server/tool/spec/codegen_java_types.dart
@@ -34,134 +34,161 @@ class CodegenJavaType extends CodegenJavaVisitor {
outputHeader(javaStyle: true);
writeln('package com.google.dart.server.generated.types;');
writeln();
- // Currently non TypeObjects aren't being passed this visitor, but it may soon to generate enums or classes
- // that hold onto the values of a particular enum from the spec.
if (typeDef.type is TypeObject) {
- writeln('import java.util.Arrays;');
- writeln('import java.util.List;');
- writeln('import java.util.Map;');
- writeln('import com.google.dart.server.utilities.general.ObjectUtilities;');
- writeln('import org.apache.commons.lang3.StringUtils;');
- writeln();
- javadocComment(toHtmlVisitor.collectHtml(() {
- toHtmlVisitor.translateHtml(typeDef.html);
- toHtmlVisitor.br();
- toHtmlVisitor.write('@coverage dart.server.generated.types');
- }));
- writeln('@SuppressWarnings("unused")');
- makeClass('public class ${className}', () {
- TypeObject type = typeDef.type as TypeObject;
- List<TypeObjectField> fields = type.fields;
- // TODO (jwren) we need to possibly remove fields such as "type" in
- // these objects: AddContentOverlay | ChangeContentOverlay | RemoveContentOverlay
- // TODO (jwren) In this case, we don't want to sort the ordering of the
- // fields, the order from the spec would be better.
- //
- // fields
- //
+ _writeTypeObject(typeDef);
+ } else if (typeDef.type is TypeEnum) {
+ _writeTypeEnum(typeDef);
+ }
+ }
+
+ void _writeTypeObject(TypeDefinition typeDef) {
+ writeln('import java.util.Arrays;');
+ writeln('import java.util.List;');
+ writeln('import java.util.Map;');
+ writeln('import com.google.dart.server.utilities.general.ObjectUtilities;');
+ writeln('import org.apache.commons.lang3.StringUtils;');
+ writeln();
+ javadocComment(toHtmlVisitor.collectHtml(() {
+ toHtmlVisitor.translateHtml(typeDef.html);
+ toHtmlVisitor.br();
+ toHtmlVisitor.write('@coverage dart.server.generated.types');
+ }));
+ writeln('@SuppressWarnings("unused")');
+ makeClass('public class ${className}', () {
+ 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) In this case, we don't want to sort the ordering of the
+ // fields, the order from the spec would be better.
+ //
+ // fields
+ //
+ for (TypeObjectField field in fields) {
+ privateField(javaName(field.name), () {
+ javadocComment(toHtmlVisitor.collectHtml(() {
+ toHtmlVisitor.translateHtml(field.html);
+ }));
+ writeln(
+ 'private final ${javaType(field.type)} ${javaName(field.name)};');
+ });
+ }
+ //
+ // constructor
+ //
+ constructor(className, () {
+ javadocComment(toHtmlVisitor.collectHtml(() {
+ toHtmlVisitor.write('Constructor for {@link ${className}}.');
+ }));
+ write('public ${className}(');
+ // write out parameters to constructor
+ List<String> parameters = new List();
for (TypeObjectField field in fields) {
- privateField(javaName(field.name), () {
- javadocComment(toHtmlVisitor.collectHtml(() {
- toHtmlVisitor.translateHtml(field.html);
- }));
- writeln('private final ${javaType(field.type)} ${javaName(field.name)};');
- });
+ parameters.add('${javaType(field.type)} ${javaName(field.name)}');
}
- //
- // constructor
- //
- constructor(className, () {
+ write(parameters.join(', '));
+ writeln(') {');
+ // write out the assignments in the body of the constructor
+ for (TypeObjectField field in fields) {
+ writeln(' this.${javaName(field.name)} = ${javaName(field.name)};');
+ }
+ writeln('}');
+ });
+ //
+ // getter methods
+ //
+ for (TypeObjectField field in fields) {
+ publicMethod('get${javaName(field.name)}', () {
javadocComment(toHtmlVisitor.collectHtml(() {
- toHtmlVisitor.write('Constructor for {@link ${className}}.');
+ toHtmlVisitor.translateHtml(field.html);
}));
- write('public ${className}(');
- // write out parameters to constructor
- List<String> parameters = new List();
- for (TypeObjectField field in fields) {
- parameters.add('${javaType(field.type)} ${javaName(field.name)}');
- }
- write(parameters.join(', '));
- writeln(') {');
- // write out the assignments in the body of the constructor
- for (TypeObjectField field in fields) {
- writeln(' this.${javaName(field.name)} = ${javaName(field.name)};');
- }
+ writeln(
+ 'public ${javaType(field.type)} get${capitalize(javaName(field.name))}() {');
+ writeln(' return ${javaName(field.name)};');
writeln('}');
});
- //
- // getter methods
- //
- for (TypeObjectField field in fields) {
- publicMethod('get${javaName(field.name)}', () {
- javadocComment(toHtmlVisitor.collectHtml(() {
- toHtmlVisitor.translateHtml(field.html);
- }));
- writeln('public ${javaType(field.type)} get${capitalize(javaName(field.name))}() {');
- writeln(' return ${javaName(field.name)};');
- writeln('}');
- });
- }
- //
- // equals method
- //
- publicMethod('equals', () {
- writeln('@Override');
- writeln('public boolean equals(Object obj) {');
+ }
+ //
+ // equals method
+ //
+ publicMethod('equals', () {
+ writeln('@Override');
+ writeln('public boolean equals(Object obj) {');
+ indent(() {
+ writeln('if (obj instanceof ${className}) {');
indent(() {
- writeln('if (obj instanceof ${className}) {');
+ writeln('${className} other = (${className}) obj;');
+ writeln('return');
indent(() {
- writeln('${className} other = (${className}) obj;');
- writeln('return');
- indent(() {
- List<String> equalsForField = new List<String>();
- for (TypeObjectField field in fields) {
- equalsForField.add(_equalsLogicForField(field, 'other'));
- }
- write(equalsForField.join(' && \n'));
- });
- writeln(';');
+ List<String> equalsForField = new List<String>();
+ for (TypeObjectField field in fields) {
+ equalsForField.add(_equalsLogicForField(field, 'other'));
+ }
+ write(equalsForField.join(' && \n'));
});
- writeln('}');
- writeln('return false;');
+ writeln(';');
});
writeln('}');
+ writeln('return false;');
});
- //
- // hashCode
- //
- // TODO (jwren) have hashCode written out
- //
- // toString
- //
- publicMethod('toString', () {
- writeln('@Override');
- writeln('public String toString() {');
- indent(() {
- writeln('StringBuilder builder = new StringBuilder();');
- writeln('builder.append(\"[\");');
- for (TypeObjectField field in fields) {
- // TODO (jwren) nit: don't print the last ", "
- writeln("builder.append(\"${javaName(field.name)}=\");");
- writeln("builder.append(${_toStringForField(field)} + \", \");");
- }
- writeln('builder.append(\"]\");');
- writeln('return builder.toString();');
- });
- writeln('}');
- writeln();
+ writeln('}');
+ });
+ //
+ // hashCode
+ //
+ // TODO (jwren) have hashCode written out
+ //
+ // toString
+ //
+ publicMethod('toString', () {
+ writeln('@Override');
+ writeln('public String toString() {');
+ indent(() {
+ writeln('StringBuilder builder = new StringBuilder();');
+ writeln('builder.append(\"[\");');
+ for (TypeObjectField field in fields) {
+ // TODO (jwren) nit: don't print the last ", "
+ writeln("builder.append(\"${javaName(field.name)}=\");");
+ writeln("builder.append(${_toStringForField(field)} + \", \");");
+ }
+ writeln('builder.append(\"]\");');
+ writeln('return builder.toString();');
});
+ writeln('}');
+ writeln();
});
- }
+ });
+ }
+
+ void _writeTypeEnum(TypeDefinition typeDef) {
+ javadocComment(toHtmlVisitor.collectHtml(() {
+ toHtmlVisitor.translateHtml(typeDef.html);
+ toHtmlVisitor.br();
+ toHtmlVisitor.write('@coverage dart.server.generated.types');
+ }));
+ makeClass('public class ${className}', () {
+ TypeEnum typeEnum = typeDef.type as TypeEnum;
+ List<TypeEnumValue> values = typeEnum.values;
+ //
+ // enum fields
+ //
+ for (TypeEnumValue value in values) {
+ privateField(javaName(value.value), () {
+ writeln(
+ 'public static final String ${value.value} = \"${value.value}\";');
+ });
+ }
+ });
}
String _equalsLogicForField(TypeObjectField field, String other) {
String name = javaName(field.name);
if (isPrimitive(field.type)) {
- return 'other.${name} == ${name}';
+ return '${other}.${name} == ${name}';
} else if (isArray(field.type)) {
return 'Arrays.equals(other.${name}, ${name})';
} else {
- return 'ObjectUtilities.equals(other.${name}, ${name})';
+ return 'ObjectUtilities.equals(${other}.${name}, ${name})';
}
}
@@ -186,15 +213,18 @@ class CodegenJavaType extends CodegenJavaVisitor {
}
}
-final String pathToGenTypes = '../../../../editor/tools/plugins/com.google.dart.server/src/com/google/dart/server/generated/types/';
+final String pathToGenTypes =
+ '../../../../editor/tools/plugins/com.google.dart.server/src/com/google/dart/server/generated/types/';
final GeneratedDirectory targetDir = new GeneratedDirectory(pathToGenTypes, () {
Api api = readApi();
- Map<String, FileContentsComputer> map = new Map<String, FileContentsComputer>();
+ Map<String, FileContentsComputer> map =
+ new Map<String, FileContentsComputer>();
for (String typeNameInSpec in api.types.keys) {
TypeDefinition typeDef = api.types[typeNameInSpec];
- if (typeDef.type is TypeObject) {
- // for situations such as 'Override' where the name in the spec doesn't match the java object that we generate:
+ if (typeDef.type is TypeObject || typeDef.type is TypeEnum) {
+ // This is for situations such as 'Override' where the name in the spec
+ // doesn't match the java object that we generate:
String typeNameInJava = typeNameInSpec;
if (_typeRenames.containsKey(typeNameInSpec)) {
typeNameInJava = _typeRenames[typeNameInSpec];
@@ -202,7 +232,9 @@ final GeneratedDirectory targetDir = new GeneratedDirectory(pathToGenTypes, () {
map['${typeNameInJava}.java'] = () {
// create the visitor
CodegenJavaType visitor = new CodegenJavaType(api, typeNameInJava);
- return visitor.collectCode(() {visitor.visitTypeDefinition(typeDef);});
+ return visitor.collectCode(() {
+ visitor.visitTypeDefinition(typeDef);
+ });
};
}
}
« no previous file with comments | « pkg/analysis_server/tool/spec/codegen_java.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698