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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 /** 5 /**
6 * Tools for Java code generation. 6 * Tools for Java code generation.
7 */ 7 */
8 library CodegenJava; 8 library CodegenJava;
9 9
10 import 'package:html5lib/dom.dart' as dom;
10 import 'api.dart'; 11 import 'api.dart';
11 import 'codegen_tools.dart'; 12 import 'codegen_tools.dart';
12 import 'from_html.dart'; 13 import 'from_html.dart';
13 import 'to_html.dart'; 14 import 'to_html.dart';
14 15
15 /** 16 /**
16 * Common code for all Java code generation. 17 * Common code for all Java code generation.
17 */ 18 */
18 class CodegenJavaVisitor extends HierarchicalApiVisitor with CodeGenerator { 19 class CodegenJavaVisitor extends HierarchicalApiVisitor with CodeGenerator {
19 _CodegenJavaState _state; 20 _CodegenJavaState _state;
20 21
21 /** 22 /**
22 * Variable names which must be changed in order to avoid conflict with 23 * Variable names which must be changed in order to avoid conflict with
23 * reserved words in Java. 24 * reserved words in Java.
24 */ 25 */
25 static const Map<String, String> _variableRenames = const { 26 static const Map<String, String> _variableRenames = const {
26 'default': 'defaultSdk' 27 'default': 'defaultSdk'
27 }; 28 };
28 29
29 /** 30 /**
30 * Type references in the spec that are named something else in Java. 31 * Type references in the spec that are named something else in Java.
31 */ 32 */
32 static const Map<String, String> _typeRenames = const { 33 static const Map<String, String> _typeRenames = const {
33 'bool': 'boolean', 34 'bool': 'boolean',
34 'FilePath': 'String', 35 'FilePath': 'String',
35 'DebugContextId': 'String', 36 'DebugContextId': 'String',
36 'object': 'Object', 37 'object': 'Object',
38 'Override': 'OverrideMember',
37 }; 39 };
38 40
39 /** 41 /**
40 * Visitor used to produce doc comments. 42 * Visitor used to produce doc comments.
41 */ 43 */
42 final ToHtmlVisitor toHtmlVisitor; 44 final ToHtmlVisitor toHtmlVisitor;
43 45
44 CodegenJavaVisitor(Api api) 46 CodegenJavaVisitor(Api api)
45 : super(api), 47 : super(api),
46 toHtmlVisitor = new ToHtmlVisitor(api); 48 toHtmlVisitor = new ToHtmlVisitor(api);
47 49
48 /** 50 /**
51 * Convenience method for subclasses for calling docComment.
52 */
53 void javadocComment(List<dom.Node> docs) {
54 docComment(docs, width: 99, javadocStyle: true);
55 }
56
57 /**
58 * Create a private field, using [callback] to create its contents.
59 */
60 void privateField(String fieldName, void callback()) {
61 _state.privateFields[fieldName] = collectCode(callback);
62 }
63
64 /**
65 * Create a constructor, using [callback] to create its contents.
66 */
67 void constructor(String name, void callback()) {
68 _state.constructors[name] = collectCode(callback);
69 }
70
71 /**
49 * Create a private method, using [callback] to create its contents. 72 * Create a private method, using [callback] to create its contents.
50 */ 73 */
51 void privateMethod(String methodName, void callback()) { 74 void privateMethod(String methodName, void callback()) {
52 _state.privateMethods[methodName] = collectCode(callback); 75 _state.privateMethods[methodName] = collectCode(callback);
53 } 76 }
54 77
55 /** 78 /**
56 * Create a public method, using [callback] to create its contents. 79 * Create a public method, using [callback] to create its contents.
57 */ 80 */
58 void publicMethod(String methodName, void callback()) { 81 void publicMethod(String methodName, void callback()) {
59 _state.publicMethods[methodName] = collectCode(callback); 82 _state.publicMethods[methodName] = collectCode(callback);
60 } 83 }
61 84
62 /** 85 /**
63 * Execute [callback], collecting any methods that are output using 86 * Execute [callback], collecting any methods that are output using
64 * [privateMethod] or [publicMethod], and insert the class (with methods 87 * [privateMethod] or [publicMethod], and insert the class (with methods
65 * sorted). [header] is the part of the class declaration before the 88 * sorted). [header] is the part of the class declaration before the
66 * opening brace. 89 * opening brace.
67 */ 90 */
68 void makeClass(String header, void callback()) { 91 void makeClass(String header, void callback()) {
69 _CodegenJavaState oldState = _state; 92 _CodegenJavaState oldState = _state;
70 try { 93 try {
71 _state = new _CodegenJavaState(); 94 _state = new _CodegenJavaState();
72 callback(); 95 callback();
73 writeln('$header {'); 96 writeln('$header {');
74 indent(() { 97 indent(() {
98 // fields
99 List<String> allFields = _valuesSortedByKey(_state.privateFields).toList ();
100 for (String field in allFields) {
101 writeln();
102 write(field);
103 }
104
105 // constructors
106 List<String> allConstructors = _valuesSortedByKey(_state.constructors).t oList();
107 for (String constructor in allConstructors) {
108 writeln();
109 write(constructor);
110 }
111
112 // methods
75 List<String> allMethods = _valuesSortedByKey(_state.publicMethods).toLis t(); 113 List<String> allMethods = _valuesSortedByKey(_state.publicMethods).toLis t();
76 allMethods.addAll(_valuesSortedByKey(_state.privateMethods)); 114 allMethods.addAll(_valuesSortedByKey(_state.privateMethods));
77 for (String method in allMethods) { 115 for (String method in allMethods) {
78 writeln(); 116 writeln();
79 write(method); 117 write(method);
80 } 118 }
81 }); 119 });
82 writeln('}'); 120 writeln('}');
83 } finally { 121 } finally {
84 _state = oldState; 122 _state = oldState;
85 } 123 }
86 } 124 }
87 125
88 /** 126 /**
89 * Convert the given [TypeDecl] to a Java type. 127 * Convert the given [TypeDecl] to a Java type.
90 */ 128 */
91 String javaType(TypeDecl type) { 129 String javaType(TypeDecl type) {
92 if (type is TypeReference) { 130 if (type is TypeReference) {
93 TypeReference resolvedType = resolveTypeReferenceChain(type); 131 TypeReference resolvedType = resolveTypeReferenceChain(type);
94 String typeName = resolvedType.typeName; 132 String typeName = resolvedType.typeName;
95 if (_typeRenames.containsKey(typeName)) { 133 if (_typeRenames.containsKey(typeName)) {
96 return _typeRenames[typeName]; 134 return _typeRenames[typeName];
97 } else { 135 } else {
98 return typeName; 136 return typeName;
99 } 137 }
100 } else if (type is TypeList) { 138 } else if (type is TypeList) {
101 return 'List<${javaType(type.itemType)}>'; 139 if (isPrimitive(type.itemType)) {
140 return '${javaType(type.itemType)}[]';
141 } else {
142 return 'List<${javaType(type.itemType)}>';
143 }
102 } else if (type is TypeMap) { 144 } else if (type is TypeMap) {
103 return 'Map<${javaType(type.keyType)}, ${javaType(type.valueType)}>'; 145 return 'Map<${javaType(type.keyType)}, ${javaType(type.valueType)}>';
104 } else if (type is TypeUnion) { 146 } else if (type is TypeUnion) {
105 return 'Object'; 147 return 'Object';
106 } else { 148 } else {
107 throw new Exception("Can't make type buildable"); 149 throw new Exception("Can't make type buildable");
108 } 150 }
109 } 151 }
110 152
111 /** 153 /**
154 * Return true iff the passed [TypeDecl] will represent a primitive Java type.
155 */
156 bool isPrimitive(TypeDecl type) {
157 if (type is TypeReference) {
158 String typeStr = javaType(type);
159 return typeStr == 'int' || typeStr == 'boolean';
160 }
161 return false;
162 }
163
164 /**
165 * Return true iff the passed [TypeDecl] will represent an array in Java.
166 */
167 bool isList(TypeDecl type) {
168 return type is TypeList && !isPrimitive(type.itemType);
169 }
170
171 /**
172 * Return true iff the passed [TypeDecl] will represent an array in Java.
173 */
174 bool isArray(TypeDecl type) {
175 return type is TypeList && isPrimitive(type.itemType);
176 }
177
178 /**
179 * Return true iff the passed [TypeDecl] will represent a Map in type.
180 */
181 bool isMap(TypeDecl type) {
182 return type is TypeMap;
183 }
184
185 /**
112 * Return a suitable representation of [name] as the name of a Java variable. 186 * Return a suitable representation of [name] as the name of a Java variable.
113 */ 187 */
114 String javaName(String name) { 188 String javaName(String name) {
115 if (_variableRenames.containsKey(name)) { 189 if (_variableRenames.containsKey(name)) {
116 return _variableRenames[name]; 190 return _variableRenames[name];
117 } 191 }
118 return name; 192 return name;
119 } 193 }
120 194
121 @override 195 @override
(...skipping 21 matching lines...) Expand all
143 class _CodegenJavaState { 217 class _CodegenJavaState {
144 /** 218 /**
145 * Temporary storage for public methods. 219 * Temporary storage for public methods.
146 */ 220 */
147 Map<String, String> publicMethods = <String, String>{}; 221 Map<String, String> publicMethods = <String, String>{};
148 222
149 /** 223 /**
150 * Temporary storage for private methods. 224 * Temporary storage for private methods.
151 */ 225 */
152 Map<String, String> privateMethods = <String, String>{}; 226 Map<String, String> privateMethods = <String, String>{};
227
228 /**
229 * Temporary storage for private fields.
230 */
231 Map<String, String> privateFields = <String, String>{};
232
233 /**
234 * Temporary storage for constructors.
235 */
236 Map<String, String> constructors = <String, String>{};
153 } 237 }
154 238
155 /** 239 /**
156 * Create a [GeneratedFile] that creates Java code and outputs it to [path]. 240 * Create a [GeneratedFile] that creates Java code and outputs it to [path].
157 * [path] uses Posix-style path separators regardless of the OS. 241 * [path] uses Posix-style path separators regardless of the OS.
158 */ 242 */
159 GeneratedFile javaGeneratedFile(String path, CodegenJavaVisitor createVisitor(Ap i api)) { 243 GeneratedFile javaGeneratedFile(String path, CodegenJavaVisitor createVisitor(Ap i api)) {
160 return new GeneratedFile(path, () { 244 return new GeneratedFile(path, () {
161 CodegenJavaVisitor visitor = createVisitor(readApi()); 245 CodegenJavaVisitor visitor = createVisitor(readApi());
162 return visitor.collectCode(visitor.visitApi); 246 return visitor.collectCode(visitor.visitApi);
163 }); 247 });
164 } 248 }
OLDNEW
« 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