Skip to content

Commit 3fbb788

Browse files
Collect all beans created using @bean
1 parent ea4aaa2 commit 3fbb788

File tree

1 file changed

+41
-11
lines changed

1 file changed

+41
-11
lines changed

env-model-generator/src/parseJson.ts

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,14 @@ interface NamedComponent {
99
component: jsonConfig.Component;
1010
}
1111

12+
interface NamedBeanDefinitionMethod {
13+
className: string;
14+
name: string;
15+
method: jsonConfig.BeanDefinitionMethod;
16+
}
17+
1218
const componentsById = new Map<string, NamedComponent>();
19+
const beansById = new Map<string, NamedBeanDefinitionMethod>();
1320

1421
export async function collectBeansFromConfigFile(filePath: string): Promise<string[]> {
1522
let json: string;
@@ -22,24 +29,47 @@ export async function collectBeansFromConfigFile(filePath: string): Promise<stri
2229
for (const qualifiedClassName in annotationConfig.components) {
2330
if (!annotationConfig.components.hasOwnProperty(qualifiedClassName))
2431
continue;
25-
// Create bean ID
2632
// TODO: Handle name value on @Component annotation
2733
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+
}
3857
}
3958
// TODO: Return paths to XML config files specified in code
4059
return [];
4160
}
4261

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+
4373
export function beanWithIdExists(id: string): boolean {
4474
return componentsById.has(id);
4575
}

0 commit comments

Comments
 (0)