| 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 15 matching lines...) Expand all Loading... |
| 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/Integer while | 34 // TODO (jwren) in some situations we want to use Boolean/Integer while |
| 35 // other situations we want to use boolean/int... | 35 // other situations we want to use boolean/int... |
| 36 'bool': 'Boolean', | 36 'bool': 'boolean', |
| 37 'int': 'Integer', | 37 'int': 'int', |
| 38 'FilePath': 'String', | 38 'FilePath': 'String', |
| 39 'DebugContextId': 'String', | 39 'DebugContextId': 'String', |
| 40 'object': 'Object', | 40 'object': 'Object', |
| 41 'Override': 'OverrideMember', | 41 'Override': 'OverrideMember', |
| 42 }; | 42 }; |
| 43 | 43 |
| 44 /** | 44 /** |
| 45 * Visitor used to produce doc comments. | 45 * Visitor used to produce doc comments. |
| 46 */ | 46 */ |
| 47 final ToHtmlVisitor toHtmlVisitor; | 47 final ToHtmlVisitor toHtmlVisitor; |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 } | 130 } |
| 131 writeln(); | 131 writeln(); |
| 132 }); | 132 }); |
| 133 writeln('}'); | 133 writeln('}'); |
| 134 } finally { | 134 } finally { |
| 135 _state = oldState; | 135 _state = oldState; |
| 136 } | 136 } |
| 137 } | 137 } |
| 138 | 138 |
| 139 /** | 139 /** |
| 140 * Return a Java type for the given [TypeObjectField]. |
| 141 */ |
| 142 String javaFieldType(TypeObjectField field) { |
| 143 return javaType(field.type, field.optional); |
| 144 } |
| 145 |
| 146 /** |
| 140 * Convert the given [TypeDecl] to a Java type. | 147 * Convert the given [TypeDecl] to a Java type. |
| 141 */ | 148 */ |
| 142 String javaType(TypeDecl type) { | 149 String javaType(TypeDecl type, [bool optional = false]) { |
| 143 if (type is TypeReference) { | 150 if (type is TypeReference) { |
| 144 TypeReference resolvedType = resolveTypeReferenceChain(type); | 151 TypeReference resolvedType = resolveTypeReferenceChain(type); |
| 145 String typeName = resolvedType.typeName; | 152 String typeName = resolvedType.typeName; |
| 146 if (_typeRenames.containsKey(typeName)) { | 153 if (_typeRenames.containsKey(typeName)) { |
| 147 return _typeRenames[typeName]; | 154 typeName = _typeRenames[typeName]; |
| 148 } else { | 155 if (optional) { |
| 149 return typeName; | 156 if (typeName == 'boolean') { |
| 157 typeName = 'Boolean'; |
| 158 } else if (typeName == 'int') { |
| 159 typeName = 'Integer'; |
| 160 } |
| 161 } |
| 150 } | 162 } |
| 163 return typeName; |
| 151 } else if (type is TypeList) { | 164 } else if (type is TypeList) { |
| 152 if (isPrimitive(type.itemType)) { | 165 if (isPrimitive(type.itemType)) { |
| 153 return '${javaType(type.itemType)}[]'; | 166 return '${javaType(type.itemType)}[]'; |
| 154 } else { | 167 } else { |
| 155 return 'List<${javaType(type.itemType)}>'; | 168 return 'List<${javaType(type.itemType)}>'; |
| 156 } | 169 } |
| 157 } else if (type is TypeMap) { | 170 } else if (type is TypeMap) { |
| 158 return 'Map<${javaType(type.keyType)}, ${javaType(type.valueType)}>'; | 171 return 'Map<${javaType(type.keyType)}, ${javaType(type.valueType)}>'; |
| 159 } else if (type is TypeUnion) { | 172 } else if (type is TypeUnion) { |
| 160 return 'Object'; | 173 return 'Object'; |
| 161 } else { | 174 } else { |
| 162 throw new Exception("Can't make type buildable"); | 175 throw new Exception("Can't make type buildable"); |
| 163 } | 176 } |
| 164 } | 177 } |
| 165 | 178 |
| 166 /** | 179 /** |
| 167 * Return true iff the passed [TypeDecl] will represent a primitive Java type. | 180 * Return true iff the passed [TypeDecl] will represent a primitive Java type. |
| 168 */ | 181 */ |
| 169 bool isPrimitive(TypeDecl type) { | 182 bool isPrimitive(TypeDecl type) { |
| 170 if (type is TypeReference) { | 183 if (type is TypeReference) { |
| 171 String typeStr = javaType(type); | 184 String typeStr = javaType(type); |
| 172 return typeStr == 'Integer' || typeStr == 'boolean'; | 185 return typeStr == 'int' || typeStr == 'boolean'; |
| 173 } | 186 } |
| 174 return false; | 187 return false; |
| 175 } | 188 } |
| 176 | 189 |
| 177 /** | 190 /** |
| 178 * Return true iff the passed [TypeDecl] is a type declared in the spec_input. | 191 * Return true iff the passed [TypeDecl] is a type declared in the spec_input. |
| 179 */ | 192 */ |
| 180 bool isDeclaredInSpec(TypeDecl type) { | 193 bool isDeclaredInSpec(TypeDecl type) { |
| 181 // TypeReference resolvedType = super.resolveTypeReferenceChain(type); | 194 // TypeReference resolvedType = super.resolveTypeReferenceChain(type); |
| 182 // if(resolvedType is TypeObject) { | 195 // if(resolvedType is TypeObject) { |
| 183 // return truye; | 196 // return truye; |
| 184 // } | 197 // } |
| 185 if (type is TypeReference) { | 198 if (type is TypeReference) { |
| 186 return api.types.containsKey(type.typeName) && javaType(type) != 'String'; | 199 return api.types.containsKey(type.typeName) && javaType(type) != 'String'; |
| 187 } | 200 } |
| 188 return false; | 201 return false; |
| 189 } | 202 } |
| 190 | 203 |
| 191 /** | 204 /** |
| 192 * Return true iff the passed [TypeDecl] will be represented as Object in Java
. | 205 * Return true iff the passed [TypeDecl] will be represented as Object in Java
. |
| 193 */ | 206 */ |
| 194 bool isObject(TypeDecl type) { | 207 bool isObject(TypeDecl type) { |
| 195 String typeStr = javaType(type); | 208 String typeStr = javaType(type); |
| 196 return typeStr == 'Object'; | 209 return typeStr == 'Object'; |
| 197 } | 210 } |
| 198 | 211 |
| 199 /** | 212 /** |
| 200 * Return true iff the passed [TypeDecl] will represent an array in Java. | 213 * Return true iff the passed [TypeDecl] will represent an array in Java. |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 280 * Create a [GeneratedFile] that creates Java code and outputs it to [path]. | 293 * Create a [GeneratedFile] that creates Java code and outputs it to [path]. |
| 281 * [path] uses Posix-style path separators regardless of the OS. | 294 * [path] uses Posix-style path separators regardless of the OS. |
| 282 */ | 295 */ |
| 283 GeneratedFile javaGeneratedFile(String path, CodegenJavaVisitor | 296 GeneratedFile javaGeneratedFile(String path, CodegenJavaVisitor |
| 284 createVisitor(Api api)) { | 297 createVisitor(Api api)) { |
| 285 return new GeneratedFile(path, () { | 298 return new GeneratedFile(path, () { |
| 286 CodegenJavaVisitor visitor = createVisitor(readApi()); | 299 CodegenJavaVisitor visitor = createVisitor(readApi()); |
| 287 return visitor.collectCode(visitor.visitApi); | 300 return visitor.collectCode(visitor.visitApi); |
| 288 }); | 301 }); |
| 289 } | 302 } |
| OLD | NEW |