Skip to content

Commit f50981b

Browse files
Added parsing of @bean elements
1 parent f440e18 commit f50981b

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

env-model-generator/src/parseJson.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as fs from "fs-extra";
22
import * as jsonConfig from "./annotationConfig";
33
import { ConfigParseError } from "./configParseError";
44
import * as model from "./model";
5+
import { getBeanById } from "./parserDriver";
56
import { lowerInitialChar } from "./utils";
67

78
interface NamedComponent {
@@ -71,7 +72,7 @@ function checkBeanId(id: string, location: string): void {
7172
}
7273

7374
export function beanWithIdExists(id: string): boolean {
74-
return componentsById.has(id);
75+
return componentsById.has(id) || beansById.has(id);
7576
}
7677

7778
export function parseAllBeans(): void {
@@ -81,12 +82,21 @@ export function parseAllBeans(): void {
8182
// We don't store the results of this parsing, it is stored in internal data structures which do enough for us
8283
parseJsonComponent(beanId, component);
8384
}
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+
}
8491
}
8592

8693
export function parseBean(id: string): model.Bean {
8794
const component = componentsById.get(id);
8895
if (component !== undefined)
8996
return parseJsonComponent(id, component);
97+
const bean = beansById.get(id);
98+
if (bean !== undefined)
99+
return parseJsonBean(id, bean);
90100
throw new Error("Called parseBean on a bean that did not exist");
91101
}
92102

@@ -105,6 +115,25 @@ function parseJsonComponent(id: string, namedBean: NamedComponent): model.Bean {
105115
return new model.Bean(undefined, id, namedBean.qualifiedClassName, false, [], properties);
106116
}
107117

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+
108137
function getBeanForClass(className: string, userString: string): model.BeanRefValue {
109138
const fieldBeanId = model.Bean.tryGetIdByClass(className);
110139
if (fieldBeanId === undefined)

0 commit comments

Comments
 (0)