Skip to content

Commit c3703e8

Browse files
Added fallback bean for when no implementation is found for a bean type
1 parent a754219 commit c3703e8

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

env-model-generator/src/model.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,37 @@ export class Bean implements IBean {
182182
}
183183
}
184184

185+
export class UndefinedBean implements IBean {
186+
private readonly typeName: string;
187+
public readonly name: string;
188+
public readonly IsAnonymous = true;
189+
190+
public constructor(identifier: string, typeName: string) {
191+
this.name = identifier;
192+
this.typeName = typeName;
193+
Repository.registerBean(this);
194+
Repository.registerIdForClass(this.typeName, this.name);
195+
}
196+
197+
public get getter(): string {
198+
return `get${upperInitialChar(this.name)}`;
199+
}
200+
201+
public get javaMembers(): Member[] {
202+
return [
203+
new Method(
204+
`public static ${this.typeName} ${this.getter}()`,
205+
[
206+
new Return(new FnCall("org.cprover.CProver.nondetWithNull", [])),
207+
],
208+
[]),
209+
new BlankLine(),
210+
];
211+
}
212+
213+
public readonly exceptions: string[] = [];
214+
}
215+
185216
export namespace Repository {
186217
const beans = new NameMap<IBean>();
187218

env-model-generator/src/parseJson.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,18 @@ function parseJsonBean(id: string, namedMethod: NamedBeanDefinitionMethod): mode
180180
}
181181

182182
function getBeanForClass(className: string, userString: string): model.BeanRefValue {
183-
const fieldBeanId = model.Repository.tryGetIdByClass(className);
183+
let fieldBeanId = model.Repository.tryGetIdByClass(className);
184184
if (fieldBeanId === undefined)
185-
throw new ConfigParseError(
186-
`${userString} depends on a class '${className}' `
187-
+ `that has ${model.Repository.hasMultipleBeansForClass(className) ? "multiple" : "no"} implementations`);
185+
{
186+
if (model.Repository.hasMultipleBeansForClass(className))
187+
throw new ConfigParseError(`${userString} depends on a class '${className}' that has multiple implementations`);
188+
// Return a default, fall-back bean implementation
189+
const id = lowerInitialChar(<string>className.split(".").pop());
190+
console.warn(`${userString} depends on a class '${className}' that has no implementations, `
191+
+ `creating a new bean called '${id}' using non-deterministic initialisation`);
192+
checkBeanId(id, `Auto-generated component to satisfy dependency for '${className}'`);
193+
const bean = new model.UndefinedBean(id, className);
194+
fieldBeanId = bean.name;
195+
}
188196
return new model.BeanRefValue(fieldBeanId);
189197
}

0 commit comments

Comments
 (0)