Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 'package:html5lib/dom.dart' as dom; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 48 toHtmlVisitor = new ToHtmlVisitor(api); | 48 toHtmlVisitor = new ToHtmlVisitor(api); |
| 49 | 49 |
| 50 /** | 50 /** |
| 51 * Convenience method for subclasses for calling docComment. | 51 * Convenience method for subclasses for calling docComment. |
| 52 */ | 52 */ |
| 53 void javadocComment(List<dom.Node> docs) { | 53 void javadocComment(List<dom.Node> docs) { |
| 54 docComment(docs, width: 99, javadocStyle: true); | 54 docComment(docs, width: 99, javadocStyle: true); |
| 55 } | 55 } |
| 56 | 56 |
| 57 /** | 57 /** |
| 58 * Create a public field, using [callback] to create its contents. | |
| 59 */ | |
| 60 void publicField(String fieldName, void callback()) { | |
| 61 _state.publicFields[fieldName] = collectCode(callback); | |
| 62 } | |
| 63 | |
| 64 /** | |
| 58 * Create a private field, using [callback] to create its contents. | 65 * Create a private field, using [callback] to create its contents. |
| 59 */ | 66 */ |
| 60 void privateField(String fieldName, void callback()) { | 67 void privateField(String fieldName, void callback()) { |
| 61 _state.privateFields[fieldName] = collectCode(callback); | 68 _state.privateFields[fieldName] = collectCode(callback); |
| 62 } | 69 } |
| 63 | 70 |
| 64 /** | 71 /** |
| 65 * Create a constructor, using [callback] to create its contents. | 72 * Create a constructor, using [callback] to create its contents. |
| 66 */ | 73 */ |
| 67 void constructor(String name, void callback()) { | 74 void constructor(String name, void callback()) { |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 89 * opening brace. | 96 * opening brace. |
| 90 */ | 97 */ |
| 91 void makeClass(String header, void callback()) { | 98 void makeClass(String header, void callback()) { |
| 92 _CodegenJavaState oldState = _state; | 99 _CodegenJavaState oldState = _state; |
| 93 try { | 100 try { |
| 94 _state = new _CodegenJavaState(); | 101 _state = new _CodegenJavaState(); |
| 95 callback(); | 102 callback(); |
| 96 writeln('$header {'); | 103 writeln('$header {'); |
| 97 indent(() { | 104 indent(() { |
| 98 // fields | 105 // fields |
| 99 List<String> allFields = | 106 List<String> allFields = _state.publicFields.values.toList(); |
| 100 _valuesSortedByKey(_state.privateFields).toList(); | 107 allFields.addAll(_state.privateFields.values.toList()); |
|
Paul Berry
2014/08/14 05:58:29
I believe ".toList()" can be omitted since _state.
jwren
2014/08/14 06:20:29
I thought so too, but this comes out of the VM if
Paul Berry
2014/08/14 12:47:37
That happens if you remove the toList() on line 10
| |
| 101 for (String field in allFields) { | 108 for (String field in allFields) { |
| 102 writeln(); | 109 writeln(); |
| 103 write(field); | 110 write(field); |
| 104 } | 111 } |
| 105 | 112 |
| 106 // constructors | 113 // constructors |
| 107 List<String> allConstructors = | 114 List<String> allConstructors =_state.constructors.values.toList(); |
| 108 _valuesSortedByKey(_state.constructors).toList(); | |
| 109 for (String constructor in allConstructors) { | 115 for (String constructor in allConstructors) { |
| 110 writeln(); | 116 writeln(); |
| 111 write(constructor); | 117 write(constructor); |
| 112 } | 118 } |
| 113 | 119 |
| 114 // methods | 120 // methods (ordered by method name) |
| 115 List<String> allMethods = | 121 List<String> allMethods = |
| 116 _valuesSortedByKey(_state.publicMethods).toList(); | 122 _valuesSortedByKey(_state.publicMethods).toList(); |
| 117 allMethods.addAll(_valuesSortedByKey(_state.privateMethods)); | 123 allMethods.addAll(_valuesSortedByKey(_state.privateMethods)); |
| 118 for (String method in allMethods) { | 124 for (String method in allMethods) { |
| 119 writeln(); | 125 writeln(); |
| 120 write(method); | 126 write(method); |
| 121 } | 127 } |
| 128 writeln(); | |
| 122 }); | 129 }); |
| 123 writeln('}'); | 130 writeln('}'); |
| 124 } finally { | 131 } finally { |
| 125 _state = oldState; | 132 _state = oldState; |
| 126 } | 133 } |
| 127 } | 134 } |
| 128 | 135 |
| 129 /** | 136 /** |
| 130 * Convert the given [TypeDecl] to a Java type. | 137 * Convert the given [TypeDecl] to a Java type. |
| 131 */ | 138 */ |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 222 * Temporary storage for public methods. | 229 * Temporary storage for public methods. |
| 223 */ | 230 */ |
| 224 Map<String, String> publicMethods = <String, String>{}; | 231 Map<String, String> publicMethods = <String, String>{}; |
| 225 | 232 |
| 226 /** | 233 /** |
| 227 * Temporary storage for private methods. | 234 * Temporary storage for private methods. |
| 228 */ | 235 */ |
| 229 Map<String, String> privateMethods = <String, String>{}; | 236 Map<String, String> privateMethods = <String, String>{}; |
| 230 | 237 |
| 231 /** | 238 /** |
| 232 * Temporary storage for private fields. | 239 * Temporary storage for private fields. |
|
Paul Berry
2014/08/14 05:58:29
s/private/public/
jwren
2014/08/14 06:20:29
Done.
| |
| 233 */ | 240 */ |
| 241 Map<String, String> publicFields = <String, String>{}; | |
| 242 | |
| 243 /** | |
| 244 * Temporary storage for private fields. | |
| 245 */ | |
| 234 Map<String, String> privateFields = <String, String>{}; | 246 Map<String, String> privateFields = <String, String>{}; |
| 235 | 247 |
| 236 /** | 248 /** |
| 237 * Temporary storage for constructors. | 249 * Temporary storage for constructors. |
| 238 */ | 250 */ |
| 239 Map<String, String> constructors = <String, String>{}; | 251 Map<String, String> constructors = <String, String>{}; |
| 240 } | 252 } |
| 241 | 253 |
| 242 /** | 254 /** |
| 243 * Create a [GeneratedFile] that creates Java code and outputs it to [path]. | 255 * Create a [GeneratedFile] that creates Java code and outputs it to [path]. |
| 244 * [path] uses Posix-style path separators regardless of the OS. | 256 * [path] uses Posix-style path separators regardless of the OS. |
| 245 */ | 257 */ |
| 246 GeneratedFile javaGeneratedFile(String path, CodegenJavaVisitor | 258 GeneratedFile javaGeneratedFile(String path, CodegenJavaVisitor |
| 247 createVisitor(Api api)) { | 259 createVisitor(Api api)) { |
| 248 return new GeneratedFile(path, () { | 260 return new GeneratedFile(path, () { |
| 249 CodegenJavaVisitor visitor = createVisitor(readApi()); | 261 CodegenJavaVisitor visitor = createVisitor(readApi()); |
| 250 return visitor.collectCode(visitor.visitApi); | 262 return visitor.collectCode(visitor.visitApi); |
| 251 }); | 263 }); |
| 252 } | 264 } |
| OLD | NEW |