mirror of
https://github.com/dart-lang/sdk
synced 2024-09-19 14:51:30 +00:00
Hoist hasReflectedType and reflectedType from ClassMirror to TypeMirror.
R=gbracha@google.com, regis@google.com Review URL: https://codereview.chromium.org//183853022 git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@33291 260f80e4-7a28-3924-810f-c04153c831b5
This commit is contained in:
parent
75f9f5bfa3
commit
d3a92a0f3d
|
@ -1054,7 +1054,9 @@ class _LocalTypeVariableMirror extends _LocalDeclarationMirror
|
|||
}
|
||||
|
||||
bool get hasReflectedType => false;
|
||||
Type get reflectedType => throw new UnsupportedError();
|
||||
Type get reflectedType {
|
||||
throw new UnsupportedError('Type variables have no reflected type');
|
||||
}
|
||||
Type get _reflectedType => _reflectee;
|
||||
|
||||
List<TypeVariableMirror> get typeVariables => emptyList;
|
||||
|
@ -1131,6 +1133,15 @@ class _LocalTypedefMirror extends _LocalDeclarationMirror
|
|||
return _referent;
|
||||
}
|
||||
|
||||
bool get hasReflectedType => !_isGenericDeclaration;
|
||||
Type get reflectedType {
|
||||
if (!hasReflectedType) {
|
||||
throw new UnsupportedError(
|
||||
"Declarations of generics have no reflected type");
|
||||
}
|
||||
return _reflectedType;
|
||||
}
|
||||
|
||||
bool get isOriginalDeclaration => !_isGeneric || _isGenericDeclaration;
|
||||
|
||||
TypedefMirror get originalDeclaration {
|
||||
|
@ -1538,6 +1549,12 @@ class _SpecialTypeMirror extends _LocalMirror
|
|||
|
||||
List<InstanceMirror> get metadata => emptyList;
|
||||
|
||||
bool get hasReflectedType => simpleName == #dynamic;
|
||||
Type get reflectedType {
|
||||
if (simpleName == #dynamic) return dynamic;
|
||||
throw new UnsupportedError("void has no reflected type");
|
||||
}
|
||||
|
||||
List<TypeVariableMirror> get typeVariables => emptyList;
|
||||
List<TypeMirror> get typeArguments => emptyList;
|
||||
|
||||
|
|
|
@ -33,6 +33,10 @@ abstract class Dart2JsTypeMirror
|
|||
return mirrorSystem._getLibrary(_type.element.getLibrary());
|
||||
}
|
||||
|
||||
bool get hasReflectedType => throw new UnimplementedError();
|
||||
|
||||
Type get reflectedType => throw new UnimplementedError();
|
||||
|
||||
bool get isOriginalDeclaration => true;
|
||||
|
||||
TypeMirror get originalDeclaration => this;
|
||||
|
|
|
@ -2367,6 +2367,10 @@ class JsTypedefMirror extends JsDeclarationMirror implements TypedefMirror {
|
|||
|
||||
String get _prettyName => 'TypedefMirror';
|
||||
|
||||
bool get hasReflectedType => throw new UnimplementedError();
|
||||
|
||||
Type get reflectedType => throw new UnimplementedError();
|
||||
|
||||
// TODO(ahe): Implement this method.
|
||||
List<TypeVariableMirror> get typeVariables => throw new UnimplementedError();
|
||||
|
||||
|
|
|
@ -614,6 +614,19 @@ abstract class LibraryMirror implements DeclarationMirror, ObjectMirror {
|
|||
* function type or type variable.
|
||||
*/
|
||||
abstract class TypeMirror implements DeclarationMirror {
|
||||
/**
|
||||
* Returns true if this mirror reflects dynamic, a non-generic class or
|
||||
* typedef, or an instantiated generic class or typedef in the current
|
||||
* isolate. Otherwise, returns false.
|
||||
*/
|
||||
bool get hasReflectedType;
|
||||
|
||||
/**
|
||||
* If [:hasReflectedType:] returns true, returns the corresponding [Type].
|
||||
* Otherwise, an [UnsupportedError] is thrown.
|
||||
*/
|
||||
Type get reflectedType;
|
||||
|
||||
/**
|
||||
* An immutable list with mirrors for all type variables for this type.
|
||||
*
|
||||
|
@ -682,18 +695,6 @@ abstract class TypeMirror implements DeclarationMirror {
|
|||
* A [ClassMirror] reflects a Dart language class.
|
||||
*/
|
||||
abstract class ClassMirror implements TypeMirror, ObjectMirror {
|
||||
/**
|
||||
* Returns true if this mirror reflects a non-generic class or an instantiated
|
||||
* generic class in the current isolate. Otherwise, returns false.
|
||||
*/
|
||||
bool get hasReflectedType;
|
||||
|
||||
/**
|
||||
* If [:hasReflectedType:] returns true, returns the corresponding [Type].
|
||||
* Otherwise, an [UnsupportedError] is thrown.
|
||||
*/
|
||||
Type get reflectedType;
|
||||
|
||||
/**
|
||||
* A mirror on the superclass on the reflectee.
|
||||
*
|
||||
|
|
|
@ -85,6 +85,10 @@ mirrors/private_symbol_test: CompileTimeError # Issue 13597
|
|||
mirrors/proxy_type_test: RuntimeError # Issue 13842
|
||||
mirrors/redirecting_factory_test/none: RuntimeError # Issue 6490
|
||||
mirrors/redirecting_factory_test/02: RuntimeError # Issue 6490
|
||||
mirrors/reflected_type_function_type_test: RuntimeError # Issue 12607
|
||||
mirrors/reflected_type_special_types_test: RuntimeError # Issue 12607
|
||||
mirrors/reflected_type_typedefs_test: RuntimeError # Issue 12607
|
||||
mirrors/reflected_type_typevars_test: RuntimeError # Issue 12607
|
||||
mirrors/relation_assignable_test: RuntimeError # Issue 6490
|
||||
mirrors/relation_subtype_test: RuntimeError # Issue 6490
|
||||
mirrors/repeated_private_anon_mixin_app_test: RuntimeError # Issue 14670
|
||||
|
|
56
tests/lib/mirrors/reflected_type_classes_test.dart
Normal file
56
tests/lib/mirrors/reflected_type_classes_test.dart
Normal file
|
@ -0,0 +1,56 @@
|
|||
// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
library test.reflected_type_classes;
|
||||
|
||||
import 'dart:mirrors';
|
||||
|
||||
import 'reflected_type_helper.dart';
|
||||
|
||||
class A<T> {}
|
||||
class B extends A {}
|
||||
class C extends A<num, int> {} /// 01: static type warning
|
||||
class D extends A<int> {}
|
||||
class E<S> extends A<S> {}
|
||||
class F<R> extends A<int> {}
|
||||
class G {}
|
||||
class H<A,B,C> {}
|
||||
|
||||
main() {
|
||||
// Declarations.
|
||||
expectReflectedType(reflectClass(A), null);
|
||||
expectReflectedType(reflectClass(B), B);
|
||||
expectReflectedType(reflectClass(C), C); /// 01: continued
|
||||
expectReflectedType(reflectClass(D), D);
|
||||
expectReflectedType(reflectClass(E), null);
|
||||
expectReflectedType(reflectClass(F), null);
|
||||
expectReflectedType(reflectClass(G), G);
|
||||
expectReflectedType(reflectClass(H), null);
|
||||
|
||||
// Instantiations.
|
||||
expectReflectedType(reflect(new A()).type, new A().runtimeType);
|
||||
expectReflectedType(reflect(new B()).type, new B().runtimeType);
|
||||
expectReflectedType(reflect(new C()).type, new C().runtimeType); /// 01: continued
|
||||
expectReflectedType(reflect(new D()).type, new D().runtimeType);
|
||||
expectReflectedType(reflect(new E()).type, new E().runtimeType);
|
||||
expectReflectedType(reflect(new F()).type, new F().runtimeType);
|
||||
expectReflectedType(reflect(new G()).type, new G().runtimeType);
|
||||
expectReflectedType(reflect(new H()).type, new H().runtimeType);
|
||||
|
||||
expectReflectedType(reflect(new A<num>()).type, new A<num>().runtimeType);
|
||||
expectReflectedType(reflect(new B<num>()).type.superclass, /// 02: static type warning
|
||||
new A<dynamic>().runtimeType); /// 02: continued
|
||||
expectReflectedType(reflect(new C<num>()).type.superclass, /// 01: continued
|
||||
new A<dynamic>().runtimeType); /// 01: continued
|
||||
expectReflectedType(reflect(new D<num>()).type.superclass, /// 03: static type warning
|
||||
new A<int>().runtimeType); /// 03: continued
|
||||
expectReflectedType(reflect(new E<num>()).type, new E<num>().runtimeType);
|
||||
expectReflectedType(reflect(new E<num>()).type.superclass,
|
||||
new A<num>().runtimeType);
|
||||
expectReflectedType(reflect(new F<num>()).type.superclass,
|
||||
new A<int>().runtimeType);
|
||||
expectReflectedType(reflect(new F<num>()).type, new F<num>().runtimeType);
|
||||
expectReflectedType(reflect(new H<num, num, num>()).type,
|
||||
new H<num, num, num>().runtimeType);
|
||||
}
|
22
tests/lib/mirrors/reflected_type_function_type_test.dart
Normal file
22
tests/lib/mirrors/reflected_type_function_type_test.dart
Normal file
|
@ -0,0 +1,22 @@
|
|||
// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
library test.reflected_type_function_types;
|
||||
|
||||
import 'dart:mirrors';
|
||||
|
||||
import 'reflected_type_helper.dart';
|
||||
|
||||
typedef bool Predicate(num n);
|
||||
|
||||
bool somePredicate(num n) => n < 0;
|
||||
|
||||
main() {
|
||||
FunctionTypeMirror numToBool1 = reflect(somePredicate).type;
|
||||
FunctionTypeMirror numToBool2 =
|
||||
(reflectType(Predicate) as TypedefMirror).referent;
|
||||
|
||||
expectReflectedType(numToBool1, somePredicate.runtimeType);
|
||||
expectReflectedType(numToBool2, somePredicate.runtimeType);
|
||||
}
|
20
tests/lib/mirrors/reflected_type_helper.dart
Normal file
20
tests/lib/mirrors/reflected_type_helper.dart
Normal file
|
@ -0,0 +1,20 @@
|
|||
// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
library test.reflected_type_helper;
|
||||
|
||||
import 'dart:mirrors';
|
||||
import 'package:expect/expect.dart';
|
||||
|
||||
expectReflectedType(TypeMirror typeMirror, Type expectedType) {
|
||||
if (expectedType == null) {
|
||||
Expect.isFalse(typeMirror.hasReflectedType);
|
||||
Expect.throws(() => typeMirror.reflectedType,
|
||||
(e) => e is UnsupportedError,
|
||||
"Should not have a reflected type");
|
||||
} else {
|
||||
Expect.isTrue(typeMirror.hasReflectedType);
|
||||
Expect.equals(expectedType, typeMirror.reflectedType);
|
||||
}
|
||||
}
|
19
tests/lib/mirrors/reflected_type_special_types_test.dart
Normal file
19
tests/lib/mirrors/reflected_type_special_types_test.dart
Normal file
|
@ -0,0 +1,19 @@
|
|||
// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
library test.reflected_type_special_types;
|
||||
|
||||
import 'dart:mirrors';
|
||||
|
||||
import 'reflected_type_helper.dart';
|
||||
|
||||
main() {
|
||||
TypeMirror dynamicMirror = currentMirrorSystem().dynamicType;
|
||||
TypeMirror dynamicMirror2 = reflectType(dynamic);
|
||||
TypeMirror voidMirror = currentMirrorSystem().voidType;
|
||||
|
||||
expectReflectedType(dynamicMirror, dynamic);
|
||||
expectReflectedType(dynamicMirror2, dynamic);
|
||||
expectReflectedType(voidMirror, null);
|
||||
}
|
28
tests/lib/mirrors/reflected_type_typedefs_test.dart
Normal file
28
tests/lib/mirrors/reflected_type_typedefs_test.dart
Normal file
|
@ -0,0 +1,28 @@
|
|||
// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
library test.reflected_type_typedefs;
|
||||
|
||||
import 'dart:mirrors';
|
||||
|
||||
import 'reflected_type_helper.dart';
|
||||
|
||||
typedef bool NonGenericPredicate(num n);
|
||||
typedef bool GenericPredicate<T>(T t);
|
||||
typedef S GenericTransform<S>(S s);
|
||||
|
||||
main() {
|
||||
TypedefMirror nonGenericPredicate = reflectType(NonGenericPredicate);
|
||||
TypedefMirror predicateOfDynamic = reflectType(GenericPredicate);
|
||||
TypedefMirror transformOfDynamic = reflectType(GenericTransform);
|
||||
|
||||
TypedefMirror predicateDecl = predicateOfDynamic.originalDeclaration;
|
||||
TypedefMirror transformDecl = transformOfDynamic.originalDeclaration;
|
||||
|
||||
expectReflectedType(nonGenericPredicate, NonGenericPredicate);
|
||||
expectReflectedType(predicateOfDynamic, GenericPredicate);
|
||||
expectReflectedType(transformOfDynamic, GenericTransform);
|
||||
expectReflectedType(predicateDecl, null);
|
||||
expectReflectedType(transformDecl, null);
|
||||
}
|
20
tests/lib/mirrors/reflected_type_typevars_test.dart
Normal file
20
tests/lib/mirrors/reflected_type_typevars_test.dart
Normal file
|
@ -0,0 +1,20 @@
|
|||
// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
library test.reflected_type_type_variables;
|
||||
|
||||
import 'dart:mirrors';
|
||||
|
||||
import 'reflected_type_helper.dart';
|
||||
|
||||
class Class<T> {}
|
||||
typedef bool Predicate<S>(S t);
|
||||
|
||||
main() {
|
||||
TypeVariableMirror tFromClass = reflectClass(Class).typeVariables[0];
|
||||
TypeVariableMirror sFromPredicate = reflectType(Predicate).typeVariables[0];
|
||||
|
||||
expectReflectedType(tFromClass, null);
|
||||
expectReflectedType(sFromPredicate, null);
|
||||
}
|
Loading…
Reference in a new issue