[test] Fork mirrors tests.

Bug: https://github.com/dart-lang/sdk/issues/40045
Change-Id: I597259b38684de87016f1e136723d24f4ac05420
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/132440
Reviewed-by: Ben Konyi <bkonyi@google.com>
Commit-Queue: Ryan Macnak <rmacnak@google.com>
This commit is contained in:
Ryan Macnak 2020-01-22 18:46:23 +00:00 committed by commit-bot@chromium.org
parent 0b06a369de
commit 00b2e580a3
299 changed files with 16690 additions and 0 deletions

6
.gitattributes vendored
View file

@ -30,6 +30,12 @@ tests/lib_2/mirrors/method_mirror_source_line_ending_lf.dart -text
tests/lib_2/mirrors/method_mirror_source_line_ending_test.dart -text
tests/lib_2/mirrors/method_mirror_source_other.dart -text
tests/lib_2/mirrors/method_mirror_source_test.dart -text
tests/lib/mirrors/method_mirror_source_line_ending_cr.dart -text
tests/lib/mirrors/method_mirror_source_line_ending_crlf.dart -text
tests/lib/mirrors/method_mirror_source_line_ending_lf.dart -text
tests/lib/mirrors/method_mirror_source_line_ending_test.dart -text
tests/lib/mirrors/method_mirror_source_other.dart -text
tests/lib/mirrors/method_mirror_source_test.dart -text
# Files to leave alone and not diff.
*.png binary

View file

@ -0,0 +1,174 @@
// 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.abstract_class_test;
import 'dart:mirrors';
import 'package:expect/expect.dart';
void main() {
testSimple();
testFunctionType();
testFakeFunction();
testGeneric();
testAnonMixinApplication();
testNamedMixinApplication();
}
abstract class Foo {
foo();
}
class Bar extends Foo {
foo() {}
}
testSimple() {
Expect.isTrue(reflectClass(Foo).isAbstract);
Expect.isFalse(reflectClass(Bar).isAbstract);
Expect.isTrue(reflect(new Bar()).type.superclass.isAbstract);
Expect.isFalse(reflect(new Bar()).type.isAbstract);
}
void baz() {}
testFunctionType() {
Expect.isFalse(reflect(baz).type.isAbstract);
}
abstract class FunctionFoo implements Function {
call();
}
class FunctionBar extends FunctionFoo {
call() {}
}
testFakeFunction() {
Expect.isTrue(reflectClass(FunctionFoo).isAbstract);
Expect.isFalse(reflectClass(FunctionBar).isAbstract);
Expect.isTrue(reflect(new FunctionBar()).type.superclass.isAbstract);
Expect.isFalse(reflect(new FunctionBar()).type.isAbstract);
}
abstract class GenericFoo<T> {
T genericFoo();
}
class GenericBar<T> extends GenericFoo<T> {
T genericFoo() {}
}
testGeneric() {
// Unbound.
Expect.isTrue(reflectClass(GenericFoo).isAbstract);
Expect.isFalse(reflectClass(GenericBar).isAbstract);
// Bound.
Expect.isTrue(reflect(new GenericBar<int>()).type.superclass.isAbstract);
Expect.isFalse(reflect(new GenericBar<int>()).type.isAbstract);
}
class S {}
abstract class M {
mixinFoo();
}
abstract class MA extends S with M {}
class SubMA extends MA {
mixinFoo() {}
}
class ConcreteMA extends S with M {
mixinFoo() {}
}
class M2 {
mixin2Foo() {}
}
abstract class MA2 extends S with M2 {
mixinBar();
}
class SubMA2 extends MA2 {
mixinBar() {}
}
class ConcreteMA2 extends S with M2 {
mixin2Foo() {}
}
testAnonMixinApplication() {
// Application is abstract.
{
// Mixin is abstract.
Expect.isFalse(reflectClass(SubMA).isAbstract);
Expect.isTrue(reflectClass(SubMA).superclass.isAbstract);
Expect.isTrue(reflectClass(SubMA).superclass.superclass.isAbstract);
Expect.isTrue(reflectClass(MA).isAbstract);
Expect.isTrue(reflectClass(MA).superclass.isAbstract);
// Mixin is concrete.
Expect.isFalse(reflectClass(SubMA2).isAbstract);
Expect.isTrue(reflectClass(SubMA2).superclass.isAbstract);
Expect.isTrue(reflectClass(SubMA2).superclass.superclass.isAbstract);
Expect.isTrue(reflectClass(MA2).isAbstract);
Expect.isTrue(reflectClass(MA2).superclass.isAbstract);
}
// Application is concrete.
{
// Mixin is abstract.
Expect.isFalse(reflectClass(ConcreteMA).isAbstract);
Expect.isTrue(reflectClass(ConcreteMA).superclass.isAbstract);
Expect.isFalse(reflectClass(ConcreteMA).superclass.superclass.isAbstract);
// Mixin is concrete.
Expect.isFalse(reflectClass(ConcreteMA2).isAbstract);
Expect.isTrue(reflectClass(ConcreteMA2).superclass.isAbstract);
Expect.isFalse(reflectClass(ConcreteMA2).superclass.superclass.isAbstract);
}
}
abstract class NamedMA = S with M;
class SubNamedMA extends NamedMA {
mixinFoo() {}
}
abstract class NamedMA2 = S with M2;
class SubNamedMA2 extends NamedMA2 {
mixinFoo() {}
}
class ConcreteNamedMA2 = S with M2;
testNamedMixinApplication() {
// Application is abstract.
{
// Mixin is abstract.
Expect.isFalse(reflectClass(SubNamedMA).isAbstract);
Expect.isTrue(reflectClass(SubNamedMA).superclass.isAbstract);
Expect.isFalse(reflectClass(SubNamedMA).superclass.superclass.isAbstract);
Expect.isTrue(reflectClass(NamedMA).isAbstract);
Expect.isFalse(reflectClass(NamedMA).superclass.isAbstract);
// Mixin is concrete.
Expect.isFalse(reflectClass(SubNamedMA2).isAbstract);
Expect.isTrue(reflectClass(SubNamedMA2).superclass.isAbstract);
Expect.isFalse(reflectClass(SubNamedMA2).superclass.superclass.isAbstract);
Expect.isTrue(reflectClass(NamedMA2).isAbstract);
Expect.isFalse(reflectClass(NamedMA2).superclass.isAbstract);
}
// Application is concrete.
{
// Mixin is concrete.
Expect.isFalse(reflectClass(ConcreteNamedMA2).isAbstract);
Expect.isFalse(reflectClass(ConcreteNamedMA2).superclass.isAbstract);
}
}

View file

@ -0,0 +1,18 @@
// Copyright (c) 2013, 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.
// Test abstract classes are retained.
library test.abstract_test;
import 'dart:mirrors';
import 'stringify.dart';
abstract class Foo {}
void main() {
expect(
'Class(s(Foo) in s(test.abstract_test), top-level)', reflectClass(Foo));
}

View file

@ -0,0 +1,308 @@
// 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.
// This test runs invokes getField and setField enough times to get cached
// closures generated and with enough different field names to trip the path
// that flushes the closure cache.
library test.hot_get_field;
import 'dart:mirrors';
import 'package:expect/expect.dart';
const int optimizationThreshold = 20;
main() {
var digits = [
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'A',
'B',
'C',
'D',
'E',
'F'
];
var symbols = new List();
for (var high in digits) {
for (var low in digits) {
symbols.add(MirrorSystem.getSymbol("v$high$low"));
}
}
var im = reflect(new C());
for (var i = 0; i < optimizationThreshold * 2; i++) {
for (var fieldName in symbols) {
im.getField(fieldName);
im.setField(fieldName, 'foo');
}
}
}
class C {
var v00;
var v01;
var v02;
var v03;
var v04;
var v05;
var v06;
var v07;
var v08;
var v09;
var v0A;
var v0B;
var v0C;
var v0D;
var v0E;
var v0F;
var v10;
var v11;
var v12;
var v13;
var v14;
var v15;
var v16;
var v17;
var v18;
var v19;
var v1A;
var v1B;
var v1C;
var v1D;
var v1E;
var v1F;
var v20;
var v21;
var v22;
var v23;
var v24;
var v25;
var v26;
var v27;
var v28;
var v29;
var v2A;
var v2B;
var v2C;
var v2D;
var v2E;
var v2F;
var v30;
var v31;
var v32;
var v33;
var v34;
var v35;
var v36;
var v37;
var v38;
var v39;
var v3A;
var v3B;
var v3C;
var v3D;
var v3E;
var v3F;
var v40;
var v41;
var v42;
var v43;
var v44;
var v45;
var v46;
var v47;
var v48;
var v49;
var v4A;
var v4B;
var v4C;
var v4D;
var v4E;
var v4F;
var v50;
var v51;
var v52;
var v53;
var v54;
var v55;
var v56;
var v57;
var v58;
var v59;
var v5A;
var v5B;
var v5C;
var v5D;
var v5E;
var v5F;
var v60;
var v61;
var v62;
var v63;
var v64;
var v65;
var v66;
var v67;
var v68;
var v69;
var v6A;
var v6B;
var v6C;
var v6D;
var v6E;
var v6F;
var v70;
var v71;
var v72;
var v73;
var v74;
var v75;
var v76;
var v77;
var v78;
var v79;
var v7A;
var v7B;
var v7C;
var v7D;
var v7E;
var v7F;
var v80;
var v81;
var v82;
var v83;
var v84;
var v85;
var v86;
var v87;
var v88;
var v89;
var v8A;
var v8B;
var v8C;
var v8D;
var v8E;
var v8F;
var v90;
var v91;
var v92;
var v93;
var v94;
var v95;
var v96;
var v97;
var v98;
var v99;
var v9A;
var v9B;
var v9C;
var v9D;
var v9E;
var v9F;
var vA0;
var vA1;
var vA2;
var vA3;
var vA4;
var vA5;
var vA6;
var vA7;
var vA8;
var vA9;
var vAA;
var vAB;
var vAC;
var vAD;
var vAE;
var vAF;
var vB0;
var vB1;
var vB2;
var vB3;
var vB4;
var vB5;
var vB6;
var vB7;
var vB8;
var vB9;
var vBA;
var vBB;
var vBC;
var vBD;
var vBE;
var vBF;
var vC0;
var vC1;
var vC2;
var vC3;
var vC4;
var vC5;
var vC6;
var vC7;
var vC8;
var vC9;
var vCA;
var vCB;
var vCC;
var vCD;
var vCE;
var vCF;
var vD0;
var vD1;
var vD2;
var vD3;
var vD4;
var vD5;
var vD6;
var vD7;
var vD8;
var vD9;
var vDA;
var vDB;
var vDC;
var vDD;
var vDE;
var vDF;
var vE0;
var vE1;
var vE2;
var vE3;
var vE4;
var vE5;
var vE6;
var vE7;
var vE8;
var vE9;
var vEA;
var vEB;
var vEC;
var vED;
var vEE;
var vEF;
var vF0;
var vF1;
var vF2;
var vF3;
var vF4;
var vF5;
var vF6;
var vF7;
var vF8;
var vF9;
var vFA;
var vFB;
var vFC;
var vFD;
var vFE;
var vFF;
}

View file

@ -0,0 +1,69 @@
// Copyright (c) 2013, 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.
// Test [Function.apply] on user-defined classes that implement [noSuchMethod].
import "package:expect/expect.dart";
import 'dart:mirrors';
class F {
call([p1]) => "call";
noSuchMethod(Invocation invocation) => "NSM";
}
class G {
call() => '42';
noSuchMethod(Invocation invocation) => invocation;
}
class H {
call(required, {a}) => required + a;
}
main() {
Expect.equals('call', Function.apply(new F(), []));
Expect.equals('call', Function.apply(new F(), [1]));
Expect.equals('NSM', Function.apply(new F(), [1, 2]));
Expect.equals('NSM', Function.apply(new F(), [1, 2, 3]));
var symbol = const Symbol('a');
var requiredParameters = [1];
var optionalParameters = new Map<Symbol, int>()..[symbol] = 42;
Invocation i =
Function.apply(new G(), requiredParameters, optionalParameters);
Expect.equals(const Symbol('call'), i.memberName);
Expect.listEquals(requiredParameters, i.positionalArguments);
Expect.mapEquals(optionalParameters, i.namedArguments);
Expect.isTrue(i.isMethod);
Expect.isFalse(i.isGetter);
Expect.isFalse(i.isSetter);
Expect.isFalse(i.isAccessor);
// Check that changing the passed list and map for parameters does
// not affect [i].
requiredParameters[0] = 42;
optionalParameters[symbol] = 12;
Expect.listEquals([1], i.positionalArguments);
Expect.mapEquals(new Map()..[symbol] = 42, i.namedArguments);
// Check that using [i] for invocation yields the same [Invocation]
// object.
var mirror = reflect(new G());
Invocation other = mirror.delegate(i);
Expect.equals(i.memberName, other.memberName);
Expect.listEquals(i.positionalArguments, other.positionalArguments);
Expect.mapEquals(i.namedArguments, other.namedArguments);
Expect.equals(i.isMethod, other.isMethod);
Expect.equals(i.isGetter, other.isGetter);
Expect.equals(i.isSetter, other.isSetter);
Expect.equals(i.isAccessor, other.isAccessor);
// Test that [i] can be used to hit an existing method.
Expect.equals(43, new H().call(1, a: 42));
Expect.equals(43, Function.apply(new H(), [1], new Map()..[symbol] = 42));
mirror = reflect(new H());
Expect.equals(43, mirror.delegate(i));
Expect.equals(43, mirror.delegate(other));
}

View file

@ -0,0 +1,19 @@
// Copyright (c) 2013, 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.
import 'dart:mirrors';
class A {
var field;
}
main() {
var a = new A();
var mirror = reflect(a);
var array = [42];
a.field = array;
var field = mirror.getField(#field);
field.invoke(#clear, []);
if (array.length == 1) throw 'Test failed';
}

View file

@ -0,0 +1,19 @@
// Copyright (c) 2013, 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.
import 'dart:mirrors';
class A {
static var field;
}
main() {
MirrorSystem mirrors = currentMirrorSystem();
ClassMirror a = reflectClass(A);
var array = [42];
A.field = array;
var field = a.getField(#field);
field.invoke(#clear, []);
if (array.length == 1) throw 'Test failed';
}

View file

@ -0,0 +1,19 @@
// Copyright (c) 2013, 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.
import 'dart:mirrors';
class A {
static var field;
}
main() {
MirrorSystem mirrors = currentMirrorSystem();
ClassMirror a = reflectClass(A);
var array = [42];
A.field = array;
var field = a.getField(#field);
field.invoke(#clear, []);
if (array.length == 1) throw 'Test failed';
}

View file

@ -0,0 +1,157 @@
// Copyright (c) 2018, 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.
import 'dart:io';
import 'dart:mirrors';
import 'package:expect/expect.dart';
int foobar = 1;
set foobaz(int x) {
foobar = x;
}
void foo(Map<String, String> m) {
print(m);
print(m['bar']);
}
void bar<T extends num>(T a) {
print(a);
}
class Foo {
Map<String, String> bork;
static Map<String, String> bark;
static set woof(Map<String, String> x) {
bark = x;
}
Foo(Map<String, String> m) {
print(m);
}
Foo.a();
static void baz(Map<String, String> m, {String bar}) {
print('baz');
print(m['bar']);
print(bar);
}
void bar(Map<String, String> m) {
print('bar');
print(m.runtimeType);
}
}
class FooBar<T extends num> {
T bar;
FooBar(this.bar) {
print(bar);
}
set barz(T x) {
bar = x;
}
factory FooBar.baz(T bar) {
print(bar);
return FooBar(bar);
}
void foobar<S>(T a, S b) {
print(a);
print(b);
}
}
void badClassStaticInvoke() {
Map<String, String> map = Map<String, String>();
map['bar'] = 'Hello world!';
final cm = reflectClass(Foo);
Expect.throwsTypeError(() => cm.invoke(#baz, [
map
], {
#bar: {'boo': 'bah'}
}));
}
void badStaticInvoke() {
final im = reflect(foo) as ClosureMirror;
Expect.throwsTypeError(() => im.apply(['Hello world!']));
}
void badInstanceInvoke() {
final fooCls = Foo.a();
final im = reflect(fooCls);
Expect.throwsTypeError(() => im.invoke(#bar, ['Hello World!']));
}
void badConstructorInvoke() {
final cm = reflectClass(Foo);
Expect.throwsTypeError(() => cm.newInstance(Symbol(''), ['Hello World!']));
}
void badSetterInvoke() {
final fooCls = Foo.a();
final im = reflect(fooCls);
Expect.throwsTypeError(() => im.setField(#bork, 'Hello World!'));
}
void badStaticSetterInvoke() {
final cm = reflectClass(Foo);
Expect.throwsTypeError(() => cm.setField(#bark, 'Hello World!'));
Expect.throwsTypeError(() => cm.setField(#woof, 'Hello World!'));
}
void badGenericConstructorInvoke() {
final cm = reflectType(FooBar, [int]) as ClassMirror;
Expect.throwsTypeError(() => cm.newInstance(Symbol(''), ['Hello World!']));
}
void badGenericClassStaticInvoke() {
final cm = reflectType(FooBar, [int]) as ClassMirror;
final im = cm.newInstance(Symbol(''), [1]);
Expect.throwsTypeError(() => im.invoke(#foobar, ['Hello', 'World']));
}
void badGenericFactoryInvoke() {
final cm = reflectType(FooBar, [int]) as ClassMirror;
Expect.throwsTypeError(() => cm.newInstance(Symbol('baz'), ['Hello World!']));
}
void badGenericStaticInvoke() {
final im = reflect(bar) as ClosureMirror;
Expect.throwsTypeError(() => im.apply(['Hello world!']));
}
void badGenericSetterInvoke() {
final cm = reflectType(FooBar, [int]) as ClassMirror;
final im = cm.newInstance(Symbol(''), [0]);
Expect.throwsTypeError(() => im.setField(#bar, 'Hello world!'));
Expect.throwsTypeError(() => im.setField(#barz, 'Hello world!'));
}
void badLibrarySetterInvoke() {
final lm = currentMirrorSystem().findLibrary(Symbol(''));
Expect.throwsTypeError(() => lm.setField(#foobar, 'Foobaz'));
Expect.throwsTypeError(() => lm.setField(#foobaz, 'Foobaz'));
}
void main() {
badClassStaticInvoke();
badStaticInvoke();
badInstanceInvoke();
badConstructorInvoke();
badSetterInvoke();
badStaticSetterInvoke();
badGenericConstructorInvoke();
badGenericClassStaticInvoke();
badGenericFactoryInvoke();
badGenericStaticInvoke();
badGenericSetterInvoke();
badLibrarySetterInvoke();
}

View file

@ -0,0 +1,52 @@
// Copyright (c) 2013, 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.basic_types_in_dart_core;
import 'dart:mirrors';
import 'package:expect/expect.dart';
main() {
LibraryMirror dartcore = currentMirrorSystem().findLibrary(#dart.core);
ClassMirror cm;
TypeMirror tm;
cm = dartcore.declarations[#int] as ClassMirror;
Expect.equals(reflectClass(int), cm);
Expect.equals(#int, cm.simpleName);
cm = dartcore.declarations[#double] as ClassMirror;
Expect.equals(reflectClass(double), cm);
Expect.equals(#double, cm.simpleName);
cm = dartcore.declarations[#num] as ClassMirror;
Expect.equals(reflectClass(num), cm);
Expect.equals(#num, cm.simpleName);
cm = dartcore.declarations[#bool] as ClassMirror;
Expect.equals(reflectClass(bool), cm);
Expect.equals(#bool, cm.simpleName);
cm = dartcore.declarations[#String] as ClassMirror;
Expect.equals(reflectClass(String), cm);
Expect.equals(#String, cm.simpleName);
cm = dartcore.declarations[#List] as ClassMirror;
Expect.equals(reflectClass(List), cm);
Expect.equals(#List, cm.simpleName);
cm = dartcore.declarations[#Null] as ClassMirror;
Expect.equals(reflectClass(Null), cm);
Expect.equals(#Null, cm.simpleName);
cm = dartcore.declarations[#Object] as ClassMirror;
Expect.equals(reflectClass(Object), cm);
Expect.equals(#Object, cm.simpleName);
tm = dartcore.declarations[#dynamic] as TypeMirror;
Expect.isNull(tm);
tm = dartcore.declarations[const Symbol('void')] as TypeMirror;
Expect.isNull(tm);
}

View file

@ -0,0 +1,39 @@
// Copyright (c) 2013, 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.
import "dart:mirrors";
import "package:expect/expect.dart";
class A {
A();
A.circular() = B.circular; // //# 01: compile-time error
const A.circular2() = B.circular2; // //# 02: compile-time error
}
class B {
B();
B.circular() = C.circular; // //# 01: continued
const B.circular2() = C.circular2; // //# 02: continued
}
class C {
C();
C.circular() = A.circular; // //# 01: continued
const C.circular2() = A.circular2; // //# 02: continued
}
main() {
ClassMirror cm = reflectClass(A);
new A.circular(); // //# 01: continued
new A.circular2(); // //# 02: continued
Expect.throwsNoSuchMethodError(
() => cm.newInstance(#circular, []),
'Should disallow circular redirection (non-const)');
Expect.throwsNoSuchMethodError(
() => cm.newInstance(#circular2, []),
'Should disallow circular redirection (const)');
}

View file

@ -0,0 +1,368 @@
// Copyright (c) 2013, 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.declarations_test;
import 'dart:mirrors';
import 'package:expect/expect.dart';
import 'stringify.dart';
import 'declarations_model.dart' as declarations_model;
Set<DeclarationMirror> inheritedDeclarations(ClassMirror cm) {
var decls = new Set<DeclarationMirror>();
while (cm != null) {
decls.addAll(cm.declarations.values);
cm = cm.superclass;
}
return decls;
}
main() {
ClassMirror cm = reflectClass(declarations_model.Class);
Expect.setEquals([
'Variable(s(_instanceVariable) in s(Class), private)',
'Variable(s(_staticVariable) in s(Class), private, static)',
'Variable(s(instanceVariable) in s(Class))',
'Variable(s(staticVariable) in s(Class), static)'
], cm.declarations.values.where((dm) => dm is VariableMirror).map(stringify),
'variables');
Expect.setEquals(
[
'Method(s(_instanceGetter) in s(Class), private, getter)',
'Method(s(_staticGetter) in s(Class), private, static, getter)',
'Method(s(instanceGetter) in s(Class), getter)',
'Method(s(staticGetter) in s(Class), static, getter)'
],
cm.declarations.values
.where((dm) => dm is MethodMirror && dm.isGetter)
.map(stringify),
'getters');
Expect.setEquals(
[
'Method(s(_instanceSetter=) in s(Class), private, setter)',
'Method(s(_staticSetter=) in s(Class), private, static, setter)',
'Method(s(instanceSetter=) in s(Class), setter)',
'Method(s(staticSetter=) in s(Class), static, setter)'
],
cm.declarations.values
.where((dm) => dm is MethodMirror && dm.isSetter)
.map(stringify),
'setters');
// dart2js stops testing here.
return; //# 01: ok
Expect.setEquals(
[
'Method(s(+) in s(Class))',
'Method(s(_instanceMethod) in s(Class), private)',
'Method(s(_staticMethod) in s(Class), private, static)',
'Method(s(abstractMethod) in s(Class), abstract)',
'Method(s(instanceMethod) in s(Class))',
'Method(s(staticMethod) in s(Class), static)'
],
cm.declarations.values
.where((dm) => dm is MethodMirror && dm.isRegularMethod)
.map(stringify),
'regular methods');
Expect.setEquals(
[
'Method(s(Class._generativeConstructor) in s(Class), private, constructor)',
'Method(s(Class._normalFactory) in s(Class), private, static, constructor)',
'Method(s(Class._redirectingConstructor)'
' in s(Class), private, constructor)',
'Method(s(Class._redirectingFactory)'
' in s(Class), private, static, constructor)',
'Method(s(Class.generativeConstructor) in s(Class), constructor)',
'Method(s(Class.normalFactory) in s(Class), static, constructor)',
'Method(s(Class.redirectingConstructor) in s(Class), constructor)',
'Method(s(Class.redirectingFactory) in s(Class), static, constructor)'
],
cm.declarations.values
.where((dm) => dm is MethodMirror && dm.isConstructor)
.map(stringify),
'constructors and factories');
Expect.setEquals(
[
'Method(s(Class._normalFactory) in s(Class), private, static, constructor)',
'Method(s(Class._redirectingFactory)'
' in s(Class), private, static, constructor)',
'Method(s(Class.normalFactory) in s(Class), static, constructor)',
'Method(s(Class.redirectingFactory) in s(Class), static, constructor)',
'Method(s(_staticGetter) in s(Class), private, static, getter)',
'Method(s(_staticMethod) in s(Class), private, static)',
'Method(s(_staticSetter=) in s(Class), private, static, setter)',
'Variable(s(_staticVariable) in s(Class), private, static)',
'Method(s(staticGetter) in s(Class), static, getter)',
'Method(s(staticMethod) in s(Class), static)',
'Method(s(staticSetter=) in s(Class), static, setter)',
'Variable(s(staticVariable) in s(Class), static)'
],
cm.declarations.values
.where((dm) => (dm as dynamic).isStatic)
.map(stringify),
'statics');
Expect.setEquals(
[
'Method(s(+) in s(Class))',
'TypeVariable(s(C) in s(Class),'
' upperBound = Class(s(Object) in s(dart.core), top-level))',
'Method(s(Class._generativeConstructor) in s(Class), private, constructor)',
'Method(s(Class._redirectingConstructor)'
' in s(Class), private, constructor)',
'Method(s(Class.generativeConstructor) in s(Class), constructor)',
'Method(s(Class.redirectingConstructor) in s(Class), constructor)',
'Method(s(_instanceGetter) in s(Class), private, getter)',
'Method(s(_instanceMethod) in s(Class), private)',
'Method(s(_instanceSetter=) in s(Class), private, setter)',
'Variable(s(_instanceVariable) in s(Class), private)',
'Method(s(abstractMethod) in s(Class), abstract)',
'Method(s(instanceGetter) in s(Class), getter)',
'Method(s(instanceMethod) in s(Class))',
'Method(s(instanceSetter=) in s(Class), setter)',
'Variable(s(instanceVariable) in s(Class))'
],
cm.declarations.values
.where((dm) => !(dm as dynamic).isStatic)
.map(stringify),
'non-statics');
Expect.setEquals(
[
'Method(s(+) in s(Class))',
'TypeVariable(s(C) in s(Class),'
' upperBound = Class(s(Object) in s(dart.core), top-level))',
'Method(s(Class.generativeConstructor) in s(Class), constructor)',
'Method(s(Class.normalFactory) in s(Class), static, constructor)',
'Method(s(Class.redirectingConstructor) in s(Class), constructor)',
'Method(s(Class.redirectingFactory) in s(Class), static, constructor)',
'Method(s(abstractMethod) in s(Class), abstract)',
'Method(s(instanceGetter) in s(Class), getter)',
'Method(s(instanceMethod) in s(Class))',
'Method(s(instanceSetter=) in s(Class), setter)',
'Variable(s(instanceVariable) in s(Class))',
'Method(s(staticGetter) in s(Class), static, getter)',
'Method(s(staticMethod) in s(Class), static)',
'Method(s(staticSetter=) in s(Class), static, setter)',
'Variable(s(staticVariable) in s(Class), static)'
],
cm.declarations.values
.where((dm) => !(dm as dynamic).isPrivate)
.map(stringify),
'public');
Expect.setEquals([
'Method(s(*) in s(Mixin))',
'Method(s(+) in s(Class))',
'Method(s(-) in s(Superclass))',
'Method(s(==) in s(Object))',
'TypeVariable(s(C) in s(Class),'
' upperBound = Class(s(Object) in s(dart.core), top-level))',
'Method(s(Class.generativeConstructor) in s(Class), constructor)',
'Method(s(Class.normalFactory) in s(Class), static, constructor)',
'Method(s(Class.redirectingConstructor) in s(Class), constructor)',
'Method(s(Class.redirectingFactory) in s(Class), static, constructor)',
'Method(s(Object) in s(Object), constructor)',
'TypeVariable(s(S) in s(Superclass),'
' upperBound = Class(s(Object) in s(dart.core), top-level))',
'Method(s(Superclass.inheritedGenerativeConstructor)'
' in s(Superclass), constructor)',
'Method(s(Superclass.inheritedNormalFactory)'
' in s(Superclass), static, constructor)',
'Method(s(Superclass.inheritedRedirectingConstructor)'
' in s(Superclass), constructor)',
'Method(s(Superclass.inheritedRedirectingFactory)'
' in s(Superclass), static, constructor)',
'Method(s(abstractMethod) in s(Class), abstract)',
'Method(s(hashCode) in s(Object), getter)',
'Method(s(inheritedInstanceGetter) in s(Superclass), getter)',
'Method(s(inheritedInstanceMethod) in s(Superclass))',
'Method(s(inheritedInstanceSetter=) in s(Superclass), setter)',
'Variable(s(inheritedInstanceVariable) in s(Superclass))',
'Method(s(inheritedStaticGetter) in s(Superclass), static, getter)',
'Method(s(inheritedStaticMethod) in s(Superclass), static)',
'Method(s(inheritedStaticSetter=) in s(Superclass), static, setter)',
'Variable(s(inheritedStaticVariable) in s(Superclass), static)',
'Method(s(instanceGetter) in s(Class), getter)',
'Method(s(instanceMethod) in s(Class))',
'Method(s(instanceSetter=) in s(Class), setter)',
'Variable(s(instanceVariable) in s(Class))',
'Method(s(mixinInstanceGetter) in s(Mixin), getter)',
'Method(s(mixinInstanceMethod) in s(Mixin))',
'Method(s(mixinInstanceSetter=) in s(Mixin), setter)',
'Variable(s(mixinInstanceVariable) in s(Mixin))',
'Method(s(noSuchMethod) in s(Object))',
'Method(s(runtimeType) in s(Object), getter)',
'Method(s(staticGetter) in s(Class), static, getter)',
'Method(s(staticMethod) in s(Class), static)',
'Method(s(staticSetter=) in s(Class), static, setter)',
'Variable(s(staticVariable) in s(Class), static)',
'Method(s(test.declarations_model.Superclass'
' with test.declarations_model.Mixin.inheritedGenerativeConstructor)'
' in s(test.declarations_model.Superclass'
' with test.declarations_model.Mixin), constructor)',
'Method(s(test.declarations_model.Superclass'
' with test.declarations_model.Mixin.inheritedRedirectingConstructor)'
' in s(test.declarations_model.Superclass'
' with test.declarations_model.Mixin), constructor)',
'Method(s(toString) in s(Object))',
'Variable(s(mixinStaticVariable) in s(Mixin), static)',
'Method(s(mixinStaticGetter) in s(Mixin), static, getter)',
'Method(s(mixinStaticSetter=) in s(Mixin), static, setter)',
'Method(s(mixinStaticMethod) in s(Mixin), static)'
], inheritedDeclarations(cm).where((dm) => !dm.isPrivate).map(stringify),
'transitive public');
// The public members of Object should be the same in all implementations, so
// we don't exclude Object here.
Expect.setEquals([
'Method(s(+) in s(Class))',
'TypeVariable(s(C) in s(Class),'
' upperBound = Class(s(Object) in s(dart.core), top-level))',
'Method(s(Class._generativeConstructor) in s(Class), private, constructor)',
'Method(s(Class._normalFactory) in s(Class), private, static, constructor)',
'Method(s(Class._redirectingConstructor)'
' in s(Class), private, constructor)',
'Method(s(Class._redirectingFactory)'
' in s(Class), private, static, constructor)',
'Method(s(Class.generativeConstructor) in s(Class), constructor)',
'Method(s(Class.normalFactory) in s(Class), static, constructor)',
'Method(s(Class.redirectingConstructor) in s(Class), constructor)',
'Method(s(Class.redirectingFactory) in s(Class), static, constructor)',
'Method(s(_instanceGetter) in s(Class), private, getter)',
'Method(s(_instanceMethod) in s(Class), private)',
'Method(s(_instanceSetter=) in s(Class), private, setter)',
'Variable(s(_instanceVariable) in s(Class), private)',
'Method(s(_staticGetter) in s(Class), private, static, getter)',
'Method(s(_staticMethod) in s(Class), private, static)',
'Method(s(_staticSetter=) in s(Class), private, static, setter)',
'Variable(s(_staticVariable) in s(Class), private, static)',
'Method(s(abstractMethod) in s(Class), abstract)',
'Method(s(instanceGetter) in s(Class), getter)',
'Method(s(instanceMethod) in s(Class))',
'Method(s(instanceSetter=) in s(Class), setter)',
'Variable(s(instanceVariable) in s(Class))',
'Method(s(staticGetter) in s(Class), static, getter)',
'Method(s(staticMethod) in s(Class), static)',
'Method(s(staticSetter=) in s(Class), static, setter)',
'Variable(s(staticVariable) in s(Class), static)'
], cm.declarations.values.map(stringify), 'declarations');
Expect.setEquals(
[
'Method(s(*) in s(Mixin))',
'Method(s(+) in s(Class))',
'Method(s(-) in s(Superclass))',
'TypeVariable(s(C) in s(Class),'
' upperBound = Class(s(Object) in s(dart.core), top-level))',
'Method(s(Class._generativeConstructor) in s(Class), private, constructor)',
'Method(s(Class._normalFactory) in s(Class), private, static, constructor)',
'Method(s(Class._redirectingConstructor)'
' in s(Class), private, constructor)',
'Method(s(Class._redirectingFactory)'
' in s(Class), private, static, constructor)',
'Method(s(Class.generativeConstructor) in s(Class), constructor)',
'Method(s(Class.normalFactory) in s(Class), static, constructor)',
'Method(s(Class.redirectingConstructor) in s(Class), constructor)',
'Method(s(Class.redirectingFactory) in s(Class), static, constructor)',
'TypeVariable(s(S) in s(Superclass),'
' upperBound = Class(s(Object) in s(dart.core), top-level))',
'Method(s(Superclass._inheritedGenerativeConstructor)'
' in s(Superclass), private, constructor)',
'Method(s(Superclass._inheritedNormalFactory)'
' in s(Superclass), private, static, constructor)',
'Method(s(Superclass._inheritedRedirectingConstructor)'
' in s(Superclass), private, constructor)',
'Method(s(Superclass._inheritedRedirectingFactory)'
' in s(Superclass), private, static, constructor)',
'Method(s(Superclass.inheritedGenerativeConstructor)'
' in s(Superclass), constructor)',
'Method(s(Superclass.inheritedNormalFactory)'
' in s(Superclass), static, constructor)',
'Method(s(Superclass.inheritedRedirectingConstructor)'
' in s(Superclass), constructor)',
'Method(s(Superclass.inheritedRedirectingFactory)'
' in s(Superclass), static, constructor)',
'Method(s(_inheritedInstanceGetter) in s(Superclass), private, getter)',
'Method(s(_inheritedInstanceMethod) in s(Superclass), private)',
'Method(s(_inheritedInstanceSetter=) in s(Superclass), private, setter)',
'Variable(s(_inheritedInstanceVariable) in s(Superclass), private)',
'Method(s(_inheritedStaticGetter)'
' in s(Superclass), private, static, getter)',
'Method(s(_inheritedStaticMethod) in s(Superclass), private, static)',
'Method(s(_inheritedStaticSetter=)'
' in s(Superclass), private, static, setter)',
'Variable(s(_inheritedStaticVariable) in s(Superclass), private, static)',
'Method(s(_instanceGetter) in s(Class), private, getter)',
'Method(s(_instanceMethod) in s(Class), private)',
'Method(s(_instanceSetter=) in s(Class), private, setter)',
'Variable(s(_instanceVariable) in s(Class), private)',
'Method(s(_mixinInstanceGetter) in s(Mixin), private, getter)',
'Method(s(_mixinInstanceMethod) in s(Mixin), private)',
'Method(s(_mixinInstanceSetter=) in s(Mixin), private, setter)',
'Variable(s(_mixinInstanceVariable) in s(Mixin), private)',
'Method(s(_staticGetter) in s(Class), private, static, getter)',
'Method(s(_staticMethod) in s(Class), private, static)',
'Method(s(_staticSetter=) in s(Class), private, static, setter)',
'Variable(s(_staticVariable) in s(Class), private, static)',
'Method(s(abstractMethod) in s(Class), abstract)',
'Method(s(inheritedInstanceGetter) in s(Superclass), getter)',
'Method(s(inheritedInstanceMethod) in s(Superclass))',
'Method(s(inheritedInstanceSetter=) in s(Superclass), setter)',
'Variable(s(inheritedInstanceVariable) in s(Superclass))',
'Method(s(inheritedStaticGetter) in s(Superclass), static, getter)',
'Method(s(inheritedStaticMethod) in s(Superclass), static)',
'Method(s(inheritedStaticSetter=) in s(Superclass), static, setter)',
'Variable(s(inheritedStaticVariable) in s(Superclass), static)',
'Method(s(instanceGetter) in s(Class), getter)',
'Method(s(instanceMethod) in s(Class))',
'Method(s(instanceSetter=) in s(Class), setter)',
'Variable(s(instanceVariable) in s(Class))',
'Method(s(mixinInstanceGetter) in s(Mixin), getter)',
'Method(s(mixinInstanceMethod) in s(Mixin))',
'Method(s(mixinInstanceSetter=) in s(Mixin), setter)',
'Variable(s(mixinInstanceVariable) in s(Mixin))',
'Method(s(staticGetter) in s(Class), static, getter)',
'Method(s(staticMethod) in s(Class), static)',
'Method(s(staticSetter=) in s(Class), static, setter)',
'Variable(s(staticVariable) in s(Class), static)',
'Method(s(test.declarations_model.Superclass'
' with test.declarations_model.Mixin._inheritedGenerativeConstructor)'
' in s(test.declarations_model.Superclass'
' with test.declarations_model.Mixin), private, constructor)',
'Method(s(test.declarations_model.Superclass'
' with test.declarations_model.Mixin._inheritedRedirectingConstructor)'
' in s(test.declarations_model.Superclass'
' with test.declarations_model.Mixin), private, constructor)',
'Method(s(test.declarations_model.Superclass'
' with test.declarations_model.Mixin.inheritedGenerativeConstructor)'
' in s(test.declarations_model.Superclass'
' with test.declarations_model.Mixin), constructor)',
'Method(s(test.declarations_model.Superclass'
' with test.declarations_model.Mixin.inheritedRedirectingConstructor)'
' in s(test.declarations_model.Superclass'
' with test.declarations_model.Mixin), constructor)',
'Variable(s(mixinStaticVariable) in s(Mixin), static)',
'Variable(s(_mixinStaticVariable) in s(Mixin), private, static)',
'Method(s(mixinStaticGetter) in s(Mixin), static, getter)',
'Method(s(mixinStaticSetter=) in s(Mixin), static, setter)',
'Method(s(mixinStaticMethod) in s(Mixin), static)',
'Method(s(_mixinStaticGetter) in s(Mixin), private, static, getter)',
'Method(s(_mixinStaticSetter=) in s(Mixin), private, static, setter)',
'Method(s(_mixinStaticMethod) in s(Mixin), private, static)'
],
inheritedDeclarations(cm)
.difference(reflectClass(Object).declarations.values.toSet())
.map(stringify),
'transitive less Object');
// The private members of Object may vary across implementations, so we
// exclude the declarations of Object in this test case.
}

View file

@ -0,0 +1,11 @@
// Copyright (c) 2015, 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.
part of test.class_location;
class ClassInOtherFile {}
class SpaceIndentedInOtherFile {}
class TabIndentedInOtherFile {}

View file

@ -0,0 +1,69 @@
// Copyright (c) 2015, 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.class_location;
import "dart:mirrors";
import "package:expect/expect.dart";
part 'class_mirror_location_other.dart';
class ClassInMainFile {}
class SpaceIndentedInMainFile {}
class TabIndentedInMainFile {}
abstract class AbstractClass {}
typedef bool Predicate(num n);
class M {}
class S {}
class MA extends S with M {}
class MA2 = S with M;
const metadata = 'metadata';
@metadata
class WithMetadata {}
enum Enum { RED, GREEN, BLUE }
@metadata
enum AnnotatedEnum { SALT, PEPPER }
// We only check for a suffix of the uri because the test might be run from
// any number of absolute paths.
expectLocation(
DeclarationMirror mirror, String uriSuffix, int line, int column) {
final location = mirror.location;
final uri = location.sourceUri;
Expect.isTrue(
uri.toString().endsWith(uriSuffix), "Expected suffix $uriSuffix in $uri");
Expect.equals(line, location.line, "line");
Expect.equals(column, location.column, "column");
}
main() {
String mainSuffix = 'class_mirror_location_test.dart';
String otherSuffix = 'class_mirror_location_other.dart';
// This file.
expectLocation(reflectClass(ClassInMainFile), mainSuffix, 12, 1);
expectLocation(reflectClass(SpaceIndentedInMainFile), mainSuffix, 13, 3);
expectLocation(reflectClass(TabIndentedInMainFile), mainSuffix, 14, 2);
expectLocation(reflectClass(AbstractClass), mainSuffix, 16, 1);
expectLocation(reflectType(Predicate), mainSuffix, 17, 1);
expectLocation(reflectClass(MA), mainSuffix, 21, 1);
expectLocation(reflectClass(MA2), mainSuffix, 22, 1);
expectLocation(reflectClass(WithMetadata), mainSuffix, 26, 1);
expectLocation(reflectClass(Enum), mainSuffix, 29, 1);
expectLocation(reflectClass(AnnotatedEnum), mainSuffix, 31, 1);
// Another part.
expectLocation(reflectClass(ClassInOtherFile), otherSuffix, 7, 1);
expectLocation(reflectClass(SpaceIndentedInOtherFile), otherSuffix, 9, 3);
expectLocation(reflectClass(TabIndentedInOtherFile), otherSuffix, 11, 2);
// Synthetic classes.
Expect.isNull(reflectClass(MA).superclass.location);
Expect.isNull((reflect(main) as ClosureMirror).type.location);
}

View file

@ -0,0 +1,25 @@
// Copyright (c) 2013, 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 class_mirror_type_variables_data;
class NoTypeParams {}
class A<T, S extends String> {}
class B<Z extends B<Z>> {}
class C<Z extends B<Z>> {}
class D<R, S, T> {
R foo(R r) => r;
S bar(S s) => s;
T baz(T t) => t;
}
class Helper<S> {}
class E<R extends Map<R, Helper<String>>> {}
class F<Z extends Helper<F<Z>>> {}

View file

@ -0,0 +1,130 @@
// Copyright (c) 2013, 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.
// Test expectations for 'class_mirror_type_variables_data.dart'.
library class_mirror_type_variables_expect;
import "dart:mirrors";
import "package:expect/expect.dart";
/// The interface of [Env] is shared between the runtime and the source mirrors
/// test.
abstract class Env {
ClassMirror getA();
ClassMirror getB();
ClassMirror getC();
ClassMirror getD();
ClassMirror getE();
ClassMirror getF();
ClassMirror getNoTypeParams();
ClassMirror getObject();
ClassMirror getString();
ClassMirror getHelperOfString();
}
void test(Env env) {
testNoTypeParams(env);
testA(env);
testBAndC(env);
testD(env);
testE(env);
testF(env);
}
testNoTypeParams(Env env) {
ClassMirror cm = env.getNoTypeParams();
Expect.equals(cm.typeVariables.length, 0);
}
void testA(Env env) {
ClassMirror a = env.getA();
Expect.equals(2, a.typeVariables.length);
TypeVariableMirror aT = a.typeVariables[0];
TypeVariableMirror aS = a.typeVariables[1];
ClassMirror aTBound = aT.upperBound as ClassMirror;
ClassMirror aSBound = aS.upperBound as ClassMirror;
Expect.isTrue(aTBound.isOriginalDeclaration);
Expect.isTrue(aSBound.isOriginalDeclaration);
Expect.equals(env.getObject(), aTBound);
Expect.equals(env.getString(), aSBound);
}
void testBAndC(Env env) {
ClassMirror b = env.getB();
ClassMirror c = env.getC();
Expect.equals(1, b.typeVariables.length);
Expect.equals(1, c.typeVariables.length);
TypeVariableMirror bZ = b.typeVariables[0];
TypeVariableMirror cZ = c.typeVariables[0];
ClassMirror bZBound = bZ.upperBound as ClassMirror;
ClassMirror cZBound = cZ.upperBound as ClassMirror;
Expect.isFalse(bZBound.isOriginalDeclaration);
Expect.isFalse(cZBound.isOriginalDeclaration);
Expect.notEquals(bZBound, cZBound);
Expect.equals(b, bZBound.originalDeclaration);
Expect.equals(b, cZBound.originalDeclaration);
TypeMirror bZBoundTypeArgument = bZBound.typeArguments.single;
TypeMirror cZBoundTypeArgument = cZBound.typeArguments.single;
TypeVariableMirror bZBoundTypeVariable = bZBound.typeVariables.single;
TypeVariableMirror cZBoundTypeVariable = cZBound.typeVariables.single;
Expect.equals(b, bZ.owner);
Expect.equals(c, cZ.owner);
Expect.equals(b, bZBoundTypeVariable.owner);
Expect.equals(b, cZBoundTypeVariable.owner);
Expect.equals(b, bZBoundTypeArgument.owner);
Expect.equals(c, cZBoundTypeArgument.owner);
Expect.notEquals(bZ, cZ);
Expect.equals(bZ, bZBoundTypeArgument);
Expect.equals(cZ, cZBoundTypeArgument);
Expect.equals(bZ, bZBoundTypeVariable);
Expect.equals(bZ, cZBoundTypeVariable);
}
testD(Env env) {
ClassMirror cm = env.getD();
Expect.equals(3, cm.typeVariables.length);
var values = cm.typeVariables;
values.forEach((e) {
Expect.equals(true, e is TypeVariableMirror);
});
Expect.equals(#R, values.elementAt(0).simpleName);
Expect.equals(#S, values.elementAt(1).simpleName);
Expect.equals(#T, values.elementAt(2).simpleName);
}
void testE(Env env) {
ClassMirror e = env.getE();
TypeVariableMirror eR = e.typeVariables.single;
ClassMirror mapRAndHelperOfString = eR.upperBound as ClassMirror;
Expect.isFalse(mapRAndHelperOfString.isOriginalDeclaration);
Expect.equals(eR, mapRAndHelperOfString.typeArguments.first);
Expect.equals(
env.getHelperOfString(), mapRAndHelperOfString.typeArguments.last);
}
void testF(Env env) {
ClassMirror f = env.getF();
TypeVariableMirror fZ = f.typeVariables[0];
ClassMirror fZBound = fZ.upperBound as ClassMirror;
ClassMirror fZBoundTypeArgument = fZBound.typeArguments.single as ClassMirror;
Expect.equals(1, f.typeVariables.length);
Expect.isFalse(fZBound.isOriginalDeclaration);
Expect.isFalse(fZBoundTypeArgument.isOriginalDeclaration);
Expect.equals(f, fZBoundTypeArgument.originalDeclaration);
Expect.equals(fZ, fZBoundTypeArgument.typeArguments.single);
}

View file

@ -0,0 +1,27 @@
// Copyright (c) 2013, 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.
import "dart:mirrors";
import "package:expect/expect.dart";
import "class_mirror_type_variables_data.dart";
import "class_mirror_type_variables_expect.dart";
class RuntimeEnv implements Env {
ClassMirror getA() => reflectClass(A);
ClassMirror getB() => reflectClass(B);
ClassMirror getC() => reflectClass(C);
ClassMirror getD() => reflectClass(D);
ClassMirror getE() => reflectClass(E);
ClassMirror getF() => reflectClass(F);
ClassMirror getNoTypeParams() => reflectClass(NoTypeParams);
ClassMirror getObject() => reflectClass(Object);
ClassMirror getString() => reflectClass(String);
ClassMirror getHelperOfString() => reflect(new Helper<String>()).type;
}
main() {
test(new RuntimeEnv());
}

View file

@ -0,0 +1,17 @@
// Copyright (c) 2013, 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 closure_mirror_import1;
export "closure_mirror_import2.dart" show firstGlobalVariableInImport2;
var globalVariableInImport1 = "globalVariableInImport1";
globalFunctionInImport1() => "globalFunctionInImport1";
class StaticClass {
static var staticField = "staticField";
static staticFunctionInStaticClass() => "staticFunctionInStaticClass";
}

View file

@ -0,0 +1,8 @@
// Copyright (c) 2013, 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 closure_mirror_import2;
var firstGlobalVariableInImport2 = "firstGlobalVariableInImport2";
var secondGlobalVariableInImport2 = "secondGlobalVariableInImport2";

View file

@ -0,0 +1,28 @@
// Copyright (c) 2013, 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.
import 'dart:mirrors';
import 'package:expect/expect.dart';
import 'stringify.dart';
testIntercepted() {
var instance = [];
var closureMirror = reflect(instance.toString) as ClosureMirror;
var methodMirror = closureMirror.function;
Expect.equals(#toString, methodMirror.simpleName);
Expect.equals('[]', closureMirror.apply([]).reflectee);
}
testNonIntercepted() {
var closure = new Map().containsKey;
var closureMirror = reflect(closure) as ClosureMirror;
var methodMirror = closureMirror.function;
Expect.equals(#containsKey, methodMirror.simpleName);
Expect.isFalse(closureMirror.apply([7]).reflectee);
}
main() {
testIntercepted();
testNonIntercepted();
}

View file

@ -0,0 +1,25 @@
// Copyright (c) 2015, 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.
import 'dart:mirrors';
import 'package:expect/expect.dart';
topLevelMethod() {}
class C {
static staticMethod() {}
instanceMethod() {}
}
main() {
LibraryMirror thisLibrary = reflectClass(C).owner as LibraryMirror;
Expect.equals(thisLibrary.declarations[#topLevelMethod],
(reflect(topLevelMethod) as ClosureMirror).function, "topLevel");
Expect.equals(reflectClass(C).declarations[#staticMethod],
(reflect(C.staticMethod) as ClosureMirror).function, "static");
Expect.equals(reflectClass(C).declarations[#instanceMethod],
(reflect(new C().instanceMethod) as ClosureMirror).function, "instance");
}

View file

@ -0,0 +1,21 @@
// 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.
// Check that compile-time evaluation of constants is consistent with runtime
// evaluation.
import 'dart:mirrors';
import 'package:expect/expect.dart';
const top_const = identical(-0.0, 0);
@top_const
class C {}
void main() {
var local_var = identical(-0.0, 0);
var metadata = reflectClass(C).metadata[0].reflectee;
Expect.equals(top_const, metadata);
Expect.equals(local_var, metadata);
}

View file

@ -0,0 +1,112 @@
// Copyright (c) 2013, 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.constructor_kinds_test;
import 'dart:mirrors';
import 'package:expect/expect.dart';
class ClassWithDefaultConstructor {}
class Class {
Class.generativeConstructor();
Class.redirectingGenerativeConstructor() : this.generativeConstructor();
factory Class.factoryConstructor() => new Class.generativeConstructor();
factory Class.redirectingFactoryConstructor() = Class.factoryConstructor;
const Class.constGenerativeConstructor();
const Class.constRedirectingGenerativeConstructor()
: this.constGenerativeConstructor();
// Not legal.
// const factory Class.constFactoryConstructor() => ...
const factory Class.constRedirectingFactoryConstructor() =
Class.constGenerativeConstructor;
}
main() {
ClassMirror cm;
MethodMirror mm;
// Multitest with and without constructor calls. On the VM, we want to check
// that constructor properties are correctly set even if the constructor
// hasn't been fully compiled. On dart2js, we want to check that constructors
// are retain even if there are no base-level calls.
new ClassWithDefaultConstructor(); // //# 01: ok
new Class.generativeConstructor(); // //# 01: ok
new Class.redirectingGenerativeConstructor(); // //# 01: ok
new Class.factoryConstructor(); // //# 01: ok
new Class.redirectingFactoryConstructor(); // //# 01: ok
const Class.constGenerativeConstructor(); // //# 01: ok
const Class.constRedirectingGenerativeConstructor(); // //# 01: ok
const Class.constRedirectingFactoryConstructor(); // //# 01: ok
cm = reflectClass(ClassWithDefaultConstructor);
mm = cm.declarations.values
.where((d) => d is MethodMirror && d.isConstructor)
.single as MethodMirror;
Expect.isTrue(mm.isConstructor);
Expect.isTrue(mm.isGenerativeConstructor);
Expect.isFalse(mm.isFactoryConstructor);
Expect.isFalse(mm.isRedirectingConstructor);
Expect.isFalse(mm.isConstConstructor);
cm = reflectClass(Class);
mm = cm.declarations[#Class.generativeConstructor] as MethodMirror;
Expect.isTrue(mm.isConstructor);
Expect.isTrue(mm.isGenerativeConstructor);
Expect.isFalse(mm.isFactoryConstructor);
Expect.isFalse(mm.isRedirectingConstructor);
Expect.isFalse(mm.isConstConstructor);
mm = cm.declarations[#Class.redirectingGenerativeConstructor] as MethodMirror;
Expect.isTrue(mm.isConstructor);
Expect.isTrue(mm.isGenerativeConstructor);
Expect.isFalse(mm.isFactoryConstructor);
Expect.isTrue(mm.isRedirectingConstructor);
Expect.isFalse(mm.isConstConstructor);
mm = cm.declarations[#Class.factoryConstructor] as MethodMirror;
Expect.isTrue(mm.isConstructor);
Expect.isFalse(mm.isGenerativeConstructor);
Expect.isTrue(mm.isFactoryConstructor);
Expect.isFalse(mm.isRedirectingConstructor);
Expect.isFalse(mm.isConstConstructor);
mm = cm.declarations[#Class.redirectingFactoryConstructor] as MethodMirror;
Expect.isTrue(mm.isConstructor);
Expect.isFalse(mm.isGenerativeConstructor);
Expect.isTrue(mm.isFactoryConstructor);
Expect.isTrue(mm.isRedirectingConstructor);
Expect.isFalse(mm.isConstConstructor);
mm = cm.declarations[#Class.constGenerativeConstructor] as MethodMirror;
Expect.isTrue(mm.isConstructor);
Expect.isTrue(mm.isGenerativeConstructor);
Expect.isFalse(mm.isFactoryConstructor);
Expect.isFalse(mm.isRedirectingConstructor);
Expect.isTrue(mm.isConstConstructor);
mm = cm.declarations[#Class.constRedirectingGenerativeConstructor] as MethodMirror;
Expect.isTrue(mm.isConstructor);
Expect.isTrue(mm.isGenerativeConstructor);
Expect.isFalse(mm.isFactoryConstructor);
Expect.isTrue(mm.isRedirectingConstructor);
Expect.isTrue(mm.isConstConstructor);
// Not legal.
// mm = cm.declarations[#Class.constFactoryConstructor] as MethodMirror;
// Expect.isTrue(mm.isConstructor);
// Expect.isFalse(mm.isGenerativeConstructor);
// Expect.isTrue(mm.isFactoryConstructor);
// Expect.isFalse(mm.isRedirectingConstructor);
// Expect.isTrue(mm.isConstConstructor);
mm = cm.declarations[#Class.constRedirectingFactoryConstructor] as MethodMirror;
Expect.isTrue(mm.isConstructor);
Expect.isFalse(mm.isGenerativeConstructor);
Expect.isTrue(mm.isFactoryConstructor);
Expect.isTrue(mm.isRedirectingConstructor);
Expect.isTrue(mm.isConstConstructor);
}

View file

@ -0,0 +1,62 @@
// Copyright (c) 2016, 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.constructor_test;
import 'dart:mirrors';
import 'package:expect/expect.dart';
class A {
factory A([x, y]) = B;
factory A.more([x, y]) = B.more;
factory A.oneMore(x, [y]) = B.more;
}
class B implements A {
final _x, _y, _z;
B([x = 'x', y = 'y'])
: _x = x,
_y = y,
_z = null;
B.more([x = 'x', y = 'y', z = 'z'])
: _x = x,
_y = y,
_z = z;
toString() => 'B(x=$_x, y=$_y, z=$_z)';
}
main() {
var d1 = new A(1);
Expect.equals('B(x=1, y=y, z=null)', '$d1', 'direct 1');
var d2 = new A.more(1);
Expect.equals('B(x=1, y=y, z=z)', '$d2', 'direct 2');
ClassMirror cm = reflectClass(A);
var v1 = cm.newInstance(Symbol.empty, []).reflectee;
var v2 = cm.newInstance(Symbol.empty, [1]).reflectee;
var v3 = cm.newInstance(Symbol.empty, [2, 3]).reflectee;
Expect.equals('B(x=x, y=y, z=null)', '$v1', 'unnamed 1');
Expect.equals('B(x=1, y=y, z=null)', '$v2', 'unnamed 2');
Expect.equals('B(x=2, y=3, z=null)', '$v3', 'unnamed 3');
var m1 = cm.newInstance(const Symbol('more'), []).reflectee;
var m2 = cm.newInstance(const Symbol('more'), [1]).reflectee;
var m3 = cm.newInstance(const Symbol('more'), [2, 3]).reflectee;
Expect.equals('B(x=x, y=y, z=z)', '$m1', 'more 1');
Expect.equals('B(x=1, y=y, z=z)', '$m2', 'more 2');
Expect.equals('B(x=2, y=3, z=z)', '$m3', 'more 3');
var o1 = cm.newInstance(const Symbol('oneMore'), [1]).reflectee;
var o2 = cm.newInstance(const Symbol('oneMore'), [2, 3]).reflectee;
Expect.equals('B(x=1, y=y, z=z)', '$o1', 'oneMore one arg');
Expect.equals('B(x=2, y=3, z=z)', '$o2', 'oneMore two args');
}

View file

@ -0,0 +1,31 @@
// Copyright (c) 2013, 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.constructors_test;
// Regression test for C1 bug.
import 'dart:mirrors';
import 'package:expect/expect.dart';
class Foo {
Foo._private();
}
class _Foo {
_Foo._private();
}
main() {
ClassMirror fooMirror = reflectClass(Foo);
Symbol constructorName =
(fooMirror.declarations[#Foo._private] as MethodMirror).constructorName;
fooMirror.newInstance(constructorName, []);
ClassMirror _fooMirror = reflectClass(_Foo);
constructorName =
(_fooMirror.declarations[#_Foo._private] as MethodMirror).constructorName;
_fooMirror.newInstance(constructorName, []);
}

View file

@ -0,0 +1,73 @@
// Copyright (c) 2013, 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.constructors_test;
import 'dart:mirrors';
import 'package:expect/expect.dart';
import 'stringify.dart';
constructorsOf(ClassMirror cm) {
var result = new Map();
cm.declarations.forEach((k, v) {
if (v is MethodMirror && v.isConstructor) result[k] = v;
});
return result;
}
class Foo {}
class Bar {
Bar();
}
class Baz {
Baz.named();
}
class Biz {
Biz();
Biz.named();
}
main() {
ClassMirror fooMirror = reflectClass(Foo);
Map<Symbol, MethodMirror> fooConstructors = constructorsOf(fooMirror);
ClassMirror barMirror = reflectClass(Bar);
Map<Symbol, MethodMirror> barConstructors = constructorsOf(barMirror);
ClassMirror bazMirror = reflectClass(Baz);
Map<Symbol, MethodMirror> bazConstructors = constructorsOf(bazMirror);
ClassMirror bizMirror = reflectClass(Biz);
Map<Symbol, MethodMirror> bizConstructors = constructorsOf(bizMirror);
expect('{Foo: Method(s(Foo) in s(Foo), constructor)}', fooConstructors);
expect('{Bar: Method(s(Bar) in s(Bar), constructor)}', barConstructors);
expect('{Baz.named: Method(s(Baz.named) in s(Baz), constructor)}',
bazConstructors);
expect(
'{Biz: Method(s(Biz) in s(Biz), constructor),'
' Biz.named: Method(s(Biz.named) in s(Biz), constructor)}',
bizConstructors);
print(bizConstructors);
expect('[]', fooConstructors.values.single.parameters);
expect('[]', barConstructors.values.single.parameters);
expect('[]', bazConstructors.values.single.parameters);
for (var constructor in bizConstructors.values) {
expect('[]', constructor.parameters);
}
expect(
'[s()]', fooConstructors.values.map((m) => m.constructorName).toList());
expect(
'[s()]', barConstructors.values.map((m) => m.constructorName).toList());
expect('[s(named)]',
bazConstructors.values.map((m) => m.constructorName).toList());
expect(
'[s(), s(named)]',
bizConstructors.values.map((m) => m.constructorName).toList()
..sort(compareSymbols));
}

View file

@ -0,0 +1,13 @@
// Copyright (c) 2013, 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.
// This test should be removed when dart2js can pass all mirror tests.
// TODO(ahe): Remove this test.
import 'mirrors_test.dart' as test;
main() {
test.isDart2js = true;
test.main();
}

View file

@ -0,0 +1,166 @@
// Copyright (c) 2013, 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.declarations_model;
var libraryVariable;
get libraryGetter => null;
set librarySetter(x) => x;
libraryMethod() => null;
var _libraryVariable;
get _libraryGetter => null;
set _librarySetter(x) => x;
_libraryMethod() => null;
typedef bool Predicate(dynamic);
abstract class Interface<I> {
operator /(x) => null;
var interfaceInstanceVariable;
get interfaceInstanceGetter;
set interfaceInstanceSetter(x);
interfaceInstanceMethod();
var _interfaceInstanceVariable;
get _interfaceInstanceGetter;
set _interfaceInstanceSetter(x);
_interfaceInstanceMethod();
static var interfaceStaticVariable;
static get interfaceStaticGetter => null;
static set interfaceStaticSetter(x) => x;
static interfaceStaticMethod() => null;
static var _interfaceStaticVariable;
static get _interfaceStaticGetter => null;
static set _interfaceStaticSetter(x) => x;
static _interfaceStaticMethod() => null;
}
class Mixin<M> {
operator *(x) => null;
var mixinInstanceVariable;
get mixinInstanceGetter => null;
set mixinInstanceSetter(x) => x;
mixinInstanceMethod() => null;
var _mixinInstanceVariable;
get _mixinInstanceGetter => null;
set _mixinInstanceSetter(x) => x;
_mixinInstanceMethod() => null;
static var mixinStaticVariable;
static get mixinStaticGetter => null;
static set mixinStaticSetter(x) => x;
static mixinStaticMethod() => null;
static var _mixinStaticVariable;
static get _mixinStaticGetter => null;
static set _mixinStaticSetter(x) => x;
static _mixinStaticMethod() => null;
}
class Superclass<S> {
operator -(x) => null;
var inheritedInstanceVariable;
get inheritedInstanceGetter => null;
set inheritedInstanceSetter(x) => x;
inheritedInstanceMethod() => null;
var _inheritedInstanceVariable;
get _inheritedInstanceGetter => null;
set _inheritedInstanceSetter(x) => x;
_inheritedInstanceMethod() => null;
static var inheritedStaticVariable;
static get inheritedStaticGetter => null;
static set inheritedStaticSetter(x) => x;
static inheritedStaticMethod() => null;
static var _inheritedStaticVariable;
static get _inheritedStaticGetter => null;
static set _inheritedStaticSetter(x) => x;
static _inheritedStaticMethod() => null;
Superclass.inheritedGenerativeConstructor(this.inheritedInstanceVariable);
Superclass.inheritedRedirectingConstructor(x)
: this.inheritedGenerativeConstructor(x * 2);
factory Superclass.inheritedNormalFactory(y) =>
new Superclass.inheritedRedirectingConstructor(y * 3);
factory Superclass.inheritedRedirectingFactory(z) =
Superclass<S>.inheritedNormalFactory;
Superclass._inheritedGenerativeConstructor(this._inheritedInstanceVariable);
Superclass._inheritedRedirectingConstructor(x)
: this._inheritedGenerativeConstructor(x * 2);
factory Superclass._inheritedNormalFactory(y) =>
new Superclass._inheritedRedirectingConstructor(y * 3);
factory Superclass._inheritedRedirectingFactory(z) =
Superclass<S>._inheritedNormalFactory;
}
abstract class Class<C> extends Superclass<C>
with Mixin<C>
implements Interface<C> {
operator +(x) => null;
abstractMethod();
var instanceVariable;
get instanceGetter => null;
set instanceSetter(x) => x;
instanceMethod() => null;
var _instanceVariable;
get _instanceGetter => null;
set _instanceSetter(x) => x;
_instanceMethod() => null;
static var staticVariable;
static get staticGetter => null;
static set staticSetter(x) => x;
static staticMethod() => null;
static var _staticVariable;
static get _staticGetter => null;
static set _staticSetter(x) => x;
static _staticMethod() => null;
Class.generativeConstructor(this.instanceVariable)
: super.inheritedGenerativeConstructor(0);
Class.redirectingConstructor(x) : this.generativeConstructor(x * 2);
factory Class.normalFactory(y) => new ConcreteClass(y * 3);
factory Class.redirectingFactory(z) = Class<C>.normalFactory;
Class._generativeConstructor(this._instanceVariable)
: super._inheritedGenerativeConstructor(0);
Class._redirectingConstructor(x) : this._generativeConstructor(x * 2);
factory Class._normalFactory(y) => new ConcreteClass(y * 3);
factory Class._redirectingFactory(z) = Class<C>._normalFactory;
}
// This is just here as a target of Class's factories to appease the analyzer.
class ConcreteClass<CC> extends Class<CC> {
abstractMethod() {}
operator /(x) => null;
var interfaceInstanceVariable;
get interfaceInstanceGetter => null;
set interfaceInstanceSetter(x) => null;
interfaceInstanceMethod() => null;
var _interfaceInstanceVariable;
get _interfaceInstanceGetter => null;
set _interfaceInstanceSetter(x) => null;
_interfaceInstanceMethod() => null;
ConcreteClass(x) : super.generativeConstructor(x);
}
class _PrivateClass {}

View file

@ -0,0 +1,84 @@
// Copyright (c) 2013, 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.declarations_model;
var libraryVariable;
get libraryGetter => null;
set librarySetter(x) => x;
libraryMethod() => null;
typedef bool Predicate(dynamic);
abstract class Interface<I> {
operator /(x) => null;
var interfaceInstanceVariable;
get interfaceInstanceGetter;
set interfaceInstanceSetter(x);
interfaceInstanceMethod();
static var interfaceStaticVariable;
static get interfaceStaticGetter => null;
static set interfaceStaticSetter(x) => x;
static interfaceStaticMethod() => null;
}
class Superclass<S> {
operator -(x) => null;
var inheritedInstanceVariable;
get inheritedInstanceGetter => null;
set inheritedInstanceSetter(x) => x;
inheritedInstanceMethod() => null;
static var inheritedStaticVariable;
static get inheritedStaticGetter => null;
static set inheritedStaticSetter(x) => x;
static inheritedStaticMethod() => null;
Superclass.inheritedGenerativeConstructor(this.inheritedInstanceVariable);
Superclass.inheritedRedirectingConstructor(x)
: this.inheritedGenerativeConstructor(x * 2);
factory Superclass.inheritedNormalFactory(y) =>
new Superclass.inheritedRedirectingConstructor(y * 3);
factory Superclass.inheritedRedirectingFactory(z) =
Superclass<S>.inheritedNormalFactory;
}
abstract class Class<C> extends Superclass<C> implements Interface<C> {
operator +(x) => null;
abstractMethod();
var instanceVariable;
get instanceGetter => null;
set instanceSetter(x) => x;
instanceMethod() => null;
static var staticVariable;
static get staticGetter => null;
static set staticSetter(x) => x;
static staticMethod() => null;
Class.generativeConstructor(this.instanceVariable)
: super.inheritedGenerativeConstructor(0);
Class.redirectingConstructor(x) : this.generativeConstructor(x * 2);
factory Class.normalFactory(y) => new ConcreteClass(y * 3);
factory Class.redirectingFactory(z) = Class<C>.normalFactory;
}
// This is just here as a target of Class's factories to appease the analyzer.
class ConcreteClass<CC> extends Class<CC> {
abstractMethod() {}
operator /(x) => null;
var interfaceInstanceVariable;
get interfaceInstanceGetter => null;
set interfaceInstanceSetter(x) => null;
interfaceInstanceMethod() => null;
ConcreteClass(x) : super.generativeConstructor(x);
}

View file

@ -0,0 +1,35 @@
// 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.
// Regression test for Issue 14972.
library test.declarations_type;
import 'dart:mirrors';
import 'package:expect/expect.dart';
class C {}
main() {
var classDeclarations = reflectClass(C).declarations;
Expect.isTrue(classDeclarations is Map<Symbol, DeclarationMirror>);
Expect.isTrue(classDeclarations.values is Iterable<DeclarationMirror>);
Expect.isTrue(classDeclarations.values.where((x) => true)
is Iterable<DeclarationMirror>);
Expect.isFalse(classDeclarations is Map<Symbol, MethodMirror>);
Expect.isFalse(classDeclarations.values is Iterable<MethodMirror>);
Expect.isFalse(
classDeclarations.values.where((x) => true) is Iterable<MethodMirror>);
var libraryDeclarations =
(reflectClass(C).owner as LibraryMirror).declarations;
Expect.isTrue(libraryDeclarations is Map<Symbol, DeclarationMirror>);
Expect.isTrue(libraryDeclarations.values is Iterable<DeclarationMirror>);
Expect.isTrue(libraryDeclarations.values.where((x) => true)
is Iterable<DeclarationMirror>);
Expect.isFalse(libraryDeclarations is Map<Symbol, ClassMirror>);
Expect.isFalse(libraryDeclarations.values is Iterable<ClassMirror>);
Expect.isFalse(
libraryDeclarations.values.where((x) => true) is Iterable<ClassMirror>);
}

View file

@ -0,0 +1,17 @@
// 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.
class C {
static int staticMethod() => 42;
}
class G<T> {}
class Const {
const Const();
const Const.namedConstructor();
static const instance = const Const();
}
const constantInstance = const Const();

View file

@ -0,0 +1,70 @@
// 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.
import 'package:expect/expect.dart';
import 'package:async_helper/async_helper.dart';
import 'dart:mirrors';
import "deferred_constraints_constants_lib.dart" deferred as lib;
const myConst1 =
lib.constantInstance; //# reference1: compile-time error
/* // //# reference1: continued
499;
*/ // //# reference1: continued
const myConst2 =
lib.Const.instance; //# reference2: compile-time error
/* // //# reference2: continued
499;
*/ // //# reference2: continued
void f1(
{a:
const lib.Const() //# default_argument1: compile-time error
/* // //# default_argument1: continued
499
*/ // //# default_argument1: continued
}) {}
void f2(
{a:
lib.constantInstance //# default_argument2: compile-time error
/* // //# default_argument2: continued
499
*/ // //# default_argument2: continued
}) {}
@lib.Const() //# metadata1: compile-time error
class H1 {}
@lib.Const.instance //# metadata2: compile-time error
class H2 {}
@lib.Const.namedConstructor() //# metadata3: compile-time error
class H3 {}
void main() {
var a1 = myConst1;
var a2 = myConst2;
asyncStart();
lib.loadLibrary().then((_) {
var instance = lib.constantInstance;
var c1 = const lib.Const(); //# constructor1: compile-time error
var c2 = const lib.Const.namedConstructor(); //# constructor2: compile-time error
f1();
f2();
var constInstance = lib.constantInstance; //# reference_after_load: ok
var h1 = new H1();
var h2 = new H2();
var h3 = new H3();
// Need to access the metadata to trigger the expected compilation error.
reflectClass(H1).metadata; //# metadata1: continued
reflectClass(H2).metadata; //# metadata2: continued
reflectClass(H3).metadata; //# metadata3: continued
asyncEnd();
});
}

View file

@ -0,0 +1,31 @@
library lib;
import "deferred_mirrors_metadata_test.dart";
import "dart:mirrors";
class H {
const H();
}
class F {
@H()
int f = 0;
}
@C()
class E {
@D()
dynamic f;
}
String foo() {
String c = reflectClass(E).metadata[0].invoke(#toString, []).reflectee;
String d = reflectClass(E)
.declarations[#f]
.metadata[0]
.invoke(#toString, []).reflectee;
InstanceMirror i = currentMirrorSystem().findLibrary(#main).metadata[0];
String a = i.invoke(#toString, []).reflectee;
String b = i.getField(#b).invoke(#toString, []).reflectee;
return a + b + c + d;
}

View file

@ -0,0 +1,45 @@
// 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.
@A(const B())
library main;
@B()
import 'package:async_helper/async_helper.dart';
import 'package:expect/expect.dart';
import "dart:math";
import 'deferred_mirrors_metadata_lib.dart' deferred as lib1;
class A {
final B b;
const A(this.b);
String toString() => "A";
}
class B {
const B();
String toString() => "B";
}
class C {
const C();
String toString() => "C";
}
class D {
const D();
String toString() => "D";
}
void main() {
asyncStart();
lib1.loadLibrary().then((_) {
Expect.equals("ABCD", lib1.foo());
new C();
new D();
asyncEnd();
});
}

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 lib;
import "dart:mirrors";
class MetaTarget {
const MetaTarget();
}
@MetaTarget()
class A {
String toString() => "A";
}
String foo() {
final a =
currentMirrorSystem().findLibrary(#lib).declarations[#A] as ClassMirror;
return a.newInstance(Symbol.empty, []).invoke(#toString, []).reflectee;
}

View file

@ -0,0 +1,18 @@
// 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.
/// Test that metaTargets can be reached via the mirrorSystem.
import 'package:async_helper/async_helper.dart';
import 'package:expect/expect.dart';
import "deferred_mirrors_metatarget_lib.dart" deferred as lib;
void main() {
asyncStart();
lib.loadLibrary().then((_) {
Expect.equals("A", lib.foo());
asyncEnd();
});
}

View file

@ -0,0 +1,10 @@
library lib;
import "dart:mirrors";
class C {}
foo() {
var a = new C();
print(reflectClass(C).owner);
}

View file

@ -0,0 +1,16 @@
library main;
// Test that the library-mirrors are updated after loading a deferred library.
import "dart:mirrors";
import "deferred_mirrors_update_lib.dart" deferred as l;
class D {}
void main() {
print(reflectClass(D).owner);
l.loadLibrary().then((_) {
l.foo();
});
}

View file

@ -0,0 +1,7 @@
// Copyright (c) 2015, 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 deferred_type_other;
class DeferredType {}

View file

@ -0,0 +1,18 @@
// Copyright (c) 2015, 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 deferred_type;
import 'dart:mirrors';
import 'package:expect/expect.dart';
import 'package:async_helper/async_helper.dart';
import 'deferred_type_other.dart' deferred as other;
bad(other.DeferredType x) {}
main() {
print((reflect(bad) as ClosureMirror).function.parameters[0].type);
throw "Should have died sooner. other.DeferredType is not loaded";
}

View file

@ -0,0 +1,46 @@
// Copyright (c) 2013, 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.invoke_call_through_getter;
import 'dart:mirrors';
import 'package:expect/expect.dart';
class FakeFunctionCall {
call(x, y) => '1 $x $y';
}
class FakeFunctionNSM {
noSuchMethod(msg) => msg.positionalArguments.join(', ');
}
class C {
get fakeFunctionCall => new FakeFunctionCall();
get fakeFunctionNSM => new FakeFunctionNSM();
get closure => (x, y) => '2 $this $x $y';
get closureOpt => (x, y, [z, w]) => '3 $this $x $y $z $w';
get closureNamed => (x, y, {z, w}) => '4 $this $x $y $z $w';
get notAClosure => 'Not a closure';
noSuchMethod(msg) => 'DNU';
toString() => 'C';
}
class Forwarder {
dynamic noSuchMethod(Invocation msg) => reflect(new C()).delegate(msg);
}
main() {
dynamic f = new Forwarder();
Expect.equals('1 5 6', f.fakeFunctionCall(5, 6));
Expect.equals('7, 8', f.fakeFunctionNSM(7, 8));
Expect.equals('2 C 9 10', f.closure(9, 10));
Expect.equals('3 C 11 12 13 null', f.closureOpt(11, 12, 13));
Expect.equals('4 C 14 15 null 16', f.closureNamed(14, 15, w: 16));
Expect.equals('DNU', f.doesNotExist(17, 18));
Expect.throwsNoSuchMethodError(() => f.closure('wrong arity'));
Expect.throwsNoSuchMethodError(() => f.notAClosure());
}

View file

@ -0,0 +1,50 @@
// Copyright (c) 2015, 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.delegate_class;
import 'dart:mirrors';
import 'package:expect/expect.dart';
class C {
static method(a, b, c) => "$a-$b-$c";
static methodWithNamed(a, {b: 'B', c}) => "$a-$b-$c";
static methodWithOpt(a, [b, c = 'C']) => "$a-$b-$c";
static get getter => 'g';
static set setter(x) {
field = x * 2;
}
static var field;
}
class Proxy {
var targetMirror;
Proxy(this.targetMirror);
noSuchMethod(invocation) => targetMirror.delegate(invocation);
}
main() {
dynamic proxy = new Proxy(reflectClass(C));
var result;
Expect.equals('X-Y-Z', proxy.method('X', 'Y', 'Z'));
Expect.equals('X-B-null', proxy.methodWithNamed('X'));
Expect.equals('X-Y-null', proxy.methodWithNamed('X', b: 'Y'));
Expect.equals('X-Y-Z', proxy.methodWithNamed('X', b: 'Y', c: 'Z'));
Expect.equals('X-null-C', proxy.methodWithOpt('X'));
Expect.equals('X-Y-C', proxy.methodWithOpt('X', 'Y'));
Expect.equals('X-Y-Z', proxy.methodWithOpt('X', 'Y', 'Z'));
Expect.equals('g', proxy.getter);
Expect.equals(5, proxy.setter = 5);
Expect.equals(10, proxy.field);
Expect.equals(5, proxy.field = 5);
Expect.equals(5, proxy.field);
}

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.delgate_function_invocation;
import 'dart:mirrors';
import 'package:expect/expect.dart';
class Proxy {
var targetMirror;
Proxy(target) : this.targetMirror = reflect(target);
noSuchMethod(invocation) => targetMirror.delegate(invocation);
}
testClosure() {
dynamic proxy = new Proxy(() => 42);
Expect.equals(42, proxy());
Expect.equals(42, proxy.call());
}
class FakeFunction {
call() => 43;
}
testFakeFunction() {
dynamic proxy = new Proxy(new FakeFunction());
Expect.equals(43, proxy());
Expect.equals(43, proxy.call());
}
topLevelFunction() => 44;
testTopLevelTearOff() {
dynamic proxy = new Proxy(topLevelFunction);
Expect.equals(44, proxy());
Expect.equals(44, proxy.call());
}
class C {
method() => 45;
}
testInstanceTearOff() {
dynamic proxy = new Proxy(new C().method);
Expect.equals(45, proxy());
Expect.equals(45, proxy.call());
}
main() {
testClosure();
testFakeFunction();
testTopLevelTearOff();
testInstanceTearOff();
}

View file

@ -0,0 +1,48 @@
// Copyright (c) 2015, 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.delegate_library;
import 'dart:mirrors';
import 'package:expect/expect.dart';
method(a, b, c) => "$a-$b-$c";
methodWithNamed(a, {b: 'B', c}) => "$a-$b-$c";
methodWithOpt(a, [b, c = 'C']) => "$a-$b-$c";
get getter => 'g';
set setter(x) {
field = x * 2;
}
var field;
class Proxy {
var targetMirror;
Proxy(this.targetMirror);
noSuchMethod(invocation) => targetMirror.delegate(invocation);
}
main() {
dynamic proxy = new Proxy(reflectClass(Proxy).owner);
var result;
Expect.equals('X-Y-Z', proxy.method('X', 'Y', 'Z'));
Expect.equals('X-B-null', proxy.methodWithNamed('X'));
Expect.equals('X-Y-null', proxy.methodWithNamed('X', b: 'Y'));
Expect.equals('X-Y-Z', proxy.methodWithNamed('X', b: 'Y', c: 'Z'));
Expect.equals('X-null-C', proxy.methodWithOpt('X'));
Expect.equals('X-Y-C', proxy.methodWithOpt('X', 'Y'));
Expect.equals('X-Y-Z', proxy.methodWithOpt('X', 'Y', 'Z'));
Expect.equals('g', proxy.getter);
Expect.equals(5, proxy.setter = 5);
Expect.equals(10, proxy.field);
Expect.equals(5, proxy.field = 5);
Expect.equals(5, proxy.field);
}

View file

@ -0,0 +1,51 @@
// Copyright (c) 2013, 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.invoke_named_test;
import 'dart:mirrors';
import 'package:expect/expect.dart';
class C {
method(a, b, c) => "$a-$b-$c";
methodWithNamed(a, {b: 'B', c}) => "$a-$b-$c";
methodWithOpt(a, [b, c = 'C']) => "$a-$b-$c";
get getter => 'g';
set setter(x) {
field = x * 2;
}
var field;
}
class Proxy {
var targetMirror;
Proxy(target) : this.targetMirror = reflect(target);
noSuchMethod(invocation) => targetMirror.delegate(invocation);
}
main() {
var c = new C();
dynamic proxy = new Proxy(c);
var result;
Expect.equals('X-Y-Z', proxy.method('X', 'Y', 'Z'));
Expect.equals('X-B-null', proxy.methodWithNamed('X'));
Expect.equals('X-Y-null', proxy.methodWithNamed('X', b: 'Y'));
Expect.equals('X-Y-Z', proxy.methodWithNamed('X', b: 'Y', c: 'Z'));
Expect.equals('X-null-C', proxy.methodWithOpt('X'));
Expect.equals('X-Y-C', proxy.methodWithOpt('X', 'Y'));
Expect.equals('X-Y-Z', proxy.methodWithOpt('X', 'Y', 'Z'));
Expect.equals('g', proxy.getter);
Expect.equals(5, proxy.setter = 5);
Expect.equals(10, proxy.field);
Expect.equals(5, proxy.field = 5);
Expect.equals(5, proxy.field);
}

View file

@ -0,0 +1,23 @@
// Copyright (c) 2013, 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.
// Ensure that reflection works on methods that would otherwise be
// tree-shaken away.
import "dart:mirrors";
class Foo {
Foo();
foo() => 42;
}
main() {
// Do NOT instantiate Foo.
var m = reflectClass(Foo);
var instanceMirror = m.newInstance(new Symbol(''), []);
var result = instanceMirror.invoke(new Symbol('foo'), []).reflectee;
if (result != 42) {
throw 'Expected 42, but got $result';
}
}

View file

@ -0,0 +1,6 @@
// Copyright (c) 2018, 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.
// A top-level parse error:
import import import import

View file

@ -0,0 +1,9 @@
// Copyright (c) 2018, 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 dynamic_load_success;
int _counter = 0;
advanceCounter() => ++_counter;

View file

@ -0,0 +1,83 @@
// Copyright (c) 2018, 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.
import 'dart:async';
import 'dart:mirrors';
import 'package:expect/expect.dart';
main() async {
IsolateMirror isolate = currentMirrorSystem().isolate;
print(isolate);
LibraryMirror success =
await isolate.loadUri(Uri.parse("dynamic_load_success.dart"));
print(success);
InstanceMirror result = success.invoke(#advanceCounter, []);
print(result);
Expect.equals(1, result.reflectee);
result = success.invoke(#advanceCounter, []);
print(result);
Expect.equals(2, result.reflectee);
LibraryMirror success2 =
await isolate.loadUri(Uri.parse("dynamic_load_success.dart"));
print(success2);
Expect.equals(success, success2);
result = success2.invoke(#advanceCounter, []);
print(result);
Expect.equals(3, result.reflectee); // Same library, same state.
LibraryMirror math = await isolate.loadUri(Uri.parse("dart:math"));
result = math.invoke(#max, [3, 4]);
print(result);
Expect.equals(4, result.reflectee);
Future<LibraryMirror> bad_load = isolate.loadUri(Uri.parse("DOES_NOT_EXIST"));
var error;
try {
await bad_load;
} catch (e) {
error = e;
}
print(error);
Expect.isTrue(error.toString().contains("Cannot open file") ||
error.toString().contains("file not found") ||
error.toString().contains("No such file or directory") ||
error.toString().contains("The system cannot find the file specified"));
Expect.isTrue(error.toString().contains("DOES_NOT_EXIST"));
Future<LibraryMirror> bad_load2 = isolate.loadUri(Uri.parse("dart:_builtin"));
var error2;
try {
await bad_load2;
} catch (e) {
error2 = e;
}
print(error2);
Expect.isTrue(error2.toString().contains("Cannot load"));
Expect.isTrue(error2.toString().contains("dart:_builtin"));
// Check error is not sticky.
LibraryMirror success3 =
await isolate.loadUri(Uri.parse("dynamic_load_success.dart"));
print(success3);
Expect.equals(success, success3);
result = success3.invoke(#advanceCounter, []);
print(result);
Expect.equals(4, result.reflectee); // Same library, same state.
Future<LibraryMirror> bad_load3 =
isolate.loadUri(Uri.parse("dynamic_load_error.dart"));
var error3;
try {
await bad_load3;
} catch (e) {
error3 = e;
}
print(error3);
Expect.isTrue(error3.toString().contains("library url expected") ||
error3.toString().contains("Error: Expected a String"));
Expect.isTrue(error3.toString().contains("dynamic_load_error.dart"));
}

View file

@ -0,0 +1,6 @@
// Copyright (c) 2015, 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.
// This library has no functions.
library empty;

View file

@ -0,0 +1,11 @@
// Copyright (c) 2015, 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.
import 'dart:mirrors';
import 'empty.dart';
main() {
var empty = currentMirrorSystem().findLibrary(#empty);
print(empty.location); // Used to crash VM.
}

View file

@ -0,0 +1,15 @@
// 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.
import 'dart:mirrors';
import 'package:expect/expect.dart';
enum Foo { BAR, BAZ }
main() {
Expect.equals('Foo.BAR', Foo.BAR.toString());
var name = reflect(Foo.BAR).invoke(#toString, []).reflectee;
Expect.equals('Foo.BAR', name);
}

View file

@ -0,0 +1,65 @@
// Copyright (c) 2015, 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.enums;
import 'dart:mirrors';
import 'package:expect/expect.dart';
import 'stringify.dart';
class C {}
enum Suite { CLUBS, DIAMONDS, SPADES, HEARTS }
main() {
Expect.isFalse(reflectClass(C).isEnum);
Expect.isTrue(reflectClass(Suite).isEnum);
Expect.isFalse(reflectClass(Suite).isAbstract);
Expect.equals(
0,
reflectClass(Suite)
.declarations
.values
.where((d) => d is MethodMirror && d.isConstructor)
.length);
Expect.equals(
reflectClass(Suite),
(reflectClass(C).owner as LibraryMirror).declarations[#Suite],
"found in library");
Expect.equals(reflectClass(Suite), reflect(Suite.CLUBS).type);
Expect.equals(0, reflect(Suite.CLUBS).getField(#index).reflectee);
Expect.equals(1, reflect(Suite.DIAMONDS).getField(#index).reflectee);
Expect.equals(2, reflect(Suite.SPADES).getField(#index).reflectee);
Expect.equals(3, reflect(Suite.HEARTS).getField(#index).reflectee);
Expect.equals(
"Suite.CLUBS", reflect(Suite.CLUBS).invoke(#toString, []).reflectee);
Expect.equals("Suite.DIAMONDS",
reflect(Suite.DIAMONDS).invoke(#toString, []).reflectee);
Expect.equals(
"Suite.SPADES", reflect(Suite.SPADES).invoke(#toString, []).reflectee);
Expect.equals(
"Suite.HEARTS", reflect(Suite.HEARTS).invoke(#toString, []).reflectee);
Expect.setEquals(
[
'Variable(s(index) in s(Suite), final)',
'Variable(s(CLUBS) in s(Suite), static, final)',
'Variable(s(DIAMONDS) in s(Suite), static, final)',
'Variable(s(SPADES) in s(Suite), static, final)',
'Variable(s(HEARTS) in s(Suite), static, final)',
'Variable(s(values) in s(Suite), static, final)',
'Method(s(hashCode) in s(Suite), getter)',
'Method(s(toString) in s(Suite))'
],
reflectClass(Suite)
.declarations
.values
.where((d) => !d.isPrivate)
.map(stringify));
}

View file

@ -0,0 +1,159 @@
// Copyright (c) 2013, 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.
// This tests uses the multi-test "ok" feature:
// none: Trimmed behaviour. Passing on the VM.
// 01: Trimmed version for dart2js.
// 02: Full version passing in the VM.
//
// TODO(rmacnak,ahe): Remove multi-test when VM and dart2js are on par.
library test.class_equality_test;
import 'dart:mirrors';
import 'package:expect/expect.dart';
class A<T> {}
class B extends A<int> {}
class BadEqualityHash {
int count = 0;
bool operator ==(other) => true;
int get hashCode => count++;
}
typedef bool Predicate(Object o);
Predicate somePredicate = (Object o) => false;
checkEquality(List<Map<String, Object>> equivalenceClasses) {
for (var equivalenceClass in equivalenceClasses) {
equivalenceClass.forEach((name, member) {
equivalenceClass.forEach((otherName, otherMember) {
// Reflexivity, symmetry and transitivity.
Expect.equals(member, otherMember, "$name == $otherName");
Expect.equals(member.hashCode, otherMember.hashCode,
"$name.hashCode == $otherName.hashCode");
});
for (var otherEquivalenceClass in equivalenceClasses) {
if (otherEquivalenceClass == equivalenceClass) continue;
otherEquivalenceClass.forEach((otherName, otherMember) {
Expect.notEquals(
member, otherMember, "$name != $otherName"); // Exclusion.
// Hash codes may or may not be equal.
});
}
});
}
}
void subroutine() {}
main() {
LibraryMirror thisLibrary = currentMirrorSystem()
.findLibrary(const Symbol('test.class_equality_test'));
var o1 = new Object();
var o2 = new Object();
var badEqualityHash1 = new BadEqualityHash();
var badEqualityHash2 = new BadEqualityHash();
checkEquality(<Map<String, Object>>[
{'reflect(o1)': reflect(o1), 'reflect(o1), again': reflect(o1)},
{'reflect(o2)': reflect(o2), 'reflect(o2), again': reflect(o2)},
{
'reflect(badEqualityHash1)': reflect(badEqualityHash1),
'reflect(badEqualityHash1), again': reflect(badEqualityHash1)
},
{
'reflect(badEqualityHash2)': reflect(badEqualityHash2),
'reflect(badEqualityHash2), again': reflect(badEqualityHash2)
},
{'reflect(true)': reflect(true), 'reflect(true), again': reflect(true)},
{'reflect(false)': reflect(false), 'reflect(false), again': reflect(false)},
{'reflect(null)': reflect(null), 'reflect(null), again': reflect(null)},
{
'reflect(3.5+4.5)': reflect(3.5 + 4.5),
'reflect(6.5+1.5)': reflect(6.5 + 1.5)
},
{'reflect(3+4)': reflect(3 + 4), 'reflect(6+1)': reflect(6 + 1)},
{'reflect("foo")': reflect("foo"), 'reflect("foo"), again': reflect("foo")},
{
'currentMirrorSystem().voidType': currentMirrorSystem().voidType,
'thisLibrary.declarations[#subroutine].returnType':
(thisLibrary.declarations[#subroutine] as MethodMirror).returnType
},
{
'currentMirrorSystem().dynamicType': currentMirrorSystem().dynamicType,
'thisLibrary.declarations[#main].returnType':
(thisLibrary.declarations[#main] as MethodMirror).returnType
},
{
'reflectClass(A)': reflectClass(A),
'thisLibrary.declarations[#A]': thisLibrary.declarations[#A],
'reflect(new A<int>()).type.originalDeclaration':
reflect(new A<int>()).type.originalDeclaration
},
{
'reflectClass(B).superclass': reflectClass(B).superclass,
'reflect(new A<int>()).type': reflect(new A<int>()).type
},
{
'reflectClass(B)': reflectClass(B),
'thisLibrary.declarations[#B]': thisLibrary.declarations[#B],
'reflect(new B()).type': reflect(new B()).type
},
{
'reflectClass(BadEqualityHash).declarations[#==]':
reflectClass(BadEqualityHash).declarations[#==],
'reflect(new BadEqualityHash()).type.declarations[#==]':
reflect(new BadEqualityHash()).type.declarations[#==]
},
{
'reflectClass(BadEqualityHash).declarations[#==].parameters[0]':
(reflectClass(BadEqualityHash).declarations[#==] as MethodMirror)
.parameters[0],
'reflect(new BadEqualityHash()).type.declarations[#==].parameters[0]':
(reflect(new BadEqualityHash()).type.declarations[#==]
as MethodMirror)
.parameters[0]
},
{
'reflectClass(BadEqualityHash).declarations[#count]':
reflectClass(BadEqualityHash).declarations[#count],
'reflect(new BadEqualityHash()).type.declarations[#count]':
reflect(new BadEqualityHash()).type.declarations[#count]
},
{
'reflectType(Predicate)': reflectType(Predicate),
'thisLibrary.declarations[#somePredicate].type':
(thisLibrary.declarations[#somePredicate] as VariableMirror).type
},
{
'reflectType(Predicate).referent':
(reflectType(Predicate) as TypedefMirror).referent,
'thisLibrary.declarations[#somePredicate].type.referent':
((thisLibrary.declarations[#somePredicate] as VariableMirror).type
as TypedefMirror)
.referent
},
{
'reflectClass(A).typeVariables.single':
reflectClass(A).typeVariables.single,
'reflect(new A<int>()).type.originalDeclaration.typeVariables.single':
reflect(new A<int>()).type.originalDeclaration.typeVariables.single
},
{'currentMirrorSystem()': currentMirrorSystem()},
{'currentMirrorSystem().isolate': currentMirrorSystem().isolate},
{
'thisLibrary': thisLibrary,
'reflectClass(A).owner': reflectClass(A).owner,
'reflectClass(B).owner': reflectClass(B).owner,
'reflect(new A()).type.owner': reflect(new A()).type.owner,
'reflect(new B()).type.owner': reflect(new B()).type.owner
},
]);
}

View file

@ -0,0 +1,48 @@
// Copyright (c) 2013, 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.
import "dart:mirrors";
import "package:expect/expect.dart";
membersOf(ClassMirror cm) {
var result = new Map();
cm.declarations.forEach((k, v) {
if (v is MethodMirror && !v.isConstructor) result[k] = v;
if (v is VariableMirror) result[k] = v;
});
return result;
}
class WannabeFunction {
int call(int a, int b) => a + b;
method(x) => x * x;
}
main() {
Expect.isTrue(new WannabeFunction() is Function);
ClosureMirror cm = reflect(new WannabeFunction()) as ClosureMirror;
Expect.equals(7, cm.invoke(#call, [3, 4]).reflectee);
Expect.throwsNoSuchMethodError(() => cm.invoke(#call, [3]), "Wrong arity");
Expect.equals(49, cm.invoke(#method, [7]).reflectee);
Expect.throwsNoSuchMethodError(() => cm.invoke(#method, [3, 4]),
"Wrong arity");
Expect.equals(7, cm.apply([3, 4]).reflectee);
Expect.throwsNoSuchMethodError(() => cm.apply([3]), "Wrong arity");
MethodMirror mm = cm.function;
Expect.equals(#call, mm.simpleName);
Expect.equals(reflectClass(WannabeFunction), mm.owner);
Expect.isTrue(mm.isRegularMethod);
Expect.equals(#int, mm.returnType.simpleName);
Expect.equals(#int, mm.parameters[0].type.simpleName);
Expect.equals(#int, mm.parameters[1].type.simpleName);
ClassMirror km = cm.type;
Expect.equals(reflectClass(WannabeFunction), km);
Expect.equals(#WannabeFunction, km.simpleName);
Expect.equals(mm.hashCode, km.declarations[#call].hashCode);
Expect.setEquals([#call, #method], membersOf(km).keys);
}

View file

@ -0,0 +1,39 @@
// Copyright (c) 2013, 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.
import "dart:mirrors";
import "package:expect/expect.dart";
class MultiArityFunction implements Function {
noSuchMethod(Invocation msg) {
if (msg.memberName != #call) return super.noSuchMethod(msg);
return msg.positionalArguments.join(",");
}
}
main() {
dynamic f = new MultiArityFunction();
Expect.isTrue(f is Function);
Expect.equals('a', f('a'));
Expect.equals('a,b', f('a', 'b'));
Expect.equals('a,b,c', f('a', 'b', 'c'));
Expect.equals('a', Function.apply(f, ['a']));
Expect.equals('a,b', Function.apply(f, ['a', 'b']));
Expect.equals('a,b,c', Function.apply(f, ['a', 'b', 'c']));
Expect.throwsNoSuchMethodError(() => f.foo('a', 'b', 'c'));
ClosureMirror cm = reflect(f) as ClosureMirror;
Expect.isTrue(cm is ClosureMirror);
Expect.equals('a', cm.apply(['a']).reflectee);
Expect.equals('a,b', cm.apply(['a', 'b']).reflectee);
Expect.equals('a,b,c', cm.apply(['a', 'b', 'c']).reflectee);
MethodMirror mm = cm.function;
Expect.isNull(mm);
ClassMirror km = cm.type;
Expect.equals(reflectClass(MultiArityFunction), km);
}

View file

@ -0,0 +1,28 @@
// compile options: --emit-metadata
// Copyright (c) 2017, 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.
// Run essentially the same test, but with emit-metadata compile option,
// which allows us to reflect on the fields.
import 'dart:mirrors';
import 'package:expect/expect.dart';
import 'field_metadata_test.dart' as field_metadata_test;
import 'field_metadata_test.dart' show Foo, Bar;
void main() {
// Make sure the other test still works.
field_metadata_test.main();
// Check that we can now reflect on the annotations.
dynamic f = new Foo();
var members = reflect(f).type.declarations;
var x = members[#x] as VariableMirror;
var bar = x.metadata.first.reflectee as Bar;
Expect.equals(bar.name, 'bar');
var y = members[#y] as VariableMirror;
var baz = y.metadata.first.reflectee as Bar;
Expect.equals(baz.name, 'baz');
}

View file

@ -0,0 +1,41 @@
// Copyright (c) 2017, 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.
import 'dart:mirrors';
import 'package:expect/expect.dart';
class Bar {
final String name;
const Bar(this.name);
}
class Foo {
@Bar('bar')
int x = 40;
@Bar('baz')
final String y = 'hi';
@Bar('foo')
void set z(int val) {
x = val;
}
}
void main() {
dynamic f = new Foo();
f.x += 2;
Expect.equals(f.x, 42);
Expect.equals(f.y, 'hi');
f.z = 0;
Expect.equals(f.x, 0);
var members = reflect(f).type.declarations;
var x = members[#x] as VariableMirror;
var y = members[#y] as VariableMirror;
Expect.equals(x.type.simpleName, #int);
Expect.equals(y.type.simpleName, #String);
}

View file

@ -0,0 +1,85 @@
// Copyright (c) 2013, 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 field_test;
import 'dart:mirrors';
import "package:expect/expect.dart";
String toplevelVariable = "";
class C {
final int i;
const C(this.i);
}
class A<T> {
static int staticField = 0;
@C(42)
@C(44)
String field = "";
var dynamicTypeField;
T typeVariableField;
H<int> parameterizedTypeField;
}
class H<T> {}
testOriginalDeclaration() {
ClassMirror a = reflectClass(A);
VariableMirror staticField = a.declarations[#staticField] as VariableMirror;
VariableMirror field = a.declarations[#field] as VariableMirror;
VariableMirror dynamicTypeField =
a.declarations[#dynamicTypeField] as VariableMirror;
VariableMirror typeVariableField =
a.declarations[#typeVariableField] as VariableMirror;
VariableMirror parameterizedTypeField =
a.declarations[#parameterizedTypeField] as VariableMirror;
Expect.equals(reflectType(int), staticField.type);
Expect.equals(reflectClass(String), field.type);
Expect.equals(reflectType(dynamic), dynamicTypeField.type);
Expect.equals(a.typeVariables.single, typeVariableField.type);
Expect.equals(reflect(new H<int>()).type, parameterizedTypeField.type);
Expect.equals(2, field.metadata.length);
Expect.equals(reflect(const C(42)), field.metadata.first);
Expect.equals(reflect(const C(44)), field.metadata.last);
}
testInstance() {
ClassMirror aOfString = reflect(new A<String>()).type;
VariableMirror staticField =
aOfString.declarations[#staticField] as VariableMirror;
VariableMirror field = aOfString.declarations[#field] as VariableMirror;
VariableMirror dynamicTypeField =
aOfString.declarations[#dynamicTypeField] as VariableMirror;
VariableMirror typeVariableField =
aOfString.declarations[#typeVariableField] as VariableMirror;
VariableMirror parameterizedTypeField =
aOfString.declarations[#parameterizedTypeField] as VariableMirror;
Expect.equals(reflectType(int), staticField.type);
Expect.equals(reflectClass(String), field.type);
Expect.equals(reflectType(dynamic), dynamicTypeField.type);
Expect.equals(reflectClass(String), typeVariableField.type);
Expect.equals(reflect(new H<int>()).type, parameterizedTypeField.type);
Expect.equals(2, field.metadata.length);
Expect.equals(reflect(const C(42)), field.metadata.first);
Expect.equals(reflect(const C(44)), field.metadata.last);
}
testTopLevel() {
LibraryMirror currentLibrary = currentMirrorSystem().findLibrary(#field_test);
VariableMirror topLevel =
currentLibrary.declarations[#toplevelVariable] as VariableMirror;
Expect.equals(reflectClass(String), topLevel.type);
}
main() {
testOriginalDeclaration();
testInstance();
testTopLevel();
}

View file

@ -0,0 +1,9 @@
// Copyright (c) 2015, 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 function_apply_mirrors_lib;
import "dart:mirrors";
bar() => reflect(499).reflectee;

View file

@ -0,0 +1,21 @@
// Copyright (c) 2015, 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.
// Only 'lib' imports mirrors.
// Function.apply is resolved, before it is known that mirrors are used.
// Dart2js has different implementations of Function.apply for different
// emitters (like --fast-startup). Dart2js must not switch the resolved
// Function.apply when it discovers the use of mirrors.
// In particular it must not switch from the fast-startup emitter to the full
// emitter without updating the Function.apply reference.
import 'function_apply_mirrors_lib.dart' as lib;
import "package:expect/expect.dart";
int foo({x: 499, y: 42}) => x + y;
main() {
Expect.equals(709, Function.apply(foo, [], {#y: 210}));
Expect.equals(499, lib.bar());
}

View file

@ -0,0 +1,39 @@
// Copyright (c) 2017, 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 lib;
import "dart:mirrors";
import "package:expect/expect.dart";
class A {
call(int x) => 123 + x;
bar(int y) => 321 + y;
}
foo(int y) => 456 + y;
main() {
// Static function.
ClosureMirror f1 = reflect(foo) as ClosureMirror;
Expect.equals(1456, f1.apply([1000]).reflectee);
// Local declaration.
chomp(int z) => z + 42;
ClosureMirror f2 = reflect(chomp) as ClosureMirror;
Expect.equals(1042, f2.apply([1000]).reflectee);
// Local expression.
ClosureMirror f3 = reflect((u) => u + 987) as ClosureMirror;
Expect.equals(1987, f3.apply([1000]).reflectee);
// Instance property extraction.
ClosureMirror f4 = reflect(new A().bar) as ClosureMirror;
Expect.equals(1321, f4.apply([1000]).reflectee);
// Instance implementing Function via call method.
ClosureMirror f5 = reflect(new A()) as ClosureMirror;
Expect.equals(1123, f5.apply([1000]).reflectee);
}

View file

@ -0,0 +1,23 @@
// Copyright (c) 2013, 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.
import "dart:mirrors";
import "package:expect/expect.dart";
typedef void FooFunction(int a, double b);
bar(int a) {}
main() {
TypedefMirror tm = reflectType(FooFunction) as TypedefMirror;
FunctionTypeMirror ftm = tm.referent;
Expect.equals(const Symbol('void'), ftm.returnType.simpleName);
Expect.equals(#int, ftm.parameters[0].type.simpleName);
Expect.equals(#double, ftm.parameters[1].type.simpleName);
ClosureMirror cm = reflect(bar) as ClosureMirror;
ftm = cm.type as FunctionTypeMirror;
Expect.equals(#dynamic, ftm.returnType.simpleName);
Expect.equals(#int, ftm.parameters[0].type.simpleName);
}

View file

@ -0,0 +1,66 @@
// Copyright (c) 2013, 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.generic_bounded_by_type_parameter;
import 'dart:mirrors';
import 'package:expect/expect.dart';
import 'generics_helper.dart';
class Super<T, R extends T> {}
class Fixed extends Super<num, int> {}
class Generic<X, Y> extends Super<X, Y> {} //# 02: compile-time error
main() {
ClassMirror superDecl = reflectClass(Super);
ClassMirror superOfNumAndInt = reflectClass(Fixed).superclass;
ClassMirror genericDecl = reflectClass(Generic); // //# 02: continued
ClassMirror superOfXAndY = genericDecl.superclass; // //# 02: continued
ClassMirror genericOfNumAndDouble = reflect(new Generic<num, double>()).type; // //# 02: continued
ClassMirror superOfNumAndDouble = genericOfNumAndDouble.superclass; // //# 02: continued
ClassMirror genericOfNumAndBool = reflect(new Generic<num, bool>()).type; // //# 02: compile-time error
ClassMirror superOfNumAndBool = genericOfNumAndBool.superclass; // //# 02: continued
Expect.isFalse(genericOfNumAndBool.isOriginalDeclaration); // //# 02: continued
Expect.isFalse(superOfNumAndBool.isOriginalDeclaration); // //# 02: continued
typeParameters(genericOfNumAndBool, [#X, #Y]); // //# 02: continued
typeParameters(superOfNumAndBool, [#T, #R]); // //# 02: continued
typeArguments(genericOfNumAndBool, [reflectClass(num), reflectClass(bool)]); // //# 02: continued
typeArguments(superOfNumAndBool, [reflectClass(num), reflectClass(bool)]); // //# 02: continued
Expect.isTrue(superDecl.isOriginalDeclaration);
Expect.isFalse(superOfNumAndInt.isOriginalDeclaration);
Expect.isTrue(genericDecl.isOriginalDeclaration); // //# 02: continued
Expect.isFalse(superOfXAndY.isOriginalDeclaration); // //# 02: continued
Expect.isFalse(genericOfNumAndDouble.isOriginalDeclaration); // //# 02: continued
Expect.isFalse(superOfNumAndDouble.isOriginalDeclaration); // //# 02: continued
TypeVariableMirror tFromSuper = superDecl.typeVariables[0];
TypeVariableMirror rFromSuper = superDecl.typeVariables[1];
TypeVariableMirror xFromGeneric = genericDecl.typeVariables[0]; // //# 02: continued
TypeVariableMirror yFromGeneric = genericDecl.typeVariables[1]; // //# 02: continued
Expect.equals(reflectClass(Object), tFromSuper.upperBound);
Expect.equals(tFromSuper, rFromSuper.upperBound);
Expect.equals(reflectClass(Object), xFromGeneric.upperBound); // //# 02: continued
Expect.equals(reflectClass(Object), yFromGeneric.upperBound); // //# 02: continued
typeParameters(superDecl, [#T, #R]);
typeParameters(superOfNumAndInt, [#T, #R]);
typeParameters(genericDecl, [#X, #Y]); // //# 02: continued
typeParameters(superOfXAndY, [#T, #R]); // //# 02: continued
typeParameters(genericOfNumAndDouble, [#X, #Y]); // //# 02: continued
typeParameters(superOfNumAndDouble, [#T, #R]); // //# 02: continued
typeArguments(superDecl, []);
typeArguments(superOfNumAndInt, [reflectClass(num), reflectClass(int)]);
typeArguments(genericDecl, []); // //# 02: continued
typeArguments(superOfXAndY, [xFromGeneric, yFromGeneric]); // //# 02: continued
typeArguments(genericOfNumAndDouble, [reflectClass(num), reflectClass(double)]); // //# 02: continued
typeArguments(superOfNumAndDouble, [reflectClass(num), reflectClass(double)]); // //# 02: continued
}

View file

@ -0,0 +1,67 @@
// Copyright (c) 2013, 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.generic_bounded;
import 'dart:mirrors';
import 'package:expect/expect.dart';
import 'generics_helper.dart';
class Super<T extends num> {}
class Fixed extends Super<int> {}
class Generic<R> extends Super<R> {} // //# 02: compile-time error
class Malbounded extends Super<String> {} //# 01: compile-time error
main() {
ClassMirror superDecl = reflectClass(Super);
ClassMirror superOfInt = reflectClass(Fixed).superclass;
ClassMirror genericDecl = reflectClass(Generic); // //# 02: continued
ClassMirror superOfR = genericDecl.superclass; // //# 02: continued
ClassMirror genericOfDouble = reflect(new Generic<double>()).type; // //# 02: continued
ClassMirror superOfDouble = genericOfDouble.superclass; // //# 02: continued
ClassMirror genericOfBool = reflect(new Generic<bool>()).type; // //# 02: compile-time error
ClassMirror superOfBool = genericOfBool.superclass; // //# 02: continued
Expect.isFalse(genericOfBool.isOriginalDeclaration); // //# 02: continued
Expect.isFalse(superOfBool.isOriginalDeclaration); // //# 02: continued
typeParameters(genericOfBool, [#R]); // //# 02: continued
typeParameters(superOfBool, [#T]); // //# 02: continued
typeArguments(genericOfBool, [reflectClass(bool)]); // //# 02: continued
typeArguments(superOfBool, [reflectClass(bool)]); // //# 02: continued
ClassMirror superOfString = reflectClass(Malbounded).superclass; // //# 01: continued
Expect.isTrue(superDecl.isOriginalDeclaration);
Expect.isFalse(superOfInt.isOriginalDeclaration);
Expect.isTrue(genericDecl.isOriginalDeclaration); // //# 02: continued
Expect.isFalse(superOfR.isOriginalDeclaration); // //# 02: continued
Expect.isFalse(genericOfDouble.isOriginalDeclaration); // //# 02: continued
Expect.isFalse(superOfDouble.isOriginalDeclaration); // //# 02: continued
Expect.isFalse(superOfString.isOriginalDeclaration); // //# 01: continued
TypeVariableMirror tFromSuper = superDecl.typeVariables.single;
TypeVariableMirror rFromGeneric = genericDecl.typeVariables.single; // //# 02: continued
Expect.equals(reflectClass(num), tFromSuper.upperBound);
Expect.equals(reflectClass(Object), rFromGeneric.upperBound); // //# 02: continued
typeParameters(superDecl, [#T]);
typeParameters(superOfInt, [#T]);
typeParameters(genericDecl, [#R]); // //# 02: continued
typeParameters(superOfR, [#T]); // //# 02: continued
typeParameters(genericOfDouble, [#R]); // //# 02: continued
typeParameters(superOfDouble, [#T]); // //# 02: continued
typeParameters(superOfString, [#T]); // //# 01: continued
typeArguments(superDecl, []);
typeArguments(superOfInt, [reflectClass(int)]);
typeArguments(genericDecl, []); // //# 02: continued
typeArguments(superOfR, [rFromGeneric]); // //# 02: continued
typeArguments(genericOfDouble, [reflectClass(double)]); // //# 02: continued
typeArguments(superOfDouble, [reflectClass(double)]); // //# 02: continued
typeArguments(superOfString, [reflectClass(String)]); // //# 01: continued
}

View file

@ -0,0 +1,94 @@
// Copyright (c) 2013, 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.
import 'dart:mirrors';
import 'package:expect/expect.dart';
import 'stringify.dart';
class A<T> {
var instanceVariable;
get instanceGetter => null;
set instanceSetter(x) => x;
instanceMethod() => null;
var _instanceVariable;
get _instanceGetter => null;
set _instanceSetter(x) => x;
_instanceMethod() => null;
static var staticVariable;
static get staticGetter => null;
static set staticSetter(x) => x;
static staticMethod() => null;
static var _staticVariable;
static get _staticGetter => null;
static set _staticSetter(x) => x;
static _staticMethod() => null;
}
main() {
ClassMirror cm = reflect(new A<String>()).type;
Expect.setEquals([
'Variable(s(_instanceVariable) in s(A), private)',
'Variable(s(_staticVariable) in s(A), private, static)',
'Variable(s(instanceVariable) in s(A))',
'Variable(s(staticVariable) in s(A), static)'
], cm.declarations.values.where((dm) => dm is VariableMirror).map(stringify),
'variables');
Expect.setEquals(
[
'Method(s(_instanceGetter) in s(A), private, getter)',
'Method(s(_staticGetter) in s(A), private, static, getter)',
'Method(s(instanceGetter) in s(A), getter)',
'Method(s(staticGetter) in s(A), static, getter)'
],
cm.declarations.values
.where((dm) => dm is MethodMirror && dm.isGetter)
.map(stringify),
'getters');
Expect.setEquals(
[
'Method(s(_instanceSetter=) in s(A), private, setter)',
'Method(s(_staticSetter=) in s(A), private, static, setter)',
'Method(s(instanceSetter=) in s(A), setter)',
'Method(s(staticSetter=) in s(A), static, setter)'
],
cm.declarations.values
.where((dm) => dm is MethodMirror && dm.isSetter)
.map(stringify),
'setters');
Expect.setEquals(
[
'Method(s(_instanceMethod) in s(A), private)',
'Method(s(_staticMethod) in s(A), private, static)',
'Method(s(instanceMethod) in s(A))',
'Method(s(staticMethod) in s(A), static)'
],
cm.declarations.values
.where((dm) => dm is MethodMirror && dm.isRegularMethod)
.map(stringify),
'methods');
Expect.setEquals(
['Method(s(A) in s(A), constructor)'],
cm.declarations.values
.where((dm) => dm is MethodMirror && dm.isConstructor)
.map(stringify),
'constructors');
Expect.setEquals(
[
'TypeVariable(s(T) in s(A), upperBound = Class(s(Object) in '
's(dart.core), top-level))'
],
cm.declarations.values
.where((dm) => dm is TypeVariableMirror)
.map(stringify),
'type variables');
}

View file

@ -0,0 +1,122 @@
// Copyright (c) 2013, 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.generic_f_bounded;
import 'dart:mirrors';
import 'package:expect/expect.dart';
import 'generics_helper.dart';
class Collection<C> {}
class Serializable<S> {}
class OrderedCollection<V> extends Collection<V>
with Serializable<OrderedCollection<V>> {}
class AbstractOrderedCollection<W> = Collection<W>
with Serializable<AbstractOrderedCollection<W>>;
class CustomOrderedCollection<Z> extends AbstractOrderedCollection<Z> {}
class OrderedIntegerCollection extends OrderedCollection<int> {}
class CustomOrderedIntegerCollection extends CustomOrderedCollection<int> {}
class Serializer<R extends Serializable<R>> {}
class CollectionSerializer extends Serializer<Collection> {}
class OrderedCollectionSerializer extends Serializer<OrderedCollection> {}
main() {
ClassMirror collectionDecl = reflectClass(Collection);
ClassMirror serializableDecl = reflectClass(Serializable);
ClassMirror orderedCollectionDecl = reflectClass(OrderedCollection);
ClassMirror abstractOrderedCollectionDecl =
reflectClass(AbstractOrderedCollection);
ClassMirror customOrderedCollectionDecl =
reflectClass(CustomOrderedCollection);
ClassMirror orderedIntegerCollection = reflectClass(OrderedIntegerCollection);
ClassMirror customOrderedIntegerCollection =
reflectClass(CustomOrderedIntegerCollection);
ClassMirror serializerDecl = reflectClass(Serializer);
ClassMirror collectionSerializerDecl = reflectClass(CollectionSerializer);
ClassMirror orderedCollectionSerializerDecl =
reflectClass(OrderedCollectionSerializer);
ClassMirror orderedCollectionOfInt = orderedIntegerCollection.superclass;
ClassMirror customOrderedCollectionOfInt =
customOrderedIntegerCollection.superclass;
ClassMirror serializerOfCollection = collectionSerializerDecl.superclass;
ClassMirror serializerOfOrderedCollection =
orderedCollectionSerializerDecl.superclass;
ClassMirror collectionOfDynamic = reflect(new Collection()).type;
ClassMirror orderedCollectionOfDynamic =
reflect(new OrderedCollection()).type;
ClassMirror collectionWithSerializableOfOrderedCollection =
orderedCollectionDecl.superclass;
Expect.isTrue(collectionDecl.isOriginalDeclaration);
Expect.isTrue(serializableDecl.isOriginalDeclaration);
Expect.isTrue(orderedCollectionDecl.isOriginalDeclaration);
Expect.isTrue(abstractOrderedCollectionDecl.isOriginalDeclaration);
Expect.isTrue(customOrderedCollectionDecl.isOriginalDeclaration);
Expect.isTrue(orderedIntegerCollection.isOriginalDeclaration);
Expect.isTrue(customOrderedIntegerCollection.isOriginalDeclaration);
Expect.isTrue(serializerDecl.isOriginalDeclaration);
Expect.isTrue(collectionSerializerDecl.isOriginalDeclaration);
Expect.isTrue(orderedCollectionSerializerDecl.isOriginalDeclaration);
Expect.isFalse(orderedCollectionOfInt.isOriginalDeclaration);
Expect.isFalse(customOrderedCollectionOfInt.isOriginalDeclaration);
Expect.isFalse(serializerOfCollection.isOriginalDeclaration);
Expect.isFalse(serializerOfOrderedCollection.isOriginalDeclaration);
Expect.isFalse(collectionOfDynamic.isOriginalDeclaration);
Expect.isFalse(
collectionWithSerializableOfOrderedCollection.isOriginalDeclaration);
TypeVariableMirror rFromSerializer = serializerDecl.typeVariables.single;
ClassMirror serializableOfR = rFromSerializer.upperBound as ClassMirror;
Expect.isFalse(serializableOfR.isOriginalDeclaration);
Expect.equals(serializableDecl, serializableOfR.originalDeclaration);
Expect.equals(rFromSerializer, serializableOfR.typeArguments.single);
typeParameters(collectionDecl, [#C]);
typeParameters(serializableDecl, [#S]);
typeParameters(orderedCollectionDecl, [#V]);
typeParameters(abstractOrderedCollectionDecl, [#W]);
typeParameters(customOrderedCollectionDecl, [#Z]);
typeParameters(orderedIntegerCollection, []);
typeParameters(customOrderedIntegerCollection, []);
typeParameters(serializerDecl, [#R]);
typeParameters(collectionSerializerDecl, []);
typeParameters(orderedCollectionSerializerDecl, []);
typeParameters(orderedCollectionOfInt, [#V]);
typeParameters(customOrderedCollectionOfInt, [#Z]);
typeParameters(serializerOfCollection, [#R]);
typeParameters(serializerOfOrderedCollection, [#R]);
typeParameters(collectionOfDynamic, [#C]);
typeParameters(collectionWithSerializableOfOrderedCollection, []);
typeArguments(collectionDecl, []);
typeArguments(serializableDecl, []);
typeArguments(orderedCollectionDecl, []);
typeArguments(abstractOrderedCollectionDecl, []);
typeArguments(customOrderedCollectionDecl, []);
typeArguments(orderedIntegerCollection, []);
typeArguments(customOrderedIntegerCollection, []);
typeArguments(serializerDecl, []);
typeArguments(collectionSerializerDecl, []);
typeArguments(orderedCollectionSerializerDecl, []);
typeArguments(orderedCollectionOfInt, [reflectClass(int)]);
typeArguments(customOrderedCollectionOfInt, [reflectClass(int)]);
typeArguments(serializerOfCollection, [collectionOfDynamic]);
typeArguments(serializerOfOrderedCollection, [orderedCollectionOfDynamic]);
typeArguments(collectionWithSerializableOfOrderedCollection, []);
}

View file

@ -0,0 +1,61 @@
// Copyright (c) 2013, 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.generic_f_bounded;
import 'dart:mirrors';
import 'package:expect/expect.dart';
import 'generics_helper.dart';
class Magnitude<T> {}
class Real extends Magnitude<Real> {}
class Sorter<R extends Magnitude<R>> {}
class RealSorter extends Sorter<Real> {}
main() {
ClassMirror magnitudeDecl = reflectClass(Magnitude);
ClassMirror realDecl = reflectClass(Real);
ClassMirror sorterDecl = reflectClass(Sorter);
ClassMirror realSorterDecl = reflectClass(RealSorter);
ClassMirror magnitudeOfReal = realDecl.superclass;
ClassMirror sorterOfReal = realSorterDecl.superclass;
Expect.isTrue(magnitudeDecl.isOriginalDeclaration);
Expect.isTrue(realDecl.isOriginalDeclaration);
Expect.isTrue(sorterDecl.isOriginalDeclaration);
Expect.isTrue(realSorterDecl.isOriginalDeclaration);
Expect.isFalse(magnitudeOfReal.isOriginalDeclaration);
Expect.isFalse(sorterOfReal.isOriginalDeclaration);
TypeVariableMirror tFromMagnitude = magnitudeDecl.typeVariables.single;
TypeVariableMirror rFromSorter = sorterDecl.typeVariables.single;
Expect.equals(reflectClass(Object), tFromMagnitude.upperBound);
ClassMirror magnitudeOfR = rFromSorter.upperBound;
Expect.isFalse(magnitudeOfR.isOriginalDeclaration);
Expect.equals(magnitudeDecl, magnitudeOfR.originalDeclaration);
Expect.equals(rFromSorter, magnitudeOfR.typeArguments.single);
typeParameters(magnitudeDecl, [#T]);
typeParameters(realDecl, []);
typeParameters(sorterDecl, [#R]);
typeParameters(realSorterDecl, []);
typeParameters(magnitudeOfReal, [#T]);
typeParameters(sorterOfReal, [#R]);
typeParameters(magnitudeOfR, [#T]);
typeArguments(magnitudeDecl, []);
typeArguments(realDecl, []);
typeArguments(sorterDecl, []);
typeArguments(realSorterDecl, []);
typeArguments(magnitudeOfReal, [realDecl]); //# 01: ok
typeArguments(sorterOfReal, [realDecl]); //# 01: ok
typeArguments(magnitudeOfR, [rFromSorter]);
}

View file

@ -0,0 +1,128 @@
// Copyright (c) 2013, 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.generic_function_typedef;
import 'dart:mirrors';
import 'package:expect/expect.dart';
import 'generics_helper.dart';
typedef bool NonGenericPredicate(num n);
typedef bool GenericPredicate<T>(T t);
typedef S GenericTransform<S>(S s);
class C<R> {
GenericPredicate<num> predicateOfNum;
GenericTransform<String> transformOfString;
GenericTransform<R> transformOfR;
}
reflectTypeDeclaration(t) => reflectType(t).originalDeclaration;
main() {
TypeMirror dynamicMirror = currentMirrorSystem().dynamicType;
TypedefMirror predicateOfNum =
(reflectClass(C).declarations[#predicateOfNum] as VariableMirror).type
as TypedefMirror;
TypedefMirror transformOfString =
(reflectClass(C).declarations[#transformOfString] as VariableMirror).type
as TypedefMirror;
TypedefMirror transformOfR =
(reflectClass(C).declarations[#transformOfR] as VariableMirror).type
as TypedefMirror;
TypedefMirror transformOfDouble = (reflect(new C<double>())
.type
.declarations[#transformOfR] as VariableMirror)
.type as TypedefMirror;
TypeVariableMirror tFromGenericPredicate =
reflectTypeDeclaration(GenericPredicate).typeVariables[0];
TypeVariableMirror sFromGenericTransform =
reflectTypeDeclaration(GenericTransform).typeVariables[0];
TypeVariableMirror rFromC = reflectClass(C).typeVariables[0];
// Typedefs.
typeParameters(reflectTypeDeclaration(NonGenericPredicate), []);
typeParameters(reflectTypeDeclaration(GenericPredicate), [#T]);
typeParameters(reflectTypeDeclaration(GenericTransform), [#S]);
typeParameters(predicateOfNum, [#T]);
typeParameters(transformOfString, [#S]);
typeParameters(transformOfR, [#S]);
typeParameters(transformOfDouble, [#S]);
typeArguments(reflectTypeDeclaration(NonGenericPredicate), []);
typeArguments(reflectTypeDeclaration(GenericPredicate), []);
typeArguments(reflectTypeDeclaration(GenericTransform), []);
typeArguments(predicateOfNum, [reflectClass(num)]);
typeArguments(transformOfString, [reflectClass(String)]);
typeArguments(transformOfR, [rFromC]);
typeArguments(transformOfDouble, [reflectClass(double)]);
Expect.isTrue(
reflectTypeDeclaration(NonGenericPredicate).isOriginalDeclaration);
Expect.isTrue(reflectTypeDeclaration(GenericPredicate).isOriginalDeclaration);
Expect.isTrue(reflectTypeDeclaration(GenericTransform).isOriginalDeclaration);
Expect.isFalse(predicateOfNum.isOriginalDeclaration);
Expect.isFalse(transformOfString.isOriginalDeclaration);
Expect.isFalse(transformOfR.isOriginalDeclaration);
Expect.isFalse(transformOfDouble.isOriginalDeclaration);
// Function types.
typeParameters(reflectTypeDeclaration(NonGenericPredicate).referent, []);
typeParameters(reflectTypeDeclaration(GenericPredicate).referent, []);
typeParameters(reflectTypeDeclaration(GenericTransform).referent, []);
typeParameters(predicateOfNum.referent, []);
typeParameters(transformOfString.referent, []);
typeParameters(transformOfR.referent, []);
typeParameters(transformOfDouble.referent, []);
typeArguments(reflectTypeDeclaration(NonGenericPredicate).referent, []);
typeArguments(reflectTypeDeclaration(GenericPredicate).referent, []);
typeArguments(reflectTypeDeclaration(GenericTransform).referent, []);
typeArguments(predicateOfNum.referent, []);
typeArguments(transformOfString.referent, []);
typeArguments(transformOfR.referent, []);
typeArguments(transformOfDouble.referent, []);
// Function types are always non-generic. Only the typedef is generic.
Expect.isTrue(reflectTypeDeclaration(NonGenericPredicate)
.referent
.isOriginalDeclaration);
Expect.isTrue(
reflectTypeDeclaration(GenericPredicate).referent.isOriginalDeclaration);
Expect.isTrue(
reflectTypeDeclaration(GenericTransform).referent.isOriginalDeclaration);
Expect.isTrue(predicateOfNum.referent.isOriginalDeclaration);
Expect.isTrue(transformOfString.referent.isOriginalDeclaration);
Expect.isTrue(transformOfR.referent.isOriginalDeclaration);
Expect.isTrue(transformOfDouble.referent.isOriginalDeclaration);
Expect.equals(reflectClass(num),
reflectTypeDeclaration(NonGenericPredicate).referent.parameters[0].type);
Expect.equals(tFromGenericPredicate,
reflectTypeDeclaration(GenericPredicate).referent.parameters[0].type);
Expect.equals(sFromGenericTransform,
reflectTypeDeclaration(GenericTransform).referent.parameters[0].type);
Expect.equals(reflectClass(num), predicateOfNum.referent.parameters[0].type);
Expect.equals(
reflectClass(String), transformOfString.referent.parameters[0].type);
Expect.equals(rFromC, transformOfR.referent.parameters[0].type);
Expect.equals(
reflectClass(double), transformOfDouble.referent.parameters[0].type);
Expect.equals(reflectClass(bool),
reflectTypeDeclaration(NonGenericPredicate).referent.returnType);
Expect.equals(reflectClass(bool),
reflectTypeDeclaration(GenericPredicate).referent.returnType);
Expect.equals(sFromGenericTransform,
reflectTypeDeclaration(GenericTransform).referent.returnType);
Expect.equals(reflectClass(bool), predicateOfNum.referent.returnType);
Expect.equals(reflectClass(String), transformOfString.referent.returnType);
Expect.equals(rFromC, transformOfR.referent.returnType);
Expect.equals(reflectClass(double), transformOfDouble.referent.returnType);
}

View file

@ -0,0 +1,123 @@
// Copyright (c) 2013, 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.generic_bounded;
import 'dart:mirrors';
import 'package:expect/expect.dart';
import 'generics_helper.dart';
class Interface<T> {}
class Bounded<S extends num> {}
class Fixed implements Interface<int> {}
class Generic<R> implements Interface<R> {}
class Bienbounded implements Bounded<int> {}
class Malbounded implements Bounded<String> {} // //# 01: compile-time error
class FBounded implements Interface<FBounded> {}
class Mixin {}
class FixedMixinApplication = Object with Mixin implements Interface<int>;
class GenericMixinApplication<X> = Object with Mixin implements Interface<X>;
class FixedClass extends Object with Mixin implements Interface<int> {}
class GenericClass<Y> extends Object with Mixin implements Interface<Y> {}
main() {
TypeMirror dynamicMirror = currentMirrorSystem().dynamicType;
ClassMirror interfaceDecl = reflectClass(Interface);
ClassMirror boundedDecl = reflectClass(Bounded);
ClassMirror interfaceOfInt = reflectClass(Fixed).superinterfaces.single;
ClassMirror interfaceOfR = reflectClass(Generic).superinterfaces.single;
ClassMirror interfaceOfBool =
reflect(new Generic<bool>()).type.superinterfaces.single;
ClassMirror boundedOfInt = reflectClass(Bienbounded).superinterfaces.single;
ClassMirror boundedOfString = reflectClass(Malbounded).superinterfaces.single; // //# 01: continued
ClassMirror interfaceOfFBounded =
reflectClass(FBounded).superinterfaces.single;
ClassMirror interfaceOfInt2 =
reflectClass(FixedMixinApplication).superinterfaces.single;
ClassMirror interfaceOfX =
reflectClass(GenericMixinApplication).superinterfaces.single;
ClassMirror interfaceOfDouble = reflect(new GenericMixinApplication<double>())
.type
.superinterfaces
.single;
ClassMirror interfaceOfInt3 = reflectClass(FixedClass).superinterfaces.single;
ClassMirror interfaceOfY = reflectClass(GenericClass).superinterfaces.single;
ClassMirror interfaceOfDouble2 =
reflect(new GenericClass<double>()).type.superinterfaces.single;
Expect.isTrue(interfaceDecl.isOriginalDeclaration);
Expect.isTrue(boundedDecl.isOriginalDeclaration);
Expect.isFalse(interfaceOfInt.isOriginalDeclaration);
Expect.isFalse(interfaceOfR.isOriginalDeclaration);
Expect.isFalse(interfaceOfBool.isOriginalDeclaration);
Expect.isFalse(boundedOfInt.isOriginalDeclaration);
Expect.isFalse(boundedOfString.isOriginalDeclaration); // //# 01: continued
Expect.isFalse(interfaceOfFBounded.isOriginalDeclaration);
Expect.isFalse(interfaceOfInt2.isOriginalDeclaration);
Expect.isFalse(interfaceOfX.isOriginalDeclaration);
Expect.isFalse(interfaceOfDouble.isOriginalDeclaration);
Expect.isFalse(interfaceOfInt3.isOriginalDeclaration);
Expect.isFalse(interfaceOfY.isOriginalDeclaration);
Expect.isFalse(interfaceOfDouble2.isOriginalDeclaration);
TypeVariableMirror tFromInterface = interfaceDecl.typeVariables.single;
TypeVariableMirror sFromBounded = boundedDecl.typeVariables.single;
TypeVariableMirror rFromGeneric = reflectClass(Generic).typeVariables.single;
TypeVariableMirror xFromGenericMixinApplication =
reflectClass(GenericMixinApplication).typeVariables.single;
TypeVariableMirror yFromGenericClass =
reflectClass(GenericClass).typeVariables.single;
Expect.equals(reflectClass(Object), tFromInterface.upperBound);
Expect.equals(reflectClass(num), sFromBounded.upperBound);
Expect.equals(reflectClass(Object), rFromGeneric.upperBound);
Expect.equals(reflectClass(Object), xFromGenericMixinApplication.upperBound);
Expect.equals(reflectClass(Object), yFromGenericClass.upperBound);
typeParameters(interfaceDecl, [#T]);
typeParameters(boundedDecl, [#S]);
typeParameters(interfaceOfInt, [#T]);
typeParameters(interfaceOfR, [#T]);
typeParameters(interfaceOfBool, [#T]);
typeParameters(boundedOfInt, [#S]);
typeParameters(boundedOfString, [#S]); // //# 01: continued
typeParameters(interfaceOfFBounded, [#T]);
typeParameters(interfaceOfInt2, [#T]);
typeParameters(interfaceOfX, [#T]);
typeParameters(interfaceOfDouble, [#T]);
typeParameters(interfaceOfInt3, [#T]);
typeParameters(interfaceOfY, [#T]);
typeParameters(interfaceOfDouble2, [#T]);
typeArguments(interfaceDecl, []);
typeArguments(boundedDecl, []);
typeArguments(interfaceOfInt, [reflectClass(int)]);
typeArguments(interfaceOfR, [rFromGeneric]);
typeArguments(interfaceOfBool, [reflectClass(bool)]);
typeArguments(boundedOfInt, [reflectClass(int)]);
typeArguments(boundedOfString, [reflectClass(String)]); // //# 01: continued
typeArguments(interfaceOfFBounded, [reflectClass(FBounded)]);
typeArguments(interfaceOfInt2, [reflectClass(int)]);
typeArguments(interfaceOfX, [xFromGenericMixinApplication]);
typeArguments(interfaceOfDouble, [reflectClass(double)]);
typeArguments(interfaceOfInt3, [reflectClass(int)]);
typeArguments(interfaceOfY, [yFromGenericClass]);
typeArguments(interfaceOfDouble2, [reflectClass(double)]);
}

View file

@ -0,0 +1,21 @@
// Copyright (c) 2013, 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.superclass;
import 'dart:mirrors';
import 'package:expect/expect.dart';
class Foo<T> {
List<T> makeList() {
if (new DateTime.now().millisecondsSinceEpoch == 42) return [];
return new List<T>();
}
}
main() {
List<String> list = new Foo<String>().makeList();
var cls = reflectClass(list.runtimeType);
Expect.isNotNull(cls, 'Failed to reflect on MyClass.');
}

View file

@ -0,0 +1,40 @@
// Copyright (c) 2013, 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.generic_function_typedef;
import 'dart:mirrors';
import 'package:expect/expect.dart';
import 'generics_helper.dart';
class C<T> {
makeClosure1() {
T closure1(T t) {}
return closure1;
}
makeClosure2() {
enclosing() {
T closure2(T t) {}
return closure2;
}
;
return enclosing();
}
}
main() {
ClosureMirror closure1 =
reflect(new C<String>().makeClosure1()) as ClosureMirror;
Expect.equals(reflectClass(String), closure1.function.returnType);
Expect.equals(reflectClass(String), closure1.function.parameters[0].type);
ClosureMirror closure2 =
reflect(new C<String>().makeClosure2()) as ClosureMirror;
Expect.equals(reflectClass(String), closure2.function.returnType);
Expect.equals(reflectClass(String), closure2.function.parameters[0].type);
}

View file

@ -0,0 +1,15 @@
// Copyright (c) 2017, 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.
import 'dart:mirrors';
import 'package:expect/expect.dart';
class Foo {
T bar<T>() => null;
}
void main() {
var type = reflectClass(Foo);
Expect.isTrue(type.declarations.keys.contains(#bar));
}

View file

@ -0,0 +1,105 @@
// Copyright (c) 2013, 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.generic_mixin_applications;
import 'dart:mirrors';
import 'package:expect/expect.dart';
import 'generics_helper.dart';
class Super<S> {}
class Mixin<M> {}
class Nixim<N> {}
class NonGenericMixinApplication1 = Super with Mixin;
class NonGenericMixinApplication2 = Super<num> with Mixin<String>;
class GenericMixinApplication1<MA> = Super<MA> with Mixin<MA>;
class GenericMixinApplication2<MA> = Super<num> with Mixin<String>;
class NonGenericClass1 extends Super with Mixin {}
class NonGenericClass2 extends Super<num> with Mixin<String> {}
class GenericClass1<C> extends Super<C> with Mixin<C> {}
class GenericClass2<C> extends Super<num> with Mixin<String> {}
class GenericMultipleMixins<A, B, C> extends Super<A> with Mixin<B>, Nixim<C> {}
main() {
TypeMirror dynamicMirror = currentMirrorSystem().dynamicType;
// Declarations.
typeParameters(reflectClass(NonGenericMixinApplication1), []);
typeParameters(reflectClass(NonGenericMixinApplication2), []);
typeParameters(reflectClass(GenericMixinApplication1), [#MA]);
typeParameters(reflectClass(GenericMixinApplication2), [#MA]);
typeParameters(reflectClass(NonGenericClass1), []);
typeParameters(reflectClass(NonGenericClass2), []);
typeParameters(reflectClass(GenericClass1), [#C]);
typeParameters(reflectClass(GenericClass2), [#C]);
typeParameters(reflectClass(GenericMultipleMixins), [#A, #B, #C]);
// Anonymous mixin applications have no type parameters or type arguments.
typeParameters(reflectClass(NonGenericClass1).superclass, []);
typeParameters(reflectClass(NonGenericClass2).superclass, []);
typeParameters(reflectClass(GenericClass1).superclass, []);
typeParameters(reflectClass(GenericClass2).superclass, []);
typeArguments(reflectClass(NonGenericMixinApplication1), []);
typeArguments(reflectClass(NonGenericMixinApplication2), []);
typeArguments(reflectClass(GenericMixinApplication1), []);
typeArguments(reflectClass(GenericMixinApplication2), []);
typeArguments(reflectClass(NonGenericClass1), []);
typeArguments(reflectClass(NonGenericClass2), []);
typeArguments(reflectClass(GenericClass1), []);
typeArguments(reflectClass(GenericClass2), []);
typeArguments(reflectClass(GenericMultipleMixins), []);
// Anonymous mixin applications have no type parameters or type arguments.
typeArguments(
reflectClass(NonGenericClass1).superclass.originalDeclaration, []);
typeArguments(
reflectClass(NonGenericClass2).superclass.originalDeclaration, []);
typeArguments(reflectClass(GenericClass1).superclass.originalDeclaration, []);
typeArguments(reflectClass(GenericClass2).superclass.originalDeclaration, []);
// Instantiations.
typeParameters(reflect(new NonGenericMixinApplication1()).type, []);
typeParameters(reflect(new NonGenericMixinApplication2()).type, []);
typeParameters(reflect(new GenericMixinApplication1<bool>()).type, [#MA]);
typeParameters(reflect(new GenericMixinApplication2<bool>()).type, [#MA]);
typeParameters(reflect(new NonGenericClass1()).type, []);
typeParameters(reflect(new NonGenericClass2()).type, []);
typeParameters(reflect(new GenericClass1<bool>()).type, [#C]);
typeParameters(reflect(new GenericClass2<bool>()).type, [#C]);
typeParameters(reflect(new GenericMultipleMixins<bool, String, int>()).type,
[#A, #B, #C]);
// Anonymous mixin applications have no type parameters or type arguments.
typeParameters(reflect(new NonGenericClass1()).type.superclass, []);
typeParameters(reflect(new NonGenericClass2()).type.superclass, []);
typeParameters(reflect(new GenericClass1<bool>()).type.superclass, []);
typeParameters(reflect(new GenericClass2<bool>()).type.superclass, []);
typeArguments(reflect(new NonGenericMixinApplication1()).type, []);
typeArguments(reflect(new NonGenericMixinApplication2()).type, []);
typeArguments(
reflect(new GenericMixinApplication1<bool>()).type, [reflectClass(bool)]);
typeArguments(
reflect(new GenericMixinApplication2<bool>()).type, [reflectClass(bool)]);
typeArguments(reflect(new NonGenericClass1()).type, []);
typeArguments(reflect(new NonGenericClass2()).type, []);
typeArguments(reflect(new GenericClass1<bool>()).type, [reflectClass(bool)]);
typeArguments(reflect(new GenericClass2<bool>()).type, [reflectClass(bool)]);
typeArguments(reflect(new GenericMultipleMixins<bool, String, int>()).type,
[reflectClass(bool), reflectClass(String), reflectClass(int)]);
// Anonymous mixin applications have no type parameters or type arguments.
typeArguments(reflect(new NonGenericClass1()).type.superclass, []);
typeArguments(reflect(new NonGenericClass2()).type.superclass, []);
typeArguments(reflect(new GenericClass1<bool>()).type.superclass, []);
typeArguments(reflect(new GenericClass2<bool>()).type.superclass, []);
}

View file

@ -0,0 +1,182 @@
// Copyright (c) 2013, 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.generic_mixin;
import 'dart:mirrors';
import 'package:expect/expect.dart';
import 'generics_helper.dart';
class Super<S> {}
class Mixin<M> {}
class Nixim<N> {}
class NonGenericMixinApplication1 = Super with Mixin;
class NonGenericMixinApplication2 = Super<num> with Mixin<String>;
class GenericMixinApplication1<MA> = Super<MA> with Mixin<MA>;
class GenericMixinApplication2<MA> = Super<num> with Mixin<String>;
class NonGenericClass1 extends Super with Mixin {}
class NonGenericClass2 extends Super<num> with Mixin<String> {}
class GenericClass1<C> extends Super<C> with Mixin<C> {}
class GenericClass2<C> extends Super<num> with Mixin<String> {}
class GenericMultipleMixins<A, B, C> extends Super<A> with Mixin<B>, Nixim<C> {}
main() {
TypeMirror dynamicMirror = currentMirrorSystem().dynamicType;
typeParameters(reflectClass(NonGenericMixinApplication1).mixin, [#M]);
typeParameters(reflectClass(NonGenericMixinApplication2).mixin, [#M]);
typeParameters(reflectClass(GenericMixinApplication1).mixin, [#M]);
typeParameters(reflectClass(GenericMixinApplication2).mixin, [#M]);
typeParameters(reflectClass(NonGenericClass1).mixin, []);
typeParameters(reflectClass(NonGenericClass2).mixin, []);
typeParameters(reflectClass(GenericClass1).mixin, [#C]);
typeParameters(reflectClass(GenericClass2).mixin, [#C]);
typeParameters(reflectClass(NonGenericClass1).superclass.mixin, [#M]);
typeParameters(reflectClass(NonGenericClass2).superclass.mixin, [#M]);
typeParameters(reflectClass(GenericClass1).superclass.mixin, [#M]);
typeParameters(reflectClass(GenericClass2).superclass.mixin, [#M]);
typeParameters(reflectClass(GenericMultipleMixins).mixin, [#A, #B, #C]);
typeParameters(reflectClass(GenericMultipleMixins).superclass.mixin, [#N]);
typeParameters(
reflectClass(GenericMultipleMixins).superclass.superclass.mixin, [#M]);
typeParameters(
reflectClass(GenericMultipleMixins)
.superclass
.superclass
.superclass
.mixin,
[#S]);
typeArguments(
reflectClass(NonGenericMixinApplication1).mixin, [dynamicMirror]);
typeArguments(
reflectClass(NonGenericMixinApplication2).mixin, [reflectClass(String)]);
typeArguments(reflectClass(GenericMixinApplication1).mixin,
[reflectClass(GenericMixinApplication1).typeVariables.single]);
typeArguments(
reflectClass(GenericMixinApplication2).mixin, [reflectClass(String)]);
typeArguments(reflectClass(NonGenericClass1).mixin, []);
typeArguments(reflectClass(NonGenericClass2).mixin, []);
typeArguments(reflectClass(GenericClass1).mixin, []);
typeArguments(reflectClass(GenericClass2).mixin, []);
typeArguments(
reflectClass(NonGenericClass1).superclass.mixin, [dynamicMirror]);
typeArguments(
reflectClass(NonGenericClass2).superclass.mixin, [reflectClass(String)]);
typeArguments(reflectClass(GenericClass1).superclass.mixin,
[reflectClass(GenericClass1).typeVariables.single]);
typeArguments(
reflectClass(GenericClass2).superclass.mixin, [reflectClass(String)]);
typeArguments(reflectClass(GenericMultipleMixins).mixin, []);
typeArguments(reflectClass(GenericMultipleMixins).superclass.mixin,
[reflectClass(GenericMultipleMixins).typeVariables[2]]);
typeArguments(reflectClass(GenericMultipleMixins).superclass.superclass.mixin,
[reflectClass(GenericMultipleMixins).typeVariables[1]]);
typeArguments(
reflectClass(GenericMultipleMixins)
.superclass
.superclass
.superclass
.mixin,
[reflectClass(GenericMultipleMixins).typeVariables[0]]);
typeParameters(reflect(new NonGenericMixinApplication1()).type.mixin, [#M]);
typeParameters(reflect(new NonGenericMixinApplication2()).type.mixin, [#M]);
typeParameters(
reflect(new GenericMixinApplication1<bool>()).type.mixin, [#M]);
typeParameters(
reflect(new GenericMixinApplication2<bool>()).type.mixin, [#M]);
typeParameters(reflect(new NonGenericClass1()).type.mixin, []);
typeParameters(reflect(new NonGenericClass2()).type.mixin, []);
typeParameters(reflect(new GenericClass1<bool>()).type.mixin, [#C]);
typeParameters(reflect(new GenericClass2<bool>()).type.mixin, [#C]);
typeParameters(reflect(new NonGenericClass1()).type.superclass.mixin, [#M]);
typeParameters(reflect(new NonGenericClass2()).type.superclass.mixin, [#M]);
typeParameters(
reflect(new GenericClass1<bool>()).type.superclass.mixin, [#M]);
typeParameters(
reflect(new GenericClass2<bool>()).type.superclass.mixin, [#M]);
typeParameters(
reflect(new GenericMultipleMixins<bool, String, int>()).type.mixin,
[#A, #B, #C]);
typeParameters(
reflect(new GenericMultipleMixins<bool, String, int>())
.type
.superclass
.mixin,
[#N]);
typeParameters(
reflect(new GenericMultipleMixins<bool, String, int>())
.type
.superclass
.superclass
.mixin,
[#M]);
typeParameters(
reflect(new GenericMultipleMixins<bool, String, int>())
.type
.superclass
.superclass
.superclass
.mixin,
[#S]);
typeArguments(
reflect(new NonGenericMixinApplication1()).type.mixin, [dynamicMirror]);
typeArguments(reflect(new NonGenericMixinApplication2()).type.mixin,
[reflectClass(String)]);
typeArguments(reflect(new GenericMixinApplication1<bool>()).type.mixin,
[reflectClass(bool)]);
typeArguments(reflect(new GenericMixinApplication2<bool>()).type.mixin,
[reflectClass(String)]);
typeArguments(reflect(new NonGenericClass1()).type.mixin, []);
typeArguments(reflect(new NonGenericClass2()).type.mixin, []);
typeArguments(
reflect(new GenericClass1<bool>()).type.mixin, [reflectClass(bool)]);
typeArguments(
reflect(new GenericClass2<bool>()).type.mixin, [reflectClass(bool)]);
typeArguments(
reflect(new NonGenericClass1()).type.superclass.mixin, [dynamicMirror]);
typeArguments(reflect(new NonGenericClass2()).type.superclass.mixin,
[reflectClass(String)]);
typeArguments(reflect(new GenericClass1<bool>()).type.superclass.mixin,
[reflectClass(bool)]);
typeArguments(reflect(new GenericClass2<bool>()).type.superclass.mixin,
[reflectClass(String)]);
typeArguments(
reflect(new GenericMultipleMixins<bool, String, int>()).type.mixin,
[reflectClass(bool), reflectClass(String), reflectClass(int)]);
typeArguments(
reflect(new GenericMultipleMixins<bool, String, int>())
.type
.superclass
.mixin,
[reflectClass(int)]);
typeArguments(
reflect(new GenericMultipleMixins<bool, String, int>())
.type
.superclass
.superclass
.mixin,
[reflectClass(String)]);
typeArguments(
reflect(new GenericMultipleMixins<bool, String, int>())
.type
.superclass
.superclass
.superclass
.mixin,
[reflectClass(bool)]);
}

View file

@ -0,0 +1,129 @@
// Copyright (c) 2013, 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.
import 'package:expect/expect.dart';
import 'dart:mirrors';
class A<T> {}
class B extends A<U> {}
class C extends A<C> {}
class D<T> extends A<T> {}
class E<X, Y> extends G<H<Y>> {}
class F<X> implements A<X> {}
class FF<X, Y> implements G<H<Y>> {}
class G<T> {}
class H<T> {}
class U {}
class R {}
void testOriginals() {
ClassMirror a = reflectClass(A);
ClassMirror b = reflectClass(B);
ClassMirror c = reflectClass(C);
ClassMirror d = reflectClass(D);
ClassMirror e = reflectClass(E);
ClassMirror f = reflectClass(F);
ClassMirror ff = reflectClass(FF);
ClassMirror superA = a.superclass;
ClassMirror superB = b.superclass;
ClassMirror superC = c.superclass;
ClassMirror superD = d.superclass;
ClassMirror superE = e.superclass;
ClassMirror superInterfaceF = f.superinterfaces[0];
ClassMirror superInterfaceFF = ff.superinterfaces[0];
TypeVariableMirror aT = a.typeVariables[0];
TypeVariableMirror dT = d.typeVariables[0];
TypeVariableMirror eX = e.typeVariables[0];
TypeVariableMirror eY = e.typeVariables[1];
TypeVariableMirror fX = f.typeVariables[0];
TypeVariableMirror feX = ff.typeVariables[0];
TypeVariableMirror feY = ff.typeVariables[1];
Expect.isTrue(superA.isOriginalDeclaration);
Expect.isFalse(superB.isOriginalDeclaration);
Expect.isFalse(superC.isOriginalDeclaration);
Expect.isFalse(superD.isOriginalDeclaration);
Expect.isFalse(superE.isOriginalDeclaration);
Expect.isFalse(superInterfaceF.isOriginalDeclaration);
Expect.isFalse(superInterfaceFF.isOriginalDeclaration);
Expect.equals(reflectClass(Object), superA);
Expect.equals(reflect(new A<U>()).type, superB);
Expect.equals(reflect(new A<C>()).type, superC); //# 01: ok
Expect.equals(reflect(new U()).type, superB.typeArguments[0]);
Expect.equals(reflect(new C()).type, superC.typeArguments[0]); //# 01: ok
Expect.equals(dT, superD.typeArguments[0]);
Expect.equals(eY, superE.typeArguments[0].typeArguments[0]);
Expect.equals(feY, superInterfaceFF.typeArguments[0].typeArguments[0]);
Expect.equals(fX, superInterfaceF.typeArguments[0]);
}
void testInstances() {
ClassMirror a = reflect(new A<U>()).type;
ClassMirror b = reflect(new B()).type;
ClassMirror c = reflect(new C()).type;
ClassMirror d = reflect(new D<U>()).type;
ClassMirror e = reflect(new E<U, R>()).type;
ClassMirror e0 = reflect(new E<U, H<R>>()).type;
ClassMirror ff = reflect(new FF<U, R>()).type;
ClassMirror f = reflect(new F<U>()).type;
ClassMirror u = reflect(new U()).type;
ClassMirror r = reflect(new R()).type;
ClassMirror hr = reflect(new H<R>()).type;
ClassMirror superA = a.superclass;
ClassMirror superB = b.superclass;
ClassMirror superC = c.superclass;
ClassMirror superD = d.superclass;
ClassMirror superE = e.superclass;
ClassMirror superE0 = e0.superclass;
ClassMirror superInterfaceF = f.superinterfaces[0];
ClassMirror superInterfaceFF = ff.superinterfaces[0];
Expect.isTrue(superA.isOriginalDeclaration);
Expect.isFalse(superB.isOriginalDeclaration);
Expect.isFalse(superC.isOriginalDeclaration);
Expect.isFalse(superD.isOriginalDeclaration);
Expect.isFalse(superE.isOriginalDeclaration);
Expect.isFalse(superE0.isOriginalDeclaration);
Expect.isFalse(superInterfaceF.isOriginalDeclaration);
Expect.isFalse(superInterfaceFF.isOriginalDeclaration);
Expect.equals(reflectClass(Object), superA);
Expect.equals(reflect(new A<U>()).type, superB);
Expect.equals(reflect(new A<C>()).type, superC); //# 01: ok
Expect.equals(reflect(new A<U>()).type, superD);
Expect.equals(reflect(new G<H<R>>()).type, superE);
Expect.equals(reflect(new G<H<H<R>>>()).type, superE0);
Expect.equals(reflect(new G<H<R>>()).type, superInterfaceFF);
Expect.equals(u, superB.typeArguments[0]);
Expect.equals(reflect(new C()).type, superC.typeArguments[0]); //# 01: ok
Expect.equals(u, superD.typeArguments[0]);
Expect.equals(r, superE.typeArguments[0].typeArguments[0]);
Expect.equals(hr, superE0.typeArguments[0].typeArguments[0]);
Expect.equals(r, superInterfaceFF.typeArguments[0].typeArguments[0]);
Expect.equals(u, superInterfaceF.typeArguments[0]);
}
void testObject() {
ClassMirror object = reflectClass(Object);
Expect.equals(null, object.superclass);
}
main() {
testOriginals();
testInstances();
testObject();
}

View file

@ -0,0 +1,92 @@
// Copyright (c) 2013, 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.
import "dart:mirrors";
import "package:expect/expect.dart";
class Foo<W, V> {
V field;
V get bar => field;
set bar(V v) {}
W m() {}
V n() {}
H<V> p() {}
o(W w) {}
}
class H<T> {}
class Bar {}
class Baz {}
void testInstance() {
ClassMirror foo = reflect((new Foo<Bar, Baz>())).type;
ClassMirror bar = reflect(new Bar()).type;
ClassMirror baz = reflect(new Baz()).type;
ClassMirror hOfBaz = reflect(new H<Baz>()).type;
VariableMirror field = foo.declarations[#field] as VariableMirror;
MethodMirror getter = foo.declarations[#bar] as MethodMirror;
MethodMirror setter = foo.declarations[const Symbol('bar=')] as MethodMirror;
MethodMirror m = foo.declarations[#m] as MethodMirror;
MethodMirror n = foo.declarations[#n] as MethodMirror;
MethodMirror o = foo.declarations[#o] as MethodMirror;
MethodMirror p = foo.declarations[#p] as MethodMirror;
Expect.equals(foo, field.owner);
Expect.equals(foo, getter.owner);
Expect.equals(foo, setter.owner);
Expect.equals(foo, m.owner);
Expect.equals(foo, n.owner);
Expect.equals(foo, o.owner);
Expect.equals(foo, p.owner);
Expect.equals(baz, field.type);
Expect.equals(baz, getter.returnType);
Expect.equals(bar, m.returnType);
Expect.equals(baz, n.returnType);
Expect.equals(bar, o.parameters.single.type);
Expect.equals(hOfBaz, p.returnType);
Expect.equals(1, p.returnType.typeArguments.length);
Expect.equals(baz, p.returnType.typeArguments[0]);
Expect.equals(baz, setter.parameters.single.type);
}
void testOriginalDeclaration() {
ClassMirror foo = reflectClass(Foo);
VariableMirror field = foo.declarations[#field] as VariableMirror;
MethodMirror getter = foo.declarations[#bar] as MethodMirror;
MethodMirror setter = foo.declarations[const Symbol('bar=')] as MethodMirror;
MethodMirror m = foo.declarations[#m] as MethodMirror;
MethodMirror n = foo.declarations[#n] as MethodMirror;
MethodMirror o = foo.declarations[#o] as MethodMirror;
MethodMirror p = foo.declarations[#p] as MethodMirror;
TypeVariableMirror w = foo.typeVariables[0] as TypeVariableMirror;
TypeVariableMirror v = foo.typeVariables[1] as TypeVariableMirror;
Expect.equals(foo, field.owner);
Expect.equals(foo, getter.owner);
Expect.equals(foo, setter.owner);
Expect.equals(foo, m.owner);
Expect.equals(foo, n.owner);
Expect.equals(foo, o.owner);
Expect.equals(foo, p.owner);
Expect.equals(v, field.type);
Expect.equals(v, getter.returnType);
Expect.equals(w, m.returnType);
Expect.equals(v, n.returnType);
Expect.equals(w, o.parameters.single.type);
Expect.equals(1, p.returnType.typeArguments.length);
Expect.equals(v, p.returnType.typeArguments[0]);
Expect.equals(v, setter.parameters.single.type);
}
main() {
testInstance();
testOriginalDeclaration();
}

View file

@ -0,0 +1,36 @@
// Copyright (c) 2013, 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.generics_double_substitution;
import 'dart:mirrors';
import 'package:expect/expect.dart';
class A<R> {}
class B<S> {}
class C<T> extends B<A<T>> {
A<T> field;
A<T> returnType() => new A<T>();
parameterType(A<T> param) {}
}
main() {
ClassMirror cOfString = reflect(new C<String>()).type;
ClassMirror aOfString = reflect(new A<String>()).type;
VariableMirror field = cOfString.declarations[#field] as VariableMirror;
Expect.equals(aOfString, field.type);
MethodMirror returnType = cOfString.declarations[#returnType] as MethodMirror;
Expect.equals(aOfString, returnType.returnType);
MethodMirror parameterType = cOfString.declarations[#parameterType] as MethodMirror;
Expect.equals(aOfString, parameterType.parameters.single.type);
ClassMirror typeArgOfSuperclass = cOfString.superclass.typeArguments.single as ClassMirror;
Expect.equals(aOfString, typeArgOfSuperclass); //# 01: ok
}

View file

@ -0,0 +1,69 @@
// Copyright (c) 2013, 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.
import 'dart:mirrors';
import 'package:expect/expect.dart';
class A<T> {}
class B<T extends A> extends A implements C {
A m(A a) {}
A field;
}
class C<S, T> {}
class D extends A<int> {}
main() {
ClassMirror aDecl = reflectClass(A);
ClassMirror bDecl = reflectClass(B);
ClassMirror cDecl = reflectClass(C);
TypeMirror aInstance = reflect(new A()).type;
TypeMirror aInstanceDynamic = reflect(new A<dynamic>()).type;
TypeMirror dInstance = reflect(new D()).type;
TypeMirror cInstance = reflect(new C<dynamic, dynamic>()).type;
TypeMirror cNestedInstance = reflect(new C<C, dynamic>()).type;
TypeMirror cTypeArgument = cNestedInstance.typeArguments.first;
TypeMirror superA = bDecl.superclass;
TypeMirror superC = bDecl.superinterfaces.single;
MethodMirror m = bDecl.declarations[#m] as MethodMirror;
VariableMirror field = bDecl.declarations[#field] as VariableMirror;
TypeMirror returnTypeA = m.returnType;
TypeMirror parameterTypeA = m.parameters.first.type;
TypeMirror fieldTypeA = field.type;
TypeMirror upperBoundA = bDecl.typeVariables.single.upperBound;
TypeMirror dynamicMirror = currentMirrorSystem().dynamicType;
Expect.isTrue(aDecl.isOriginalDeclaration);
Expect.isTrue(reflect(dInstance).type.isOriginalDeclaration);
Expect.isFalse(aInstance.isOriginalDeclaration);
Expect.isFalse(aInstanceDynamic.isOriginalDeclaration);
Expect.isFalse(superA.isOriginalDeclaration);
Expect.isFalse(superC.isOriginalDeclaration);
Expect.isFalse(returnTypeA.isOriginalDeclaration);
Expect.isFalse(parameterTypeA.isOriginalDeclaration);
Expect.isFalse(fieldTypeA.isOriginalDeclaration);
Expect.isFalse(upperBoundA.isOriginalDeclaration);
Expect.isFalse(cInstance.isOriginalDeclaration);
Expect.isFalse(cNestedInstance.isOriginalDeclaration);
Expect.isFalse(cTypeArgument.isOriginalDeclaration);
Expect.isTrue(aDecl.typeArguments.isEmpty);
Expect.isTrue(dInstance.typeArguments.isEmpty);
Expect.equals(dynamicMirror, aInstance.typeArguments.single);
Expect.equals(dynamicMirror, aInstanceDynamic.typeArguments.single);
Expect.equals(dynamicMirror, superA.typeArguments.single);
Expect.equals(dynamicMirror, superC.typeArguments.first);
Expect.equals(dynamicMirror, superC.typeArguments.last);
Expect.equals(dynamicMirror, returnTypeA.typeArguments.single);
Expect.equals(dynamicMirror, parameterTypeA.typeArguments.single);
Expect.equals(dynamicMirror, fieldTypeA.typeArguments.single);
Expect.equals(dynamicMirror, upperBoundA.typeArguments.single);
Expect.equals(dynamicMirror, cInstance.typeArguments.first);
Expect.equals(dynamicMirror, cInstance.typeArguments.last);
Expect.equals(dynamicMirror, cNestedInstance.typeArguments.last);
Expect.equals(dynamicMirror, cTypeArgument.typeArguments.first);
Expect.equals(dynamicMirror, cTypeArgument.typeArguments.last);
}

View file

@ -0,0 +1,16 @@
// Copyright (c) 2013, 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 generics_helper;
import 'package:expect/expect.dart';
typeParameters(mirror, parameterNames) {
Expect.listEquals(
parameterNames, mirror.typeVariables.map((v) => v.simpleName).toList());
}
typeArguments(mirror, argumentMirrors) {
Expect.listEquals(argumentMirrors, mirror.typeArguments);
}

View file

@ -0,0 +1,29 @@
// Copyright (c) 2013, 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.generics_special_types;
import 'dart:mirrors';
import 'package:expect/expect.dart';
main() {
TypeMirror dynamicMirror = currentMirrorSystem().dynamicType;
Expect.isTrue(dynamicMirror.isOriginalDeclaration);
Expect.equals(dynamicMirror, dynamicMirror.originalDeclaration);
Expect.listEquals([], dynamicMirror.typeVariables);
Expect.listEquals([], dynamicMirror.typeArguments);
TypeMirror voidMirror = currentMirrorSystem().voidType;
Expect.isTrue(voidMirror.isOriginalDeclaration);
Expect.equals(voidMirror, voidMirror.originalDeclaration);
Expect.listEquals([], voidMirror.typeVariables);
Expect.listEquals([], voidMirror.typeArguments);
TypeMirror dynamicMirror2 = reflectType(dynamic);
Expect.equals(dynamicMirror, dynamicMirror2);
Expect.isTrue(dynamicMirror2.isOriginalDeclaration);
Expect.equals(dynamicMirror2, dynamicMirror2.originalDeclaration);
Expect.listEquals([], dynamicMirror2.typeVariables);
Expect.listEquals([], dynamicMirror2.typeArguments);
}

View file

@ -0,0 +1,55 @@
// Copyright (c) 2013, 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.generics_substitution;
import 'dart:mirrors';
import 'package:expect/expect.dart';
class SuperGeneric<R, S> {
R r;
s(S s) {}
}
class Generic<T> extends SuperGeneric<T, int> {
T t() => throw "does-not-return"; //
}
main() {
ClassMirror genericDecl = reflectClass(Generic);
ClassMirror genericOfString = reflect(new Generic<String>()).type;
ClassMirror superGenericDecl = reflectClass(SuperGeneric);
ClassMirror superOfTAndInt = genericDecl.superclass;
ClassMirror superOfStringAndInt = genericOfString.superclass;
Expect.isTrue(genericDecl.isOriginalDeclaration);
Expect.isFalse(genericOfString.isOriginalDeclaration);
Expect.isTrue(superGenericDecl.isOriginalDeclaration);
Expect.isFalse(superOfTAndInt.isOriginalDeclaration);
Expect.isFalse(superOfStringAndInt.isOriginalDeclaration);
Symbol r(ClassMirror cm) =>
(cm.declarations[#r] as VariableMirror).type.simpleName;
Symbol s(ClassMirror cm) =>
(cm.declarations[#s] as MethodMirror).parameters[0].type.simpleName;
Symbol t(ClassMirror cm) =>
(cm.declarations[#t] as MethodMirror).returnType.simpleName;
Expect.equals(#T, r(genericDecl.superclass));
Expect.equals(#int, s(genericDecl.superclass));
Expect.equals(#T, t(genericDecl));
Expect.equals(#String, r(genericOfString.superclass));
Expect.equals(#int, s(genericOfString.superclass));
Expect.equals(#String, t(genericOfString));
Expect.equals(#R, r(superGenericDecl));
Expect.equals(#S, s(superGenericDecl));
Expect.equals(#T, r(superOfTAndInt));
Expect.equals(#int, s(superOfTAndInt));
Expect.equals(#String, r(superOfStringAndInt));
Expect.equals(#int, s(superOfStringAndInt));
}

View file

@ -0,0 +1,165 @@
// Copyright (c) 2013, 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.type_arguments_test;
import 'dart:mirrors';
import 'package:expect/expect.dart';
import 'generics_helper.dart';
class A<T> {}
class Z<T> {}
class B extends A {}
class C
extends A<num, int> //# 01: compile-time error
{}
class D extends A<int> {}
class E<S> extends A<S> {}
class F<R> extends A<int> {}
class G {}
class H<A, B, C> {}
class I extends G {}
main() {
// Declarations.
typeParameters(reflectClass(A), [#T]);
typeParameters(reflectClass(G), []);
typeParameters(reflectClass(B), []);
typeParameters(reflectClass(C), []);
typeParameters(reflectClass(D), []);
typeParameters(reflectClass(E), [#S]);
typeParameters(reflectClass(F), [#R]);
typeParameters(reflectClass(G), []);
typeParameters(reflectClass(H), [#A, #B, #C]);
typeParameters(reflectClass(I), []);
typeArguments(reflectClass(A), []);
typeArguments(reflectClass(B), []);
typeArguments(reflectClass(C), []);
typeArguments(reflectClass(D), []);
typeArguments(reflectClass(E), []);
typeArguments(reflectClass(F), []);
typeArguments(reflectClass(G), []);
typeArguments(reflectClass(H), []);
typeArguments(reflectClass(I), []);
Expect.isTrue(reflectClass(A).isOriginalDeclaration);
Expect.isTrue(reflectClass(B).isOriginalDeclaration);
Expect.isTrue(reflectClass(C).isOriginalDeclaration);
Expect.isTrue(reflectClass(D).isOriginalDeclaration);
Expect.isTrue(reflectClass(E).isOriginalDeclaration);
Expect.isTrue(reflectClass(F).isOriginalDeclaration);
Expect.isTrue(reflectClass(G).isOriginalDeclaration);
Expect.isTrue(reflectClass(H).isOriginalDeclaration);
Expect.isTrue(reflectClass(I).isOriginalDeclaration);
Expect.equals(reflectClass(A), reflectClass(A).originalDeclaration);
Expect.equals(reflectClass(B), reflectClass(B).originalDeclaration);
Expect.equals(reflectClass(C), reflectClass(C).originalDeclaration);
Expect.equals(reflectClass(D), reflectClass(D).originalDeclaration);
Expect.equals(reflectClass(E), reflectClass(E).originalDeclaration);
Expect.equals(reflectClass(F), reflectClass(F).originalDeclaration);
Expect.equals(reflectClass(G), reflectClass(G).originalDeclaration);
Expect.equals(reflectClass(H), reflectClass(H).originalDeclaration);
Expect.equals(reflectClass(I), reflectClass(I).originalDeclaration);
// Instantiations.
typeParameters(reflect(new A<num>()).type, [#T]);
typeParameters(reflect(new B()).type, []);
typeParameters(reflect(new C()).type, []);
typeParameters(reflect(new D()).type, []);
typeParameters(reflect(new E()).type, [#S]);
typeParameters(reflect(new F<num>()).type, [#R]);
typeParameters(reflect(new G()).type, []);
typeParameters(reflect(new H()).type, [#A, #B, #C]);
typeParameters(reflect(new I()).type, []);
var numMirror = reflectClass(num);
var dynamicMirror = currentMirrorSystem().dynamicType;
typeArguments(reflect(new A<num>()).type, [numMirror]);
typeArguments(reflect(new A<dynamic>()).type, [dynamicMirror]);
typeArguments(reflect(new A()).type, [dynamicMirror]);
typeArguments(reflect(new B()).type, []);
typeArguments(reflect(new C()).type, []);
typeArguments(reflect(new D()).type, []);
typeArguments(reflect(new E<num>()).type, [numMirror]);
typeArguments(reflect(new E<dynamic>()).type, [dynamicMirror]);
typeArguments(reflect(new E()).type, [dynamicMirror]);
typeArguments(reflect(new F<num>()).type, [numMirror]);
typeArguments(reflect(new F<dynamic>()).type, [dynamicMirror]);
typeArguments(reflect(new F()).type, [dynamicMirror]);
typeArguments(reflect(new G()).type, []);
typeArguments(reflect(new H<dynamic, num, dynamic>()).type,
[dynamicMirror, numMirror, dynamicMirror]);
typeArguments(reflect(new I()).type, []);
Expect.isFalse(reflect(new A<num>()).type.isOriginalDeclaration);
Expect.isTrue(reflect(new B()).type.isOriginalDeclaration);
Expect.isTrue(reflect(new C()).type.isOriginalDeclaration);
Expect.isTrue(reflect(new D()).type.isOriginalDeclaration);
Expect.isFalse(reflect(new E<num>()).type.isOriginalDeclaration);
Expect.isFalse(reflect(new F<num>()).type.isOriginalDeclaration);
Expect.isTrue(reflect(new G()).type.isOriginalDeclaration);
Expect.isFalse(reflect(new H()).type.isOriginalDeclaration);
Expect.isTrue(reflect(new I()).type.isOriginalDeclaration);
Expect.equals(
reflectClass(A), reflect(new A<num>()).type.originalDeclaration);
Expect.equals(reflectClass(B), reflect(new B()).type.originalDeclaration);
Expect.equals(reflectClass(C), reflect(new C()).type.originalDeclaration);
Expect.equals(reflectClass(D), reflect(new D()).type.originalDeclaration);
Expect.equals(
reflectClass(E), reflect(new E<num>()).type.originalDeclaration);
Expect.equals(
reflectClass(F), reflect(new F<num>()).type.originalDeclaration);
Expect.equals(reflectClass(G), reflect(new G()).type.originalDeclaration);
Expect.equals(reflectClass(H), reflect(new H()).type.originalDeclaration);
Expect.equals(reflectClass(I), reflect(new I()).type.originalDeclaration);
Expect.notEquals(reflect(new A<num>()).type,
reflect(new A<num>()).type.originalDeclaration);
Expect.equals(
reflect(new B()).type, reflect(new B()).type.originalDeclaration);
Expect.equals(
reflect(new C()).type, reflect(new C()).type.originalDeclaration);
Expect.equals(
reflect(new D()).type, reflect(new D()).type.originalDeclaration);
Expect.notEquals(reflect(new E<num>()).type,
reflect(new E<num>()).type.originalDeclaration);
Expect.notEquals(reflect(new F<num>()).type,
reflect(new F<num>()).type.originalDeclaration);
Expect.equals(
reflect(new G()).type, reflect(new G()).type.originalDeclaration);
Expect.notEquals(
reflect(new H()).type, reflect(new H()).type.originalDeclaration);
Expect.equals(
reflect(new I()).type, reflect(new I()).type.originalDeclaration);
// Library members are all uninstantiated generics or non-generics.
currentMirrorSystem().libraries.values.forEach((libraryMirror) {
libraryMirror.declarations.values.forEach((declaration) {
if (declaration is ClassMirror) {
Expect.isTrue(declaration.isOriginalDeclaration);
Expect.equals(declaration, declaration.originalDeclaration);
}
});
});
Expect.equals(reflectClass(A).typeVariables[0].owner, reflectClass(A));
Expect.equals(reflectClass(Z).typeVariables[0].owner, reflectClass(Z));
Expect.notEquals(
reflectClass(A).typeVariables[0], reflectClass(Z).typeVariables[0]);
Expect.equals(
reflectClass(A).typeVariables[0], reflectClass(A).typeVariables[0]);
}

View file

@ -0,0 +1,35 @@
// 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.
import 'dart:mirrors';
import 'package:expect/expect.dart';
class A {
toString() => "A";
}
class B {
int x = 99;
toString() => "B";
}
void main() {
var a = new A();
var am = reflect(a);
for (int i = 0; i < 10; i++) {
// Adds a probe function on the symbol.
am.getField(#toString);
}
var b = new B();
var bm = reflect(b);
for (int i = 0; i < 10; i++) {
// Adds a field-cache on the mirror.
bm.getField(#x);
}
// There is a cache now, but the cache should not contain 'toString' from
// JavaScript's Object.prototype.
var toString = bm.getField(#toString).reflectee;
Expect.equals("B", toString());
}

View file

@ -0,0 +1,30 @@
// 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.
import 'dart:mirrors';
import 'package:expect/expect.dart';
class A {
static foo(y, [x]) => y;
static get bar => 499;
static operator$foo([optional = 499]) => optional;
static var x = 42;
static final y = "toto";
static const z = true;
}
main() {
var cm = reflectClass(A);
var closure = cm.getField(#foo).reflectee;
Expect.equals("b", closure("b"));
closure = cm.getField(#operator$foo).reflectee;
Expect.equals(499, closure());
Expect.equals(499, cm.getField(#bar).reflectee);
Expect.equals(42, cm.getField(#x).reflectee);
Expect.equals("toto", cm.getField(#y).reflectee); // //# 00: ok
Expect.equals(true, cm.getField(#z).reflectee); // //# 00: ok
}

View file

@ -0,0 +1,24 @@
// Copyright (c) 2013, 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.
import 'dart:mirrors';
import 'package:expect/expect.dart';
class A {
foo(y, [x]) => y;
operator +(other) => null;
get bar => 499;
operator$foo([optional = 499]) => optional;
}
main() {
// We are using `getField` to tear off `foo`. We must make sure that all
// stub methods are installed.
var closure = reflect(new A()).getField(#foo).reflectee;
Expect.equals("b", closure("b"));
closure = reflect(new A()).getField(#operator$foo).reflectee;
Expect.equals(499, closure());
}

View file

@ -0,0 +1,35 @@
// Copyright (c) 2013, 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.
/// Test that MirrorSystem.getName works correctly on symbols returned from
/// Invocation.memberName. This is especially relevant when minifying.
import 'dart:mirrors' show MirrorSystem;
class Foo {
String noSuchMethod(Invocation invocation) {
return MirrorSystem.getName(invocation.memberName);
}
}
expect(expected, actual) {
if (expected != actual) {
throw 'Expected: "$expected", but got "$actual"';
}
}
main() {
dynamic foo = new Foo();
expect('foo', foo.foo);
expect('foo', foo.foo());
expect('foo', foo.foo(null));
expect('foo', foo.foo(null, null));
expect('foo', foo.foo(a: null, b: null));
expect('baz', foo.baz);
expect('baz', foo.baz());
expect('baz', foo.baz(null));
expect('baz', foo.baz(null, null));
expect('baz', foo.baz(a: null, b: null));
}

View file

@ -0,0 +1,16 @@
// Copyright (c) 2013, 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.
import 'dart:mirrors' show MirrorSystem;
expect(expected, actual) {
if (expected != actual) {
throw 'Expected: "$expected", but got "$actual"';
}
}
main() {
expect('fisk', MirrorSystem.getName(const Symbol('fisk')));
expect('fisk', MirrorSystem.getName(new Symbol('fisk')));
}

View file

@ -0,0 +1,37 @@
// 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.
// Dart2js crashed on this example. It globalized both closures and created
// top-level classes for closures (here the globalized_closure{2}). There was a
// name-clash (both being named "main_closure") which led to a crash.
library main;
import 'dart:mirrors';
import 'package:expect/expect.dart';
confuse(x) {
if (new DateTime.now().millisecondsSinceEpoch == 42) {
return confuse(() => print(42));
}
return x;
}
main() {
var globalized_closure = confuse(() => 499);
var globalized_closure2 = confuse(() => 99);
globalized_closure();
globalized_closure2();
final ms = currentMirrorSystem();
var lib = ms.findLibrary(#main);
var collectedParents = [];
var classes = lib.declarations.values;
for (var c in classes) {
if (c is ClassMirror && c.superclass != null) {
collectedParents.add(MirrorSystem.getName(c.superclass.simpleName));
}
}
Expect.isTrue(collectedParents.isEmpty); // //# 00: ok
}

View file

@ -0,0 +1,36 @@
// 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.
// Dart2js crashed on this example. It globalized closures and created
// top-level classes for closures (here the globalized_closure). There was a
// name-clash with the global "main_closure" class which led to a crash.
library main;
import 'dart:mirrors';
import 'package:expect/expect.dart';
class main_closure {}
confuse(x) {
if (new DateTime.now().millisecondsSinceEpoch == 42) return confuse(() => 42);
return x;
}
main() {
new main_closure();
var globalized_closure = confuse(() => 499);
globalized_closure();
final ms = currentMirrorSystem();
var lib = ms.findLibrary(#main);
var collectedParents = [];
var classes = lib.declarations.values;
for (var c in classes) {
if (c is ClassMirror && c.superclass != null) {
collectedParents.add(MirrorSystem.getName(c.superclass.simpleName));
}
}
Expect.listEquals(["Object"], collectedParents); // //# 00: ok
}

View file

@ -0,0 +1,42 @@
// Copyright (c) 2013, 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.hierarchy_invariants_test;
import 'dart:mirrors';
import 'package:expect/expect.dart';
isAnonymousMixinApplication(classMirror) {
return MirrorSystem.getName(classMirror.simpleName).contains(' with ');
}
checkClass(classMirror) {
Expect.isTrue(classMirror.simpleName is Symbol);
Expect.notEquals(null, classMirror.owner);
Expect.isTrue(classMirror.owner is LibraryMirror);
if (!isAnonymousMixinApplication(classMirror)) {
Expect.equals(classMirror.originalDeclaration,
classMirror.owner.declarations[classMirror.simpleName]);
} else {
Expect.isNull(classMirror.owner.declarations[classMirror.simpleName]);
}
Expect.isTrue(classMirror.superinterfaces is List);
if (classMirror.superclass == null) {
Expect.isTrue((reflectClass(Object) == classMirror) ||
(classMirror.toString() == "ClassMirror on 'FutureOr'"));
} else {
checkClass(classMirror.superclass);
}
}
checkLibrary(libraryMirror) {
libraryMirror.declarations.values
.where((d) => d is ClassMirror)
.forEach(checkClass);
}
main() {
currentMirrorSystem().libraries.values.forEach(checkLibrary);
}

View file

@ -0,0 +1,67 @@
// 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.hot_get_field;
import 'dart:mirrors';
import 'package:expect/expect.dart';
class C {
var field;
var _field;
operator +(other) => field + other;
}
const int optimizationThreshold = 20;
testPublic() {
var c = new C();
var im = reflect(c);
for (int i = 0; i < (2 * optimizationThreshold); i++) {
c.field = i;
Expect.equals(i, im.getField(#field).reflectee);
}
}
testPrivate() {
var c = new C();
var im = reflect(c);
for (int i = 0; i < (2 * optimizationThreshold); i++) {
c._field = i;
Expect.equals(i, im.getField(#_field).reflectee);
}
}
testPrivateWrongLibrary() {
var c = new C();
var im = reflect(c);
var selector = MirrorSystem.getSymbol(
'_field', reflectClass(Mirror).owner as LibraryMirror);
for (int i = 0; i < (2 * optimizationThreshold); i++) {
Expect.throwsNoSuchMethodError(() => im.getField(selector));
}
}
testOperator() {
var plus = const Symbol("+");
var c = new C();
var im = reflect(c);
for (int i = 0; i < (2 * optimizationThreshold); i++) {
c.field = i;
var closurizedPlus = im.getField(plus).reflectee;
Expect.isTrue(closurizedPlus is Function);
Expect.equals(2 * i, closurizedPlus(i));
}
}
main() {
testPublic();
testPrivate();
testPrivateWrongLibrary();
testOperator();
}

View file

@ -0,0 +1,52 @@
// 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.hot_set_field;
import 'dart:mirrors';
import 'package:expect/expect.dart';
class C {
var field;
var _field;
}
const int optimizationThreshold = 20;
testPublic() {
var c = new C();
var im = reflect(c);
for (int i = 0; i < (2 * optimizationThreshold); i++) {
im.setField(#field, i);
Expect.equals(i, c.field);
}
}
testPrivate() {
var c = new C();
var im = reflect(c);
for (int i = 0; i < (2 * optimizationThreshold); i++) {
im.setField(#_field, i);
Expect.equals(i, c._field);
}
}
testPrivateWrongLibrary() {
var c = new C();
var im = reflect(c);
var selector = MirrorSystem.getSymbol(
'_field', reflectClass(Mirror).owner as LibraryMirror);
for (int i = 0; i < (2 * optimizationThreshold); i++) {
Expect.throwsNoSuchMethodError(() => im.setField(selector, i));
}
}
main() {
testPublic();
testPrivate();
testPrivateWrongLibrary();
}

View file

@ -0,0 +1,81 @@
// Copyright (c) 2013, 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.immutable_collections;
import 'dart:mirrors';
import 'package:expect/expect.dart';
bool someException(e) => e is Exception || e is Error;
checkList(dynamic l, String reason) {
Expect.throws(() => l[0] = 'value', someException, reason);
Expect.throws(() => l.add('value'), someException, reason);
Expect.throws(() => l.clear(), someException, reason);
}
checkMap(Map m, String reason) {
Expect.throws(() => m[#key] = 'value', someException, reason);
checkList(m.keys, '$reason keys');
checkList(m.values, '$reason values');
}
checkVariable(VariableMirror vm) {
checkList(vm.metadata, 'VariableMirror.metadata');
}
checkTypeVariable(TypeVariableMirror tvm) {
checkList(tvm.metadata, 'TypeVariableMirror.metadata');
}
checkParameter(ParameterMirror pm) {
checkList(pm.metadata, 'ParameterMirror.metadata');
}
checkMethod(MethodMirror mm) {
checkList(mm.parameters, 'MethodMirror.parameters');
checkList(mm.metadata, 'MethodMirror.metadata');
mm.parameters.forEach(checkParameter);
}
checkClass(ClassMirror cm) {
checkMap(cm.declarations, 'ClassMirror.declarations');
checkMap(cm.instanceMembers, 'ClassMirror.instanceMembers');
checkMap(cm.staticMembers, 'ClassMirror.staticMembers');
checkList(cm.metadata, 'ClassMirror.metadata');
checkList(cm.superinterfaces, 'ClassMirror.superinterfaces');
checkList(cm.typeArguments, 'ClassMirror.typeArguments');
checkList(cm.typeVariables, 'ClassMirror.typeVariables');
cm.declarations.values.forEach(checkDeclaration);
cm.instanceMembers.values.forEach(checkDeclaration);
cm.staticMembers.values.forEach(checkDeclaration);
cm.typeVariables.forEach(checkTypeVariable);
}
checkType(TypeMirror tm) {
checkList(tm.metadata, 'TypeMirror.metadata');
}
checkDeclaration(DeclarationMirror dm) {
if (dm is MethodMirror) checkMethod(dm);
if (dm is ClassMirror) checkClass(dm);
if (dm is TypeMirror) checkType(dm);
if (dm is VariableMirror) checkVariable(dm);
if (dm is TypeVariableMirror) checkTypeVariable(dm);
}
checkLibrary(LibraryMirror lm) {
checkMap(lm.declarations, 'LibraryMirror.declarations');
checkList(lm.metadata, 'LibraryMirror.metadata');
lm.declarations.values.forEach(checkDeclaration);
}
main() {
currentMirrorSystem().libraries.values.forEach(checkLibrary);
checkType(currentMirrorSystem().voidType);
checkType(currentMirrorSystem().dynamicType);
}

View file

@ -0,0 +1,26 @@
// Copyright (c) 2013, 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.
// Regression test for dart2js that did type inferencing on parameters
// whose type may change at runtime due to an invocation through
// [InstanceMirror.delegate].
import 'dart:mirrors';
import 'package:expect/expect.dart';
class A {
noSuchMethod(im) {
reflect(new B()).delegate(im);
}
}
class B {
foo(a) => a + 42;
}
main() {
Expect.equals(42, new B().foo(0));
dynamic a = new A();
Expect.throwsTypeError(() => a.foo('foo'));
}

View file

@ -0,0 +1,23 @@
// Copyright (c) 2013, 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.
// Test inherited fields.
library test.inherit_field_test;
import 'dart:mirrors';
import 'stringify.dart';
class Foo {
var field;
}
class Bar extends Foo {}
void main() {
expect(
'Variable(s(field) in s(Foo))', reflectClass(Foo).declarations[#field]);
expect('<null>', reflectClass(Bar).declarations[#field]);
}

View file

@ -0,0 +1,44 @@
// 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.mirrors;
import 'dart:mirrors';
import 'package:expect/expect.dart';
class RemoteClass {
final String name;
const RemoteClass([this.name = "default"]);
}
class A {}
@RemoteClass("ASF")
class B extends A {}
class C extends B {}
void main() {
bool foundB = false;
MirrorSystem mirrorSystem = currentMirrorSystem();
mirrorSystem.libraries.forEach((lk, l) {
l.declarations.forEach((dk, d) {
if (d is ClassMirror) {
d.metadata.forEach((md) {
InstanceMirror metadata = md as InstanceMirror;
// Metadata must not be inherited.
if (metadata.type == reflectClass(RemoteClass)) {
Expect.isFalse(foundB);
Expect.equals(#B, d.simpleName);
foundB = true;
}
});
}
});
});
Expect.isTrue(foundB);
}

View file

@ -0,0 +1,159 @@
// Copyright (c) 2013, 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.initializing_formals;
import 'dart:mirrors';
import 'package:expect/expect.dart';
class Class<T> {
num numField = 0;
bool boolField = false;
String stringField = "";
T tField;
dynamic _privateField;
Class.nongeneric(this.numField);
Class.named({this.boolField = false});
Class.optPos([this.stringField = 'default']);
Class.generic(this.tField);
Class.private(this._privateField);
Class.explicitType(num this.numField);
Class.withVar(var this.numField);
Class.withSubtype(int this.numField);
}
class Constant {
final num value;
const Constant(this.value);
const Constant.marked(final this.value);
}
main() {
MethodMirror mm;
ParameterMirror pm;
mm = reflectClass(Class).declarations[#Class.nongeneric] as MethodMirror;
pm = mm.parameters.single;
Expect.equals(#numField, pm.simpleName);
Expect.equals(reflectClass(num), pm.type);
Expect.isFalse(pm.isNamed); // //# 01: ok
Expect.isFalse(pm.isFinal); // //# 01: ok
Expect.isFalse(pm.isOptional); // //# 01: ok
Expect.isFalse(pm.hasDefaultValue); // //# 01: ok
Expect.isFalse(pm.isPrivate);
Expect.isFalse(pm.isStatic);
Expect.isFalse(pm.isTopLevel);
mm = reflectClass(Class).declarations[#Class.named] as MethodMirror;
pm = mm.parameters.single;
Expect.equals(#boolField, pm.simpleName);
Expect.equals(reflectClass(bool), pm.type);
Expect.isTrue(pm.isNamed); // //# 01: ok
Expect.isFalse(pm.isFinal); // //# 01: ok
Expect.isTrue(pm.isOptional); // //# 01: ok
Expect.isTrue(pm.hasDefaultValue); // //# 01: ok
Expect.equals(false, pm.defaultValue.reflectee); // //# 01: ok
Expect.isFalse(pm.isPrivate);
Expect.isFalse(pm.isStatic);
Expect.isFalse(pm.isTopLevel);
mm = reflectClass(Class).declarations[#Class.optPos] as MethodMirror;
pm = mm.parameters.single;
Expect.equals(#stringField, pm.simpleName);
Expect.equals(reflectClass(String), pm.type);
Expect.isFalse(pm.isNamed); // //# 01: ok
Expect.isFalse(pm.isFinal); // //# 01: ok
Expect.isTrue(pm.isOptional); // //# 01: ok
Expect.isTrue(pm.hasDefaultValue); // //# 01: ok
Expect.equals('default', pm.defaultValue.reflectee); // //# 01: ok
Expect.isFalse(pm.isPrivate);
Expect.isFalse(pm.isStatic);
Expect.isFalse(pm.isTopLevel);
mm = reflectClass(Class).declarations[#Class.generic] as MethodMirror;
pm = mm.parameters.single;
Expect.equals(#tField, pm.simpleName);
Expect.equals(reflectClass(Class).typeVariables.single, pm.type);
Expect.isFalse(pm.isNamed); // //# 01: ok
Expect.isFalse(pm.isFinal); // //# 01: ok
Expect.isFalse(pm.isOptional); // //# 01: ok
Expect.isFalse(pm.hasDefaultValue); // //# 01: ok
Expect.isFalse(pm.isPrivate);
Expect.isFalse(pm.isStatic);
Expect.isFalse(pm.isTopLevel);
mm = reflectClass(Class).declarations[#Class.private] as MethodMirror;
pm = mm.parameters.single;
Expect.equals(#_privateField, pm.simpleName); // //# 03: ok
Expect.equals(currentMirrorSystem().dynamicType, pm.type);
Expect.isFalse(pm.isNamed); // //# 01: ok
Expect.isFalse(pm.isFinal); // //# 01: ok
Expect.isFalse(pm.isOptional); // //# 01: ok
Expect.isFalse(pm.hasDefaultValue); // //# 01: ok
Expect.isTrue(pm.isPrivate);
Expect.isFalse(pm.isStatic);
Expect.isFalse(pm.isTopLevel);
mm = reflectClass(Class).declarations[#Class.explicitType] as MethodMirror;
pm = mm.parameters.single;
Expect.equals(#numField, pm.simpleName);
Expect.equals(reflectClass(num), pm.type);
Expect.isFalse(pm.isNamed); // //# 01: ok
Expect.isFalse(pm.isFinal); // //# 01: ok
Expect.isFalse(pm.isOptional); // //# 01: ok
Expect.isFalse(pm.hasDefaultValue); // //# 01: ok
Expect.isFalse(pm.isPrivate);
Expect.isFalse(pm.isStatic);
Expect.isFalse(pm.isTopLevel);
mm = reflectClass(Class).declarations[#Class.withVar] as MethodMirror;
pm = mm.parameters.single;
Expect.equals(#numField, pm.simpleName);
Expect.equals(reflectClass(num), pm.type);
Expect.isFalse(pm.isNamed); // //# 01: ok
Expect.isFalse(pm.isFinal); // //# 01: ok
Expect.isFalse(pm.isOptional); // //# 01: ok
Expect.isFalse(pm.hasDefaultValue); // //# 01: ok
Expect.isFalse(pm.isPrivate);
Expect.isFalse(pm.isStatic);
Expect.isFalse(pm.isTopLevel);
mm = reflectClass(Class).declarations[#Class.withSubtype] as MethodMirror;
pm = mm.parameters.single;
Expect.equals(#numField, pm.simpleName);
Expect.equals(reflectClass(int), pm.type);
Expect.isFalse(pm.isNamed); // //# 01: ok
Expect.isFalse(pm.isFinal); // //# 01: ok
Expect.isFalse(pm.isOptional); // //# 01: ok
Expect.isFalse(pm.hasDefaultValue); // //# 01: ok
Expect.isFalse(pm.isPrivate);
Expect.isFalse(pm.isStatic);
Expect.isFalse(pm.isTopLevel);
mm = reflectClass(Constant).declarations[#Constant] as MethodMirror;
pm = mm.parameters.single;
Expect.equals(#value, pm.simpleName);
Expect.equals(reflectClass(num), pm.type);
Expect.isFalse(pm.isNamed); // //# 01: ok
Expect.isFalse(pm.isFinal); // N.B. // //# 01: ok
Expect.isFalse(pm.isOptional); // //# 01: ok
Expect.isFalse(pm.hasDefaultValue); // //# 01: ok
Expect.isFalse(pm.isPrivate);
Expect.isFalse(pm.isStatic);
Expect.isFalse(pm.isTopLevel);
mm = reflectClass(Constant).declarations[#Constant.marked] as MethodMirror;
pm = mm.parameters.single;
Expect.equals(#value, pm.simpleName);
Expect.equals(reflectClass(num), pm.type);
Expect.isFalse(pm.isNamed); // //# 01: ok
Expect.isTrue(pm.isFinal); // N.B. // //# 01: ok
Expect.isFalse(pm.isOptional); // //# 01: ok
Expect.isFalse(pm.hasDefaultValue); // //# 01: ok
Expect.isFalse(pm.isPrivate);
Expect.isFalse(pm.isStatic);
Expect.isFalse(pm.isTopLevel);
}

View file

@ -0,0 +1,29 @@
// Copyright (c) 2015, 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.
// Verify that instance creation expressions inside function
// annotations are properly handled. See dartbug.com/23354
import 'dart:mirrors';
import 'package:expect/expect.dart';
class C {
final String s;
const C(this.s);
}
class D {
final C c;
const D(this.c);
}
@D(const C('foo'))
f() {}
main() {
ClosureMirror closureMirror = reflect(f) as ClosureMirror;
List<InstanceMirror> metadata = closureMirror.function.metadata;
Expect.equals(1, metadata.length);
Expect.equals(metadata[0].reflectee.c.s, 'foo');
}

View file

@ -0,0 +1,91 @@
// Copyright (c) 2013, 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.instance_members;
import 'dart:mirrors';
import 'package:expect/expect.dart';
import 'declarations_model_easier.dart' as declarations_model;
selectKeys(map, predicate) {
return map.keys.where((key) => predicate(map[key]));
}
class EasierSuperclass {
shuper() {}
static staticShuper() {}
}
class EasierMixin {
mixin() {}
static staticMixin() {}
}
class EasierMixinApplication extends EasierSuperclass with EasierMixin {
application() {}
static staticApplication() {}
}
class Derived extends EasierMixinApplication {
derived() {}
static staticDerived() {}
}
main() {
ClassMirror cm = reflectClass(declarations_model.Class);
Expect.setEquals([
#+,
#instanceVariable,
const Symbol('instanceVariable='),
#instanceGetter,
const Symbol('instanceSetter='),
#instanceMethod,
#-,
#inheritedInstanceVariable,
const Symbol('inheritedInstanceVariable='),
#inheritedInstanceGetter,
const Symbol('inheritedInstanceSetter='),
#inheritedInstanceMethod,
#hashCode,
#runtimeType,
#==,
#noSuchMethod,
#toString
], selectKeys(cm.instanceMembers, (dm) => !dm.isPrivate));
// Filter out private to avoid implementation-specific members of Object.
Expect.setEquals([
#instanceVariable,
const Symbol('instanceVariable='),
#inheritedInstanceVariable,
const Symbol('inheritedInstanceVariable=')
], selectKeys(cm.instanceMembers, (dm) => !dm.isPrivate && dm.isSynthetic));
cm = reflectClass(Derived);
Expect.setEquals([
#derived,
#shuper,
#mixin,
#application,
#hashCode,
#runtimeType,
#==,
#noSuchMethod,
#toString
], selectKeys(cm.instanceMembers, (dm) => !dm.isPrivate));
cm = reflectClass(EasierMixinApplication);
Expect.setEquals([
#shuper,
#mixin,
#application,
#hashCode,
#runtimeType,
#==,
#noSuchMethod,
#toString
], selectKeys(cm.instanceMembers, (dm) => !dm.isPrivate));
}

Some files were not shown because too many files have changed in this diff Show more