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:
rmacnak@google.com 2014-03-04 19:01:26 +00:00
parent 75f9f5bfa3
commit d3a92a0f3d
11 changed files with 208 additions and 13 deletions

View file

@ -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;

View file

@ -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;

View file

@ -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();

View file

@ -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.
*

View file

@ -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

View 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);
}

View 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);
}

View 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);
}
}

View 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);
}

View 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);
}

View 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);
}