Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(242)

Side by Side Diff: runtime/lib/mirrors_impl.dart

Issue 126823004: Add TypeMirror.isSubtypeOf, TypeMirror.isAssignableTo, ClassMirror.isSubclassOf (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 // VM-specific implementation of the dart:mirrors library. 5 // VM-specific implementation of the dart:mirrors library.
6 6
7 import "dart:collection"; 7 import "dart:collection";
8 8
9 final emptyList = new UnmodifiableListView([]); 9 final emptyList = new UnmodifiableListView([]);
10 final emptyMap = new _UnmodifiableMapView({}); 10 final emptyMap = new _UnmodifiableMapView({});
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
101 buf.write(']'); 101 buf.write(']');
102 } 102 }
103 buf.write(') -> '); 103 buf.write(') -> ');
104 buf.write(_n(returnType.qualifiedName)); 104 buf.write(_n(returnType.qualifiedName));
105 return buf.toString(); 105 return buf.toString();
106 } 106 }
107 107
108 List _metadata(reflectee) 108 List _metadata(reflectee)
109 native 'DeclarationMirror_metadata'; 109 native 'DeclarationMirror_metadata';
110 110
111 bool _subtypeTest(Type a, Type b)
112 native 'TypeMirror_subtypeTest';
113
114 bool _moreSpecificTest(Type a, Type b)
115 native 'TypeMirror_moreSpecificTest';
116
111 class _LocalMirrorSystem extends MirrorSystem { 117 class _LocalMirrorSystem extends MirrorSystem {
112 final Map<Uri, LibraryMirror> libraries; 118 final Map<Uri, LibraryMirror> libraries;
113 final IsolateMirror isolate; 119 final IsolateMirror isolate;
114 120
115 _LocalMirrorSystem(List<LibraryMirror> libraries, this.isolate) 121 _LocalMirrorSystem(List<LibraryMirror> libraries, this.isolate)
116 : this.libraries = new Map<Uri, LibraryMirror>.fromIterable( 122 : this.libraries = new Map<Uri, LibraryMirror>.fromIterable(
117 libraries, key: (e) => e.uri); 123 libraries, key: (e) => e.uri);
118 124
119 TypeMirror _dynamicType = null; 125 TypeMirror _dynamicType = null;
120 TypeMirror get dynamicType { 126 TypeMirror get dynamicType {
(...skipping 632 matching lines...) Expand 10 before | Expand all | Expand 10 after
753 759
754 bool operator ==(other) { 760 bool operator ==(other) {
755 return this.runtimeType == other.runtimeType && 761 return this.runtimeType == other.runtimeType &&
756 this._reflectee == other._reflectee && 762 this._reflectee == other._reflectee &&
757 this._reflectedType == other._reflectedType && 763 this._reflectedType == other._reflectedType &&
758 this._isGenericDeclaration == other._isGenericDeclaration; 764 this._isGenericDeclaration == other._isGenericDeclaration;
759 } 765 }
760 766
761 int get hashCode => simpleName.hashCode; 767 int get hashCode => simpleName.hashCode;
762 768
769 bool isSubtypeOf(TypeMirror other) {
770 if (other == currentMirrorSystem().dynamicType) return true;
771 if (other == currentMirrorSystem().voidType) return false;
772 return _subtypeTest(_reflectedType, other._reflectedType);
773 }
774
775 bool isAssignableTo(TypeMirror other) {
776 if (other == currentMirrorSystem().dynamicType) return true;
777 if (other == currentMirrorSystem().voidType) return false;
778 return _moreSpecificTest(_reflectedType, other._reflectedType)
779 || _moreSpecificTest(other._reflectedType, _reflectedType);
780 }
781
782 bool isSubclassOf(ClassMirror other) {
783 if (other is! ClassMirror) throw new ArgumentError(other);
784 ClassMirror otherDeclaration = other.originalDeclaration;
785 ClassMirror c = this;
786 while (c != null) {
787 c = c.originalDeclaration;
788 if (c == otherDeclaration) return true;
789 c = c.superclass;
790 }
791 return false;
792 }
793
763 static _library(reflectee) 794 static _library(reflectee)
764 native "ClassMirror_library"; 795 native "ClassMirror_library";
765 796
766 static _supertype(reflectedType) 797 static _supertype(reflectedType)
767 native "ClassMirror_supertype"; 798 native "ClassMirror_supertype";
768 799
769 static _supertypeInstantiated(reflectedType) 800 static _supertypeInstantiated(reflectedType)
770 native "ClassMirror_supertype_instantiated"; 801 native "ClassMirror_supertype_instantiated";
771 802
772 static _nativeInterfaces(reflectedType) 803 static _nativeInterfaces(reflectedType)
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
925 956
926 TypeMirror _upperBound = null; 957 TypeMirror _upperBound = null;
927 TypeMirror get upperBound { 958 TypeMirror get upperBound {
928 if (_upperBound == null) { 959 if (_upperBound == null) {
929 _upperBound = reflectType(_TypeVariableMirror_upper_bound(_reflectee)); 960 _upperBound = reflectType(_TypeVariableMirror_upper_bound(_reflectee));
930 } 961 }
931 return _upperBound; 962 return _upperBound;
932 } 963 }
933 964
934 bool get hasReflectedType => false; 965 bool get hasReflectedType => false;
935 Type get reflectedType => throw new UnsupportedError() ; 966 Type get reflectedType => throw new UnsupportedError();
967 Type get _reflectedType => _reflectee;
936 968
937 List<TypeVariableMirror> get typeVariables => emptyList; 969 List<TypeVariableMirror> get typeVariables => emptyList;
938 List<TypeMirror> get typeArguments => emptyList; 970 List<TypeMirror> get typeArguments => emptyList;
939 971
940 bool get isOriginalDeclaration => true; 972 bool get isOriginalDeclaration => true;
941 TypeMirror get originalDeclaration => this; 973 TypeMirror get originalDeclaration => this;
942 974
943 String toString() => "TypeVariableMirror on '${_n(simpleName)}'"; 975 String toString() => "TypeVariableMirror on '${_n(simpleName)}'";
944 976
945 operator ==(other) { 977 operator ==(other) {
946 return other is TypeVariableMirror 978 return other is TypeVariableMirror
947 && simpleName == other.simpleName 979 && simpleName == other.simpleName
948 && owner == other.owner; 980 && owner == other.owner;
949 } 981 }
950 int get hashCode => simpleName.hashCode; 982 int get hashCode => simpleName.hashCode;
951 983
984 bool isSubtypeOf(TypeMirror other) {
985 if (other == currentMirrorSystem().dynamicType) return true;
986 if (other == currentMirrorSystem().voidType) return false;
987 return _subtypeTest(_reflectedType, other._reflectedType);
988 }
989
990 bool isAssignableTo(TypeMirror other) {
991 if (other == currentMirrorSystem().dynamicType) return true;
992 if (other == currentMirrorSystem().voidType) return false;
993 return _moreSpecificTest(_reflectedType, other._reflectedType)
994 || _moreSpecificTest(other._reflectedType, _reflectedType);
995 }
996
952 static DeclarationMirror _TypeVariableMirror_owner(reflectee) 997 static DeclarationMirror _TypeVariableMirror_owner(reflectee)
953 native "TypeVariableMirror_owner"; 998 native "TypeVariableMirror_owner";
954 999
955 static Type _TypeVariableMirror_upper_bound(reflectee) 1000 static Type _TypeVariableMirror_upper_bound(reflectee)
956 native "TypeVariableMirror_upper_bound"; 1001 native "TypeVariableMirror_upper_bound";
957 } 1002 }
958 1003
959 1004
960 class _LocalTypedefMirror extends _LocalDeclarationMirror 1005 class _LocalTypedefMirror extends _LocalDeclarationMirror
961 implements TypedefMirror { 1006 implements TypedefMirror {
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
1029 } else { 1074 } else {
1030 _typeArguments = new UnmodifiableListView( 1075 _typeArguments = new UnmodifiableListView(
1031 _LocalClassMirror._computeTypeArguments(_reflectedType)); 1076 _LocalClassMirror._computeTypeArguments(_reflectedType));
1032 } 1077 }
1033 } 1078 }
1034 return _typeArguments; 1079 return _typeArguments;
1035 } 1080 }
1036 1081
1037 String toString() => "TypedefMirror on '${_n(simpleName)}'"; 1082 String toString() => "TypedefMirror on '${_n(simpleName)}'";
1038 1083
1084 bool isSubtypeOf(TypeMirror other) {
1085 if (other == currentMirrorSystem().dynamicType) return true;
1086 if (other == currentMirrorSystem().voidType) return false;
1087 return _subtypeTest(_reflectedType, other._reflectedType);
1088 }
1089
1090 bool isAssignableTo(TypeMirror other) {
1091 if (other == currentMirrorSystem().dynamicType) return true;
1092 if (other == currentMirrorSystem().voidType) return false;
1093 return _moreSpecificTest(_reflectedType, other._reflectedType)
1094 || _moreSpecificTest(other._reflectedType, _reflectedType);
1095 }
1096
1039 static _nativeReferent(reflectedType) 1097 static _nativeReferent(reflectedType)
1040 native "TypedefMirror_referent"; 1098 native "TypedefMirror_referent";
1041 1099
1042 static _nativeDeclaration(reflectedType) 1100 static _nativeDeclaration(reflectedType)
1043 native "TypedefMirror_declaration"; 1101 native "TypedefMirror_declaration";
1044 } 1102 }
1045 1103
1046 Symbol _asSetter(Symbol getter, LibraryMirror library) { 1104 Symbol _asSetter(Symbol getter, LibraryMirror library) {
1047 var unwrapped = MirrorSystem.getName(getter); 1105 var unwrapped = MirrorSystem.getName(getter);
1048 return MirrorSystem.getSymbol('${unwrapped}=', library); 1106 return MirrorSystem.getSymbol('${unwrapped}=', library);
(...skipping 385 matching lines...) Expand 10 before | Expand all | Expand 10 after
1434 bool operator ==(other) { 1492 bool operator ==(other) {
1435 if (other is! _SpecialTypeMirror) { 1493 if (other is! _SpecialTypeMirror) {
1436 return false; 1494 return false;
1437 } 1495 }
1438 return this.simpleName == other.simpleName; 1496 return this.simpleName == other.simpleName;
1439 } 1497 }
1440 1498
1441 int get hashCode => simpleName.hashCode; 1499 int get hashCode => simpleName.hashCode;
1442 1500
1443 String toString() => "TypeMirror on '${_n(simpleName)}'"; 1501 String toString() => "TypeMirror on '${_n(simpleName)}'";
1502
1503 bool isSubtypeOf(TypeMirror other) {
1504 return this == other ||
1505 (other is _SpecialTypeMirror && other.simpleName == #dynamic);
1506 }
1507
1508 bool isAssignableTo(TypeMirror other) {
1509 return simpleName == #dynamic || other is _SpecialTypeMirror;
1510 }
1444 } 1511 }
1445 1512
1446 class _Mirrors { 1513 class _Mirrors {
1447 static MirrorSystem _currentMirrorSystem = null; 1514 static MirrorSystem _currentMirrorSystem = null;
1448 1515
1449 // Creates a new local MirrorSystem. 1516 // Creates a new local MirrorSystem.
1450 static MirrorSystem makeLocalMirrorSystem() 1517 static MirrorSystem makeLocalMirrorSystem()
1451 native 'Mirrors_makeLocalMirrorSystem'; 1518 native 'Mirrors_makeLocalMirrorSystem';
1452 1519
1453 // The MirrorSystem for the current isolate. 1520 // The MirrorSystem for the current isolate.
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
1490 if (typeMirror == null) { 1557 if (typeMirror == null) {
1491 typeMirror = makeLocalTypeMirror(key); 1558 typeMirror = makeLocalTypeMirror(key);
1492 _instanitationCache[key] = typeMirror; 1559 _instanitationCache[key] = typeMirror;
1493 if (typeMirror is ClassMirror && !typeMirror._isGeneric) { 1560 if (typeMirror is ClassMirror && !typeMirror._isGeneric) {
1494 _declarationCache[key] = typeMirror; 1561 _declarationCache[key] = typeMirror;
1495 } 1562 }
1496 } 1563 }
1497 return typeMirror; 1564 return typeMirror;
1498 } 1565 }
1499 } 1566 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698