22// for details. All rights reserved. Use of this source code is governed by a 
33// BSD-style license that can be found in the LICENSE file. 
44
5- library  source_gen.annotation;
6- 
7- import  'dart:io' ;
85import  'dart:mirrors' ;
96
107import  'package:analyzer/dart/ast/ast.dart' ;
118import  'package:analyzer/dart/element/element.dart' ;
129import  'package:analyzer/dart/element/type.dart' ;
1310import  'package:analyzer/src/dart/element/element.dart' ;
1411import  'package:analyzer/src/generated/constant.dart' ;
15- import  'package:analyzer/src/generated/resolver.dart' ;
1612import  'package:analyzer/src/generated/utilities_dart.dart' ;
17- import  'package:path/path.dart'  as  p;
13+ 
14+ import  'constants.dart' ;
15+ import  'type_checker.dart' ;
1816
1917dynamic  instantiateAnnotation (ElementAnnotation  annotation) {
2018  var  annotationObject =  annotation.constantValue;
2119  try  {
22-     return  _getValue (annotationObject,  annotation.element.context.typeProvider );
20+     return  _getValue (annotation.constantValue );
2321  } on  CannotCreateFromAnnotationException  catch  (e) {
2422    if  (e.innerException !=  null ) {
2523      // If there was a issue creating a nested object, there's not much we 
@@ -51,33 +49,35 @@ dynamic instantiateAnnotation(ElementAnnotation annotation) {
5149      "${valueDeclaration .runtimeType }." );
5250}
5351
54- dynamic  _getValue (DartObject  object, TypeProvider  typeProvider) {
55-   if  (object.isNull) {
52+ dynamic  _getValue (DartObject  object) {
53+   var  reader =  new  ConstantReader (object);
54+ 
55+   if  (reader.isNull) {
5656    return  null ;
5757  }
5858
59-   if  (object.type  ==  typeProvider.boolType ) {
60-     return  object. toBoolValue () ;
59+   if  (reader.isBool ) {
60+     return  reader.boolValue ;
6161  }
6262
63-   if  (object.type  ==  typeProvider.intType ) {
64-     return  object. toIntValue () ;
63+   if  (reader.isInt ) {
64+     return  reader.intValue ;
6565  }
6666
67-   if  (object.type  ==  typeProvider.stringType ) {
68-     return  object. toStringValue () ;
67+   if  (reader.isString ) {
68+     return  reader.stringValue ;
6969  }
7070
71-   if  (object.type  ==  typeProvider.doubleType ) {
72-     return  object. toDoubleValue () ;
71+   if  (reader.isDouble ) {
72+     return  reader.doubleValue ;
7373  }
7474
75-   if  (object.type  ==  typeProvider.symbolType ) {
76-     return  new   Symbol (object. toSymbolValue ()) ;
75+   if  (reader.isSymbol ) {
76+     return  reader.symbolValue ;
7777  }
7878
79-   if  (object.type  ==  typeProvider.typeType ) {
80-     var  typeData =  object. toTypeValue () ;
79+   if  (reader.isType ) {
80+     var  typeData =  reader.typeValue ;
8181
8282    if  (typeData is  InterfaceType ) {
8383      var  declarationMirror = 
@@ -91,20 +91,17 @@ dynamic _getValue(DartObject object, TypeProvider typeProvider) {
9191  }
9292
9393  try  {
94-     var  listValue =  object.toListValue ();
95-     if  (listValue !=  null ) {
96-       return  listValue
97-           .map ((DartObject  element) =>  _getValue (element, typeProvider))
98-           .toList ();
99-     }
94+     if  (reader.isList) {
95+       var  listValue =  reader.listValue;
10096
101-     var  mapValue =  object.toMapValue ();
102-     if  (mapValue !=  null ) {
97+       return  listValue.map ((DartObject  element) =>  _getValue (element)).toList ();
98+     } else  if  (reader.isMap) {
99+       var  mapValue =  reader.mapValue;
103100      var  result =  {};
104101      mapValue.forEach ((DartObject  key, DartObject  value) {
105-         dynamic  mappedKey =  _getValue (key, typeProvider );
102+         dynamic  mappedKey =  _getValue (key);
106103        if  (mappedKey !=  null ) {
107-           result[mappedKey] =  _getValue (value, typeProvider );
104+           result[mappedKey] =  _getValue (value);
108105        }
109106      });
110107      return  result;
@@ -169,13 +166,11 @@ dynamic _createFromConstructor(
169166      fieldName =  initializer.fieldName.name;
170167    }
171168
172-     var  typeProvider =  ctor.context.typeProvider;
173- 
174169    var  fieldObjectImpl =  obj.fields[fieldName];
175170    if  (p.parameterKind ==  ParameterKind .NAMED ) {
176-       namedArgs[new  Symbol (p.name)] =  _getValue (fieldObjectImpl, typeProvider );
171+       namedArgs[new  Symbol (p.name)] =  _getValue (fieldObjectImpl);
177172    } else  {
178-       positionalArgs.add (_getValue (fieldObjectImpl, typeProvider ));
173+       positionalArgs.add (_getValue (fieldObjectImpl));
179174    }
180175  }
181176
@@ -225,81 +220,7 @@ bool matchAnnotation(Type annotationType, ElementAnnotation annotation) {
225220        'Could not determine type of annotation. Are you missing a dependency?' );
226221  }
227222
228-   return  matchTypes (annotationType, annotationValueType);
229- }
230- 
231- /// Checks whether [annotationValueType]  is equivalent to [annotationType] . 
232- /// 
233- /// Currently, this uses mirrors to compare the name and library uri of the two 
234- /// types. 
235- bool  matchTypes (Type  annotationType, ParameterizedType  annotationValueType) {
236-   var  classMirror =  reflectClass (annotationType);
237-   var  classMirrorSymbol =  classMirror.simpleName;
238- 
239-   var  annTypeName =  annotationValueType.name;
240-   var  annotationTypeSymbol =  new  Symbol (annTypeName);
241- 
242-   if  (classMirrorSymbol !=  annotationTypeSymbol) {
243-     return  false ;
244-   }
245- 
246-   var  annotationLibSource =  annotationValueType.element.library.source;
247- 
248-   var  libOwnerUri =  (classMirror.owner as  LibraryMirror ).uri;
249-   var  annotationLibSourceUri =  annotationLibSource.uri;
250- 
251-   if  (annotationLibSourceUri.scheme ==  'file'  && 
252-       libOwnerUri.scheme ==  'package' ) {
253-     // try to turn the libOwnerUri into a file uri 
254-     libOwnerUri =  _fileUriFromPackageUri (libOwnerUri);
255-   } else  if  (annotationLibSourceUri.scheme ==  'asset'  && 
256-       libOwnerUri.scheme ==  'package' ) {
257-     // try to turn the libOwnerUri into a asset uri 
258-     libOwnerUri =  _assetUriFromPackageUri (libOwnerUri);
259-   }
260- 
261-   return  annotationLibSource.uri ==  libOwnerUri;
262- }
263- 
264- Uri  _fileUriFromPackageUri (Uri  libraryPackageUri) {
265-   assert (libraryPackageUri.scheme ==  'package' );
266- 
267-   var  fullLibraryPath =  p.join (_packageRoot, libraryPackageUri.path);
268- 
269-   var  file =  new  File (fullLibraryPath);
270- 
271-   assert (file.existsSync ());
272- 
273-   var  normalPath =  file.resolveSymbolicLinksSync ();
274- 
275-   return  new  Uri .file (normalPath);
276- }
277- 
278- Uri  _assetUriFromPackageUri (Uri  libraryPackageUri) {
279-   assert (libraryPackageUri.scheme ==  'package' );
280-   var  originalSegments =  libraryPackageUri.pathSegments;
281-   var  newSegments =  [originalSegments[0 ]]
282-     ..add ('lib' )
283-     ..addAll (originalSegments.getRange (1 , originalSegments.length));
223+   var  runtimeChecker =  new  TypeChecker .fromRuntime (annotationType);
284224
285-   return  new   Uri (scheme :   'asset' , pathSegments :  newSegments );
225+   return  runtimeChecker. isExactlyType (annotationValueType );
286226}
287- 
288- String  get  _packageRoot {
289-   if  (_packageRootCache ==  null ) {
290-     var  dir =  Platform .packageRoot;
291- 
292-     if  (dir.isEmpty) {
293-       dir =  p.join (p.current, 'packages' );
294-     }
295- 
296-     // Handle the case where we're running via pub and dir is a file: URI 
297-     dir =  p.prettyUri (dir);
298- 
299-     assert (FileSystemEntity .isDirectorySync (dir));
300-     _packageRootCache =  dir;
301-   }
302-   return  _packageRootCache;
303- }
304- 
305- String  _packageRootCache;
0 commit comments