@@ -2,6 +2,7 @@ import * as fs from "fs-extra";
2
2
import * as jsonConfig from "./annotationConfig" ;
3
3
import { ConfigParseError } from "./configParseError" ;
4
4
import * as model from "./model" ;
5
+ import { getBeanById } from "./parserDriver" ;
5
6
import { lowerInitialChar } from "./utils" ;
6
7
7
8
interface NamedComponent {
@@ -71,7 +72,7 @@ function checkBeanId(id: string, location: string): void {
71
72
}
72
73
73
74
export function beanWithIdExists ( id : string ) : boolean {
74
- return componentsById . has ( id ) ;
75
+ return componentsById . has ( id ) || beansById . has ( id ) ;
75
76
}
76
77
77
78
export function parseAllBeans ( ) : void {
@@ -81,12 +82,21 @@ export function parseAllBeans(): void {
81
82
// We don't store the results of this parsing, it is stored in internal data structures which do enough for us
82
83
parseJsonComponent ( beanId , component ) ;
83
84
}
85
+ for ( const [ beanId , namedMethod ] of beansById ) {
86
+ if ( model . Bean . tryGet ( beanId ) !== undefined )
87
+ continue ;
88
+ // We don't store the results of this parsing, it is stored in internal data structures which do enough for us
89
+ parseJsonBean ( beanId , namedMethod ) ;
90
+ }
84
91
}
85
92
86
93
export function parseBean ( id : string ) : model . Bean {
87
94
const component = componentsById . get ( id ) ;
88
95
if ( component !== undefined )
89
96
return parseJsonComponent ( id , component ) ;
97
+ const bean = beansById . get ( id ) ;
98
+ if ( bean !== undefined )
99
+ return parseJsonBean ( id , bean ) ;
90
100
throw new Error ( "Called parseBean on a bean that did not exist" ) ;
91
101
}
92
102
@@ -105,6 +115,25 @@ function parseJsonComponent(id: string, namedBean: NamedComponent): model.Bean {
105
115
return new model . Bean ( undefined , id , namedBean . qualifiedClassName , false , [ ] , properties ) ;
106
116
}
107
117
118
+ function parseJsonBean ( id : string , namedMethod : NamedBeanDefinitionMethod ) : model . Bean {
119
+ const fieldBeanId = model . Bean . tryGetIdByClass ( namedMethod . className ) ;
120
+ if ( fieldBeanId === undefined )
121
+ throw new ConfigParseError (
122
+ `Factory method '${ namedMethod . name } ' is defined in a class '${ namedMethod . className } ' `
123
+ + `that has ${ model . Bean . hasMultipleBeansForClass ( namedMethod . className ) ? "multiple" : "no" } implementations` ) ;
124
+ const factoryBean = getBeanById ( fieldBeanId ) ;
125
+ if ( factoryBean === undefined )
126
+ throw new ConfigParseError (
127
+ `Couldn't find bean '${ namedMethod . className } ' that is configuration containing factory method '${ namedMethod . name } '` ) ;
128
+ const method = namedMethod . method ;
129
+ const argumentValues : model . Value [ ] = [ ] ;
130
+ for ( const parameterType of method . parameterTypes ) {
131
+ argumentValues . push ( getBeanForClass ( parameterType , `Parameter to factory method '${ namedMethod . className } .${ namedMethod . name } '` ) ) ;
132
+ }
133
+ const isLazyInit = method . scope !== undefined && method . scope !== "singleton" || method . isLazyInit === true ;
134
+ return new model . Bean ( undefined , id , { bean : factoryBean , method : namedMethod . name } , isLazyInit , argumentValues , [ ] ) ;
135
+ }
136
+
108
137
function getBeanForClass ( className : string , userString : string ) : model . BeanRefValue {
109
138
const fieldBeanId = model . Bean . tryGetIdByClass ( className ) ;
110
139
if ( fieldBeanId === undefined )
0 commit comments