|
5 | 5 | import 'dart:mirrors'; |
6 | 6 |
|
7 | 7 | import 'package:analyzer/dart/ast/ast.dart'; |
| 8 | +import 'package:analyzer/dart/constant/value.dart'; |
8 | 9 | import 'package:analyzer/dart/element/element.dart'; |
9 | 10 | import 'package:analyzer/dart/element/type.dart'; |
10 | 11 | import 'package:analyzer/src/dart/element/element.dart'; |
11 | | -import 'package:analyzer/src/generated/constant.dart'; |
12 | | -import 'package:analyzer/src/generated/utilities_dart.dart'; |
13 | 12 |
|
14 | 13 | import 'constants.dart'; |
15 | 14 | import 'type_checker.dart'; |
16 | 15 |
|
17 | 16 | dynamic instantiateAnnotation(ElementAnnotation annotation) { |
18 | | - var annotationObject = annotation.constantValue; |
| 17 | + var annotationObject = annotation.computeConstantValue(); |
19 | 18 | try { |
20 | 19 | return _getValue(annotation.constantValue); |
21 | 20 | } on CannotCreateFromAnnotationException catch (e) { |
@@ -131,57 +130,23 @@ class CannotCreateFromAnnotationException implements Exception { |
131 | 130 | } |
132 | 131 | } |
133 | 132 |
|
134 | | -final _cannotCreate = new Object(); |
| 133 | +dynamic _createFromConstructor(ConstructorElement ctor, DartObject obj) { |
| 134 | + var reader = new ConstantReader(obj); |
| 135 | + var revivable = reader.revive(); |
135 | 136 |
|
136 | | -dynamic _createFromConstructor( |
137 | | - ConstructorElementImpl ctor, DartObjectImpl obj) { |
138 | | - var positionalArgs = []; |
139 | | - var namedArgs = <Symbol, dynamic>{}; |
140 | | - for (var p in ctor.parameters) { |
141 | | - var paramName = p.name; |
142 | | - String fieldName; |
143 | | - if (p is FieldFormalParameterElement) { |
144 | | - fieldName = p.name; |
145 | | - } else { |
146 | | - // Trying to find the relationship between the ctor argument name and the |
147 | | - // field assigned in the object. Then we can take the field value and |
148 | | - // set it as the argument value |
149 | | - |
150 | | - var initializer = ctor.constantInitializers.singleWhere((ci) { |
151 | | - var expression = (ci as ConstructorFieldInitializer).expression; |
152 | | - if (expression is SimpleIdentifier) { |
153 | | - return expression.name == paramName; |
154 | | - } |
| 137 | + var positionalArgs = |
| 138 | + revivable.positionalArguments.map((pa) => _getValue(pa)).toList(); |
155 | 139 |
|
156 | | - if (expression is NullLiteral) { |
157 | | - return false; |
158 | | - } |
159 | | - |
160 | | - throw new UnsupportedError( |
161 | | - "${ctor.enclosingElement.type} is too complex. Initializers of " |
162 | | - "type '${expression.runtimeType}' are not supported."); |
163 | | - }) as ConstructorFieldInitializer; |
164 | | - |
165 | | - // get the field value now |
166 | | - fieldName = initializer.fieldName.name; |
167 | | - } |
168 | | - |
169 | | - var fieldObjectImpl = obj.fields[fieldName]; |
170 | | - if (p.parameterKind == ParameterKind.NAMED) { |
171 | | - namedArgs[new Symbol(p.name)] = _getValue(fieldObjectImpl); |
172 | | - } else { |
173 | | - positionalArgs.add(_getValue(fieldObjectImpl)); |
174 | | - } |
175 | | - } |
176 | | - |
177 | | - var ctorName = new Symbol(ctor.name ?? ''); |
| 140 | + var namedArgs = <Symbol, dynamic>{}; |
| 141 | + revivable.namedArguments.forEach((k, v) { |
| 142 | + namedArgs[new Symbol(k)] = _getValue(v); |
| 143 | + }); |
178 | 144 |
|
179 | 145 | var declarationMirror = |
180 | 146 | _getDeclarationMirrorFromType(ctor.enclosingElement.type) as ClassMirror; |
181 | 147 |
|
182 | | - // figure out which ctor was used! |
183 | | - var instanceMirror = |
184 | | - declarationMirror.newInstance(ctorName, positionalArgs, namedArgs); |
| 148 | + var instanceMirror = declarationMirror.newInstance( |
| 149 | + new Symbol(revivable.accessor), positionalArgs, namedArgs); |
185 | 150 | return instanceMirror.reflectee; |
186 | 151 | } |
187 | 152 |
|
|
0 commit comments