use of org.qi4j.runtime.property.PropertyModel in project qi4j-sdk by Qi4j.
the class ModuleInstance method newTransientBuilder.
// Implementation of TransientBuilderFactory
@Override
public <T> TransientBuilder<T> newTransientBuilder(Class<T> mixinType) throws NoSuchTransientException {
NullArgumentException.validateNotNull("mixinType", mixinType);
ModelModule<TransientModel> modelModule = typeLookup.lookupTransientModel(mixinType);
if (modelModule == null) {
throw new NoSuchTransientException(mixinType.getName(), name());
}
Map<AccessibleObject, Property<?>> properties = new HashMap<AccessibleObject, Property<?>>();
for (PropertyModel propertyModel : modelModule.model().state().properties()) {
Property<?> property = new PropertyInstance<Object>(propertyModel.getBuilderInfo(), propertyModel.initialValue(modelModule.module()));
properties.put(propertyModel.accessor(), property);
}
TransientStateInstance state = new TransientStateInstance(properties);
return new TransientBuilderInstance<T>(modelModule, state, UsesInstance.EMPTY_USES);
}
use of org.qi4j.runtime.property.PropertyModel in project qi4j-sdk by Qi4j.
the class CompositeAssemblyImpl method newPropertyModel.
protected PropertyModel newPropertyModel(AccessibleObject accessor, Iterable<Class<? extends Constraint<?, ?>>> constraintClasses) {
Iterable<Annotation> annotations = Annotations.findAccessorAndTypeAnnotationsIn(accessor);
boolean optional = first(filter(isType(Optional.class), annotations)) != null;
ValueConstraintsModel valueConstraintsModel = constraintsFor(annotations, GenericPropertyInfo.propertyTypeOf(accessor), ((Member) accessor).getName(), optional, constraintClasses, accessor);
ValueConstraintsInstance valueConstraintsInstance = null;
if (valueConstraintsModel.isConstrained()) {
valueConstraintsInstance = valueConstraintsModel.newInstance();
}
MetaInfo metaInfo = stateDeclarations.metaInfoFor(accessor);
Object initialValue = stateDeclarations.initialValueOf(accessor);
boolean useDefaults = metaInfo.get(UseDefaults.class) != null || stateDeclarations.useDefaults(accessor);
boolean immutable = this.immutable || metaInfo.get(Immutable.class) != null;
PropertyModel propertyModel = new PropertyModel(accessor, immutable, useDefaults, valueConstraintsInstance, metaInfo, initialValue);
return propertyModel;
}
use of org.qi4j.runtime.property.PropertyModel in project qi4j-sdk by Qi4j.
the class ModuleUnitOfWork method newEntityBuilderWithState.
@Override
public <T> EntityBuilder<T> newEntityBuilderWithState(Class<T> type, String identity, Function<PropertyDescriptor, Object> propertyFunction, Function<AssociationDescriptor, EntityReference> associationFunction, Function<AssociationDescriptor, Iterable<EntityReference>> manyAssociationFunction, Function<AssociationDescriptor, Map<String, EntityReference>> namedAssociationFunction) throws EntityTypeNotFoundException {
NullArgumentException.validateNotNull("propertyFunction", propertyFunction);
NullArgumentException.validateNotNull("associationFunction", associationFunction);
NullArgumentException.validateNotNull("manyAssociationFunction", manyAssociationFunction);
NullArgumentException.validateNotNull("namedAssociationFunction", namedAssociationFunction);
ModelModule<EntityModel> model = moduleInstance.typeLookup().lookupEntityModel(type);
if (model == null) {
throw new EntityTypeNotFoundException(type.getName());
}
EntityStore entityStore = model.module().entityStore();
StateResolver stateResolver = new FunctionStateResolver(propertyFunction, associationFunction, manyAssociationFunction, namedAssociationFunction);
if (identity == null) {
// Use identity from StateResolver if available
PropertyModel identityModel = model.model().state().findPropertyModelByQualifiedName(IDENTITY_STATE_NAME);
identity = (String) stateResolver.getPropertyState(identityModel);
if (identity == null) {
// Generate identity
IdentityGenerator idGen = model.module().identityGenerator();
if (idGen == null) {
throw new NoSuchServiceException(IdentityGenerator.class.getName(), model.module().name());
}
identity = idGen.generate(first(model.model().types()));
}
}
return new EntityBuilderInstance<>(model, this, uow.getEntityStoreUnitOfWork(entityStore, moduleInstance), identity, stateResolver);
}
use of org.qi4j.runtime.property.PropertyModel in project qi4j-sdk by Qi4j.
the class ServiceModel method newInstance.
public ServiceInstance newInstance(final ModuleInstance module) {
Object[] mixins = mixinsModel.newMixinHolder();
Map<AccessibleObject, Property<?>> properties = new HashMap<AccessibleObject, Property<?>>();
for (PropertyModel propertyModel : stateModel.properties()) {
Object initialValue = propertyModel.initialValue(module);
if (propertyModel.accessor().equals(identityMethod)) {
initialValue = identity;
}
Property property = new PropertyInstance<Object>(propertyModel, initialValue);
properties.put(propertyModel.accessor(), property);
}
TransientStateInstance state = new TransientStateInstance(properties);
ServiceInstance compositeInstance = new ServiceInstance(this, module, mixins, state);
// Instantiate all mixins
int i = 0;
UsesInstance uses = UsesInstance.EMPTY_USES.use(this);
InjectionContext injectionContext = new InjectionContext(compositeInstance, uses, state);
for (MixinModel mixinModel : mixinsModel.mixinModels()) {
mixins[i++] = mixinModel.newInstance(injectionContext);
}
return compositeInstance;
}
use of org.qi4j.runtime.property.PropertyModel in project qi4j-sdk by Qi4j.
the class EntityStateInstance method propertyFor.
@Override
public <T> Property<T> propertyFor(AccessibleObject accessor) throws IllegalArgumentException {
Map<AccessibleObject, Object> state = state();
Property<T> property = (Property<T>) state.get(accessor);
if (property == null) {
PropertyModel entityPropertyModel = stateModel.propertyModelFor(accessor);
if (entityPropertyModel == null) {
throw new IllegalArgumentException("No such property:" + accessor);
}
property = new EntityPropertyInstance<T>(entityState instanceof BuilderEntityState ? entityPropertyModel.getBuilderInfo() : entityPropertyModel, entityState);
state.put(accessor, property);
}
return property;
}
Aggregations