tad wonky, but generator works
This commit is contained in:
parent
4e8845a410
commit
d0efa81089
4 changed files with 416 additions and 54 deletions
|
@ -4,6 +4,7 @@ import 'package:reflectable/reflectable.dart';
|
|||
/// A [Reflectable] instance that can be used as an annotation on types to generate metadata for them.
|
||||
const Reflectable contained = const ContainedReflectable();
|
||||
|
||||
@contained
|
||||
class ContainedReflectable extends Reflectable {
|
||||
const ContainedReflectable()
|
||||
: super(
|
||||
|
@ -13,7 +14,8 @@ class ContainedReflectable extends Reflectable {
|
|||
metadataCapability,
|
||||
newInstanceCapability,
|
||||
reflectedTypeCapability,
|
||||
typeRelationsCapability);
|
||||
typeRelationsCapability,
|
||||
typeCapability);
|
||||
}
|
||||
|
||||
/// A [Reflector] instance that uses a [Reflectable] to reflect upon data.
|
||||
|
@ -24,8 +26,7 @@ class GeneratedReflector implements Reflector {
|
|||
|
||||
@override
|
||||
String getName(Symbol symbol) {
|
||||
// TODO: implement getName
|
||||
throw new UnimplementedError();
|
||||
return symbol.toString().substring(7);
|
||||
}
|
||||
|
||||
@override
|
||||
|
@ -35,8 +36,17 @@ class GeneratedReflector implements Reflector {
|
|||
|
||||
@override
|
||||
ReflectedFunction reflectFunction(Function function) {
|
||||
// TODO: implement reflectFunction
|
||||
throw new UnimplementedError();
|
||||
if (!reflectable.canReflect(function)) {
|
||||
throw new UnsupportedError('Cannot reflect $function.');
|
||||
}
|
||||
|
||||
var mirror = reflectable.reflect(function);
|
||||
|
||||
if (mirror is ClosureMirror) {
|
||||
return new _GeneratedReflectedFunction(mirror.function, this, mirror);
|
||||
} else {
|
||||
throw new ArgumentError('$function is not a Function.');
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
|
@ -44,7 +54,8 @@ class GeneratedReflector implements Reflector {
|
|||
if (!reflectable.canReflect(object)) {
|
||||
throw new UnsupportedError('Cannot reflect $object.');
|
||||
} else {
|
||||
return new _GeneratedReflectedInstance(reflectable.reflect(object), this);
|
||||
var mirror = reflectable.reflect(object);
|
||||
return new _GeneratedReflectedInstance(mirror, this);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -109,8 +120,9 @@ class _GeneratedReflectedClass extends ReflectedClass {
|
|||
ReflectedInstance newInstance(
|
||||
String constructorName, List positionalArguments,
|
||||
[Map<String, dynamic> namedArguments, List<Type> typeArguments]) {
|
||||
return mirror.newInstance(constructorName, positionalArguments,
|
||||
var result = mirror.newInstance(constructorName, positionalArguments,
|
||||
namedArguments.map((k, v) => new MapEntry(new Symbol(k), v)));
|
||||
return reflector.reflectInstance(result);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -143,24 +155,82 @@ class _GeneratedReflectedType extends ReflectedType {
|
|||
}
|
||||
}
|
||||
|
||||
// TODO: Reflect functions?
|
||||
class _GeneratedReflectedFunction extends ReflectedFunction {
|
||||
final MethodMirror mirror;
|
||||
final Reflector reflector;
|
||||
final ClosureMirror closure;
|
||||
|
||||
_GeneratedReflectedFunction(this.mirror, this.reflector, [this.closure])
|
||||
: super(
|
||||
mirror.simpleName,
|
||||
[],
|
||||
null,
|
||||
!mirror.isRegularMethod
|
||||
? null
|
||||
: new _GeneratedReflectedType(mirror.returnType),
|
||||
mirror.parameters
|
||||
.map((p) => _convertParameter(p, reflector))
|
||||
.toList(),
|
||||
mirror.isGetter,
|
||||
mirror.isSetter);
|
||||
|
||||
@override
|
||||
List<ReflectedInstance> get annotations =>
|
||||
mirror.metadata.map(reflector.reflectInstance).toList();
|
||||
|
||||
@override
|
||||
ReflectedInstance invoke(Invocation invocation) {
|
||||
if (closure != null) {
|
||||
throw new UnsupportedError('Only closures can be invoked directly.');
|
||||
} else {
|
||||
var result = closure.delegate(invocation);
|
||||
return reflector.reflectInstance(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
List<ReflectedFunction> _constructorsOf(
|
||||
Map<String, DeclarationMirror> map, Reflector reflector) {
|
||||
print(map);
|
||||
return map.entries.fold<List<ReflectedFunction>>([], (out, entry) {
|
||||
var k = entry.key, v = entry.value;
|
||||
var v = entry.value;
|
||||
|
||||
if (v is MethodMirror && v.isConstructor) {
|
||||
return out..add(new _GeneratedReflectedFunction(v, reflector));
|
||||
} else {
|
||||
return out;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
List<ReflectedDeclaration> _declarationsOf(
|
||||
Map<String, DeclarationMirror> map, Reflector reflector) {
|
||||
print(map);
|
||||
return map.entries.fold<List<ReflectedDeclaration>>([], (out, entry) {
|
||||
var k = entry.key, v = entry.value;
|
||||
var v = entry.value;
|
||||
|
||||
if (v is VariableMirror) {
|
||||
var decl = new ReflectedDeclaration(v.simpleName, v.isStatic, null);
|
||||
return out..add(decl);
|
||||
}
|
||||
if (v is MethodMirror) {
|
||||
var decl = new ReflectedDeclaration(v.simpleName, v.isStatic,
|
||||
new _GeneratedReflectedFunction(v, reflector));
|
||||
return out..add(decl);
|
||||
} else {
|
||||
return out;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
ReflectedTypeParameter _convertTypeVariable(TypeVariableMirror mirror) {
|
||||
return new ReflectedTypeParameter(mirror.simpleName);
|
||||
}
|
||||
|
||||
ReflectedParameter _convertParameter(
|
||||
ParameterMirror mirror, Reflector reflector) {
|
||||
return new ReflectedParameter(
|
||||
mirror.simpleName,
|
||||
mirror.metadata.map(reflector.reflectInstance).toList(),
|
||||
reflector.reflectType(mirror.type.reflectedType),
|
||||
!mirror.isOptional,
|
||||
mirror.isNamed);
|
||||
}
|
||||
|
|
|
@ -10,4 +10,5 @@ dependencies:
|
|||
reflectable: ^2.0.0
|
||||
dev_dependencies:
|
||||
build_runner: ^1.0.0
|
||||
build_test:
|
||||
test: ^1.0.0
|
|
@ -1,10 +1,19 @@
|
|||
import 'package:angel_container/angel_container.dart';
|
||||
import 'package:angel_container_generator/angel_container_generator.dart';
|
||||
import 'package:test/test.dart';
|
||||
import 'reflector_test.reflectable.dart';
|
||||
|
||||
void main() {
|
||||
var reflector = const GeneratedReflector();
|
||||
initializeReflectable();
|
||||
var artist = new Artist();
|
||||
var reflector = const GeneratedReflector();
|
||||
Container container;
|
||||
|
||||
setUp(() {
|
||||
container = new Container(reflector);
|
||||
container.registerSingleton(artist);
|
||||
//container.registerSingleton(new Artist(name: 'Tobe Osakwe'));
|
||||
});
|
||||
|
||||
group('reflectClass', () {
|
||||
var mirror = reflector.reflectClass(Artist);
|
||||
|
@ -13,9 +22,31 @@ void main() {
|
|||
expect(mirror.name, 'Artist');
|
||||
});
|
||||
});
|
||||
|
||||
test('inject constructor parameters', () {
|
||||
var album = container.make<Album>();
|
||||
print(album.title);
|
||||
expect(album.title, 'flowers by ${artist.lowerName}');
|
||||
});
|
||||
}
|
||||
|
||||
@contained
|
||||
class Artist {
|
||||
String name;
|
||||
//final String name;
|
||||
|
||||
//Artist({this.name});
|
||||
|
||||
String get lowerName {
|
||||
//return name.toLowerCase();
|
||||
return hashCode.toString().toLowerCase();
|
||||
}
|
||||
}
|
||||
|
||||
@contained
|
||||
class Album {
|
||||
final Artist artist;
|
||||
|
||||
Album(this.artist);
|
||||
|
||||
String get title => 'flowers by ${artist.lowerName}';
|
||||
}
|
||||
|
|
|
@ -4,6 +4,8 @@
|
|||
import "dart:core";
|
||||
import 'package:angel_container_generator/angel_container_generator.dart'
|
||||
as prefix0;
|
||||
import 'package:reflectable/capability.dart' as prefix2;
|
||||
import 'package:reflectable/mirrors.dart' as prefix3;
|
||||
import 'reflector_test.dart' as prefix1;
|
||||
|
||||
// ignore:unused_import
|
||||
|
@ -22,8 +24,8 @@ final _data = <r.Reflectable, r.ReflectorData>{
|
|||
7,
|
||||
0,
|
||||
const prefix0.ContainedReflectable(),
|
||||
const <int>[0, 3],
|
||||
const <int>[4, 5, 6, 7, 8, 1, 2],
|
||||
const <int>[1, 2],
|
||||
const <int>[3, 4, 5, 6, 7, 1],
|
||||
const <int>[],
|
||||
-1,
|
||||
{},
|
||||
|
@ -33,21 +35,64 @@ final _data = <r.Reflectable, r.ReflectorData>{
|
|||
0,
|
||||
const <int>[],
|
||||
const <Object>[prefix0.contained],
|
||||
null),
|
||||
new r.NonGenericClassMirrorImpl(
|
||||
r"Album",
|
||||
r".Album",
|
||||
7,
|
||||
1,
|
||||
const prefix0.ContainedReflectable(),
|
||||
const <int>[0, 9, 10],
|
||||
const <int>[3, 4, 5, 6, 7, 8, 9],
|
||||
const <int>[],
|
||||
-1,
|
||||
{},
|
||||
{},
|
||||
{r"": (b) => (artist) => b ? new prefix1.Album(artist) : null},
|
||||
-1,
|
||||
1,
|
||||
const <int>[],
|
||||
const <Object>[prefix0.contained],
|
||||
null),
|
||||
new r.NonGenericClassMirrorImpl(
|
||||
r"ContainedReflectable",
|
||||
r".ContainedReflectable",
|
||||
7,
|
||||
2,
|
||||
const prefix0.ContainedReflectable(),
|
||||
const <int>[11],
|
||||
const <int>[3, 4, 5, 6, 7, 12, 13, 14, 15, 16, 17, 18, 19],
|
||||
const <int>[],
|
||||
-1,
|
||||
{},
|
||||
{},
|
||||
{r"": (b) => () => b ? new prefix0.ContainedReflectable() : null},
|
||||
-1,
|
||||
2,
|
||||
const <int>[],
|
||||
const <Object>[prefix0.contained],
|
||||
null)
|
||||
],
|
||||
<m.DeclarationMirror>[
|
||||
new r.VariableMirrorImpl(
|
||||
r"name",
|
||||
32773,
|
||||
0,
|
||||
const prefix0.ContainedReflectable(),
|
||||
-1,
|
||||
r"artist",
|
||||
33797,
|
||||
1,
|
||||
1, const <int>[], const <Object>[]),
|
||||
new r.ImplicitGetterMirrorImpl(
|
||||
const prefix0.ContainedReflectable(), 0, 1, 1, 1),
|
||||
new r.ImplicitSetterMirrorImpl(
|
||||
const prefix0.ContainedReflectable(), 0, 1, 1, 2),
|
||||
const prefix0.ContainedReflectable(),
|
||||
0,
|
||||
0,
|
||||
0, const <int>[], const <Object>[]),
|
||||
new r.MethodMirrorImpl(
|
||||
r"lowerName",
|
||||
131075,
|
||||
0,
|
||||
-1,
|
||||
3,
|
||||
3,
|
||||
const <int>[],
|
||||
const <int>[],
|
||||
const prefix0.ContainedReflectable(),
|
||||
const <Object>[]),
|
||||
new r.MethodMirrorImpl(r"", 64, 0, -1, 0, 0, const <int>[],
|
||||
const <int>[], const prefix0.ContainedReflectable(), const []),
|
||||
new r.MethodMirrorImpl(
|
||||
|
@ -55,10 +100,10 @@ final _data = <r.Reflectable, r.ReflectorData>{
|
|||
131074,
|
||||
null,
|
||||
-1,
|
||||
2,
|
||||
2,
|
||||
4,
|
||||
4,
|
||||
const <int>[],
|
||||
const <int>[1],
|
||||
const <int>[0],
|
||||
const prefix0.ContainedReflectable(),
|
||||
const <Object>[]),
|
||||
new r.MethodMirrorImpl(
|
||||
|
@ -66,8 +111,8 @@ final _data = <r.Reflectable, r.ReflectorData>{
|
|||
131074,
|
||||
null,
|
||||
-1,
|
||||
1,
|
||||
1,
|
||||
3,
|
||||
3,
|
||||
const <int>[],
|
||||
const <int>[],
|
||||
const prefix0.ContainedReflectable(),
|
||||
|
@ -80,7 +125,7 @@ final _data = <r.Reflectable, r.ReflectorData>{
|
|||
null,
|
||||
null,
|
||||
const <int>[],
|
||||
const <int>[2],
|
||||
const <int>[1],
|
||||
const prefix0.ContainedReflectable(),
|
||||
const <Object>[const pragma("vm:entry-point")]),
|
||||
new r.MethodMirrorImpl(
|
||||
|
@ -88,8 +133,8 @@ final _data = <r.Reflectable, r.ReflectorData>{
|
|||
131075,
|
||||
null,
|
||||
-1,
|
||||
3,
|
||||
3,
|
||||
5,
|
||||
5,
|
||||
const <int>[],
|
||||
const <int>[],
|
||||
const prefix0.ContainedReflectable(),
|
||||
|
@ -99,30 +144,141 @@ final _data = <r.Reflectable, r.ReflectorData>{
|
|||
131075,
|
||||
null,
|
||||
-1,
|
||||
4,
|
||||
4,
|
||||
6,
|
||||
6,
|
||||
const <int>[],
|
||||
const <int>[],
|
||||
const prefix0.ContainedReflectable(),
|
||||
const <Object>[])
|
||||
],
|
||||
<m.ParameterMirror>[
|
||||
new r.ParameterMirrorImpl(
|
||||
r"_name",
|
||||
32870,
|
||||
2,
|
||||
const <Object>[]),
|
||||
new r.ImplicitGetterMirrorImpl(
|
||||
const prefix0.ContainedReflectable(), 0, 0, 0, 8),
|
||||
new r.MethodMirrorImpl(
|
||||
r"title",
|
||||
131075,
|
||||
1,
|
||||
-1,
|
||||
3,
|
||||
3,
|
||||
const <int>[],
|
||||
const <int>[],
|
||||
const prefix0.ContainedReflectable(),
|
||||
const <Object>[]),
|
||||
new r.MethodMirrorImpl(
|
||||
r"",
|
||||
0,
|
||||
1,
|
||||
-1,
|
||||
1,
|
||||
1,
|
||||
const <int>[],
|
||||
const [],
|
||||
const <int>[2],
|
||||
const prefix0.ContainedReflectable(),
|
||||
const <Object>[]),
|
||||
new r.MethodMirrorImpl(
|
||||
r"",
|
||||
128,
|
||||
2,
|
||||
-1,
|
||||
2,
|
||||
2,
|
||||
const <int>[],
|
||||
const <int>[],
|
||||
const prefix0.ContainedReflectable(),
|
||||
const <Object>[]),
|
||||
new r.MethodMirrorImpl(
|
||||
r"capabilities",
|
||||
4325379,
|
||||
null,
|
||||
null),
|
||||
-1,
|
||||
8,
|
||||
9,
|
||||
const <int>[7],
|
||||
const <int>[],
|
||||
const prefix0.ContainedReflectable(),
|
||||
const <Object>[]),
|
||||
new r.MethodMirrorImpl(
|
||||
r"canReflect",
|
||||
131074,
|
||||
null,
|
||||
-1,
|
||||
4,
|
||||
4,
|
||||
const <int>[],
|
||||
const <int>[3],
|
||||
const prefix0.ContainedReflectable(),
|
||||
const <Object>[override]),
|
||||
new r.MethodMirrorImpl(
|
||||
r"reflect",
|
||||
131074,
|
||||
null,
|
||||
-1,
|
||||
10,
|
||||
10,
|
||||
const <int>[],
|
||||
const <int>[4],
|
||||
const prefix0.ContainedReflectable(),
|
||||
const <Object>[override]),
|
||||
new r.MethodMirrorImpl(
|
||||
r"canReflectType",
|
||||
131074,
|
||||
null,
|
||||
-1,
|
||||
4,
|
||||
4,
|
||||
const <int>[],
|
||||
const <int>[5],
|
||||
const prefix0.ContainedReflectable(),
|
||||
const <Object>[override]),
|
||||
new r.MethodMirrorImpl(
|
||||
r"reflectType",
|
||||
131074,
|
||||
null,
|
||||
-1,
|
||||
11,
|
||||
11,
|
||||
const <int>[],
|
||||
const <int>[6],
|
||||
const prefix0.ContainedReflectable(),
|
||||
const <Object>[override]),
|
||||
new r.MethodMirrorImpl(
|
||||
r"findLibrary",
|
||||
131074,
|
||||
null,
|
||||
-1,
|
||||
12,
|
||||
12,
|
||||
const <int>[],
|
||||
const <int>[7],
|
||||
const prefix0.ContainedReflectable(),
|
||||
const <Object>[override]),
|
||||
new r.MethodMirrorImpl(
|
||||
r"libraries",
|
||||
4325379,
|
||||
null,
|
||||
-1,
|
||||
14,
|
||||
15,
|
||||
const <int>[13, 12],
|
||||
const <int>[],
|
||||
const prefix0.ContainedReflectable(),
|
||||
const <Object>[override]),
|
||||
new r.MethodMirrorImpl(
|
||||
r"annotatedClasses",
|
||||
4325379,
|
||||
null,
|
||||
-1,
|
||||
17,
|
||||
18,
|
||||
const <int>[16],
|
||||
const <int>[],
|
||||
const prefix0.ContainedReflectable(),
|
||||
const <Object>[override])
|
||||
],
|
||||
<m.ParameterMirror>[
|
||||
new r.ParameterMirrorImpl(
|
||||
r"other",
|
||||
16390,
|
||||
4,
|
||||
3,
|
||||
const prefix0.ContainedReflectable(),
|
||||
null,
|
||||
null,
|
||||
|
@ -134,27 +290,131 @@ final _data = <r.Reflectable, r.ReflectorData>{
|
|||
new r.ParameterMirrorImpl(
|
||||
r"invocation",
|
||||
32774,
|
||||
6,
|
||||
5,
|
||||
const prefix0.ContainedReflectable(),
|
||||
-1,
|
||||
5,
|
||||
5,
|
||||
19,
|
||||
19,
|
||||
const <int>[],
|
||||
const <Object>[],
|
||||
null,
|
||||
null),
|
||||
new r.ParameterMirrorImpl(
|
||||
r"artist",
|
||||
32774,
|
||||
10,
|
||||
const prefix0.ContainedReflectable(),
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
const <int>[],
|
||||
const <Object>[],
|
||||
null,
|
||||
null),
|
||||
new r.ParameterMirrorImpl(
|
||||
r"reflectee",
|
||||
32774,
|
||||
13,
|
||||
const prefix0.ContainedReflectable(),
|
||||
-1,
|
||||
20,
|
||||
20,
|
||||
const <int>[],
|
||||
const <Object>[],
|
||||
null,
|
||||
null),
|
||||
new r.ParameterMirrorImpl(
|
||||
r"reflectee",
|
||||
32774,
|
||||
14,
|
||||
const prefix0.ContainedReflectable(),
|
||||
-1,
|
||||
20,
|
||||
20,
|
||||
const <int>[],
|
||||
const <Object>[],
|
||||
null,
|
||||
null),
|
||||
new r.ParameterMirrorImpl(
|
||||
r"type",
|
||||
32774,
|
||||
15,
|
||||
const prefix0.ContainedReflectable(),
|
||||
-1,
|
||||
6,
|
||||
6,
|
||||
const <int>[],
|
||||
const <Object>[],
|
||||
null,
|
||||
null),
|
||||
new r.ParameterMirrorImpl(
|
||||
r"type",
|
||||
32774,
|
||||
16,
|
||||
const prefix0.ContainedReflectable(),
|
||||
-1,
|
||||
6,
|
||||
6,
|
||||
const <int>[],
|
||||
const <Object>[],
|
||||
null,
|
||||
null),
|
||||
new r.ParameterMirrorImpl(
|
||||
r"libraryName",
|
||||
32774,
|
||||
17,
|
||||
const prefix0.ContainedReflectable(),
|
||||
-1,
|
||||
3,
|
||||
3,
|
||||
const <int>[],
|
||||
const <Object>[],
|
||||
null,
|
||||
null)
|
||||
],
|
||||
<Type>[prefix1.Artist, String, bool, int, Type, Invocation],
|
||||
1,
|
||||
<Type>[
|
||||
prefix1.Artist,
|
||||
prefix1.Album,
|
||||
prefix0.ContainedReflectable,
|
||||
String,
|
||||
bool,
|
||||
int,
|
||||
Type,
|
||||
prefix2.ReflectCapability,
|
||||
const m.TypeValue<List<prefix2.ReflectCapability>>().type,
|
||||
List,
|
||||
prefix3.InstanceMirror,
|
||||
prefix3.TypeMirror,
|
||||
prefix3.LibraryMirror,
|
||||
Uri,
|
||||
const m.TypeValue<Map<Uri, prefix3.LibraryMirror>>().type,
|
||||
Map,
|
||||
prefix3.ClassMirror,
|
||||
const m.TypeValue<Iterable<prefix3.ClassMirror>>().type,
|
||||
Iterable,
|
||||
Invocation,
|
||||
Object
|
||||
],
|
||||
3,
|
||||
{
|
||||
r"==": (dynamic instance) => (x) => instance == x,
|
||||
r"toString": (dynamic instance) => instance.toString,
|
||||
r"noSuchMethod": (dynamic instance) => instance.noSuchMethod,
|
||||
r"hashCode": (dynamic instance) => instance.hashCode,
|
||||
r"runtimeType": (dynamic instance) => instance.runtimeType,
|
||||
r"name": (dynamic instance) => instance.name
|
||||
r"lowerName": (dynamic instance) => instance.lowerName,
|
||||
r"artist": (dynamic instance) => instance.artist,
|
||||
r"title": (dynamic instance) => instance.title,
|
||||
r"capabilities": (dynamic instance) => instance.capabilities,
|
||||
r"canReflect": (dynamic instance) => instance.canReflect,
|
||||
r"reflect": (dynamic instance) => instance.reflect,
|
||||
r"canReflectType": (dynamic instance) => instance.canReflectType,
|
||||
r"reflectType": (dynamic instance) => instance.reflectType,
|
||||
r"findLibrary": (dynamic instance) => instance.findLibrary,
|
||||
r"libraries": (dynamic instance) => instance.libraries,
|
||||
r"annotatedClasses": (dynamic instance) => instance.annotatedClasses
|
||||
},
|
||||
{r"name=": (dynamic instance, value) => instance.name = value},
|
||||
{},
|
||||
null,
|
||||
[])
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue