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

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

Issue 473533003: Initial code to generate the Java types from the spec. (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.dart
diff --git a/pkg/analysis_server/tool/spec/codegen_java.dart b/pkg/analysis_server/tool/spec/codegen_java.dart
index e534c40e47e822777882c5951b8e3dbf3b84b107..3139bf114b8217cedb504cc864a2259021799462 100644
--- a/pkg/analysis_server/tool/spec/codegen_java.dart
+++ b/pkg/analysis_server/tool/spec/codegen_java.dart
@@ -7,6 +7,7 @@
*/
library CodegenJava;
+import 'package:html5lib/dom.dart' as dom;
import 'api.dart';
import 'codegen_tools.dart';
import 'from_html.dart';
@@ -34,6 +35,7 @@ class CodegenJavaVisitor extends HierarchicalApiVisitor with CodeGenerator {
'FilePath': 'String',
'DebugContextId': 'String',
'object': 'Object',
+ 'Override': 'OverrideMember',
};
/**
@@ -46,6 +48,27 @@ class CodegenJavaVisitor extends HierarchicalApiVisitor with CodeGenerator {
toHtmlVisitor = new ToHtmlVisitor(api);
/**
+ * Convenience method for subclasses for calling docComment.
+ */
+ void javadocComment(List<dom.Node> docs) {
+ docComment(docs, width: 99, javadocStyle: true);
+ }
+
+ /**
+ * Create a private field, using [callback] to create its contents.
+ */
+ void privateField(String fieldName, void callback()) {
+ _state.privateFields[fieldName] = collectCode(callback);
+ }
+
+ /**
+ * Create a constructor, using [callback] to create its contents.
+ */
+ void constructor(String name, void callback()) {
+ _state.constructors[name] = collectCode(callback);
+ }
+
+ /**
* Create a private method, using [callback] to create its contents.
*/
void privateMethod(String methodName, void callback()) {
@@ -72,6 +95,21 @@ class CodegenJavaVisitor extends HierarchicalApiVisitor with CodeGenerator {
callback();
writeln('$header {');
indent(() {
+ // fields
+ List<String> allFields = _valuesSortedByKey(_state.privateFields).toList();
+ for (String field in allFields) {
+ writeln();
+ write(field);
+ }
+
+ // constructors
+ List<String> allConstructors = _valuesSortedByKey(_state.constructors).toList();
+ for (String constructor in allConstructors) {
+ writeln();
+ write(constructor);
+ }
+
+ // methods
List<String> allMethods = _valuesSortedByKey(_state.publicMethods).toList();
allMethods.addAll(_valuesSortedByKey(_state.privateMethods));
for (String method in allMethods) {
@@ -98,7 +136,11 @@ class CodegenJavaVisitor extends HierarchicalApiVisitor with CodeGenerator {
return typeName;
}
} else if (type is TypeList) {
- return 'List<${javaType(type.itemType)}>';
+ if (isPrimitive(type.itemType)) {
+ return '${javaType(type.itemType)}[]';
+ } else {
+ return 'List<${javaType(type.itemType)}>';
+ }
} else if (type is TypeMap) {
return 'Map<${javaType(type.keyType)}, ${javaType(type.valueType)}>';
} else if (type is TypeUnion) {
@@ -109,6 +151,38 @@ class CodegenJavaVisitor extends HierarchicalApiVisitor with CodeGenerator {
}
/**
+ * Return true iff the passed [TypeDecl] will represent a primitive Java type.
+ */
+ bool isPrimitive(TypeDecl type) {
+ if (type is TypeReference) {
+ String typeStr = javaType(type);
+ return typeStr == 'int' || typeStr == 'boolean';
+ }
+ return false;
+ }
+
+ /**
+ * Return true iff the passed [TypeDecl] will represent an array in Java.
+ */
+ bool isList(TypeDecl type) {
+ return type is TypeList && !isPrimitive(type.itemType);
+ }
+
+ /**
+ * Return true iff the passed [TypeDecl] will represent an array in Java.
+ */
+ bool isArray(TypeDecl type) {
+ return type is TypeList && isPrimitive(type.itemType);
+ }
+
+ /**
+ * Return true iff the passed [TypeDecl] will represent a Map in type.
+ */
+ bool isMap(TypeDecl type) {
+ return type is TypeMap;
+ }
+
+ /**
* Return a suitable representation of [name] as the name of a Java variable.
*/
String javaName(String name) {
@@ -150,6 +224,16 @@ class _CodegenJavaState {
* Temporary storage for private methods.
*/
Map<String, String> privateMethods = <String, String>{};
+
+ /**
+ * Temporary storage for private fields.
+ */
+ Map<String, String> privateFields = <String, String>{};
+
+ /**
+ * Temporary storage for constructors.
+ */
+ Map<String, String> constructors = <String, String>{};
}
/**
« no previous file with comments | « pkg/analysis_server/tool/spec/codegen_analysis_server.dart ('k') | pkg/analysis_server/tool/spec/codegen_java_types.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698