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