Skip to content

Commit c0bd1b4

Browse files
Add ability to get a bean by its class
1 parent 0c01658 commit c0bd1b4

File tree

5 files changed

+45
-5
lines changed

5 files changed

+45
-5
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export default class MapTrackingDuplicates<K, V>
2+
{
3+
private _map = new Map<K, V | null>();
4+
5+
public has(key: K): boolean {
6+
const value = this._map.get(key);
7+
return value !== undefined && value !== null;
8+
}
9+
10+
public hasDuplicates(key: K): boolean {
11+
return this._map.get(key) === null;
12+
}
13+
14+
public get(key: K): V | undefined {
15+
const value = this._map.get(key);
16+
return value === null ? undefined : value;
17+
}
18+
19+
public set(key: K, value: V): void {
20+
this._map.set(key, this._map.has(key) ? null : value);
21+
}
22+
}

env-model-generator/src/model.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
StringConstant,
1717
Symbol,
1818
} from "./javaCode";
19+
import MapTrackingDuplicates from "./mapTrackingDuplicates";
1920
import NameMap from "./nameMap";
2021
import { makeIdentifier, upperInitialChar } from "./utils";
2122

@@ -237,6 +238,20 @@ export class Bean {
237238
public static getAliases(): Iterable<[ string, string ]> {
238239
return Bean.aliases;
239240
}
241+
242+
private static beanIdByClass = new MapTrackingDuplicates<string, string>();
243+
244+
public static registerIdForClass(qualifiedClassName: string, beanId: string) {
245+
Bean.beanIdByClass.set(qualifiedClassName, beanId);
246+
}
247+
248+
public static tryGetIdByClass(qualifiedClassName: string): string | undefined {
249+
return Bean.beanIdByClass.get(qualifiedClassName);
250+
}
251+
252+
public static hasMultipleBeansForClass(qualifiedClassName: string): boolean {
253+
return Bean.beanIdByClass.hasDuplicates(qualifiedClassName);
254+
}
240255
}
241256

242257
export abstract class Value {

env-model-generator/src/parseJson.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export async function collectBeansFromConfigFile(filePath: string): Promise<stri
3434
// Create a bean model for the bean
3535
const bean = { qualifiedClassName: qualifiedClassName, bean: annotationConfig.beans[qualifiedClassName] };
3636
beansById.set(id, bean);
37+
model.Bean.registerIdForClass(bean.qualifiedClassName, id);
3738
}
3839
// TODO: Return paths to XML config files specified in code
3940
return [];

env-model-generator/src/parseSpringXml.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as pathUtils from "path";
44
import * as spring from "../xmlns/www.springframework.org/schema/beans";
55
import { ConfigParseError } from "./configParseError";
66
import * as model from "./model";
7-
import { getBean } from "./parserDriver";
7+
import { getBeanById } from "./parserDriver";
88
import { makeIdentifier, parseXmlString } from "./utils";
99

1010
const beansById = new Map<string, spring.BeanType>();
@@ -58,6 +58,8 @@ export async function collectBeansFromConfigFile(filePath: string): Promise<stri
5858
model.Bean.addAlias(ids[i], id);
5959
// Record the actual bean
6060
beansById.set(id, bean);
61+
if (bean.class)
62+
model.Bean.registerIdForClass(bean.class, id);
6163
}
6264
}
6365
return imports;
@@ -92,14 +94,14 @@ export function parseBean(id: string): model.Bean {
9294
function parseXmlBean(id: string | undefined, bean: spring.BeanType): model.Bean {
9395
let parent;
9496
if (bean.parent) {
95-
parent = getBean(bean.parent);
97+
parent = getBeanById(bean.parent);
9698
if (parent === undefined)
9799
throw new ConfigParseError(
98100
`Couldn't find bean '${bean.parent}' specified as parent of ${id === undefined ? "anonymous bean" : `'${id}'`}`);
99101
}
100102
let factoryBean;
101103
if (bean.factoryBean) {
102-
factoryBean = getBean(bean.factoryBean);
104+
factoryBean = getBeanById(bean.factoryBean);
103105
if (factoryBean === undefined)
104106
throw new ConfigParseError(
105107
`Couldn't find bean '${bean.factoryBean}' specified as factoryBean of ${id === undefined ? "anonymous bean" : `'${id}'`}`);

env-model-generator/src/parserDriver.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ async function collectBeansFromConfigFiles(filePaths: string[]): Promise<void> {
3232
}
3333
}
3434

35-
export function getBean(beanId: string): model.Bean | undefined {
35+
export function getBeanById(beanId: string): model.Bean | undefined {
3636
const beanModel = model.Bean.tryGet(beanId);
3737
if (beanModel !== undefined)
3838
return beanModel;
@@ -45,6 +45,6 @@ export function getBean(beanId: string): model.Bean | undefined {
4545
return xmlParser.parseBean(beanId);
4646
const aliasedBeanId = model.Bean.getAlias(beanId);
4747
if (aliasedBeanId !== undefined)
48-
return getBean(aliasedBeanId);
48+
return getBeanById(aliasedBeanId);
4949
return undefined;
5050
}

0 commit comments

Comments
 (0)