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 13 matching lines...) Expand all Loading... | |
| 24 * reserved words in Java. | 24 * reserved words in Java. |
| 25 */ | 25 */ |
| 26 static const Map<String, String> _variableRenames = const { | 26 static const Map<String, String> _variableRenames = const { |
| 27 'default': 'defaultSdk' | 27 'default': 'defaultSdk' |
| 28 }; | 28 }; |
| 29 | 29 |
| 30 /** | 30 /** |
| 31 * 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. |
| 32 */ | 32 */ |
| 33 static const Map<String, String> _typeRenames = const { | 33 static const Map<String, String> _typeRenames = const { |
| 34 // TODO (jwren) in some situations we want to use Boolean while other | 34 // TODO (jwren) in some situations we want to use Boolean/Integer while |
| 35 // situations we want to use boolean... | 35 // other situations we want to use boolean/int... |
| 36 'bool': 'Boolean', | 36 'bool': 'Boolean', |
| 37 'int': 'Integer', | |
| 37 'FilePath': 'String', | 38 'FilePath': 'String', |
| 38 'DebugContextId': 'String', | 39 'DebugContextId': 'String', |
| 39 'object': 'Object', | 40 'object': 'Object', |
| 40 'Override': 'OverrideMember', | 41 'Override': 'OverrideMember', |
| 41 }; | 42 }; |
| 42 | 43 |
| 43 /** | 44 /** |
| 44 * Visitor used to produce doc comments. | 45 * Visitor used to produce doc comments. |
| 45 */ | 46 */ |
| 46 final ToHtmlVisitor toHtmlVisitor; | 47 final ToHtmlVisitor toHtmlVisitor; |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 106 indent(() { | 107 indent(() { |
| 107 // fields | 108 // fields |
| 108 List<String> allFields = _state.publicFields.values.toList(); | 109 List<String> allFields = _state.publicFields.values.toList(); |
| 109 allFields.addAll(_state.privateFields.values.toList()); | 110 allFields.addAll(_state.privateFields.values.toList()); |
| 110 for (String field in allFields) { | 111 for (String field in allFields) { |
| 111 writeln(); | 112 writeln(); |
| 112 write(field); | 113 write(field); |
| 113 } | 114 } |
| 114 | 115 |
| 115 // constructors | 116 // constructors |
| 116 List<String> allConstructors =_state.constructors.values.toList(); | 117 List<String> allConstructors = _state.constructors.values.toList(); |
| 117 for (String constructor in allConstructors) { | 118 for (String constructor in allConstructors) { |
| 118 writeln(); | 119 writeln(); |
| 119 write(constructor); | 120 write(constructor); |
| 120 } | 121 } |
| 121 | 122 |
| 122 // methods (ordered by method name) | 123 // methods (ordered by method name) |
| 123 List<String> allMethods = | 124 List<String> allMethods = |
| 124 _valuesSortedByKey(_state.publicMethods).toList(); | 125 _valuesSortedByKey(_state.publicMethods).toList(); |
| 125 allMethods.addAll(_valuesSortedByKey(_state.privateMethods)); | 126 allMethods.addAll(_valuesSortedByKey(_state.privateMethods)); |
| 126 for (String method in allMethods) { | 127 for (String method in allMethods) { |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 161 throw new Exception("Can't make type buildable"); | 162 throw new Exception("Can't make type buildable"); |
| 162 } | 163 } |
| 163 } | 164 } |
| 164 | 165 |
| 165 /** | 166 /** |
| 166 * Return true iff the passed [TypeDecl] will represent a primitive Java type. | 167 * Return true iff the passed [TypeDecl] will represent a primitive Java type. |
| 167 */ | 168 */ |
| 168 bool isPrimitive(TypeDecl type) { | 169 bool isPrimitive(TypeDecl type) { |
| 169 if (type is TypeReference) { | 170 if (type is TypeReference) { |
| 170 String typeStr = javaType(type); | 171 String typeStr = javaType(type); |
| 171 return typeStr == 'int' || typeStr == 'boolean'; | 172 return typeStr == 'Integer' || typeStr == 'boolean'; |
| 172 } | 173 } |
| 173 return false; | 174 return false; |
| 174 } | 175 } |
| 175 | 176 |
| 176 /** | 177 /** |
| 178 * Return true iff the passed [TypeDecl] is a type declared in the spec_input. | |
| 179 */ | |
| 180 bool isDeclaredInSpec(TypeDecl type) { | |
| 181 if (type is TypeReference) { | |
|
Paul Berry
2014/08/19 20:51:12
You could also do:
if (type is TypeReference) {
jwren
2014/08/20 20:51:21
Acknowledged.
| |
| 182 String typeStr = javaType(type); | |
| 183 return typeStr != 'Integer' && typeStr != 'Boolean' && typeStr != 'String' ; | |
| 184 } | |
| 185 return false; | |
| 186 } | |
| 187 | |
| 188 /** | |
| 189 * Return true iff the passed [TypeDecl] will be represented as Object in Java . | |
| 190 */ | |
| 191 bool isObject(TypeDecl type) { | |
| 192 if (type is TypeReference) { | |
| 193 String typeStr = javaType(type); | |
| 194 return typeStr == 'Object'; | |
| 195 } | |
| 196 return true; | |
| 197 } | |
| 198 | |
| 199 /** | |
| 177 * Return true iff the passed [TypeDecl] will represent an array in Java. | 200 * Return true iff the passed [TypeDecl] will represent an array in Java. |
| 178 */ | 201 */ |
| 179 bool isList(TypeDecl type) { | 202 bool isList(TypeDecl type) { |
| 180 return type is TypeList && !isPrimitive(type.itemType); | 203 return type is TypeList && !isPrimitive(type.itemType); |
| 181 } | 204 } |
| 182 | 205 |
| 183 /** | 206 /** |
| 184 * Return true iff the passed [TypeDecl] will represent an array in Java. | 207 * Return true iff the passed [TypeDecl] will represent an array in Java. |
| 185 */ | 208 */ |
| 186 bool isArray(TypeDecl type) { | 209 bool isArray(TypeDecl type) { |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 257 * Create a [GeneratedFile] that creates Java code and outputs it to [path]. | 280 * Create a [GeneratedFile] that creates Java code and outputs it to [path]. |
| 258 * [path] uses Posix-style path separators regardless of the OS. | 281 * [path] uses Posix-style path separators regardless of the OS. |
| 259 */ | 282 */ |
| 260 GeneratedFile javaGeneratedFile(String path, CodegenJavaVisitor | 283 GeneratedFile javaGeneratedFile(String path, CodegenJavaVisitor |
| 261 createVisitor(Api api)) { | 284 createVisitor(Api api)) { |
| 262 return new GeneratedFile(path, () { | 285 return new GeneratedFile(path, () { |
| 263 CodegenJavaVisitor visitor = createVisitor(readApi()); | 286 CodegenJavaVisitor visitor = createVisitor(readApi()); |
| 264 return visitor.collectCode(visitor.visitApi); | 287 return visitor.collectCode(visitor.visitApi); |
| 265 }); | 288 }); |
| 266 } | 289 } |
| OLD | NEW |