feat(Reflection): extract reflection capabilities into a separate module
This commit is contained in:
parent
044625a098
commit
6e8175a816
|
@ -44,6 +44,7 @@ module.exports = function(config) {
|
|||
// Local dependencies, transpiled from the source.
|
||||
'/packages/core': 'http://localhost:9877/base/modules/core/src',
|
||||
'/packages/change_detection': 'http://localhost:9877/base/modules/change_detection/src',
|
||||
'/packages/reflection': 'http://localhost:9877/base/modules/reflection/src',
|
||||
'/packages/di': 'http://localhost:9877/base/modules/di/src',
|
||||
'/packages/facade': 'http://localhost:9877/base/modules/facade/src',
|
||||
'/packages/test_lib': 'http://localhost:9877/base/modules/test_lib/src',
|
||||
|
|
|
@ -6,11 +6,10 @@ import {MapWrapper} from 'facade/collection';
|
|||
import {AnnotatedType} from 'core/compiler/annotated_type';
|
||||
|
||||
import {Parser} from 'change_detection/parser/parser';
|
||||
import {ClosureMap} from 'change_detection/parser/closure_map';
|
||||
import {Lexer} from 'change_detection/parser/lexer';
|
||||
|
||||
import {Compiler} from 'core/compiler/compiler';
|
||||
import {Reflector} from 'core/compiler/reflector';
|
||||
import {DirectiveMetadataReader} from 'core/compiler/directive_metadata_reader';
|
||||
|
||||
import {Component} from 'core/annotations/annotations';
|
||||
import {Decorator} from 'core/annotations/annotations';
|
||||
|
@ -22,10 +21,9 @@ var compiler;
|
|||
var annotatedComponent;
|
||||
|
||||
function setup() {
|
||||
var closureMap = new ClosureMap();
|
||||
var reflector = new CachingReflector();
|
||||
compiler = new Compiler(null, reflector, new Parser(new Lexer(), closureMap), closureMap);
|
||||
annotatedComponent = reflector.annotatedType(BenchmarkComponent);
|
||||
var reader = new CachingDirectiveMetadataReader();
|
||||
compiler = new Compiler(null, reader, new Parser(new Lexer()));
|
||||
annotatedComponent = reader.annotatedType(BenchmarkComponent);
|
||||
}
|
||||
|
||||
export function main() {
|
||||
|
@ -63,7 +61,7 @@ function loadTemplate(templateId, repeatCount) {
|
|||
}
|
||||
|
||||
// Caching reflector as reflection in Dart using Mirrors
|
||||
class CachingReflector extends Reflector {
|
||||
class CachingDirectiveMetadataReader extends DirectiveMetadataReader {
|
||||
_cache: Map;
|
||||
constructor() {
|
||||
this._cache = MapWrapper.create();
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
import {FIELD, autoConvertAdd, isBlank, isPresent, FunctionWrapper, BaseException} from "facade/lang";
|
||||
import {List, Map, ListWrapper, MapWrapper} from "facade/collection";
|
||||
import {ClosureMap} from "./closure_map";
|
||||
|
||||
export class AST {
|
||||
eval(context) {
|
||||
|
@ -316,11 +315,9 @@ export class MethodCall extends AST {
|
|||
|
||||
export class FunctionCall extends AST {
|
||||
target:AST;
|
||||
closureMap:ClosureMap;
|
||||
args:List;
|
||||
constructor(target:AST, closureMap:ClosureMap, args:List) {
|
||||
constructor(target:AST, args:List) {
|
||||
this.target = target;
|
||||
this.closureMap = closureMap;
|
||||
this.args = args;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,22 +0,0 @@
|
|||
library change_detection.parser.closure_map;
|
||||
|
||||
import 'dart:mirrors';
|
||||
|
||||
typedef SetterFn(Object obj, value);
|
||||
|
||||
class ClosureMap {
|
||||
Function getter(String name) {
|
||||
var symbol = new Symbol(name);
|
||||
return (receiver) => reflect(receiver).getField(symbol).reflectee;
|
||||
}
|
||||
|
||||
Function setter(String name) {
|
||||
var symbol = new Symbol(name);
|
||||
return (receiver, value) => reflect(receiver).setField(symbol, value).reflectee;
|
||||
}
|
||||
|
||||
Function fn(String name) {
|
||||
var symbol = new Symbol(name);
|
||||
return (receiver, posArgs) => reflect(receiver).invoke(symbol, posArgs).reflectee;
|
||||
}
|
||||
}
|
|
@ -1,18 +0,0 @@
|
|||
export var SetterFn = Function;
|
||||
|
||||
export class ClosureMap {
|
||||
getter(name:string) {
|
||||
return new Function('o', 'return o.' + name + ';');
|
||||
}
|
||||
|
||||
setter(name:string) {
|
||||
return new Function('o', 'v', 'return o.' + name + ' = v;');
|
||||
}
|
||||
|
||||
fn(name:string) {
|
||||
var method = `o.${name}`;
|
||||
return new Function('o', 'args',
|
||||
`if (!${method}) throw new Error('"${name}" is undefined');` +
|
||||
`return ${method}.apply(o, args);`);
|
||||
}
|
||||
}
|
|
@ -1,8 +1,8 @@
|
|||
import {FIELD, int, isBlank, BaseException, StringWrapper} from 'facade/lang';
|
||||
import {FIELD, int, isBlank, isPresent, BaseException, StringWrapper} from 'facade/lang';
|
||||
import {ListWrapper, List} from 'facade/collection';
|
||||
import {Lexer, EOF, Token, $PERIOD, $COLON, $SEMICOLON, $LBRACKET, $RBRACKET,
|
||||
$COMMA, $LBRACE, $RBRACE, $LPAREN, $RPAREN} from './lexer';
|
||||
import {ClosureMap} from './closure_map';
|
||||
import {reflector, Reflector} from 'reflection/reflection';
|
||||
import {
|
||||
AST,
|
||||
ImplicitReceiver,
|
||||
|
@ -29,41 +29,41 @@ var _implicitReceiver = new ImplicitReceiver();
|
|||
|
||||
export class Parser {
|
||||
_lexer:Lexer;
|
||||
_closureMap:ClosureMap;
|
||||
constructor(lexer:Lexer, closureMap:ClosureMap){
|
||||
_reflector:Reflector;
|
||||
constructor(lexer:Lexer, providedReflector:Reflector = null){
|
||||
this._lexer = lexer;
|
||||
this._closureMap = closureMap;
|
||||
this._reflector = isPresent(providedReflector) ? providedReflector : reflector;
|
||||
}
|
||||
|
||||
parseAction(input:string):ASTWithSource {
|
||||
var tokens = this._lexer.tokenize(input);
|
||||
var ast = new _ParseAST(input, tokens, this._closureMap, true).parseChain();
|
||||
var ast = new _ParseAST(input, tokens, this._reflector, true).parseChain();
|
||||
return new ASTWithSource(ast, input);
|
||||
}
|
||||
|
||||
parseBinding(input:string):ASTWithSource {
|
||||
var tokens = this._lexer.tokenize(input);
|
||||
var ast = new _ParseAST(input, tokens, this._closureMap, false).parseChain();
|
||||
var ast = new _ParseAST(input, tokens, this._reflector, false).parseChain();
|
||||
return new ASTWithSource(ast, input);
|
||||
}
|
||||
|
||||
parseTemplateBindings(input:string):List<TemplateBinding> {
|
||||
var tokens = this._lexer.tokenize(input);
|
||||
return new _ParseAST(input, tokens, this._closureMap, false).parseTemplateBindings();
|
||||
return new _ParseAST(input, tokens, this._reflector, false).parseTemplateBindings();
|
||||
}
|
||||
}
|
||||
|
||||
class _ParseAST {
|
||||
input:string;
|
||||
tokens:List<Token>;
|
||||
closureMap:ClosureMap;
|
||||
reflector:Reflector;
|
||||
parseAction:boolean;
|
||||
index:int;
|
||||
constructor(input:string, tokens:List, closureMap:ClosureMap, parseAction:boolean) {
|
||||
constructor(input:string, tokens:List, reflector:Reflector, parseAction:boolean) {
|
||||
this.input = input;
|
||||
this.tokens = tokens;
|
||||
this.index = 0;
|
||||
this.closureMap = closureMap;
|
||||
this.reflector = reflector;
|
||||
this.parseAction = parseAction;
|
||||
}
|
||||
|
||||
|
@ -311,7 +311,7 @@ class _ParseAST {
|
|||
} else if (this.optionalCharacter($LPAREN)) {
|
||||
var args = this.parseCallArguments();
|
||||
this.expectCharacter($RPAREN);
|
||||
result = new FunctionCall(result, this.closureMap, args);
|
||||
result = new FunctionCall(result, args);
|
||||
|
||||
} else {
|
||||
return result;
|
||||
|
@ -398,12 +398,12 @@ class _ParseAST {
|
|||
if (this.optionalCharacter($LPAREN)) {
|
||||
var args = this.parseCallArguments();
|
||||
this.expectCharacter($RPAREN);
|
||||
var fn = this.closureMap.fn(id);
|
||||
var fn = this.reflector.method(id);
|
||||
return new MethodCall(receiver, fn, args);
|
||||
|
||||
} else {
|
||||
var getter = this.closureMap.getter(id);
|
||||
var setter = this.closureMap.setter(id);
|
||||
var getter = this.reflector.getter(id);
|
||||
var setter = this.reflector.setter(id);
|
||||
return new AccessMember(receiver, id, getter, setter);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
import {ProtoRecordRange, RecordRange} from './record_range';
|
||||
import {FIELD, isPresent, isBlank, int, StringWrapper, FunctionWrapper, BaseException} from 'facade/lang';
|
||||
import {List, Map, ListWrapper, MapWrapper} from 'facade/collection';
|
||||
import {ClosureMap} from 'change_detection/parser/closure_map';
|
||||
|
||||
var _fresh = new Object();
|
||||
|
||||
|
|
|
@ -4,7 +4,6 @@ import {isPresent} from 'facade/lang';
|
|||
import {List, ListWrapper, MapWrapper} from 'facade/collection';
|
||||
import {Parser} from 'change_detection/parser/parser';
|
||||
import {Lexer} from 'change_detection/parser/lexer';
|
||||
import {ClosureMap} from 'change_detection/parser/closure_map';
|
||||
|
||||
import {
|
||||
ChangeDetector,
|
||||
|
@ -18,7 +17,7 @@ import {Record} from 'change_detection/record';
|
|||
|
||||
export function main() {
|
||||
function ast(exp:string) {
|
||||
var parser = new Parser(new Lexer(), new ClosureMap());
|
||||
var parser = new Parser(new Lexer());
|
||||
return parser.parseBinding(exp).ast;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
import {ddescribe, describe, it, xit, iit, expect, beforeEach} from 'test_lib/test_lib';
|
||||
import {BaseException, isBlank, isPresent} from 'facade/lang';
|
||||
import {reflector} from 'reflection/reflection';
|
||||
import {MapWrapper, ListWrapper} from 'facade/collection';
|
||||
import {Parser} from 'change_detection/parser/parser';
|
||||
import {Lexer} from 'change_detection/parser/lexer';
|
||||
import {Formatter, LiteralPrimitive} from 'change_detection/parser/ast';
|
||||
import {ClosureMap} from 'change_detection/parser/closure_map';
|
||||
|
||||
class TestData {
|
||||
a;
|
||||
|
@ -31,7 +31,7 @@ export function main() {
|
|||
}
|
||||
|
||||
function createParser() {
|
||||
return new Parser(new Lexer(), new ClosureMap());
|
||||
return new Parser(new Lexer(), reflector);
|
||||
}
|
||||
|
||||
function parseAction(text) {
|
||||
|
|
|
@ -4,7 +4,6 @@ import {List, ListWrapper, MapWrapper} from 'facade/collection';
|
|||
import {isPresent} from 'facade/lang';
|
||||
import {Parser} from 'change_detection/parser/parser';
|
||||
import {Lexer} from 'change_detection/parser/lexer';
|
||||
import {ClosureMap} from 'change_detection/parser/closure_map';
|
||||
|
||||
import {
|
||||
ChangeDetector,
|
||||
|
|
|
@ -3,20 +3,23 @@ import {Type, FIELD, isBlank, isPresent, BaseException} from 'facade/lang';
|
|||
import {DOM, Element} from 'facade/dom';
|
||||
import {Compiler} from './compiler/compiler';
|
||||
import {ProtoView} from './compiler/view';
|
||||
import {ClosureMap} from 'change_detection/parser/closure_map';
|
||||
import {Reflector, reflector} from 'reflection/reflection';
|
||||
import {ReflectionCapabilities} from 'reflection/reflection_capabilities';
|
||||
import {Parser} from 'change_detection/parser/parser';
|
||||
import {Lexer} from 'change_detection/parser/lexer';
|
||||
import {ChangeDetector} from 'change_detection/change_detector';
|
||||
import {RecordRange} from 'change_detection/record_range';
|
||||
import {TemplateLoader} from './compiler/template_loader';
|
||||
import {Reflector} from './compiler/reflector';
|
||||
import {DirectiveMetadataReader} from './compiler/directive_metadata_reader';
|
||||
import {AnnotatedType} from './compiler/annotated_type';
|
||||
import {ListWrapper} from 'facade/collection';
|
||||
|
||||
var _rootInjector: Injector;
|
||||
|
||||
// Contains everything that is safe to share between applications.
|
||||
var _rootBindings = [Compiler, TemplateLoader, Reflector, Parser, Lexer, ClosureMap];
|
||||
var _rootBindings = [
|
||||
bind(Reflector).toValue(reflector), Compiler, TemplateLoader, DirectiveMetadataReader, Parser, Lexer
|
||||
];
|
||||
|
||||
export var appViewToken = new Object();
|
||||
export var appWatchGroupToken = new Object();
|
||||
|
@ -27,12 +30,12 @@ export var appDocumentToken = new Object();
|
|||
// Exported only for tests that need to overwrite default document binding.
|
||||
export function documentDependentBindings(appComponentType) {
|
||||
return [
|
||||
bind(appComponentAnnotatedTypeToken).toFactory((reflector) => {
|
||||
bind(appComponentAnnotatedTypeToken).toFactory((reader) => {
|
||||
// TODO(rado): inspect annotation here and warn if there are bindings,
|
||||
// lightDomServices, and other component annotations that are skipped
|
||||
// for bootstrapping components.
|
||||
return reflector.annotatedType(appComponentType);
|
||||
}, [Reflector]),
|
||||
return reader.annotatedType(appComponentType);
|
||||
}, [DirectiveMetadataReader]),
|
||||
|
||||
bind(appElementToken).toFactory((appComponentAnnotatedType, appDocument) => {
|
||||
var selector = appComponentAnnotatedType.annotation.selector;
|
||||
|
@ -71,6 +74,8 @@ function _injectorBindings(appComponentType) {
|
|||
// Multiple calls to this method are allowed. Each application would only share
|
||||
// _rootInjector, which is not user-configurable by design, thus safe to share.
|
||||
export function bootstrap(appComponentType: Type, bindings=null) {
|
||||
reflector.reflectionCapabilities = new ReflectionCapabilities();
|
||||
|
||||
// TODO(rado): prepopulate template cache, so applications with only
|
||||
// index.html and main.js are possible.
|
||||
if (isBlank(_rootInjector)) _rootInjector = new Injector(_rootBindings);
|
||||
|
|
|
@ -4,9 +4,8 @@ import {List, ListWrapper} from 'facade/collection';
|
|||
import {DOM, Element} from 'facade/dom';
|
||||
|
||||
import {Parser} from 'change_detection/parser/parser';
|
||||
import {ClosureMap} from 'change_detection/parser/closure_map';
|
||||
|
||||
import {Reflector} from './reflector';
|
||||
import {DirectiveMetadataReader} from './directive_metadata_reader';
|
||||
import {ProtoView} from './view';
|
||||
import {CompilePipeline} from './pipeline/compile_pipeline';
|
||||
import {CompileElement} from './pipeline/compile_element';
|
||||
|
@ -22,14 +21,12 @@ import {Component} from '../annotations/annotations';
|
|||
*/
|
||||
export class Compiler {
|
||||
_templateLoader:TemplateLoader;
|
||||
_reflector: Reflector;
|
||||
_reader: DirectiveMetadataReader;
|
||||
_parser:Parser;
|
||||
_closureMap:ClosureMap;
|
||||
constructor(templateLoader:TemplateLoader, reflector: Reflector, parser:Parser, closureMap:ClosureMap) {
|
||||
constructor(templateLoader:TemplateLoader, reader: DirectiveMetadataReader, parser:Parser) {
|
||||
this._templateLoader = templateLoader;
|
||||
this._reflector = reflector;
|
||||
this._reader = reader;
|
||||
this._parser = parser;
|
||||
this._closureMap = closureMap;
|
||||
}
|
||||
|
||||
createSteps(component:AnnotatedType):List<CompileStep> {
|
||||
|
@ -37,16 +34,16 @@ export class Compiler {
|
|||
var directives = annotation.template.directives;
|
||||
var annotatedDirectives = ListWrapper.create();
|
||||
for (var i=0; i<directives.length; i++) {
|
||||
ListWrapper.push(annotatedDirectives, this._reflector.annotatedType(directives[i]));
|
||||
ListWrapper.push(annotatedDirectives, this._reader.annotatedType(directives[i]));
|
||||
}
|
||||
return createDefaultSteps(this._parser, this._closureMap, annotatedDirectives);
|
||||
return createDefaultSteps(this._parser, annotatedDirectives);
|
||||
}
|
||||
|
||||
compile(component:Type, templateRoot:Element = null):Promise<ProtoView> {
|
||||
// TODO load all components transitively from the cache first
|
||||
var cache = null;
|
||||
return PromiseWrapper.resolve(this.compileWithCache(
|
||||
cache, this._reflector.annotatedType(component), templateRoot)
|
||||
cache, this._reader.annotatedType(component), templateRoot)
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import {Type, isPresent, BaseException} from 'facade/lang';
|
||||
import {Type, isPresent, BaseException, stringify} from 'facade/lang';
|
||||
import {Directive} from '../annotations/annotations';
|
||||
import {AnnotatedType} from './annotated_type';
|
||||
import {reflector} from 'reflection/reflection';
|
||||
|
||||
/**
|
||||
* Interface representing a way of extracting [Directive] annotations from
|
||||
|
@ -10,10 +11,10 @@ import {AnnotatedType} from './annotated_type';
|
|||
* 2) Dart reflective implementation
|
||||
* 3) Dart transformer generated implementation
|
||||
*/
|
||||
export class Reflector {
|
||||
export class DirectiveMetadataReader {
|
||||
annotatedType(type:Type):AnnotatedType {
|
||||
var annotations = type.annotations;
|
||||
if (annotations) {
|
||||
var annotations = reflector.annotations(type);
|
||||
if (isPresent(annotations)) {
|
||||
for (var i=0; i<annotations.length; i++) {
|
||||
var annotation = annotations[i];
|
||||
if (annotation instanceof Directive) {
|
||||
|
@ -21,6 +22,6 @@ export class Reflector {
|
|||
}
|
||||
}
|
||||
}
|
||||
throw new BaseException('No Directive annotation found on ' + type.name);
|
||||
throw new BaseException(`No Directive annotation found on ${stringify(type)}`);
|
||||
}
|
||||
}
|
|
@ -1,5 +1,4 @@
|
|||
import {Parser} from 'change_detection/parser/parser';
|
||||
import {ClosureMap} from 'change_detection/parser/closure_map';
|
||||
import {List} from 'facade/collection';
|
||||
|
||||
import {PropertyBindingParser} from './property_binding_parser';
|
||||
|
@ -16,10 +15,7 @@ import {ElementBinderBuilder} from './element_binder_builder';
|
|||
* Takes in an HTMLElement and produces the ProtoViews,
|
||||
* ProtoElementInjectors and ElementBinders in the end.
|
||||
*/
|
||||
export function createDefaultSteps(
|
||||
parser:Parser, closureMap:ClosureMap,
|
||||
directives: List<AnnotatedType>
|
||||
) {
|
||||
export function createDefaultSteps(parser:Parser, directives: List<AnnotatedType>) {
|
||||
return [
|
||||
new ViewSplitter(parser),
|
||||
new TextInterpolationParser(parser),
|
||||
|
@ -28,6 +24,6 @@ export function createDefaultSteps(
|
|||
new ElementBindingMarker(),
|
||||
new ProtoViewBuilder(),
|
||||
new ProtoElementInjectorBuilder(),
|
||||
new ElementBinderBuilder(closureMap)
|
||||
new ElementBinderBuilder()
|
||||
];
|
||||
}
|
|
@ -10,7 +10,6 @@ import {Component} from '../../annotations/annotations';
|
|||
import {CompileStep} from './compile_step';
|
||||
import {CompileElement} from './compile_element';
|
||||
import {CompileControl} from './compile_control';
|
||||
import {Reflector} from '../reflector';
|
||||
|
||||
/**
|
||||
* Parses the directives on a single element. Assumes ViewSplitter has already created
|
||||
|
|
|
@ -2,8 +2,9 @@ import {int, isPresent, isBlank, Type, BaseException, stringify} from 'facade/la
|
|||
import {Element} from 'facade/dom';
|
||||
import {ListWrapper, List, MapWrapper, StringMapWrapper} from 'facade/collection';
|
||||
|
||||
import {reflector} from 'reflection/reflection';
|
||||
|
||||
import {Parser} from 'change_detection/parser/parser';
|
||||
import {ClosureMap} from 'change_detection/parser/closure_map';
|
||||
import {ProtoRecordRange} from 'change_detection/record_range';
|
||||
|
||||
import {Component, Directive} from '../../annotations/annotations';
|
||||
|
@ -11,7 +12,6 @@ import {AnnotatedType} from '../annotated_type';
|
|||
import {ProtoView, ElementPropertyMemento, DirectivePropertyMemento} from '../view';
|
||||
import {ProtoElementInjector} from '../element_injector';
|
||||
import {ElementBinder} from '../element_binder';
|
||||
import {Reflector} from '../reflector';
|
||||
|
||||
import {CompileStep} from './compile_step';
|
||||
import {CompileElement} from './compile_element';
|
||||
|
@ -43,11 +43,6 @@ import {CompileControl} from './compile_control';
|
|||
* with the flag `isViewRoot`.
|
||||
*/
|
||||
export class ElementBinderBuilder extends CompileStep {
|
||||
_closureMap:ClosureMap;
|
||||
constructor(closureMap:ClosureMap) {
|
||||
this._closureMap = closureMap;
|
||||
}
|
||||
|
||||
process(parent:CompileElement, current:CompileElement, control:CompileControl) {
|
||||
var elementBinder = null;
|
||||
if (current.hasBindings) {
|
||||
|
@ -125,7 +120,7 @@ export class ElementBinderBuilder extends CompileStep {
|
|||
directiveIndex++,
|
||||
expression.ast,
|
||||
dirProp,
|
||||
this._closureMap.setter(dirProp)
|
||||
reflector.setter(dirProp)
|
||||
);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,27 +0,0 @@
|
|||
library facade.compiler.reflector;
|
||||
|
||||
import 'dart:mirrors';
|
||||
import '../annotations/annotations.dart';
|
||||
import './annotated_type.dart';
|
||||
import 'package:facade/lang.dart';
|
||||
|
||||
/**
|
||||
* Interface representing a way of extracting [Directive] annotations from
|
||||
* [Type]. This interface has three native implementations:
|
||||
*
|
||||
* 1) JavaScript native implementation
|
||||
* 2) Dart reflective implementation
|
||||
* 3) Dart transformer generated implementation
|
||||
*/
|
||||
class Reflector {
|
||||
AnnotatedType annotatedType(Type type) {
|
||||
var directiveAnnotations = reflectType(type).metadata
|
||||
.map( (im) => im.reflectee)
|
||||
.where( (annotation) => annotation is Directive);
|
||||
if (directiveAnnotations.isEmpty) {
|
||||
throw new BaseException('No Directive annotation found on '+stringify(type));
|
||||
}
|
||||
return new AnnotatedType(type, directiveAnnotations.first);
|
||||
}
|
||||
|
||||
}
|
|
@ -7,7 +7,7 @@ import {AST} from 'change_detection/parser/ast';
|
|||
import {ProtoElementInjector, ElementInjector, PreBuiltObjects} from './element_injector';
|
||||
import {ElementBinder} from './element_binder';
|
||||
import {AnnotatedType} from './annotated_type';
|
||||
import {SetterFn} from 'change_detection/parser/closure_map';
|
||||
import {SetterFn} from 'reflection/types';
|
||||
import {FIELD, IMPLEMENTS, int, isPresent, isBlank} from 'facade/lang';
|
||||
import {Injector} from 'di/di';
|
||||
import {NgElement} from 'core/dom/element';
|
||||
|
|
|
@ -4,7 +4,7 @@ import {List} from 'facade/collection';
|
|||
|
||||
import {Compiler} from 'core/compiler/compiler';
|
||||
import {ProtoView} from 'core/compiler/view';
|
||||
import {Reflector} from 'core/compiler/reflector';
|
||||
import {DirectiveMetadataReader} from 'core/compiler/directive_metadata_reader';
|
||||
import {TemplateLoader} from 'core/compiler/template_loader';
|
||||
import {Component} from 'core/annotations/annotations';
|
||||
import {TemplateConfig} from 'core/annotations/template_config';
|
||||
|
@ -14,20 +14,18 @@ import {CompileControl} from 'core/compiler/pipeline/compile_control';
|
|||
|
||||
import {Parser} from 'change_detection/parser/parser';
|
||||
import {Lexer} from 'change_detection/parser/lexer';
|
||||
import {ClosureMap} from 'change_detection/parser/closure_map';
|
||||
|
||||
export function main() {
|
||||
describe('compiler', function() {
|
||||
var compiler, reflector;
|
||||
var compiler, reader;
|
||||
|
||||
beforeEach( () => {
|
||||
reflector = new Reflector();
|
||||
reader = new DirectiveMetadataReader();
|
||||
});
|
||||
|
||||
function createCompiler(processClosure) {
|
||||
var closureMap = new ClosureMap();
|
||||
var steps = [new MockStep(processClosure)];
|
||||
return new TestableCompiler(null, reflector, new Parser(new Lexer(), closureMap), closureMap, steps);
|
||||
return new TestableCompiler(null, reader, new Parser(new Lexer()), steps);
|
||||
}
|
||||
|
||||
it('should run the steps and return the ProtoView of the root element', (done) => {
|
||||
|
@ -68,7 +66,7 @@ export function main() {
|
|||
current.inheritedProtoView = new ProtoView(current.element, null);
|
||||
current.inheritedElementBinder = current.inheritedProtoView.bindElement(null);
|
||||
if (current.element === mainEl) {
|
||||
current.componentDirective = reflector.annotatedType(NestedComponent);
|
||||
current.componentDirective = reader.annotatedType(NestedComponent);
|
||||
}
|
||||
});
|
||||
compiler.compile(MainComponent, mainEl).then( (protoView) => {
|
||||
|
@ -99,8 +97,8 @@ class NestedComponent {}
|
|||
|
||||
class TestableCompiler extends Compiler {
|
||||
steps:List;
|
||||
constructor(templateLoader:TemplateLoader, reflector:Reflector, parser, closureMap, steps:List<CompileStep>) {
|
||||
super(templateLoader, reflector, parser, closureMap);
|
||||
constructor(templateLoader:TemplateLoader, reader:DirectiveMetadataReader, parser, steps:List<CompileStep>) {
|
||||
super(templateLoader, reader, parser);
|
||||
this.steps = steps;
|
||||
}
|
||||
createSteps(component):List<CompileStep> {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import {ddescribe, describe, it, iit, expect, beforeEach} from 'test_lib/test_lib';
|
||||
import {Reflector} from 'core/compiler/reflector';
|
||||
import {DirectiveMetadataReader} from 'core/compiler/directive_metadata_reader';
|
||||
import {Decorator} from 'core/annotations/annotations';
|
||||
import {AnnotatedType} from 'core/compiler/annotated_type';
|
||||
|
||||
|
@ -13,22 +13,22 @@ class SomeDirectiveWithoutAnnotation {
|
|||
}
|
||||
|
||||
export function main() {
|
||||
describe("reflector", () => {
|
||||
var reflector;
|
||||
describe("DirectiveMetadataReader", () => {
|
||||
var rader;
|
||||
|
||||
beforeEach( () => {
|
||||
reflector = new Reflector();
|
||||
rader = new DirectiveMetadataReader();
|
||||
});
|
||||
|
||||
it('should read out the annotation', () => {
|
||||
var annoatedDirective = reflector.annotatedType(SomeDirective);
|
||||
var annoatedDirective = rader.annotatedType(SomeDirective);
|
||||
expect(annoatedDirective).toEqual(
|
||||
new AnnotatedType(SomeDirective, new Decorator({selector: 'someSelector'})));
|
||||
});
|
||||
|
||||
it('should throw if not matching annotation is found', () => {
|
||||
expect(() => {
|
||||
reflector.annotatedType(SomeDirectiveWithoutAnnotation);
|
||||
rader.annotatedType(SomeDirectiveWithoutAnnotation);
|
||||
}).toThrowError('No Directive annotation found on SomeDirectiveWithoutAnnotation');
|
||||
});
|
||||
|
|
@ -5,11 +5,10 @@ import {DOM} from 'facade/dom';
|
|||
import {Injector} from 'di/di';
|
||||
import {ChangeDetector} from 'change_detection/change_detector';
|
||||
import {Parser} from 'change_detection/parser/parser';
|
||||
import {ClosureMap} from 'change_detection/parser/closure_map';
|
||||
import {Lexer} from 'change_detection/parser/lexer';
|
||||
|
||||
import {Compiler} from 'core/compiler/compiler';
|
||||
import {Reflector} from 'core/compiler/reflector';
|
||||
import {DirectiveMetadataReader} from 'core/compiler/directive_metadata_reader';
|
||||
|
||||
import {Component} from 'core/annotations/annotations';
|
||||
import {Decorator} from 'core/annotations/annotations';
|
||||
|
@ -20,8 +19,7 @@ export function main() {
|
|||
var compiler;
|
||||
|
||||
beforeEach( () => {
|
||||
var closureMap = new ClosureMap();
|
||||
compiler = new Compiler(null, new Reflector(), new Parser(new Lexer(), closureMap), closureMap);
|
||||
compiler = new Compiler(null, new DirectiveMetadataReader(), new Parser(new Lexer()));
|
||||
});
|
||||
|
||||
describe('react to record changes', function() {
|
||||
|
|
|
@ -11,26 +11,24 @@ import {Component} from 'core/annotations/annotations';
|
|||
import {Decorator} from 'core/annotations/annotations';
|
||||
import {Template} from 'core/annotations/annotations';
|
||||
import {TemplateConfig} from 'core/annotations/template_config';
|
||||
import {Reflector} from 'core/compiler/reflector';
|
||||
import {DirectiveMetadataReader} from 'core/compiler/directive_metadata_reader';
|
||||
import {Parser} from 'change_detection/parser/parser';
|
||||
import {Lexer} from 'change_detection/parser/lexer';
|
||||
import {ClosureMap} from 'change_detection/parser/closure_map';
|
||||
|
||||
export function main() {
|
||||
describe('DirectiveParser', () => {
|
||||
var reflector, directives;
|
||||
var reader, directives;
|
||||
|
||||
beforeEach( () => {
|
||||
reflector = new Reflector();
|
||||
reader = new DirectiveMetadataReader();
|
||||
directives = [SomeDecorator, SomeTemplate, SomeTemplate2, SomeComponent, SomeComponent2];
|
||||
});
|
||||
|
||||
function createPipeline({propertyBindings, variableBindings}={}) {
|
||||
var closureMap = new ClosureMap();
|
||||
var parser = new Parser(new Lexer(), closureMap);
|
||||
var parser = new Parser(new Lexer());
|
||||
var annotatedDirectives = ListWrapper.create();
|
||||
for (var i=0; i<directives.length; i++) {
|
||||
ListWrapper.push(annotatedDirectives, reflector.annotatedType(directives[i]));
|
||||
ListWrapper.push(annotatedDirectives, reader.annotatedType(directives[i]));
|
||||
}
|
||||
|
||||
return new CompilePipeline([new MockStep((parent, current, control) => {
|
||||
|
@ -55,7 +53,7 @@ export function main() {
|
|||
describe('component directives', () => {
|
||||
it('should detect them in attributes', () => {
|
||||
var results = createPipeline().process(createElement('<div some-comp></div>'));
|
||||
expect(results[0].componentDirective).toEqual(reflector.annotatedType(SomeComponent));
|
||||
expect(results[0].componentDirective).toEqual(reader.annotatedType(SomeComponent));
|
||||
});
|
||||
|
||||
it('should detect them in property bindings', () => {
|
||||
|
@ -63,7 +61,7 @@ export function main() {
|
|||
'some-comp': 'someExpr'
|
||||
}});
|
||||
var results = pipeline.process(createElement('<div></div>'));
|
||||
expect(results[0].componentDirective).toEqual(reflector.annotatedType(SomeComponent));
|
||||
expect(results[0].componentDirective).toEqual(reader.annotatedType(SomeComponent));
|
||||
});
|
||||
|
||||
it('should detect them in variable bindings', () => {
|
||||
|
@ -71,7 +69,7 @@ export function main() {
|
|||
'some-comp': 'someExpr'
|
||||
}});
|
||||
var results = pipeline.process(createElement('<div></div>'));
|
||||
expect(results[0].componentDirective).toEqual(reflector.annotatedType(SomeComponent));
|
||||
expect(results[0].componentDirective).toEqual(reader.annotatedType(SomeComponent));
|
||||
});
|
||||
|
||||
it('should not allow multiple component directives on the same element', () => {
|
||||
|
@ -94,7 +92,7 @@ export function main() {
|
|||
describe('template directives', () => {
|
||||
it('should detect them in attributes', () => {
|
||||
var results = createPipeline().process(createElement('<template some-templ></template>'));
|
||||
expect(results[0].templateDirective).toEqual(reflector.annotatedType(SomeTemplate));
|
||||
expect(results[0].templateDirective).toEqual(reader.annotatedType(SomeTemplate));
|
||||
});
|
||||
|
||||
it('should detect them in property bindings', () => {
|
||||
|
@ -102,7 +100,7 @@ export function main() {
|
|||
'some-templ': 'someExpr'
|
||||
}});
|
||||
var results = pipeline.process(createElement('<template></template>'));
|
||||
expect(results[0].templateDirective).toEqual(reflector.annotatedType(SomeTemplate));
|
||||
expect(results[0].templateDirective).toEqual(reader.annotatedType(SomeTemplate));
|
||||
});
|
||||
|
||||
it('should detect them in variable bindings', () => {
|
||||
|
@ -110,7 +108,7 @@ export function main() {
|
|||
'some-templ': 'someExpr'
|
||||
}});
|
||||
var results = pipeline.process(createElement('<template></template>'));
|
||||
expect(results[0].templateDirective).toEqual(reflector.annotatedType(SomeTemplate));
|
||||
expect(results[0].templateDirective).toEqual(reader.annotatedType(SomeTemplate));
|
||||
});
|
||||
|
||||
it('should not allow multiple template directives on the same element', () => {
|
||||
|
@ -133,7 +131,7 @@ export function main() {
|
|||
describe('decorator directives', () => {
|
||||
it('should detect them in attributes', () => {
|
||||
var results = createPipeline().process(createElement('<div some-decor></div>'));
|
||||
expect(results[0].decoratorDirectives).toEqual([reflector.annotatedType(SomeDecorator)]);
|
||||
expect(results[0].decoratorDirectives).toEqual([reader.annotatedType(SomeDecorator)]);
|
||||
});
|
||||
|
||||
it('should detect them in property bindings', () => {
|
||||
|
@ -141,7 +139,7 @@ export function main() {
|
|||
'some-decor': 'someExpr'
|
||||
}});
|
||||
var results = pipeline.process(createElement('<div></div>'));
|
||||
expect(results[0].decoratorDirectives).toEqual([reflector.annotatedType(SomeDecorator)]);
|
||||
expect(results[0].decoratorDirectives).toEqual([reader.annotatedType(SomeDecorator)]);
|
||||
});
|
||||
|
||||
it('should detect them in variable bindings', () => {
|
||||
|
@ -149,7 +147,7 @@ export function main() {
|
|||
'some-decor': 'someExpr'
|
||||
}});
|
||||
var results = pipeline.process(createElement('<div></div>'));
|
||||
expect(results[0].decoratorDirectives).toEqual([reflector.annotatedType(SomeDecorator)]);
|
||||
expect(results[0].decoratorDirectives).toEqual([reader.annotatedType(SomeDecorator)]);
|
||||
});
|
||||
|
||||
it('should not allow decorator directives on <template> elements', () => {
|
||||
|
|
|
@ -14,12 +14,11 @@ import {Template} from 'core/annotations/annotations';
|
|||
import {Component} from 'core/annotations/annotations';
|
||||
import {ProtoView, ElementPropertyMemento, DirectivePropertyMemento} from 'core/compiler/view';
|
||||
import {ProtoElementInjector} from 'core/compiler/element_injector';
|
||||
import {Reflector} from 'core/compiler/reflector';
|
||||
import {DirectiveMetadataReader} from 'core/compiler/directive_metadata_reader';
|
||||
|
||||
import {ProtoRecordRange} from 'change_detection/record_range';
|
||||
import {Parser} from 'change_detection/parser/parser';
|
||||
import {Lexer} from 'change_detection/parser/lexer';
|
||||
import {ClosureMap} from 'change_detection/parser/closure_map';
|
||||
import {ChangeDetector} from 'change_detection/change_detector';
|
||||
import {Injector} from 'di/di';
|
||||
|
||||
|
@ -29,9 +28,8 @@ export function main() {
|
|||
|
||||
function createPipeline({textNodeBindings, propertyBindings, eventBindings, directives, protoElementInjector
|
||||
}={}) {
|
||||
var reflector = new Reflector();
|
||||
var closureMap = new ClosureMap();
|
||||
var parser = new Parser(new Lexer(), closureMap);
|
||||
var reflector = new DirectiveMetadataReader();
|
||||
var parser = new Parser(new Lexer());
|
||||
return new CompilePipeline([
|
||||
new MockStep((parent, current, control) => {
|
||||
if (isPresent(current.element.getAttribute('viewroot'))) {
|
||||
|
@ -74,7 +72,7 @@ export function main() {
|
|||
current.hasBindings = true;
|
||||
DOM.addClass(current.element, 'ng-binding');
|
||||
}
|
||||
}), new ElementBinderBuilder(closureMap)
|
||||
}), new ElementBinderBuilder()
|
||||
]);
|
||||
}
|
||||
|
||||
|
|
|
@ -8,16 +8,14 @@ import {CompilePipeline} from 'core/compiler/pipeline/compile_pipeline';
|
|||
import {CompileElement} from 'core/compiler/pipeline/compile_element';
|
||||
import {CompileStep} from 'core/compiler/pipeline/compile_step'
|
||||
import {CompileControl} from 'core/compiler/pipeline/compile_control';
|
||||
import {Reflector} from 'core/compiler/reflector';
|
||||
import {Template} from 'core/annotations/annotations';
|
||||
import {Decorator} from 'core/annotations/annotations';
|
||||
import {Component} from 'core/annotations/annotations';
|
||||
import {DirectiveMetadataReader} from 'core/compiler/directive_metadata_reader';
|
||||
import {Template, Decorator, Component} from 'core/annotations/annotations';
|
||||
|
||||
export function main() {
|
||||
describe('ElementBindingMarker', () => {
|
||||
|
||||
function createPipeline({textNodeBindings, propertyBindings, variableBindings, eventBindings, directives}={}) {
|
||||
var reflector = new Reflector();
|
||||
var reader = new DirectiveMetadataReader();
|
||||
return new CompilePipeline([
|
||||
new MockStep((parent, current, control) => {
|
||||
if (isPresent(textNodeBindings)) {
|
||||
|
@ -34,7 +32,7 @@ export function main() {
|
|||
}
|
||||
if (isPresent(directives)) {
|
||||
for (var i=0; i<directives.length; i++) {
|
||||
current.addDirective(reflector.annotatedType(directives[i]));
|
||||
current.addDirective(reader.annotatedType(directives[i]));
|
||||
}
|
||||
}
|
||||
}), new ElementBindingMarker()
|
||||
|
|
|
@ -5,13 +5,12 @@ import {DOM} from 'facade/dom';
|
|||
import {MapWrapper} from 'facade/collection';
|
||||
|
||||
import {Parser} from 'change_detection/parser/parser';
|
||||
import {ClosureMap} from 'change_detection/parser/closure_map';
|
||||
import {Lexer} from 'change_detection/parser/lexer';
|
||||
|
||||
export function main() {
|
||||
describe('PropertyBindingParser', () => {
|
||||
function createPipeline() {
|
||||
return new CompilePipeline([new PropertyBindingParser(new Parser(new Lexer(), new ClosureMap()))]);
|
||||
return new CompilePipeline([new PropertyBindingParser(new Parser(new Lexer()))]);
|
||||
}
|
||||
|
||||
it('should detect [] syntax', () => {
|
||||
|
|
|
@ -9,10 +9,8 @@ import {CompileElement} from 'core/compiler/pipeline/compile_element';
|
|||
import {CompileStep} from 'core/compiler/pipeline/compile_step'
|
||||
import {CompileControl} from 'core/compiler/pipeline/compile_control';
|
||||
import {ProtoView} from 'core/compiler/view';
|
||||
import {Reflector} from 'core/compiler/reflector';
|
||||
import {Template} from 'core/annotations/annotations';
|
||||
import {Decorator} from 'core/annotations/annotations';
|
||||
import {Component} from 'core/annotations/annotations';
|
||||
import {DirectiveMetadataReader} from 'core/compiler/directive_metadata_reader';
|
||||
import {Template, Decorator, Component} from 'core/annotations/annotations';
|
||||
import {ProtoElementInjector} from 'core/compiler/element_injector';
|
||||
|
||||
export function main() {
|
||||
|
@ -27,14 +25,14 @@ export function main() {
|
|||
if (isBlank(directives)) {
|
||||
directives = [];
|
||||
}
|
||||
var reflector = new Reflector();
|
||||
var reader = new DirectiveMetadataReader();
|
||||
return new CompilePipeline([new MockStep((parent, current, control) => {
|
||||
if (isPresent(current.element.getAttribute('viewroot'))) {
|
||||
current.isViewRoot = true;
|
||||
}
|
||||
if (isPresent(current.element.getAttribute('directives'))) {
|
||||
for (var i=0; i<directives.length; i++) {
|
||||
current.addDirective(reflector.annotatedType(directives[i]));
|
||||
current.addDirective(reader.annotatedType(directives[i]));
|
||||
}
|
||||
}
|
||||
current.inheritedProtoView = protoView;
|
||||
|
|
|
@ -5,13 +5,12 @@ import {DOM} from 'facade/dom';
|
|||
import {MapWrapper} from 'facade/collection';
|
||||
|
||||
import {Parser} from 'change_detection/parser/parser';
|
||||
import {ClosureMap} from 'change_detection/parser/closure_map';
|
||||
import {Lexer} from 'change_detection/parser/lexer';
|
||||
|
||||
export function main() {
|
||||
describe('TextInterpolationParser', () => {
|
||||
function createPipeline() {
|
||||
return new CompilePipeline([new TextInterpolationParser(new Parser(new Lexer(), new ClosureMap()))]);
|
||||
return new CompilePipeline([new TextInterpolationParser(new Parser(new Lexer()))]);
|
||||
}
|
||||
|
||||
it('should find text interpolation in normal elements', () => {
|
||||
|
|
|
@ -7,14 +7,13 @@ import {CompilePipeline} from 'core/compiler/pipeline/compile_pipeline';
|
|||
import {DOM, TemplateElement} from 'facade/dom';
|
||||
|
||||
import {Parser} from 'change_detection/parser/parser';
|
||||
import {ClosureMap} from 'change_detection/parser/closure_map';
|
||||
import {Lexer} from 'change_detection/parser/lexer';
|
||||
|
||||
export function main() {
|
||||
describe('ViewSplitter', () => {
|
||||
|
||||
function createPipeline() {
|
||||
return new CompilePipeline([new ViewSplitter(new Parser(new Lexer(), new ClosureMap()))]);
|
||||
return new CompilePipeline([new ViewSplitter(new Parser(new Lexer()))]);
|
||||
}
|
||||
|
||||
it('should mark root elements as viewRoot', () => {
|
||||
|
|
|
@ -1,28 +1,26 @@
|
|||
import {describe, xit, it, expect, beforeEach, ddescribe, iit} from 'test_lib/test_lib';
|
||||
import {ProtoView, ElementPropertyMemento, DirectivePropertyMemento} from 'core/compiler/view';
|
||||
import {ProtoElementInjector, ElementInjector} from 'core/compiler/element_injector';
|
||||
import {Reflector} from 'core/compiler/reflector';
|
||||
import {Component} from 'core/annotations/annotations';
|
||||
import {Decorator} from 'core/annotations/annotations';
|
||||
import {DirectiveMetadataReader} from 'core/compiler/directive_metadata_reader';
|
||||
import {Component, Decorator} from 'core/annotations/annotations';
|
||||
import {ProtoRecordRange} from 'change_detection/record_range';
|
||||
import {ChangeDetector} from 'change_detection/change_detector';
|
||||
import {TemplateConfig} from 'core/annotations/template_config';
|
||||
import {Parser} from 'change_detection/parser/parser';
|
||||
import {ClosureMap} from 'change_detection/parser/closure_map';
|
||||
import {Lexer} from 'change_detection/parser/lexer';
|
||||
import {DOM, Element} from 'facade/dom';
|
||||
import {FIELD} from 'facade/lang';
|
||||
import {Injector} from 'di/di';
|
||||
import {View} from 'core/compiler/view';
|
||||
import {reflector} from 'reflection/reflection';
|
||||
|
||||
export function main() {
|
||||
describe('view', function() {
|
||||
var parser, closureMap, someComponentDirective;
|
||||
var parser, someComponentDirective;
|
||||
|
||||
beforeEach(() => {
|
||||
closureMap = new ClosureMap();
|
||||
parser = new Parser(new Lexer(), closureMap);
|
||||
someComponentDirective = new Reflector().annotatedType(SomeComponent);
|
||||
parser = new Parser(new Lexer());
|
||||
someComponentDirective = new DirectiveMetadataReader().annotatedType(SomeComponent);
|
||||
});
|
||||
|
||||
|
||||
|
@ -263,7 +261,7 @@ export function main() {
|
|||
var pv = new ProtoView(createElement('<div class="ng-binding"></div>'),
|
||||
new ProtoRecordRange());
|
||||
pv.bindElement(new ProtoElementInjector(null, 0, [SomeDirective]));
|
||||
pv.bindDirectiveProperty( 0, parser.parseBinding('foo').ast, 'prop', closureMap.setter('prop'));
|
||||
pv.bindDirectiveProperty( 0, parser.parseBinding('foo').ast, 'prop', reflector.setter('prop'));
|
||||
createView(pv);
|
||||
|
||||
ctx.foo = 'buz';
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
import {FIELD, Type, isBlank} from 'facade/lang';
|
||||
import {FIELD, Type, isBlank, isPresent} from 'facade/lang';
|
||||
import {List, MapWrapper, ListWrapper} from 'facade/collection';
|
||||
import {reflector} from './reflector';
|
||||
import {reflector} from 'reflection/reflection';
|
||||
import {Key} from './key';
|
||||
import {Inject, InjectLazy, InjectPromise, DependencyAnnotation} from './annotations';
|
||||
import {NoAnnotationError} from './exceptions';
|
||||
|
||||
export class Dependency {
|
||||
key:Key;
|
||||
|
@ -43,8 +45,8 @@ export class BindingBuilder {
|
|||
toClass(type:Type):Binding {
|
||||
return new Binding(
|
||||
Key.get(this.token),
|
||||
reflector.factoryFor(type),
|
||||
reflector.dependencies(type),
|
||||
reflector.factory(type),
|
||||
_dependenciesFor(type),
|
||||
false
|
||||
);
|
||||
}
|
||||
|
@ -78,7 +80,49 @@ export class BindingBuilder {
|
|||
|
||||
_constructDependencies(factoryFunction:Function, dependencies:List) {
|
||||
return isBlank(dependencies) ?
|
||||
reflector.dependencies(factoryFunction) :
|
||||
_dependenciesFor(factoryFunction) :
|
||||
ListWrapper.map(dependencies, (t) => new Dependency(Key.get(t), false, false, []));
|
||||
}
|
||||
}
|
||||
|
||||
function _dependenciesFor(typeOrFunc):List {
|
||||
var params = reflector.parameters(typeOrFunc);
|
||||
if (isBlank(params)) return [];
|
||||
if (ListWrapper.any(params, (p) => isBlank(p))) throw new NoAnnotationError(typeOrFunc);
|
||||
return ListWrapper.map(params, (p) => _extractToken(typeOrFunc, p));
|
||||
}
|
||||
|
||||
function _extractToken(typeOrFunc, annotations) {
|
||||
var type;
|
||||
var depProps = [];
|
||||
|
||||
for (var i = 0; i < annotations.length; ++i) {
|
||||
var paramAnnotation = annotations[i];
|
||||
|
||||
if (paramAnnotation instanceof Type) {
|
||||
type = paramAnnotation;
|
||||
|
||||
} else if (paramAnnotation instanceof Inject) {
|
||||
return _createDependency(paramAnnotation.token, false, false, []);
|
||||
|
||||
} else if (paramAnnotation instanceof InjectPromise) {
|
||||
return _createDependency(paramAnnotation.token, true, false, []);
|
||||
|
||||
} else if (paramAnnotation instanceof InjectLazy) {
|
||||
return _createDependency(paramAnnotation.token, false, true, []);
|
||||
|
||||
} else if (paramAnnotation instanceof DependencyAnnotation) {
|
||||
ListWrapper.push(depProps, paramAnnotation);
|
||||
}
|
||||
}
|
||||
|
||||
if (isPresent(type)) {
|
||||
return _createDependency(type, false, false, depProps);
|
||||
} else {
|
||||
throw new NoAnnotationError(typeOrFunc);
|
||||
}
|
||||
}
|
||||
|
||||
function _createDependency(token, asPromise, lazy, depProps):Dependency {
|
||||
return new Dependency(Key.get(token), asPromise, lazy, depProps);
|
||||
}
|
||||
|
|
|
@ -2,10 +2,9 @@ import {Map, List, MapWrapper, ListWrapper} from 'facade/collection';
|
|||
import {Binding, BindingBuilder, bind} from './binding';
|
||||
import {ProviderError, NoProviderError, InvalidBindingError,
|
||||
AsyncBindingError, CyclicDependencyError, InstantiationError} from './exceptions';
|
||||
import {Type, isPresent, isBlank} from 'facade/lang';
|
||||
import {FunctionWrapper, Type, isPresent, isBlank} from 'facade/lang';
|
||||
import {Promise, PromiseWrapper} from 'facade/async';
|
||||
import {Key} from './key';
|
||||
import {reflector} from './reflector';
|
||||
|
||||
var _constructing = new Object();
|
||||
|
||||
|
@ -159,7 +158,7 @@ class _SyncInjectorStrategy {
|
|||
|
||||
_createInstance(key:Key, binding:Binding, deps:List) {
|
||||
try {
|
||||
var instance = reflector.invoke(binding.factory, deps);
|
||||
var instance = FunctionWrapper.apply(binding.factory, deps);
|
||||
this.injector._setInstance(key, instance);
|
||||
return instance;
|
||||
} catch (e) {
|
||||
|
@ -221,7 +220,7 @@ class _AsyncInjectorStrategy {
|
|||
try {
|
||||
var instance = this.injector._getInstance(key);
|
||||
if (!_isWaiting(instance)) return instance;
|
||||
return reflector.invoke(binding.factory, deps);
|
||||
return FunctionWrapper.apply(binding.factory, deps);
|
||||
} catch (e) {
|
||||
this.injector._clear(key);
|
||||
throw new InstantiationError(e, key);
|
||||
|
|
|
@ -1,94 +0,0 @@
|
|||
library facade.di.reflector;
|
||||
|
||||
import 'dart:mirrors';
|
||||
import 'annotations.dart' show Inject, InjectPromise, InjectLazy, DependencyAnnotation;
|
||||
import 'key.dart' show Key;
|
||||
import 'binding.dart' show Dependency;
|
||||
import 'exceptions.dart' show NoAnnotationError;
|
||||
|
||||
class Reflector {
|
||||
Function factoryFor(Type type) {
|
||||
ClassMirror classMirror = reflectType(type);
|
||||
MethodMirror ctor = classMirror.declarations[classMirror.simpleName];
|
||||
Function create = classMirror.newInstance;
|
||||
Symbol name = ctor.constructorName;
|
||||
int length = ctor.parameters.length;
|
||||
|
||||
switch (length) {
|
||||
case 0: return () =>
|
||||
create(name, []).reflectee;
|
||||
case 1: return (a1) =>
|
||||
create(name, [a1]).reflectee;
|
||||
case 2: return (a1, a2) =>
|
||||
create(name, [a1, a2]).reflectee;
|
||||
case 3: return (a1, a2, a3) =>
|
||||
create(name, [a1, a2, a3]).reflectee;
|
||||
case 4: return (a1, a2, a3, a4) =>
|
||||
create(name, [a1, a2, a3, a4]).reflectee;
|
||||
case 5: return (a1, a2, a3, a4, a5) =>
|
||||
create(name, [a1, a2, a3, a4, a5]).reflectee;
|
||||
case 6: return (a1, a2, a3, a4, a5, a6) =>
|
||||
create(name, [a1, a2, a3, a4, a5, a6]).reflectee;
|
||||
case 7: return (a1, a2, a3, a4, a5, a6, a7) =>
|
||||
create(name, [a1, a2, a3, a4, a5, a6, a7]).reflectee;
|
||||
case 8: return (a1, a2, a3, a4, a5, a6, a7, a8) =>
|
||||
create(name, [a1, a2, a3, a4, a5, a6, a7, a8]).reflectee;
|
||||
case 9: return (a1, a2, a3, a4, a5, a6, a7, a8, a9) =>
|
||||
create(name, [a1, a2, a3, a4, a5, a6, a7, a8, a9]).reflectee;
|
||||
case 10: return (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) =>
|
||||
create(name, [a1, a2, a3, a4, a5, a6, a7, a8, a9, a10]).reflectee;
|
||||
};
|
||||
|
||||
throw "Factory cannot take more than 10 arguments";
|
||||
}
|
||||
|
||||
invoke(Function factory, List args) {
|
||||
return Function.apply(factory, args);
|
||||
}
|
||||
|
||||
List<Dependency> dependencies(typeOrFunc) {
|
||||
final parameters = typeOrFunc is Type ?
|
||||
_constructorParameters(typeOrFunc) :
|
||||
_functionParameters(typeOrFunc);
|
||||
|
||||
return new List.generate(parameters.length, (int pos) {
|
||||
ParameterMirror p = parameters[pos];
|
||||
|
||||
final metadata = p.metadata.map((m) => m.reflectee);
|
||||
|
||||
var inject = metadata.firstWhere((m) => m is Inject, orElse: () => null);
|
||||
var injectPromise = metadata.firstWhere((m) => m is InjectPromise, orElse: () => null);
|
||||
var injectLazy = metadata.firstWhere((m) => m is InjectLazy, orElse: () => null);
|
||||
|
||||
if (inject != null) {
|
||||
return new Dependency(Key.get(inject.token), false, false, []);
|
||||
|
||||
} else if (injectPromise != null) {
|
||||
return new Dependency(Key.get(injectPromise.token), true, false, []);
|
||||
|
||||
} else if (injectLazy != null) {
|
||||
return new Dependency(Key.get(injectLazy.token), false, true, []);
|
||||
|
||||
} else if (p.type.qualifiedName != #dynamic) {
|
||||
var depProps = metadata.where((m) => m is DependencyAnnotation).toList();
|
||||
return new Dependency(Key.get(p.type.reflectedType), false, false, depProps);
|
||||
|
||||
} else {
|
||||
throw new NoAnnotationError(typeOrFunc);
|
||||
}
|
||||
}, growable:false);
|
||||
}
|
||||
|
||||
List<ParameterMirror> _functionParameters(Function func) {
|
||||
var closureMirror = reflect(func);
|
||||
return closureMirror.function.parameters;
|
||||
}
|
||||
|
||||
List<ParameterMirror> _constructorParameters(Type type) {
|
||||
ClassMirror classMirror = reflectType(type);
|
||||
MethodMirror ctor = classMirror.declarations[classMirror.simpleName];
|
||||
return ctor.parameters;
|
||||
}
|
||||
}
|
||||
|
||||
final Reflector reflector = new Reflector();
|
|
@ -1,84 +0,0 @@
|
|||
import {Type, isPresent} from 'facade/lang';
|
||||
import {List} from 'facade/collection';
|
||||
import {Inject, InjectPromise, InjectLazy, DependencyAnnotation} from './annotations';
|
||||
import {Key} from './key';
|
||||
import {Dependency} from './binding';
|
||||
import {NoAnnotationError} from './exceptions';
|
||||
|
||||
class Reflector {
|
||||
factoryFor(type:Type):Function {
|
||||
var length = type.parameters ? type.parameters.length : 0;
|
||||
switch (length) {
|
||||
case 0: return () =>
|
||||
new type();
|
||||
case 1: return (a1) =>
|
||||
new type(a1);
|
||||
case 2: return (a1, a2) =>
|
||||
new type(a1, a2);
|
||||
case 3: return (a1, a2, a3) =>
|
||||
new type(a1, a2, a3);
|
||||
case 4: return (a1, a2, a3, a4) =>
|
||||
new type(a1, a2, a3, a4);
|
||||
case 5: return (a1, a2, a3, a4, a5) =>
|
||||
new type(a1, a2, a3, a4, a5);
|
||||
case 6: return (a1, a2, a3, a4, a5, a6) =>
|
||||
new type(a1, a2, a3, a4, a5, a6);
|
||||
case 7: return (a1, a2, a3, a4, a5, a6, a7) =>
|
||||
new type(a1, a2, a3, a4, a5, a6, a7);
|
||||
case 8: return (a1, a2, a3, a4, a5, a6, a7, a8) =>
|
||||
new type(a1, a2, a3, a4, a5, a6, a7, a8);
|
||||
case 9: return (a1, a2, a3, a4, a5, a6, a7, a8, a9) =>
|
||||
new type(a1, a2, a3, a4, a5, a6, a7, a8, a9);
|
||||
case 10: return (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) =>
|
||||
new type(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10);
|
||||
};
|
||||
|
||||
throw "Factory cannot take more than 10 arguments";
|
||||
}
|
||||
|
||||
invoke(factory:Function, args:List) {
|
||||
return factory(...args);
|
||||
}
|
||||
|
||||
dependencies(typeOrFunc):List {
|
||||
var p = typeOrFunc.parameters;
|
||||
if (p == undefined && typeOrFunc.length == 0) return [];
|
||||
if (p == undefined) throw new NoAnnotationError(typeOrFunc);
|
||||
return typeOrFunc.parameters.map((p) => this._extractToken(typeOrFunc, p));
|
||||
}
|
||||
|
||||
_extractToken(typeOrFunc, annotations) {
|
||||
var type;
|
||||
var depProps = [];
|
||||
|
||||
for (var paramAnnotation of annotations) {
|
||||
if (paramAnnotation instanceof Type) {
|
||||
type = paramAnnotation;
|
||||
|
||||
} else if (paramAnnotation instanceof Inject) {
|
||||
return this._createDependency(paramAnnotation.token, false, false, []);
|
||||
|
||||
} else if (paramAnnotation instanceof InjectPromise) {
|
||||
return this._createDependency(paramAnnotation.token, true, false, []);
|
||||
|
||||
} else if (paramAnnotation instanceof InjectLazy) {
|
||||
return this._createDependency(paramAnnotation.token, false, true, []);
|
||||
|
||||
} else if (paramAnnotation instanceof DependencyAnnotation) {
|
||||
depProps.push(paramAnnotation);
|
||||
}
|
||||
}
|
||||
|
||||
if (isPresent(type)) {
|
||||
return this._createDependency(type, false, false, depProps);
|
||||
} else {
|
||||
throw new NoAnnotationError(typeOrFunc);
|
||||
}
|
||||
}
|
||||
|
||||
_createDependency(token, asPromise, lazy, depProps):Dependency {
|
||||
return new Dependency(Key.get(token), asPromise, lazy, depProps);
|
||||
}
|
||||
}
|
||||
|
||||
export var reflector:Reflector = new Reflector();
|
|
@ -1,23 +0,0 @@
|
|||
import {ddescribe, describe, it, iit, expect} from 'test_lib/test_lib';
|
||||
import {Key, Inject, DependencyAnnotation} from 'di/di';
|
||||
import {CONST} from 'facade/lang';
|
||||
import {reflector, Token} from 'di/reflector';
|
||||
|
||||
class Parent extends DependencyAnnotation {
|
||||
@CONST()
|
||||
constructor() {
|
||||
}
|
||||
}
|
||||
|
||||
export function main() {
|
||||
describe("reflector", function () {
|
||||
describe("dependencies", function () {
|
||||
it('should collect annotations implementing DependencyAnnotation as properties', function () {
|
||||
function f(@Parent() arg:Function) {}
|
||||
|
||||
var dep = reflector.dependencies(f)[0];
|
||||
expect(dep.properties[0]).toBeAnInstanceOf(Parent);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
name: reflection
|
||||
environment:
|
||||
sdk: '>=1.4.0'
|
||||
dependencies:
|
||||
facade:
|
||||
path: ../facade
|
||||
dev_dependencies:
|
||||
test_lib:
|
||||
path: ../test_lib
|
||||
guinness: ">=0.1.16 <0.2.0"
|
|
@ -0,0 +1,34 @@
|
|||
library reflection.reflection;
|
||||
|
||||
import 'reflector.dart';
|
||||
import 'types.dart';
|
||||
export 'reflector.dart';
|
||||
import 'package:facade/lang.dart';
|
||||
|
||||
class NoReflectionCapabilities {
|
||||
Function factory(Type type){
|
||||
throw "Cannot find reflection information on ${stringify(type)}";
|
||||
}
|
||||
|
||||
List parameters(Type type) {
|
||||
throw "Cannot find reflection information on ${stringify(type)}";
|
||||
}
|
||||
|
||||
List annotations(Type type) {
|
||||
throw "Cannot find reflection information on ${stringify(type)}";
|
||||
}
|
||||
|
||||
GetterFn getter(String name) {
|
||||
throw "Cannot find getter ${name}";
|
||||
}
|
||||
|
||||
SetterFn setter(String name) {
|
||||
throw "Cannot find getter ${name}";
|
||||
}
|
||||
|
||||
MethodFn method(String name) {
|
||||
throw "Cannot find method ${name}";
|
||||
}
|
||||
}
|
||||
|
||||
final Reflector reflector = new Reflector(new NoReflectionCapabilities());
|
|
@ -0,0 +1,7 @@
|
|||
import {Type, isPresent} from 'facade/lang';
|
||||
import {List, ListWrapper} from 'facade/collection';
|
||||
import {Reflector} from './reflector';
|
||||
export {Reflector} from './reflector';;
|
||||
import {ReflectionCapabilities} from './reflection_capabilities';;
|
||||
|
||||
export var reflector = new Reflector(new ReflectionCapabilities());
|
|
@ -0,0 +1,89 @@
|
|||
library reflection.reflection_capabilities;
|
||||
|
||||
import 'reflection.dart';
|
||||
import 'types.dart';
|
||||
import 'dart:mirrors';
|
||||
|
||||
class ReflectionCapabilities {
|
||||
Function factory(Type type) {
|
||||
ClassMirror classMirror = reflectType(type);
|
||||
MethodMirror ctor = classMirror.declarations[classMirror.simpleName];
|
||||
Function create = classMirror.newInstance;
|
||||
Symbol name = ctor.constructorName;
|
||||
int length = ctor.parameters.length;
|
||||
|
||||
switch (length) {
|
||||
case 0: return () => create(name, []).reflectee;
|
||||
case 1: return (a1) => create(name, [a1]).reflectee;
|
||||
case 2: return (a1, a2) => create(name, [a1, a2]).reflectee;
|
||||
case 3: return (a1, a2, a3) => create(name, [a1, a2, a3]).reflectee;
|
||||
case 4: return (a1, a2, a3, a4) => create(name, [a1, a2, a3, a4]).reflectee;
|
||||
case 5: return (a1, a2, a3, a4, a5) => create(name, [a1, a2, a3, a4, a5]).reflectee;
|
||||
case 6: return (a1, a2, a3, a4, a5, a6) => create(name, [a1, a2, a3, a4, a5, a6]).reflectee;
|
||||
case 7: return (a1, a2, a3, a4, a5, a6, a7) => create(name, [a1, a2, a3, a4, a5, a6, a7]).reflectee;
|
||||
case 8: return (a1, a2, a3, a4, a5, a6, a7, a8) => create(name, [a1, a2, a3, a4, a5, a6, a7, a8]).reflectee;
|
||||
case 9: return (a1, a2, a3, a4, a5, a6, a7, a8, a9) => create(name, [a1, a2, a3, a4, a5, a6, a7, a8, a9]).reflectee;
|
||||
case 10: return (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) => create(name, [a1, a2, a3, a4, a5, a6, a7, a8, a9, a10]).reflectee;
|
||||
};
|
||||
|
||||
throw "Factory cannot take more than 10 arguments";
|
||||
}
|
||||
|
||||
List<List> parameters(typeOrFunc) {
|
||||
final parameters = typeOrFunc is Type ?
|
||||
_constructorParameters(typeOrFunc) :
|
||||
_functionParameters(typeOrFunc);
|
||||
return parameters.map(_convertParameter).toList();
|
||||
}
|
||||
|
||||
List _convertParameter(ParameterMirror p) {
|
||||
var t = p.type.reflectedType;
|
||||
var res = t == dynamic ? [] : [t];
|
||||
res.addAll(p.metadata.map((m) => m.reflectee));
|
||||
return res;
|
||||
}
|
||||
|
||||
List annotations(typeOrFunc) {
|
||||
final meta = typeOrFunc is Type ?
|
||||
_constructorMetadata(typeOrFunc) :
|
||||
_functionMetadata(typeOrFunc);
|
||||
|
||||
return meta.map((m) => m.reflectee).toList();
|
||||
}
|
||||
|
||||
GetterFn getter(String name) {
|
||||
var symbol = new Symbol(name);
|
||||
return (receiver) => reflect(receiver).getField(symbol).reflectee;
|
||||
}
|
||||
|
||||
SetterFn setter(String name) {
|
||||
var symbol = new Symbol(name);
|
||||
return (receiver, value) => reflect(receiver).setField(symbol, value).reflectee;
|
||||
}
|
||||
|
||||
MethodFn method(String name) {
|
||||
var symbol = new Symbol(name);
|
||||
return (receiver, posArgs) => reflect(receiver).invoke(symbol, posArgs).reflectee;
|
||||
}
|
||||
|
||||
List _functionParameters(Function func) {
|
||||
var closureMirror = reflect(func);
|
||||
return closureMirror.function.parameters;
|
||||
}
|
||||
|
||||
List _constructorParameters(Type type) {
|
||||
ClassMirror classMirror = reflectType(type);
|
||||
MethodMirror ctor = classMirror.declarations[classMirror.simpleName];
|
||||
return ctor.parameters;
|
||||
}
|
||||
|
||||
List _functionMetadata(Function func) {
|
||||
var closureMirror = reflect(func);
|
||||
return closureMirror.function.metadata;
|
||||
}
|
||||
|
||||
List _constructorMetadata(Type type) {
|
||||
ClassMirror classMirror = reflectType(type);
|
||||
return classMirror.metadata;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,59 @@
|
|||
import {Type, isPresent} from 'facade/lang';
|
||||
import {List, ListWrapper} from 'facade/collection';
|
||||
import {GetterFn, SetterFn, MethodFn} from './types';
|
||||
|
||||
export class ReflectionCapabilities {
|
||||
factory(type:Type):Function {
|
||||
switch (type.length) {
|
||||
case 0:
|
||||
return function(){return new type();};
|
||||
case 1:
|
||||
return function(a1){return new type(a1);};
|
||||
case 2:
|
||||
return function(a1, a2){return new type(a1, a2);};
|
||||
case 3:
|
||||
return function(a1, a2, a3){return new type(a1, a2, a3);};
|
||||
case 4:
|
||||
return function(a1, a2, a3, a4){return new type(a1, a2, a3, a4);};
|
||||
case 5:
|
||||
return function(a1, a2, a3, a4, a5){return new type(a1, a2, a3, a4, a5);};
|
||||
case 6:
|
||||
return function(a1, a2, a3, a4, a5, a6){return new type(a1, a2, a3, a4, a5, a6);};
|
||||
case 7:
|
||||
return function(a1, a2, a3, a4, a5, a6, a7){return new type(a1, a2, a3, a4, a5, a6, a7);};
|
||||
case 8:
|
||||
return function(a1, a2, a3, a4, a5, a6, a7, a8){return new type(a1, a2, a3, a4, a5, a6, a7, a8);};
|
||||
case 9:
|
||||
return function(a1, a2, a3, a4, a5, a6, a7, a8, a9){return new type(a1, a2, a3, a4, a5, a6, a7, a8, a9);};
|
||||
case 10:
|
||||
return function(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10){return new type(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10);};
|
||||
};
|
||||
|
||||
throw new Error("Factory cannot take more than 10 arguments");
|
||||
}
|
||||
|
||||
parameters(typeOfFunc):List<List> {
|
||||
return isPresent(typeOfFunc.parameters) ?
|
||||
typeOfFunc.parameters :
|
||||
ListWrapper.createFixedSize(typeOfFunc.length);
|
||||
}
|
||||
|
||||
annotations(typeOfFunc):List {
|
||||
return isPresent(typeOfFunc.annotations) ? typeOfFunc.annotations : [];
|
||||
}
|
||||
|
||||
getter(name:string):GetterFn {
|
||||
return new Function('o', 'return o.' + name + ';');
|
||||
}
|
||||
|
||||
setter(name:string):SetterFn {
|
||||
return new Function('o', 'v', 'return o.' + name + ' = v;');
|
||||
}
|
||||
|
||||
method(name:string):MethodFn {
|
||||
var method = `o.${name}`;
|
||||
return new Function('o', 'args',
|
||||
`if (!${method}) throw new Error('"${name}" is undefined');` +
|
||||
`return ${method}.apply(o, args);`);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,88 @@
|
|||
import {Type, isPresent, stringify, BaseException} from 'facade/lang';
|
||||
import {List, ListWrapper, Map, MapWrapper, StringMapWrapper} from 'facade/collection';
|
||||
import {SetterFn, GetterFn, MethodFn} from './types';
|
||||
export {SetterFn, GetterFn, MethodFn} from './types';
|
||||
|
||||
export class Reflector {
|
||||
_typeInfo:Map;
|
||||
_getters:Map;
|
||||
_setters:Map;
|
||||
_methods:Map;
|
||||
reflectionCapabilities:any;
|
||||
|
||||
constructor(reflectionCapabilities) {
|
||||
this._typeInfo = MapWrapper.create();
|
||||
this._getters = MapWrapper.create();
|
||||
this._setters = MapWrapper.create();
|
||||
this._methods = MapWrapper.create();
|
||||
this.reflectionCapabilities = reflectionCapabilities;
|
||||
}
|
||||
|
||||
registerType(type, typeInfo){
|
||||
MapWrapper.set(this._typeInfo, type, typeInfo);
|
||||
}
|
||||
|
||||
registerGetters(getters){
|
||||
_mergeMaps(this._getters, getters);
|
||||
}
|
||||
|
||||
registerSetters(setters){
|
||||
_mergeMaps(this._setters, setters);
|
||||
}
|
||||
|
||||
registerMethods(methods){
|
||||
_mergeMaps(this._methods, methods);
|
||||
}
|
||||
|
||||
factory(type:Type):Function {
|
||||
if(MapWrapper.contains(this._typeInfo, type)) {
|
||||
return MapWrapper.get(this._typeInfo, type)["factory"];
|
||||
} else {
|
||||
return this.reflectionCapabilities.factory(type);
|
||||
}
|
||||
}
|
||||
|
||||
parameters(typeOfFunc):List {
|
||||
if(MapWrapper.contains(this._typeInfo, typeOfFunc)) {
|
||||
return MapWrapper.get(this._typeInfo, typeOfFunc)["parameters"];
|
||||
} else {
|
||||
return this.reflectionCapabilities.parameters(typeOfFunc);
|
||||
}
|
||||
}
|
||||
|
||||
annotations(typeOfFunc):List {
|
||||
if(MapWrapper.contains(this._typeInfo, typeOfFunc)) {
|
||||
return MapWrapper.get(this._typeInfo, typeOfFunc)["annotations"];
|
||||
} else {
|
||||
return this.reflectionCapabilities.annotations(typeOfFunc);
|
||||
}
|
||||
}
|
||||
|
||||
getter(name:string):GetterFn {
|
||||
if(MapWrapper.contains(this._getters, name)) {
|
||||
return MapWrapper.get(this._getters, name);
|
||||
} else {
|
||||
return this.reflectionCapabilities.getter(name);
|
||||
}
|
||||
}
|
||||
|
||||
setter(name:string):SetterFn {
|
||||
if(MapWrapper.contains(this._setters, name)) {
|
||||
return MapWrapper.get(this._setters, name);
|
||||
} else {
|
||||
return this.reflectionCapabilities.setter(name);
|
||||
}
|
||||
}
|
||||
|
||||
method(name:string):MethodFn {
|
||||
if(MapWrapper.contains(this._methods, name)) {
|
||||
return MapWrapper.get(this._methods, name);
|
||||
} else {
|
||||
return this.reflectionCapabilities.method(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function _mergeMaps(target:Map, config) {
|
||||
StringMapWrapper.forEach(config, (v, k) => MapWrapper.set(target, k, v));
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
library reflection.types;
|
||||
|
||||
typedef SetterFn(Object obj, value);
|
||||
typedef GetterFn(Object obj);
|
||||
typedef MethodFn(Object obj, List args);
|
|
@ -0,0 +1,3 @@
|
|||
export var SetterFn = Function;
|
||||
export var GetterFn = Function;
|
||||
export var MethodFn = Function;
|
|
@ -0,0 +1,168 @@
|
|||
import {describe, it, iit, ddescribe, expect, beforeEach} from 'test_lib/test_lib';
|
||||
import {Reflector} from 'reflection/reflection';
|
||||
import {ReflectionCapabilities} from 'reflection/reflection_capabilities';
|
||||
import {CONST} from 'facade/lang';
|
||||
|
||||
class Annotation {
|
||||
value;
|
||||
|
||||
@CONST()
|
||||
constructor(value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
|
||||
class AType {
|
||||
value;
|
||||
|
||||
constructor(value){
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
|
||||
@Annotation('class')
|
||||
class ClassWithAnnotations {
|
||||
a;
|
||||
b;
|
||||
|
||||
constructor(@Annotation("a") a:AType, @Annotation("b") b:AType) {
|
||||
this.a = a;
|
||||
this.b = b;
|
||||
}
|
||||
}
|
||||
|
||||
class ClassWithoutAnnotations {
|
||||
constructor(a,b){}
|
||||
}
|
||||
|
||||
class TestObjWith11Args {
|
||||
constructor(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11) {
|
||||
}
|
||||
}
|
||||
|
||||
@Annotation('func')
|
||||
function testFunc(@Annotation("a") a:AType, @Annotation("b") b:AType) {
|
||||
}
|
||||
|
||||
class TestObj {
|
||||
a;
|
||||
b;
|
||||
|
||||
constructor(a, b) {
|
||||
this.a = a;
|
||||
this.b = b;
|
||||
}
|
||||
|
||||
identity(arg) {
|
||||
return arg;
|
||||
}
|
||||
}
|
||||
|
||||
export function main() {
|
||||
describe('Reflector', () => {
|
||||
var reflector;
|
||||
|
||||
beforeEach(() => {
|
||||
reflector = new Reflector(new ReflectionCapabilities());
|
||||
});
|
||||
|
||||
describe("factory", () => {
|
||||
it("should create a factory for the given type", () => {
|
||||
var obj = reflector.factory(TestObj)(1, 2);
|
||||
|
||||
expect(obj.a).toEqual(1);
|
||||
expect(obj.b).toEqual(2);
|
||||
});
|
||||
|
||||
it("should throw when more than 10 arguments", () => {
|
||||
expect(() => reflector.factory(TestObjWith11Args)(1,2,3,4,5,6,7,8,9,10,11)).toThrowError();
|
||||
});
|
||||
|
||||
it("should return a registered factory if available", () => {
|
||||
reflector.registerType(TestObj, {"factory" : () => "fake"});
|
||||
expect(reflector.factory(TestObj)()).toEqual("fake");
|
||||
});
|
||||
});
|
||||
|
||||
describe("parameters", () => {
|
||||
it("should return an array of parameters for a type", () => {
|
||||
var p = reflector.parameters(ClassWithAnnotations);
|
||||
expect(p).toEqual([[AType, new Annotation('a')], [AType, new Annotation('b')]]);
|
||||
});
|
||||
|
||||
it("should return an array of parameters for a function", () => {
|
||||
var p = reflector.parameters(testFunc);
|
||||
expect(p).toEqual([[AType, new Annotation('a')], [AType, new Annotation('b')]]);
|
||||
});
|
||||
|
||||
it("should work for a class without annotations", () => {
|
||||
var p = reflector.parameters(ClassWithoutAnnotations);
|
||||
expect(p.length).toEqual(2);
|
||||
});
|
||||
|
||||
it("should return registered parameters if available", () => {
|
||||
reflector.registerType(TestObj, {"parameters" : [1,2]});
|
||||
expect(reflector.parameters(TestObj)).toEqual([1,2]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("annotations", () => {
|
||||
it("should return an array of annotations for a type", () => {
|
||||
var p = reflector.annotations(ClassWithAnnotations);
|
||||
expect(p).toEqual([new Annotation('class')]);
|
||||
});
|
||||
|
||||
it("should return an array of annotations for a function", () => {
|
||||
var p = reflector.annotations(testFunc);
|
||||
expect(p).toEqual([new Annotation('func')]);
|
||||
});
|
||||
|
||||
it("should return registered annotations if available", () => {
|
||||
reflector.registerType(TestObj, {"annotations" : [1,2]});
|
||||
expect(reflector.annotations(TestObj)).toEqual([1,2]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getter", () => {
|
||||
it("returns a function reading a property", () => {
|
||||
var getA = reflector.getter('a');
|
||||
expect(getA(new TestObj(1, 2))).toEqual(1);
|
||||
});
|
||||
|
||||
it("should return a registered getter if available", () => {
|
||||
reflector.registerGetters({"abc" : (obj) => "fake"});
|
||||
expect(reflector.getter("abc")("anything")).toEqual("fake");
|
||||
});
|
||||
});
|
||||
|
||||
describe("setter", () => {
|
||||
it("returns a function setting a property", () => {
|
||||
var setA = reflector.setter('a');
|
||||
var obj = new TestObj(1, 2);
|
||||
setA(obj, 100);
|
||||
expect(obj.a).toEqual(100);
|
||||
});
|
||||
|
||||
it("should return a registered setter if available", () => {
|
||||
var updateMe;
|
||||
reflector.registerSetters({"abc" : (obj, value) => {updateMe = value;}});
|
||||
reflector.setter("abc")("anything", "fake");
|
||||
|
||||
expect(updateMe).toEqual("fake");
|
||||
});
|
||||
});
|
||||
|
||||
describe("method", () => {
|
||||
it("returns a function invoking a method", () => {
|
||||
var func = reflector.method('identity');
|
||||
var obj = new TestObj(1, 2);
|
||||
expect(func(obj, ['value'])).toEqual('value');
|
||||
});
|
||||
|
||||
it("should return a registered method if available", () => {
|
||||
reflector.registerMethods({"abc" : (obj, args) => args});
|
||||
expect(reflector.method("abc")("anything", ["fake"])).toEqual(['fake']);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
|
@ -4,5 +4,7 @@ environment:
|
|||
dependencies:
|
||||
facade:
|
||||
path: ../facade
|
||||
reflection:
|
||||
path: ../reflection
|
||||
dev_dependencies:
|
||||
guinness: ">=0.1.16 <0.2.0"
|
|
@ -5,6 +5,8 @@ export 'package:guinness/guinness_html.dart';
|
|||
import 'package:unittest/unittest.dart' hide expect;
|
||||
import 'dart:mirrors';
|
||||
import 'dart:async';
|
||||
import 'package:reflection/reflection.dart';
|
||||
import 'package:reflection/reflection_capabilities.dart';
|
||||
|
||||
bool IS_DARTIUM = true;
|
||||
|
||||
|
@ -32,11 +34,18 @@ class NotExpect extends gns.NotExpect {
|
|||
}
|
||||
|
||||
it(name, fn) {
|
||||
gns.it(name, _handleAsync(fn));
|
||||
gns.it(name, _enableReflection(_handleAsync(fn)));
|
||||
}
|
||||
|
||||
iit(name, fn) {
|
||||
gns.iit(name, _handleAsync(fn));
|
||||
gns.iit(name, _enableReflection(_handleAsync(fn)));
|
||||
}
|
||||
|
||||
_enableReflection(fn) {
|
||||
return () {
|
||||
reflector.reflectionCapabilities = new ReflectionCapabilities();
|
||||
return fn();
|
||||
};
|
||||
}
|
||||
|
||||
_handleAsync(fn) {
|
||||
|
|
|
@ -24,6 +24,9 @@ System.paths = {
|
|||
'di/*': './di/src/*.js',
|
||||
'di/test/*': './di/test/*.js',
|
||||
|
||||
'reflection/*': './reflection/src/*.js',
|
||||
'reflection/test/*': './reflection/test/*.js',
|
||||
|
||||
'rtts_assert/*': './rtts_assert/src/*.js',
|
||||
'rtts_assert/test/*': './rtts_assert/test/*.js',
|
||||
|
||||
|
|
Loading…
Reference in New Issue