11import { ComponentUtil } from "./ComponentDecorator" ;
22import { TypeUtils } from "../helpers/TypeUtils" ;
33import { InjectionError } from "../errors/InjectionError" ;
4+ import { DecoratorUsageError } from "../errors/DecoratorUsageError" ;
5+ import { DecoratorType , DecoratorUtil } from "../helpers/DecoratorUtils" ;
46
57const INJECT_DECORATOR_TOKEN = Symbol ( 'injector_decorator_token' ) ;
68
@@ -24,15 +26,22 @@ export class InjectionData {
2426}
2527
2628export function Inject ( dependencyToken ?: Symbol ) {
27- return function ( target : any , fieldName : string ) {
29+ return function ( ...args ) {
30+ if ( ! DecoratorUtil . isType ( DecoratorType . PROPERTY , args ) ) {
31+ let subject = DecoratorUtil . getSubjectName ( args ) ;
32+ throw new DecoratorUsageError ( `@Inject can be set only on properties of a @Component class! (${ subject } )` ) ;
33+ }
34+ let target = args [ 0 ] ;
35+ let fieldName = args [ 1 ] ;
2836 let token = dependencyToken ;
2937 let type = ( < any > Reflect ) . getMetadata ( 'design:type' , target , fieldName ) ;
3038 if ( ! token ) {
3139 // fallback to field type
3240 if ( ComponentUtil . isComponent ( type ) ) {
3341 token = ComponentUtil . getClassToken ( type ) ;
3442 } else {
35- throw new InjectionError ( `Cannot inject dependency which is not a @Component! (${ type . name } )` ) ;
43+ let sub = DecoratorUtil . getSubjectName ( args ) ;
44+ throw new InjectionError ( `Cannot inject dependency (${ type . name } ) which is not a @Component! (${ sub } )` ) ;
3645 }
3746 }
3847 // NOTE assumption: if type not declared or any then type is Object and isArray is false
@@ -41,12 +50,23 @@ export function Inject(dependencyToken?: Symbol) {
4150 } ;
4251}
4352
44- export function Autowire ( ) {
45- return Inject ( ) ;
53+ export function Autowired ( ) {
54+ return function ( ...args ) {
55+ if ( ! DecoratorUtil . isType ( DecoratorType . PROPERTY , args ) ) {
56+ let subj = DecoratorUtil . getSubjectName ( args ) ;
57+ throw new DecoratorUsageError ( `@Autowired can be set only on properties of a @Component class! (${ subj } )` ) ;
58+ }
59+ return Inject ( ) ( ...args ) ;
60+ } ;
4661}
4762
4863export function Value ( preopertyKey ) {
4964 return function ( target : any , fieldName : string ) {
65+ let args = Array . prototype . slice . call ( arguments ) ;
66+ if ( ! DecoratorUtil . isType ( DecoratorType . PROPERTY , args ) ) {
67+ let subject = DecoratorUtil . getSubjectName ( args ) ;
68+ throw new DecoratorUsageError ( `@Value can be set only on properties of a @Component class! (${ subject } )` ) ;
69+ }
5070 InjectUtil . initIfDoesntExist ( target ) . properties . set ( fieldName , preopertyKey ) ;
5171 } ;
5272}
0 commit comments