DefaultCreator implements Function<FactoryContext<R>, F> { @SuppressWarnings("unchecked") @Override public F apply(FactoryContext<R> context) { FactoryMetadata<R, F> factoryMetadata = FactoryMetadataManager.getMetadata(clazz); F result = factoryMetadata.newInstance(); result.internal().visitAttributesFlat((attributeMetadata, attribute) -> { if (attribute instanceof FactoryPolymorphicAttribute){ FactoryPolymorphicAttribute factoryPolymorphicAttribute = (FactoryPolymorphicAttribute) attribute; if (attributeMetadata.liveObjectClass!=null){ for (FactoryBase<?, R> factory : context.getListFromLiveObjectClass(attributeMetadata.liveObjectClass,clazz)) { factoryPolymorphicAttribute.set(factory); break; } } return; } if (attribute instanceof FactoryPolymorphicListAttribute){ FactoryPolymorphicListAttribute factoryPolymorphicListAttribute = (FactoryPolymorphicListAttribute) attribute; if (attributeMetadata.liveObjectClass!=null){ for (FactoryBase<?, R> factory : context.getListFromLiveObjectClass(attributeMetadata.liveObjectClass,clazz)) { factoryPolymorphicListAttribute.add(factory); break; } } return; } if (attribute instanceof FactoryBaseAttribute ){ FactoryBaseAttribute factoryBaseAttribute = (FactoryBaseAttribute) attribute; Class<? extends FactoryBase<?,?>> clazz = attributeMetadata.referenceClass; validateAttributeClass(attributeMetadata.attributeVariableName, clazz); FactoryBase<?,?> factoryBase = context.getUnchecked(clazz); factoryBaseAttribute.set(factoryBase); if (factoryBaseAttribute.internal_required()){ if (factoryBaseAttribute.get()==null){ throw new IllegalStateException( "\nbuilder missing Factory: "+attributeMetadata.liveObjectClass+"\n"+ "required in: "+clazz+"\n"+ "from attribute: "+attributeMetadata.attributeVariableName ); } } return; } if (attribute instanceof FactoryListAttribute){ FactoryListAttribute factoryListAttribute = (FactoryListAttribute) attribute; Class<? extends FactoryBase> clazz = attributeMetadata.referenceClass; validateAttributeClass(attributeMetadata.attributeVariableName, clazz); factoryListAttribute.set(context.getList(clazz)); } }); return result; } DefaultCreator(Class<F> clazz); @SuppressWarnings("unchecked") @Override F apply(FactoryContext<R> context);  }
@Test public void test_missing_factory(){ Assertions.assertThrows(IllegalStateException.class, () -> { DefaultCreator<ExampleFactoryANotNullable, ExampleFactoryANotNullable> defaultCreator = new DefaultCreator<>(ExampleFactoryANotNullable.class); defaultCreator.apply(new FactoryContext<>()); }); }
@Test public void test_missing_factory_nullable(){ DefaultCreator<ExampleFactoryNullableRef,ExampleFactoryNullableRef> defaultCreator = new DefaultCreator<>(ExampleFactoryNullableRef.class); defaultCreator.apply(new FactoryContext<>()); }