OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 // This code was auto-generated, is not intended to be edited, and is subject to |
| 6 // significant change. Please see the README file for more information. |
| 7 |
| 8 library engine.testing.element_factory; |
| 9 |
| 10 import 'dart:collection'; |
| 11 import 'package:analyzer/src/generated/java_core.dart'; |
| 12 import 'package:analyzer/src/generated/utilities_dart.dart'; |
| 13 import 'package:analyzer/src/generated/ast.dart'; |
| 14 import 'package:analyzer/src/generated/constant.dart'; |
| 15 import 'package:analyzer/src/generated/source.dart'; |
| 16 import 'package:analyzer/src/generated/element.dart'; |
| 17 import 'package:analyzer/src/generated/engine.dart'; |
| 18 import 'package:analyzer/src/generated/resolver.dart'; |
| 19 |
| 20 /** |
| 21 * The class `ElementFactory` defines utility methods used to create elements fo
r testing |
| 22 * purposes. The elements that are created are complete in the sense that as muc
h of the element |
| 23 * model as can be created, given the provided information, has been created. |
| 24 */ |
| 25 class ElementFactory { |
| 26 /** |
| 27 * The element representing the class 'Object'. |
| 28 */ |
| 29 static ClassElementImpl _objectElement; |
| 30 |
| 31 static ClassElementImpl classElement(String typeName, InterfaceType superclass
Type, List<String> parameterNames) { |
| 32 ClassElementImpl element = new ClassElementImpl(typeName, 0); |
| 33 element.supertype = superclassType; |
| 34 InterfaceTypeImpl type = new InterfaceTypeImpl.con1(element); |
| 35 element.type = type; |
| 36 int count = parameterNames.length; |
| 37 if (count > 0) { |
| 38 List<TypeParameterElementImpl> typeParameters = new List<TypeParameterElem
entImpl>(count); |
| 39 List<TypeParameterTypeImpl> typeParameterTypes = new List<TypeParameterTyp
eImpl>(count); |
| 40 for (int i = 0; i < count; i++) { |
| 41 TypeParameterElementImpl typeParameter = new TypeParameterElementImpl(pa
rameterNames[i], 0); |
| 42 typeParameters[i] = typeParameter; |
| 43 typeParameterTypes[i] = new TypeParameterTypeImpl(typeParameter); |
| 44 typeParameter.type = typeParameterTypes[i]; |
| 45 } |
| 46 element.typeParameters = typeParameters; |
| 47 type.typeArguments = typeParameterTypes; |
| 48 } |
| 49 return element; |
| 50 } |
| 51 |
| 52 static ClassElementImpl classElement2(String typeName, List<String> parameterN
ames) => classElement(typeName, objectType, parameterNames); |
| 53 |
| 54 static CompilationUnitElementImpl compilationUnit(String fileName) { |
| 55 Source source = new NonExistingSource(fileName, UriKind.FILE_URI); |
| 56 CompilationUnitElementImpl unit = new CompilationUnitElementImpl(fileName); |
| 57 unit.source = source; |
| 58 return unit; |
| 59 } |
| 60 |
| 61 static ConstructorElementImpl constructorElement(ClassElement definingClass, S
tring name, bool isConst, List<DartType> argumentTypes) { |
| 62 DartType type = definingClass.type; |
| 63 ConstructorElementImpl constructor = name == null ? new ConstructorElementIm
pl("", -1) : new ConstructorElementImpl(name, 0); |
| 64 constructor.const2 = isConst; |
| 65 int count = argumentTypes.length; |
| 66 List<ParameterElement> parameters = new List<ParameterElement>(count); |
| 67 for (int i = 0; i < count; i++) { |
| 68 ParameterElementImpl parameter = new ParameterElementImpl("a${i}", i); |
| 69 parameter.type = argumentTypes[i]; |
| 70 parameter.parameterKind = ParameterKind.REQUIRED; |
| 71 parameters[i] = parameter; |
| 72 } |
| 73 constructor.parameters = parameters; |
| 74 constructor.returnType = type; |
| 75 FunctionTypeImpl constructorType = new FunctionTypeImpl.con1(constructor); |
| 76 constructor.type = constructorType; |
| 77 return constructor; |
| 78 } |
| 79 |
| 80 static ConstructorElementImpl constructorElement2(ClassElement definingClass,
String name, List<DartType> argumentTypes) => constructorElement(definingClass,
name, false, argumentTypes); |
| 81 |
| 82 static ClassElementImpl enumElement(TypeProvider typeProvider, String enumName
, List<String> constantNames) { |
| 83 // |
| 84 // Build the enum. |
| 85 // |
| 86 ClassElementImpl enumElement = new ClassElementImpl(enumName, -1); |
| 87 InterfaceTypeImpl enumType = new InterfaceTypeImpl.con1(enumElement); |
| 88 enumElement.type = enumType; |
| 89 enumElement.supertype = objectType; |
| 90 // |
| 91 // Populate the fields. |
| 92 // |
| 93 List<FieldElement> fields = new List<FieldElement>(); |
| 94 InterfaceType intType = typeProvider.intType; |
| 95 InterfaceType stringType = typeProvider.stringType; |
| 96 String indexFieldName = "index"; |
| 97 FieldElementImpl indexField = new FieldElementImpl(indexFieldName, -1); |
| 98 indexField.final2 = true; |
| 99 indexField.type = intType; |
| 100 fields.add(indexField); |
| 101 String nameFieldName = "_name"; |
| 102 FieldElementImpl nameField = new FieldElementImpl(nameFieldName, -1); |
| 103 nameField.final2 = true; |
| 104 nameField.type = stringType; |
| 105 fields.add(nameField); |
| 106 FieldElementImpl valuesField = new FieldElementImpl("values", -1); |
| 107 valuesField.static = true; |
| 108 valuesField.const3 = true; |
| 109 valuesField.type = typeProvider.listType.substitute4(<DartType> [enumType]); |
| 110 fields.add(valuesField); |
| 111 // |
| 112 // Build the enum constants. |
| 113 // |
| 114 int constantCount = constantNames.length; |
| 115 for (int i = 0; i < constantCount; i++) { |
| 116 String constantName = constantNames[i]; |
| 117 FieldElementImpl constantElement = new ConstFieldElementImpl.con2(constant
Name, -1); |
| 118 constantElement.static = true; |
| 119 constantElement.const3 = true; |
| 120 constantElement.type = enumType; |
| 121 HashMap<String, DartObjectImpl> fieldMap = new HashMap<String, DartObjectI
mpl>(); |
| 122 fieldMap[indexFieldName] = new DartObjectImpl(intType, new IntState(i)); |
| 123 fieldMap[nameFieldName] = new DartObjectImpl(stringType, new StringState(c
onstantName)); |
| 124 DartObjectImpl value = new DartObjectImpl(enumType, new GenericState(field
Map)); |
| 125 constantElement.evaluationResult = new ValidResult(value); |
| 126 fields.add(constantElement); |
| 127 } |
| 128 // |
| 129 // Finish building the enum. |
| 130 // |
| 131 enumElement.fields = new List.from(fields); |
| 132 // Client code isn't allowed to invoke the constructor, so we do not model i
t. |
| 133 return enumElement; |
| 134 } |
| 135 |
| 136 static ExportElementImpl exportFor(LibraryElement exportedLibrary, List<Namesp
aceCombinator> combinators) { |
| 137 ExportElementImpl spec = new ExportElementImpl(); |
| 138 spec.exportedLibrary = exportedLibrary; |
| 139 spec.combinators = combinators; |
| 140 return spec; |
| 141 } |
| 142 |
| 143 static FieldElementImpl fieldElement(String name, bool isStatic, bool isFinal,
bool isConst, DartType type) { |
| 144 FieldElementImpl field = new FieldElementImpl(name, 0); |
| 145 field.const3 = isConst; |
| 146 field.final2 = isFinal; |
| 147 field.static = isStatic; |
| 148 field.type = type; |
| 149 PropertyAccessorElementImpl getter = new PropertyAccessorElementImpl.forVari
able(field); |
| 150 getter.getter = true; |
| 151 getter.synthetic = true; |
| 152 getter.variable = field; |
| 153 getter.returnType = type; |
| 154 field.getter = getter; |
| 155 FunctionTypeImpl getterType = new FunctionTypeImpl.con1(getter); |
| 156 getter.type = getterType; |
| 157 if (!isConst && !isFinal) { |
| 158 PropertyAccessorElementImpl setter = new PropertyAccessorElementImpl.forVa
riable(field); |
| 159 setter.setter = true; |
| 160 setter.synthetic = true; |
| 161 setter.variable = field; |
| 162 setter.parameters = <ParameterElement> [requiredParameter2("_${name}", typ
e)]; |
| 163 setter.returnType = VoidTypeImpl.instance; |
| 164 setter.type = new FunctionTypeImpl.con1(setter); |
| 165 field.setter = setter; |
| 166 } |
| 167 return field; |
| 168 } |
| 169 |
| 170 static FieldFormalParameterElementImpl fieldFormalParameter(Identifier name) =
> new FieldFormalParameterElementImpl(name); |
| 171 |
| 172 static FunctionElementImpl functionElement(String functionName) => functionEle
ment4(functionName, null, null, null, null); |
| 173 |
| 174 static FunctionElementImpl functionElement2(String functionName, ClassElement
returnElement) => functionElement3(functionName, returnElement, null, null); |
| 175 |
| 176 static FunctionElementImpl functionElement3(String functionName, ClassElement
returnElement, List<ClassElement> normalParameters, List<ClassElement> optionalP
arameters) { |
| 177 // We don't create parameter elements because we don't have parameter names |
| 178 FunctionElementImpl functionElement = new FunctionElementImpl(functionName,
0); |
| 179 FunctionTypeImpl functionType = new FunctionTypeImpl.con1(functionElement); |
| 180 functionElement.type = functionType; |
| 181 // return type |
| 182 if (returnElement == null) { |
| 183 functionElement.returnType = VoidTypeImpl.instance; |
| 184 } else { |
| 185 functionElement.returnType = returnElement.type; |
| 186 } |
| 187 // parameters |
| 188 int normalCount = normalParameters == null ? 0 : normalParameters.length; |
| 189 int optionalCount = optionalParameters == null ? 0 : optionalParameters.leng
th; |
| 190 int totalCount = normalCount + optionalCount; |
| 191 List<ParameterElement> parameters = new List<ParameterElement>(totalCount); |
| 192 for (int i = 0; i < totalCount; i++) { |
| 193 ParameterElementImpl parameter = new ParameterElementImpl("a${i}", i); |
| 194 if (i < normalCount) { |
| 195 parameter.type = normalParameters[i].type; |
| 196 parameter.parameterKind = ParameterKind.REQUIRED; |
| 197 } else { |
| 198 parameter.type = optionalParameters[i - normalCount].type; |
| 199 parameter.parameterKind = ParameterKind.POSITIONAL; |
| 200 } |
| 201 parameters[i] = parameter; |
| 202 } |
| 203 functionElement.parameters = parameters; |
| 204 // done |
| 205 return functionElement; |
| 206 } |
| 207 |
| 208 static FunctionElementImpl functionElement4(String functionName, ClassElement
returnElement, List<ClassElement> normalParameters, List<String> names, List<Cla
ssElement> namedParameters) { |
| 209 FunctionElementImpl functionElement = new FunctionElementImpl(functionName,
0); |
| 210 FunctionTypeImpl functionType = new FunctionTypeImpl.con1(functionElement); |
| 211 functionElement.type = functionType; |
| 212 // parameters |
| 213 int normalCount = normalParameters == null ? 0 : normalParameters.length; |
| 214 int nameCount = names == null ? 0 : names.length; |
| 215 int typeCount = namedParameters == null ? 0 : namedParameters.length; |
| 216 if (names != null && nameCount != typeCount) { |
| 217 throw new IllegalStateException("The passed String[] and ClassElement[] ar
rays had different lengths."); |
| 218 } |
| 219 int totalCount = normalCount + nameCount; |
| 220 List<ParameterElement> parameters = new List<ParameterElement>(totalCount); |
| 221 for (int i = 0; i < totalCount; i++) { |
| 222 if (i < normalCount) { |
| 223 ParameterElementImpl parameter = new ParameterElementImpl("a${i}", i); |
| 224 parameter.type = normalParameters[i].type; |
| 225 parameter.parameterKind = ParameterKind.REQUIRED; |
| 226 parameters[i] = parameter; |
| 227 } else { |
| 228 ParameterElementImpl parameter = new ParameterElementImpl(names[i - norm
alCount], i); |
| 229 parameter.type = namedParameters[i - normalCount].type; |
| 230 parameter.parameterKind = ParameterKind.NAMED; |
| 231 parameters[i] = parameter; |
| 232 } |
| 233 } |
| 234 functionElement.parameters = parameters; |
| 235 // return type |
| 236 if (returnElement == null) { |
| 237 functionElement.returnType = VoidTypeImpl.instance; |
| 238 } else { |
| 239 functionElement.returnType = returnElement.type; |
| 240 } |
| 241 return functionElement; |
| 242 } |
| 243 |
| 244 static FunctionElementImpl functionElement5(String functionName, List<ClassEle
ment> normalParameters) => functionElement3(functionName, null, normalParameters
, null); |
| 245 |
| 246 static FunctionElementImpl functionElement6(String functionName, List<ClassEle
ment> normalParameters, List<ClassElement> optionalParameters) => functionElemen
t3(functionName, null, normalParameters, optionalParameters); |
| 247 |
| 248 static FunctionElementImpl functionElement7(String functionName, List<ClassEle
ment> normalParameters, List<String> names, List<ClassElement> namedParameters)
=> functionElement4(functionName, null, normalParameters, names, namedParameters
); |
| 249 |
| 250 static FunctionElementImpl functionElementWithParameters(String functionName,
DartType returnType, List<ParameterElement> parameters) { |
| 251 FunctionElementImpl functionElement = new FunctionElementImpl(functionName,
0); |
| 252 functionElement.returnType = returnType == null ? VoidTypeImpl.instance : re
turnType; |
| 253 functionElement.parameters = parameters; |
| 254 FunctionTypeImpl functionType = new FunctionTypeImpl.con1(functionElement); |
| 255 functionElement.type = functionType; |
| 256 return functionElement; |
| 257 } |
| 258 |
| 259 static ClassElementImpl get object { |
| 260 if (_objectElement == null) { |
| 261 _objectElement = classElement("Object", null, []); |
| 262 } |
| 263 return _objectElement; |
| 264 } |
| 265 |
| 266 static InterfaceType get objectType => object.type; |
| 267 |
| 268 static PropertyAccessorElementImpl getterElement(String name, bool isStatic, D
artType type) { |
| 269 FieldElementImpl field = new FieldElementImpl(name, -1); |
| 270 field.static = isStatic; |
| 271 field.synthetic = true; |
| 272 field.type = type; |
| 273 PropertyAccessorElementImpl getter = new PropertyAccessorElementImpl.forVari
able(field); |
| 274 getter.getter = true; |
| 275 getter.variable = field; |
| 276 getter.returnType = type; |
| 277 field.getter = getter; |
| 278 FunctionTypeImpl getterType = new FunctionTypeImpl.con1(getter); |
| 279 getter.type = getterType; |
| 280 return getter; |
| 281 } |
| 282 |
| 283 static HtmlElementImpl htmlUnit(AnalysisContext context, String fileName) { |
| 284 Source source = new NonExistingSource(fileName, UriKind.FILE_URI); |
| 285 HtmlElementImpl unit = new HtmlElementImpl(context, fileName); |
| 286 unit.source = source; |
| 287 return unit; |
| 288 } |
| 289 |
| 290 static ImportElementImpl importFor(LibraryElement importedLibrary, PrefixEleme
nt prefix, List<NamespaceCombinator> combinators) { |
| 291 ImportElementImpl spec = new ImportElementImpl(0); |
| 292 spec.importedLibrary = importedLibrary; |
| 293 spec.prefix = prefix; |
| 294 spec.combinators = combinators; |
| 295 return spec; |
| 296 } |
| 297 |
| 298 static LibraryElementImpl library(AnalysisContext context, String libraryName)
{ |
| 299 String fileName = "/${libraryName}.dart"; |
| 300 CompilationUnitElementImpl unit = compilationUnit(fileName); |
| 301 LibraryElementImpl library = new LibraryElementImpl(context, libraryName, 0)
; |
| 302 library.definingCompilationUnit = unit; |
| 303 return library; |
| 304 } |
| 305 |
| 306 static LocalVariableElementImpl localVariableElement(Identifier name) => new L
ocalVariableElementImpl.forNode(name); |
| 307 |
| 308 static LocalVariableElementImpl localVariableElement2(String name) => new Loca
lVariableElementImpl(name, 0); |
| 309 |
| 310 static MethodElementImpl methodElement(String methodName, DartType returnType,
List<DartType> argumentTypes) { |
| 311 MethodElementImpl method = new MethodElementImpl(methodName, 0); |
| 312 int count = argumentTypes.length; |
| 313 List<ParameterElement> parameters = new List<ParameterElement>(count); |
| 314 for (int i = 0; i < count; i++) { |
| 315 ParameterElementImpl parameter = new ParameterElementImpl("a${i}", i); |
| 316 parameter.type = argumentTypes[i]; |
| 317 parameter.parameterKind = ParameterKind.REQUIRED; |
| 318 parameters[i] = parameter; |
| 319 } |
| 320 method.parameters = parameters; |
| 321 method.returnType = returnType; |
| 322 FunctionTypeImpl methodType = new FunctionTypeImpl.con1(method); |
| 323 method.type = methodType; |
| 324 return method; |
| 325 } |
| 326 |
| 327 static MethodElementImpl methodElementWithParameters(String methodName, List<D
artType> typeArguments, DartType returnType, List<ParameterElement> parameters)
{ |
| 328 MethodElementImpl method = new MethodElementImpl(methodName, 0); |
| 329 method.parameters = parameters; |
| 330 method.returnType = returnType; |
| 331 FunctionTypeImpl methodType = new FunctionTypeImpl.con1(method); |
| 332 methodType.typeArguments = typeArguments; |
| 333 method.type = methodType; |
| 334 return method; |
| 335 } |
| 336 |
| 337 static ParameterElementImpl namedParameter(String name) { |
| 338 ParameterElementImpl parameter = new ParameterElementImpl(name, 0); |
| 339 parameter.parameterKind = ParameterKind.NAMED; |
| 340 return parameter; |
| 341 } |
| 342 |
| 343 static ParameterElementImpl namedParameter2(String name, DartType type) { |
| 344 ParameterElementImpl parameter = new ParameterElementImpl(name, 0); |
| 345 parameter.parameterKind = ParameterKind.NAMED; |
| 346 parameter.type = type; |
| 347 return parameter; |
| 348 } |
| 349 |
| 350 static ParameterElementImpl positionalParameter(String name) { |
| 351 ParameterElementImpl parameter = new ParameterElementImpl(name, 0); |
| 352 parameter.parameterKind = ParameterKind.POSITIONAL; |
| 353 return parameter; |
| 354 } |
| 355 |
| 356 static ParameterElementImpl positionalParameter2(String name, DartType type) { |
| 357 ParameterElementImpl parameter = new ParameterElementImpl(name, 0); |
| 358 parameter.parameterKind = ParameterKind.POSITIONAL; |
| 359 parameter.type = type; |
| 360 return parameter; |
| 361 } |
| 362 |
| 363 static PrefixElementImpl prefix(String name) => new PrefixElementImpl(name, 0)
; |
| 364 |
| 365 static ParameterElementImpl requiredParameter(String name) { |
| 366 ParameterElementImpl parameter = new ParameterElementImpl(name, 0); |
| 367 parameter.parameterKind = ParameterKind.REQUIRED; |
| 368 return parameter; |
| 369 } |
| 370 |
| 371 static ParameterElementImpl requiredParameter2(String name, DartType type) { |
| 372 ParameterElementImpl parameter = new ParameterElementImpl(name, 0); |
| 373 parameter.parameterKind = ParameterKind.REQUIRED; |
| 374 parameter.type = type; |
| 375 return parameter; |
| 376 } |
| 377 |
| 378 static PropertyAccessorElementImpl setterElement(String name, bool isStatic, D
artType type) { |
| 379 FieldElementImpl field = new FieldElementImpl(name, -1); |
| 380 field.static = isStatic; |
| 381 field.synthetic = true; |
| 382 field.type = type; |
| 383 PropertyAccessorElementImpl getter = new PropertyAccessorElementImpl.forVari
able(field); |
| 384 getter.getter = true; |
| 385 getter.variable = field; |
| 386 getter.returnType = type; |
| 387 field.getter = getter; |
| 388 FunctionTypeImpl getterType = new FunctionTypeImpl.con1(getter); |
| 389 getter.type = getterType; |
| 390 ParameterElementImpl parameter = requiredParameter2("a", type); |
| 391 PropertyAccessorElementImpl setter = new PropertyAccessorElementImpl.forVari
able(field); |
| 392 setter.setter = true; |
| 393 setter.synthetic = true; |
| 394 setter.variable = field; |
| 395 setter.parameters = <ParameterElement> [parameter]; |
| 396 setter.returnType = VoidTypeImpl.instance; |
| 397 setter.type = new FunctionTypeImpl.con1(setter); |
| 398 field.setter = setter; |
| 399 return setter; |
| 400 } |
| 401 |
| 402 static TopLevelVariableElementImpl topLevelVariableElement(Identifier name) =>
new TopLevelVariableElementImpl.forNode(name); |
| 403 |
| 404 static TopLevelVariableElementImpl topLevelVariableElement2(String name) => to
pLevelVariableElement3(name, false, false, null); |
| 405 |
| 406 static TopLevelVariableElementImpl topLevelVariableElement3(String name, bool
isConst, bool isFinal, DartType type) { |
| 407 TopLevelVariableElementImpl variable = new TopLevelVariableElementImpl(name,
-1); |
| 408 variable.const3 = isConst; |
| 409 variable.final2 = isFinal; |
| 410 variable.synthetic = true; |
| 411 PropertyAccessorElementImpl getter = new PropertyAccessorElementImpl.forVari
able(variable); |
| 412 getter.getter = true; |
| 413 getter.synthetic = true; |
| 414 getter.variable = variable; |
| 415 getter.returnType = type; |
| 416 variable.getter = getter; |
| 417 FunctionTypeImpl getterType = new FunctionTypeImpl.con1(getter); |
| 418 getter.type = getterType; |
| 419 if (!isFinal) { |
| 420 PropertyAccessorElementImpl setter = new PropertyAccessorElementImpl.forVa
riable(variable); |
| 421 setter.setter = true; |
| 422 setter.static = true; |
| 423 setter.synthetic = true; |
| 424 setter.variable = variable; |
| 425 setter.parameters = <ParameterElement> [requiredParameter2("_${name}", typ
e)]; |
| 426 setter.returnType = VoidTypeImpl.instance; |
| 427 setter.type = new FunctionTypeImpl.con1(setter); |
| 428 variable.setter = setter; |
| 429 } |
| 430 return variable; |
| 431 } |
| 432 } |
OLD | NEW |