From b5237e99577188dbba244d26c976d98adf64fc4d Mon Sep 17 00:00:00 2001 From: Tobe O Date: Thu, 8 Nov 2018 11:54:23 -0500 Subject: [PATCH] unrequire pkg:meta --- angel_container/pubspec.yaml | 1 - .../lib/src/library_generator.dart | 66 ++++++++++++------- .../test/reflector.reflector.g.dart | 15 ++--- 3 files changed, 47 insertions(+), 35 deletions(-) diff --git a/angel_container/pubspec.yaml b/angel_container/pubspec.yaml index 971e69d0..ffac5efe 100644 --- a/angel_container/pubspec.yaml +++ b/angel_container/pubspec.yaml @@ -7,7 +7,6 @@ environment: sdk: ">=1.8.0 <3.0.0" dependencies: collection: ^1.0.0 - meta: ^1.0.0 quiver: ^2.0.0 dev_dependencies: test: \ No newline at end of file diff --git a/angel_container_generator/lib/src/library_generator.dart b/angel_container_generator/lib/src/library_generator.dart index 43041aef..d69f0c38 100644 --- a/angel_container_generator/lib/src/library_generator.dart +++ b/angel_container_generator/lib/src/library_generator.dart @@ -12,55 +12,71 @@ class ReflectorLibraryGenerator { ReflectorLibraryGenerator(this.element, this.annotation); + String get reflectorClassName { +// Select the name + if (annotation.name?.isNotEmpty == true) { + return new ReCase(annotation.name).pascalCase + 'Reflector'; + } else if (element.name?.isNotEmpty == true) { + var rc = new ReCase(element.name); + return rc.pascalCase + 'Reflector'; + } else { + throw new StateError( + 'A statically-generated Reflector must reside in a named library, or include the `name` attribute in @GenerateReflector().'); + } + } + + String get typeMapName => new ReCase(reflectorClassName).camelCase + 'Types'; + String toSource() { return generate().accept(new DartEmitter()).toString(); } Library generate() { return new Library((lib) { - lib.body.add(generateReflectorClass()); + var clazz = generateReflectorClass(); // Generate a ReflectedClass for each type + var staticTypes = {}; + for (var type in annotation.types) { if (type is InterfaceType) { - lib.body.add(generateReflectedClass(type)); + var reflected = generateReflectedClass(type); + lib.body.add(reflected); + staticTypes[type.name] = + refer(reflected.name).constInstanceNamed('_', []); } else { // TODO: Handle these } } + + clazz = clazz.rebuild((b) { + // Generate static values + b.fields.add(new Field((b) => b + ..name = 'staticTypes' + ..modifier = FieldModifier.constant + ..static = true + ..assignment = literalConstMap(staticTypes + .map((name, type) => new MapEntry(refer(name), type))).code)); + }); + + lib.body.add(clazz); }); } Class generateReflectorClass() { return new Class((clazz) { - // Select the name - if (annotation.name?.isNotEmpty == true) { - clazz.name = annotation.name; - } else { - var rc = new ReCase(element.name); - clazz.name = rc.pascalCase + 'Reflector'; - } + clazz.name = reflectorClassName; - // implements Reflector - clazz - ..extend = refer('StaticReflector') - ..implements.add(refer('Reflector')); + // extends StaticReflector + clazz.extend = refer('StaticReflector'); // Add a const constructor clazz.constructors.add(new Constructor((b) { - b..constant = true; - })); - - // Add a reflectClass that just forwards to reflectType - clazz.methods.add(new Method((b) { b - ..name = 'reflectClass' - ..returns = refer('ReflectedClass') - ..annotations.add(refer('override')) - ..requiredParameters.add(new Parameter((b) => b - ..name = 'type' - ..type = refer('Type'))) - ..body = new Code('return reflectType(type) as ReflectedClass;'); + ..constant = true + ..initializers.add( + refer('super').call([], {'types': refer('staticTypes')}).code); + // TODO: Invoke super with static info })); }); } diff --git a/angel_container_generator/test/reflector.reflector.g.dart b/angel_container_generator/test/reflector.reflector.g.dart index 6d335be4..2e9cb5c7 100644 --- a/angel_container_generator/test/reflector.reflector.g.dart +++ b/angel_container_generator/test/reflector.reflector.g.dart @@ -6,16 +6,13 @@ part of angel_container_generator_test; // AngelContainerGenerator // ************************************************************************** -class AngelContainerGeneratorTestReflector implements Reflector { - const AngelContainerGeneratorTestReflector(); - - @override - ReflectedClass reflectClass(Type type) { - return reflectType(type) as ReflectedClass; - } -} - class _ReflectedArtist extends ReflectedClass { const _ReflectedArtist._() : super('Artist', const [], const [], const [], const [], Artist); } + +class AngelContainerGeneratorTestReflector extends StaticReflector { + const AngelContainerGeneratorTestReflector() : super(types: staticTypes); + + static const staticTypes = const {Artist: const _ReflectedArtist._()}; +}