unrequire pkg:meta
This commit is contained in:
parent
feb489949a
commit
b5237e9957
3 changed files with 47 additions and 35 deletions
|
@ -7,7 +7,6 @@ environment:
|
||||||
sdk: ">=1.8.0 <3.0.0"
|
sdk: ">=1.8.0 <3.0.0"
|
||||||
dependencies:
|
dependencies:
|
||||||
collection: ^1.0.0
|
collection: ^1.0.0
|
||||||
meta: ^1.0.0
|
|
||||||
quiver: ^2.0.0
|
quiver: ^2.0.0
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
test:
|
test:
|
|
@ -12,55 +12,71 @@ class ReflectorLibraryGenerator {
|
||||||
|
|
||||||
ReflectorLibraryGenerator(this.element, this.annotation);
|
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() {
|
String toSource() {
|
||||||
return generate().accept(new DartEmitter()).toString();
|
return generate().accept(new DartEmitter()).toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
Library generate() {
|
Library generate() {
|
||||||
return new Library((lib) {
|
return new Library((lib) {
|
||||||
lib.body.add(generateReflectorClass());
|
var clazz = generateReflectorClass();
|
||||||
|
|
||||||
// Generate a ReflectedClass for each type
|
// Generate a ReflectedClass for each type
|
||||||
|
var staticTypes = <String, Expression>{};
|
||||||
|
|
||||||
for (var type in annotation.types) {
|
for (var type in annotation.types) {
|
||||||
if (type is InterfaceType) {
|
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 {
|
} else {
|
||||||
// TODO: Handle these
|
// 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() {
|
Class generateReflectorClass() {
|
||||||
return new Class((clazz) {
|
return new Class((clazz) {
|
||||||
// Select the name
|
clazz.name = reflectorClassName;
|
||||||
if (annotation.name?.isNotEmpty == true) {
|
|
||||||
clazz.name = annotation.name;
|
|
||||||
} else {
|
|
||||||
var rc = new ReCase(element.name);
|
|
||||||
clazz.name = rc.pascalCase + 'Reflector';
|
|
||||||
}
|
|
||||||
|
|
||||||
// implements Reflector
|
// extends StaticReflector
|
||||||
clazz
|
clazz.extend = refer('StaticReflector');
|
||||||
..extend = refer('StaticReflector')
|
|
||||||
..implements.add(refer('Reflector'));
|
|
||||||
|
|
||||||
// Add a const constructor
|
// Add a const constructor
|
||||||
clazz.constructors.add(new Constructor((b) {
|
clazz.constructors.add(new Constructor((b) {
|
||||||
b..constant = true;
|
|
||||||
}));
|
|
||||||
|
|
||||||
// Add a reflectClass that just forwards to reflectType
|
|
||||||
clazz.methods.add(new Method((b) {
|
|
||||||
b
|
b
|
||||||
..name = 'reflectClass'
|
..constant = true
|
||||||
..returns = refer('ReflectedClass')
|
..initializers.add(
|
||||||
..annotations.add(refer('override'))
|
refer('super').call([], {'types': refer('staticTypes')}).code);
|
||||||
..requiredParameters.add(new Parameter((b) => b
|
// TODO: Invoke super with static info
|
||||||
..name = 'type'
|
|
||||||
..type = refer('Type')))
|
|
||||||
..body = new Code('return reflectType(type) as ReflectedClass;');
|
|
||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,16 +6,13 @@ part of angel_container_generator_test;
|
||||||
// AngelContainerGenerator
|
// AngelContainerGenerator
|
||||||
// **************************************************************************
|
// **************************************************************************
|
||||||
|
|
||||||
class AngelContainerGeneratorTestReflector implements Reflector {
|
|
||||||
const AngelContainerGeneratorTestReflector();
|
|
||||||
|
|
||||||
@override
|
|
||||||
ReflectedClass reflectClass(Type type) {
|
|
||||||
return reflectType(type) as ReflectedClass;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class _ReflectedArtist extends ReflectedClass {
|
class _ReflectedArtist extends ReflectedClass {
|
||||||
const _ReflectedArtist._()
|
const _ReflectedArtist._()
|
||||||
: super('Artist', const [], const [], const [], const [], Artist);
|
: super('Artist', const [], const [], const [], const [], Artist);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class AngelContainerGeneratorTestReflector extends StaticReflector {
|
||||||
|
const AngelContainerGeneratorTestReflector() : super(types: staticTypes);
|
||||||
|
|
||||||
|
static const staticTypes = const {Artist: const _ReflectedArtist._()};
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue