chore: move core modules into core directory
BREAKING CHANGE: This change moves the http module into angular2/, so its import path is now angular2/http instead of http/http. Many other modules have also been moved around inside of angular2, but the public API paths have not changed as of this commit.
This commit is contained in:
@ -0,0 +1,58 @@
|
||||
library reflection.debug_reflection_capabilities;
|
||||
|
||||
import 'dart:mirrors';
|
||||
import 'package:logging/logging.dart' as log;
|
||||
import 'package:stack_trace/stack_trace.dart';
|
||||
import 'types.dart';
|
||||
import 'reflection_capabilities.dart' as standard;
|
||||
|
||||
class ReflectionCapabilities extends standard.ReflectionCapabilities {
|
||||
final bool _verbose;
|
||||
final log.Logger _log = new log.Logger('ReflectionCapabilities');
|
||||
|
||||
ReflectionCapabilities({bool verbose: false})
|
||||
: _verbose = verbose,
|
||||
super() {
|
||||
log.hierarchicalLoggingEnabled = true;
|
||||
_log.level = _verbose ? log.Level.ALL : log.Level.INFO;
|
||||
_log.onRecord.listen((log.LogRecord rec) {
|
||||
print('[${rec.loggerName}(${rec.level.name})]: ${rec.message}');
|
||||
});
|
||||
}
|
||||
|
||||
void _notify(String methodName, param) {
|
||||
var trace = _verbose ? ' ${Trace.format(new Trace.current())}' : '';
|
||||
_log.info('"$methodName" requested for "$param".$trace');
|
||||
}
|
||||
|
||||
Function factory(Type type) {
|
||||
ClassMirror classMirror = reflectType(type);
|
||||
_notify('factory', '${classMirror.qualifiedName}');
|
||||
return super.factory(type);
|
||||
}
|
||||
|
||||
List<List> parameters(typeOrFunc) {
|
||||
_notify('parameters', typeOrFunc);
|
||||
return super.parameters(typeOrFunc);
|
||||
}
|
||||
|
||||
List annotations(typeOrFunc) {
|
||||
_notify('annotations', typeOrFunc);
|
||||
return super.annotations(typeOrFunc);
|
||||
}
|
||||
|
||||
GetterFn getter(String name) {
|
||||
_notify('getter', name);
|
||||
return super.getter(name);
|
||||
}
|
||||
|
||||
SetterFn setter(String name) {
|
||||
_notify('setter', name);
|
||||
return super.setter(name);
|
||||
}
|
||||
|
||||
MethodFn method(String name) {
|
||||
_notify('method', name);
|
||||
return super.method(name);
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
import {Type} from 'angular2/src/facade/lang';
|
||||
import {GetterFn, SetterFn, MethodFn} from './types';
|
||||
import {List} from 'angular2/src/facade/collection';
|
||||
|
||||
export interface PlatformReflectionCapabilities {
|
||||
isReflectionEnabled(): boolean;
|
||||
factory(type: Type): Function;
|
||||
interfaces(type: Type): List<any>;
|
||||
parameters(type: Type): List<List<any>>;
|
||||
annotations(type: Type): List<any>;
|
||||
getter(name: string): GetterFn;
|
||||
setter(name: string): SetterFn;
|
||||
method(name: string): MethodFn;
|
||||
}
|
43
modules/angular2/src/core/reflection/reflection.dart
Normal file
43
modules/angular2/src/core/reflection/reflection.dart
Normal file
@ -0,0 +1,43 @@
|
||||
library reflection.reflection;
|
||||
|
||||
import 'reflector.dart';
|
||||
import 'types.dart';
|
||||
export 'reflector.dart';
|
||||
import 'platform_reflection_capabilities.dart';
|
||||
import 'package:angular2/src/facade/lang.dart';
|
||||
|
||||
class NoReflectionCapabilities implements PlatformReflectionCapabilities {
|
||||
bool isReflectionEnabled() {
|
||||
return false;
|
||||
}
|
||||
|
||||
Function factory(Type type) {
|
||||
throw "Cannot find reflection information on ${stringify(type)}";
|
||||
}
|
||||
|
||||
List interfaces(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 setter ${name}";
|
||||
}
|
||||
|
||||
MethodFn method(String name) {
|
||||
throw "Cannot find method ${name}";
|
||||
}
|
||||
}
|
||||
|
||||
final Reflector reflector = new Reflector(new NoReflectionCapabilities());
|
7
modules/angular2/src/core/reflection/reflection.ts
Normal file
7
modules/angular2/src/core/reflection/reflection.ts
Normal file
@ -0,0 +1,7 @@
|
||||
import {Type, isPresent} from 'angular2/src/facade/lang';
|
||||
import {List, ListWrapper} from 'angular2/src/facade/collection';
|
||||
import {Reflector} from './reflector';
|
||||
export {Reflector, ReflectionInfo} from './reflector';
|
||||
import {ReflectionCapabilities} from './reflection_capabilities';
|
||||
|
||||
export var reflector = new Reflector(new ReflectionCapabilities());
|
@ -0,0 +1,300 @@
|
||||
library reflection.reflection_capabilities;
|
||||
|
||||
import 'package:angular2/src/facade/lang.dart';
|
||||
import 'types.dart';
|
||||
import 'dart:mirrors';
|
||||
import 'platform_reflection_capabilities.dart';
|
||||
|
||||
class ReflectionCapabilities implements PlatformReflectionCapabilities {
|
||||
ReflectionCapabilities([metadataReader]) {}
|
||||
|
||||
bool isReflectionEnabled() {
|
||||
return true;
|
||||
}
|
||||
|
||||
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;
|
||||
case 11:
|
||||
return (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11) =>
|
||||
create(name, [a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11])
|
||||
.reflectee;
|
||||
case 12:
|
||||
return (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12) =>
|
||||
create(name, [a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12])
|
||||
.reflectee;
|
||||
case 13:
|
||||
return (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13) =>
|
||||
create(name, [
|
||||
a1,
|
||||
a2,
|
||||
a3,
|
||||
a4,
|
||||
a5,
|
||||
a6,
|
||||
a7,
|
||||
a8,
|
||||
a9,
|
||||
a10,
|
||||
a11,
|
||||
a12,
|
||||
a13
|
||||
]).reflectee;
|
||||
case 14:
|
||||
return (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14) =>
|
||||
create(name, [
|
||||
a1,
|
||||
a2,
|
||||
a3,
|
||||
a4,
|
||||
a5,
|
||||
a6,
|
||||
a7,
|
||||
a8,
|
||||
a9,
|
||||
a10,
|
||||
a11,
|
||||
a12,
|
||||
a13,
|
||||
a14
|
||||
]).reflectee;
|
||||
case 15:
|
||||
return (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14,
|
||||
a15) =>
|
||||
create(name, [
|
||||
a1,
|
||||
a2,
|
||||
a3,
|
||||
a4,
|
||||
a5,
|
||||
a6,
|
||||
a7,
|
||||
a8,
|
||||
a9,
|
||||
a10,
|
||||
a11,
|
||||
a12,
|
||||
a13,
|
||||
a14,
|
||||
a15
|
||||
]).reflectee;
|
||||
case 16:
|
||||
return (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14,
|
||||
a15, a16) =>
|
||||
create(name, [
|
||||
a1,
|
||||
a2,
|
||||
a3,
|
||||
a4,
|
||||
a5,
|
||||
a6,
|
||||
a7,
|
||||
a8,
|
||||
a9,
|
||||
a10,
|
||||
a11,
|
||||
a12,
|
||||
a13,
|
||||
a14,
|
||||
a15,
|
||||
a16
|
||||
]).reflectee;
|
||||
case 17:
|
||||
return (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14,
|
||||
a15, a16, a17) =>
|
||||
create(name, [
|
||||
a1,
|
||||
a2,
|
||||
a3,
|
||||
a4,
|
||||
a5,
|
||||
a6,
|
||||
a7,
|
||||
a8,
|
||||
a9,
|
||||
a10,
|
||||
a11,
|
||||
a12,
|
||||
a13,
|
||||
a14,
|
||||
a15,
|
||||
a16,
|
||||
a17
|
||||
]).reflectee;
|
||||
case 18:
|
||||
return (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14,
|
||||
a15, a16, a17, a18) =>
|
||||
create(name, [
|
||||
a1,
|
||||
a2,
|
||||
a3,
|
||||
a4,
|
||||
a5,
|
||||
a6,
|
||||
a7,
|
||||
a8,
|
||||
a9,
|
||||
a10,
|
||||
a11,
|
||||
a12,
|
||||
a13,
|
||||
a14,
|
||||
a15,
|
||||
a16,
|
||||
a17,
|
||||
a18
|
||||
]).reflectee;
|
||||
case 19:
|
||||
return (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14,
|
||||
a15, a16, a17, a18, a19) =>
|
||||
create(name, [
|
||||
a1,
|
||||
a2,
|
||||
a3,
|
||||
a4,
|
||||
a5,
|
||||
a6,
|
||||
a7,
|
||||
a8,
|
||||
a9,
|
||||
a10,
|
||||
a11,
|
||||
a12,
|
||||
a13,
|
||||
a14,
|
||||
a15,
|
||||
a16,
|
||||
a17,
|
||||
a18,
|
||||
a19
|
||||
]).reflectee;
|
||||
case 20:
|
||||
return (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14,
|
||||
a15, a16, a17, a18, a19, a20) =>
|
||||
create(name, [
|
||||
a1,
|
||||
a2,
|
||||
a3,
|
||||
a4,
|
||||
a5,
|
||||
a6,
|
||||
a7,
|
||||
a8,
|
||||
a9,
|
||||
a10,
|
||||
a11,
|
||||
a12,
|
||||
a13,
|
||||
a14,
|
||||
a15,
|
||||
a16,
|
||||
a17,
|
||||
a18,
|
||||
a19,
|
||||
a20
|
||||
]).reflectee;
|
||||
}
|
||||
|
||||
throw "Cannot create a factory for '${stringify(type)}' because its constructor has more than 20 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;
|
||||
var res = (!t.hasReflectedType || t.reflectedType == dynamic)
|
||||
? []
|
||||
: [t.reflectedType];
|
||||
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();
|
||||
}
|
||||
|
||||
List interfaces(type) {
|
||||
ClassMirror classMirror = reflectType(type);
|
||||
return classMirror.superinterfaces.map((si) => si.reflectedType).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;
|
||||
}
|
||||
}
|
158
modules/angular2/src/core/reflection/reflection_capabilities.ts
Normal file
158
modules/angular2/src/core/reflection/reflection_capabilities.ts
Normal file
@ -0,0 +1,158 @@
|
||||
import {
|
||||
Type,
|
||||
isPresent,
|
||||
isFunction,
|
||||
global,
|
||||
stringify,
|
||||
BaseException
|
||||
} from 'angular2/src/facade/lang';
|
||||
import {List, ListWrapper} from 'angular2/src/facade/collection';
|
||||
import {GetterFn, SetterFn, MethodFn} from './types';
|
||||
import {PlatformReflectionCapabilities} from 'platform_reflection_capabilities';
|
||||
|
||||
export class ReflectionCapabilities implements PlatformReflectionCapabilities {
|
||||
private _reflect: any;
|
||||
|
||||
constructor(reflect?: any) { this._reflect = isPresent(reflect) ? reflect : global.Reflect; }
|
||||
|
||||
isReflectionEnabled(): boolean { return true; }
|
||||
|
||||
factory(t: Type): Function {
|
||||
switch (t.length) {
|
||||
case 0:
|
||||
return () => new t();
|
||||
case 1:
|
||||
return (a1) => new t(a1);
|
||||
case 2:
|
||||
return (a1, a2) => new t(a1, a2);
|
||||
case 3:
|
||||
return (a1, a2, a3) => new t(a1, a2, a3);
|
||||
case 4:
|
||||
return (a1, a2, a3, a4) => new t(a1, a2, a3, a4);
|
||||
case 5:
|
||||
return (a1, a2, a3, a4, a5) => new t(a1, a2, a3, a4, a5);
|
||||
case 6:
|
||||
return (a1, a2, a3, a4, a5, a6) => new t(a1, a2, a3, a4, a5, a6);
|
||||
case 7:
|
||||
return (a1, a2, a3, a4, a5, a6, a7) => new t(a1, a2, a3, a4, a5, a6, a7);
|
||||
case 8:
|
||||
return (a1, a2, a3, a4, a5, a6, a7, a8) => new t(a1, a2, a3, a4, a5, a6, a7, a8);
|
||||
case 9:
|
||||
return (a1, a2, a3, a4, a5, a6, a7, a8, a9) => new t(a1, a2, a3, a4, a5, a6, a7, a8, a9);
|
||||
case 10:
|
||||
return (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) =>
|
||||
new t(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10);
|
||||
case 11:
|
||||
return (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11) =>
|
||||
new t(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11);
|
||||
case 12:
|
||||
return (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12) =>
|
||||
new t(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12);
|
||||
case 13:
|
||||
return (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13) =>
|
||||
new t(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13);
|
||||
case 14:
|
||||
return (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14) =>
|
||||
new t(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14);
|
||||
case 15:
|
||||
return (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15) =>
|
||||
new t(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15);
|
||||
case 16:
|
||||
return (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16) =>
|
||||
new t(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16);
|
||||
case 17:
|
||||
return (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17) =>
|
||||
new t(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16,
|
||||
a17);
|
||||
case 18:
|
||||
return (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18) =>
|
||||
new t(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17,
|
||||
a18);
|
||||
case 19:
|
||||
return (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18,
|
||||
a19) => new t(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16,
|
||||
a17, a18, a19);
|
||||
case 20:
|
||||
return (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18,
|
||||
a19, a20) => new t(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15,
|
||||
a16, a17, a18, a19, a20);
|
||||
};
|
||||
|
||||
throw new Error(
|
||||
`Cannot create a factory for '${stringify(t)}' because its constructor has more than 20 arguments`);
|
||||
}
|
||||
|
||||
_zipTypesAndAnnotaions(paramTypes, paramAnnotations): List<List<any>> {
|
||||
var result;
|
||||
|
||||
if (typeof paramTypes === 'undefined') {
|
||||
result = ListWrapper.createFixedSize(paramAnnotations.length);
|
||||
} else {
|
||||
result = ListWrapper.createFixedSize(paramTypes.length);
|
||||
}
|
||||
|
||||
for (var i = 0; i < result.length; i++) {
|
||||
// TS outputs Object for parameters without types, while Traceur omits
|
||||
// the annotations. For now we preserve the Traceur behavior to aid
|
||||
// migration, but this can be revisited.
|
||||
if (typeof paramTypes === 'undefined') {
|
||||
result[i] = [];
|
||||
} else if (paramTypes[i] != Object) {
|
||||
result[i] = [paramTypes[i]];
|
||||
} else {
|
||||
result[i] = [];
|
||||
}
|
||||
if (isPresent(paramAnnotations) && isPresent(paramAnnotations[i])) {
|
||||
result[i] = result[i].concat(paramAnnotations[i]);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
parameters(typeOfFunc: Type): List<List<any>> {
|
||||
// Prefer the direct API.
|
||||
if (isPresent((<any>typeOfFunc).parameters)) {
|
||||
return (<any>typeOfFunc).parameters;
|
||||
}
|
||||
if (isPresent(this._reflect) && isPresent(this._reflect.getMetadata)) {
|
||||
var paramAnnotations = this._reflect.getMetadata('parameters', typeOfFunc);
|
||||
var paramTypes = this._reflect.getMetadata('design:paramtypes', typeOfFunc);
|
||||
if (isPresent(paramTypes) || isPresent(paramAnnotations)) {
|
||||
return this._zipTypesAndAnnotaions(paramTypes, paramAnnotations);
|
||||
}
|
||||
}
|
||||
return ListWrapper.createFixedSize((<any>typeOfFunc).length);
|
||||
}
|
||||
|
||||
annotations(typeOfFunc: Type): List<any> {
|
||||
// Prefer the direct API.
|
||||
if (isPresent((<any>typeOfFunc).annotations)) {
|
||||
var annotations = (<any>typeOfFunc).annotations;
|
||||
if (isFunction(annotations) && annotations.annotations) {
|
||||
annotations = annotations.annotations;
|
||||
}
|
||||
return annotations;
|
||||
}
|
||||
if (isPresent(this._reflect) && isPresent(this._reflect.getMetadata)) {
|
||||
var annotations = this._reflect.getMetadata('annotations', typeOfFunc);
|
||||
if (isPresent(annotations)) return annotations;
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
interfaces(type: Type): List<any> {
|
||||
throw new BaseException("JavaScript does not support interfaces");
|
||||
}
|
||||
|
||||
getter(name: string): GetterFn { return <GetterFn>new Function('o', 'return o.' + name + ';'); }
|
||||
|
||||
setter(name: string): SetterFn {
|
||||
return <SetterFn>new Function('o', 'v', 'return o.' + name + ' = v;');
|
||||
}
|
||||
|
||||
method(name: string): MethodFn {
|
||||
let functionBody = `if (!o.${name}) throw new Error('"${name}" is undefined');
|
||||
return o.${name}.apply(o, args);`;
|
||||
return <MethodFn>new Function('o', 'args', functionBody);
|
||||
}
|
||||
}
|
162
modules/angular2/src/core/reflection/reflector.ts
Normal file
162
modules/angular2/src/core/reflection/reflector.ts
Normal file
@ -0,0 +1,162 @@
|
||||
import {Type, isPresent, stringify, BaseException} from 'angular2/src/facade/lang';
|
||||
import {
|
||||
List,
|
||||
ListWrapper,
|
||||
Map,
|
||||
MapWrapper,
|
||||
Set,
|
||||
SetWrapper,
|
||||
StringMap,
|
||||
StringMapWrapper
|
||||
} from 'angular2/src/facade/collection';
|
||||
import {SetterFn, GetterFn, MethodFn} from './types';
|
||||
import {PlatformReflectionCapabilities} from './platform_reflection_capabilities';
|
||||
export {SetterFn, GetterFn, MethodFn} from './types';
|
||||
export {PlatformReflectionCapabilities} from './platform_reflection_capabilities';
|
||||
|
||||
export class ReflectionInfo {
|
||||
_factory: Function;
|
||||
_annotations: List<any>;
|
||||
_parameters: List<List<any>>;
|
||||
_interfaces: List<any>;
|
||||
|
||||
constructor(annotations?: List<any>, parameters?: List<List<any>>, factory?: Function,
|
||||
interfaces?: List<any>) {
|
||||
this._annotations = annotations;
|
||||
this._parameters = parameters;
|
||||
this._factory = factory;
|
||||
this._interfaces = interfaces;
|
||||
}
|
||||
}
|
||||
|
||||
export class Reflector {
|
||||
_injectableInfo: Map<any, ReflectionInfo>;
|
||||
_getters: Map<string, GetterFn>;
|
||||
_setters: Map<string, SetterFn>;
|
||||
_methods: Map<string, MethodFn>;
|
||||
_usedKeys: Set<any>;
|
||||
reflectionCapabilities: PlatformReflectionCapabilities;
|
||||
|
||||
constructor(reflectionCapabilities: PlatformReflectionCapabilities) {
|
||||
this._injectableInfo = new Map();
|
||||
this._getters = new Map();
|
||||
this._setters = new Map();
|
||||
this._methods = new Map();
|
||||
this._usedKeys = null;
|
||||
this.reflectionCapabilities = reflectionCapabilities;
|
||||
}
|
||||
|
||||
isReflectionEnabled(): boolean { return this.reflectionCapabilities.isReflectionEnabled(); }
|
||||
|
||||
/**
|
||||
* Causes `this` reflector to track keys used to access
|
||||
* {@link ReflectionInfo} objects.
|
||||
*/
|
||||
trackUsage(): void { this._usedKeys = new Set(); }
|
||||
|
||||
/**
|
||||
* Lists types for which reflection information was not requested since
|
||||
* {@link #trackUsage} was called. This list could later be audited as
|
||||
* potential dead code.
|
||||
*/
|
||||
listUnusedKeys(): List<any> {
|
||||
if (this._usedKeys == null) {
|
||||
throw new BaseException('Usage tracking is disabled');
|
||||
}
|
||||
var allTypes = MapWrapper.keys(this._injectableInfo);
|
||||
return ListWrapper.filter(allTypes, (key) => { return !SetWrapper.has(this._usedKeys, key); });
|
||||
}
|
||||
|
||||
registerFunction(func: Function, funcInfo: ReflectionInfo): void {
|
||||
this._injectableInfo.set(func, funcInfo);
|
||||
}
|
||||
|
||||
registerType(type: Type, typeInfo: ReflectionInfo): void {
|
||||
this._injectableInfo.set(type, typeInfo);
|
||||
}
|
||||
|
||||
registerGetters(getters: StringMap<string, GetterFn>): void {
|
||||
_mergeMaps(this._getters, getters);
|
||||
}
|
||||
|
||||
registerSetters(setters: StringMap<string, SetterFn>): void {
|
||||
_mergeMaps(this._setters, setters);
|
||||
}
|
||||
|
||||
registerMethods(methods: StringMap<string, MethodFn>): void {
|
||||
_mergeMaps(this._methods, methods);
|
||||
}
|
||||
|
||||
factory(type: Type): Function {
|
||||
if (this._containsReflectionInfo(type)) {
|
||||
var res = this._getReflectionInfo(type)._factory;
|
||||
return isPresent(res) ? res : null;
|
||||
} else {
|
||||
return this.reflectionCapabilities.factory(type);
|
||||
}
|
||||
}
|
||||
|
||||
parameters(typeOrFunc: /*Type*/ any): List<any> {
|
||||
if (this._injectableInfo.has(typeOrFunc)) {
|
||||
var res = this._getReflectionInfo(typeOrFunc)._parameters;
|
||||
return isPresent(res) ? res : [];
|
||||
} else {
|
||||
return this.reflectionCapabilities.parameters(typeOrFunc);
|
||||
}
|
||||
}
|
||||
|
||||
annotations(typeOrFunc: /*Type*/ any): List<any> {
|
||||
if (this._injectableInfo.has(typeOrFunc)) {
|
||||
var res = this._getReflectionInfo(typeOrFunc)._annotations;
|
||||
return isPresent(res) ? res : [];
|
||||
} else {
|
||||
return this.reflectionCapabilities.annotations(typeOrFunc);
|
||||
}
|
||||
}
|
||||
|
||||
interfaces(type: Type): List<any> {
|
||||
if (this._injectableInfo.has(type)) {
|
||||
var res = this._getReflectionInfo(type)._interfaces;
|
||||
return isPresent(res) ? res : [];
|
||||
} else {
|
||||
return this.reflectionCapabilities.interfaces(type);
|
||||
}
|
||||
}
|
||||
|
||||
getter(name: string): GetterFn {
|
||||
if (this._getters.has(name)) {
|
||||
return this._getters.get(name);
|
||||
} else {
|
||||
return this.reflectionCapabilities.getter(name);
|
||||
}
|
||||
}
|
||||
|
||||
setter(name: string): SetterFn {
|
||||
if (this._setters.has(name)) {
|
||||
return this._setters.get(name);
|
||||
} else {
|
||||
return this.reflectionCapabilities.setter(name);
|
||||
}
|
||||
}
|
||||
|
||||
method(name: string): MethodFn {
|
||||
if (this._methods.has(name)) {
|
||||
return this._methods.get(name);
|
||||
} else {
|
||||
return this.reflectionCapabilities.method(name);
|
||||
}
|
||||
}
|
||||
|
||||
_getReflectionInfo(typeOrFunc) {
|
||||
if (isPresent(this._usedKeys)) {
|
||||
this._usedKeys.add(typeOrFunc);
|
||||
}
|
||||
return this._injectableInfo.get(typeOrFunc);
|
||||
}
|
||||
|
||||
_containsReflectionInfo(typeOrFunc) { return this._injectableInfo.has(typeOrFunc); }
|
||||
}
|
||||
|
||||
function _mergeMaps(target: Map<any, any>, config: StringMap<string, Function>): void {
|
||||
StringMapWrapper.forEach(config, (v, k) => target.set(k, v));
|
||||
}
|
5
modules/angular2/src/core/reflection/types.dart
Normal file
5
modules/angular2/src/core/reflection/types.dart
Normal file
@ -0,0 +1,5 @@
|
||||
library reflection.types;
|
||||
|
||||
typedef SetterFn(Object obj, value);
|
||||
typedef GetterFn(Object obj);
|
||||
typedef MethodFn(Object obj, List args);
|
6
modules/angular2/src/core/reflection/types.ts
Normal file
6
modules/angular2/src/core/reflection/types.ts
Normal file
@ -0,0 +1,6 @@
|
||||
import {Type} from 'angular2/src/facade/lang';
|
||||
import {List} from 'angular2/src/facade/collection';
|
||||
|
||||
export type SetterFn = (obj: any, value: any) => void;
|
||||
export type GetterFn = (obj: any) => any;
|
||||
export type MethodFn = (obj: any, args: List<any>) => any;
|
Reference in New Issue
Block a user