@@ -9,7 +9,14 @@ interface NamedComponent {
9
9
component : jsonConfig . Component ;
10
10
}
11
11
12
+ interface NamedBeanDefinitionMethod {
13
+ className : string ;
14
+ name : string ;
15
+ method : jsonConfig . BeanDefinitionMethod ;
16
+ }
17
+
12
18
const componentsById = new Map < string , NamedComponent > ( ) ;
19
+ const beansById = new Map < string , NamedBeanDefinitionMethod > ( ) ;
13
20
14
21
export async function collectBeansFromConfigFile ( filePath : string ) : Promise < string [ ] > {
15
22
let json : string ;
@@ -22,24 +29,47 @@ export async function collectBeansFromConfigFile(filePath: string): Promise<stri
22
29
for ( const qualifiedClassName in annotationConfig . components ) {
23
30
if ( ! annotationConfig . components . hasOwnProperty ( qualifiedClassName ) )
24
31
continue ;
25
- // Create bean ID
26
32
// TODO: Handle name value on @Component annotation
27
33
const id = lowerInitialChar ( < string > qualifiedClassName . split ( "." ) . pop ( ) ) ;
28
- // Check for duplicate ids from other beans
29
- if ( componentsById . has ( id ) )
30
- throw new ConfigParseError ( `Found multiple components with id '${ id } '` ) ;
31
- const aliasedBeanId = model . Bean . getAlias ( id ) ;
32
- if ( aliasedBeanId !== undefined )
33
- throw new ConfigParseError ( `Found a bean with id same as an alias for '${ aliasedBeanId } '` ) ;
34
- // Create a bean model for the bean
35
- const component = { qualifiedClassName : qualifiedClassName , component : annotationConfig . components [ qualifiedClassName ] } ;
36
- componentsById . set ( id , component ) ;
37
- model . Bean . registerIdForClass ( component . qualifiedClassName , id ) ;
34
+ checkBeanId ( id , `@Component annotation on class '${ qualifiedClassName } '` ) ;
35
+ componentsById . set ( id , { qualifiedClassName : qualifiedClassName , component : annotationConfig . components [ qualifiedClassName ] } ) ;
36
+ model . Bean . registerIdForClass ( qualifiedClassName , id ) ;
37
+ }
38
+ for ( const configurationClassName in annotationConfig . configurations ) {
39
+ if ( ! annotationConfig . configurations . hasOwnProperty ( configurationClassName ) )
40
+ continue ;
41
+ const beanDefinitionMethods = annotationConfig . configurations [ configurationClassName ] . beanDefinitionMethods ;
42
+ for ( const beanDefinitionMethodName in beanDefinitionMethods ) {
43
+ if ( ! beanDefinitionMethods . hasOwnProperty ( beanDefinitionMethodName ) )
44
+ continue ;
45
+ const beanDefinitionMethod = beanDefinitionMethods [ beanDefinitionMethodName ] ;
46
+ let id = beanDefinitionMethodName ;
47
+ if ( beanDefinitionMethod . specifiedNames !== undefined && beanDefinitionMethod . specifiedNames . length !== 0 ) {
48
+ id = beanDefinitionMethod . specifiedNames [ 0 ] ;
49
+ // Add aliases
50
+ for ( let i = 1 ; i < beanDefinitionMethod . specifiedNames . length ; ++ i )
51
+ model . Bean . addAlias ( beanDefinitionMethod . specifiedNames [ i ] , id ) ;
52
+ }
53
+ checkBeanId ( id , `@Bean annotation on '${ configurationClassName } .${ beanDefinitionMethodName } '` ) ;
54
+ beansById . set ( id , { className : configurationClassName , name : configurationClassName , method : beanDefinitionMethod } ) ;
55
+ model . Bean . registerIdForClass ( beanDefinitionMethod . type , id ) ;
56
+ }
38
57
}
39
58
// TODO: Return paths to XML config files specified in code
40
59
return [ ] ;
41
60
}
42
61
62
+ // Check for duplicate ids from other beans
63
+ function checkBeanId ( id : string , location : string ) : void {
64
+ if ( componentsById . has ( id ) )
65
+ throw new ConfigParseError ( `${ location } : There is an existing component with id '${ id } '` ) ;
66
+ if ( beansById . has ( id ) )
67
+ throw new ConfigParseError ( `${ location } : There is an existing bean with id '${ id } '` ) ;
68
+ const aliasedBeanId = model . Bean . getAlias ( id ) ;
69
+ if ( aliasedBeanId !== undefined )
70
+ throw new ConfigParseError ( `${ location } : There is an existing alias with identifier '${ aliasedBeanId } '` ) ;
71
+ }
72
+
43
73
export function beanWithIdExists ( id : string ) : boolean {
44
74
return componentsById . has ( id ) ;
45
75
}
0 commit comments