Java Code Examples for org.springframework.data.convert.EntityInstantiator#createInstance()
The following examples show how to use
org.springframework.data.convert.EntityInstantiator#createInstance() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MappingSolrConverter.java From dubbox with Apache License 2.0 | 5 votes |
private <S extends Object> S read(final SolrPersistentEntity<S> entity, final Map<String, ?> source, Object parent) { ParameterValueProvider<SolrPersistentProperty> parameterValueProvider = getParameterValueProvider(entity, source, parent); EntityInstantiator instantiator = instantiators.getInstantiatorFor(entity); final S instance = instantiator.createInstance(entity, parameterValueProvider); final PersistentPropertyAccessor accessor = new ConvertingPropertyAccessor(entity.getPropertyAccessor(instance), getConversionService()); entity.doWithProperties(new PropertyHandler<SolrPersistentProperty>() { @Override public void doWithPersistentProperty(SolrPersistentProperty persistentProperty) { if (entity.isConstructorArgument(persistentProperty)) { return; } Object o = getValue(persistentProperty, source, instance); if (o != null) { accessor.setProperty(persistentProperty, o); } } }); return instance; }
Example 2
Source File: MappingVaultConverter.java From spring-vault with Apache License 2.0 | 5 votes |
private <S> S read(VaultPersistentEntity<S> entity, SecretDocument source) { ParameterValueProvider<VaultPersistentProperty> provider = getParameterProvider(entity, source); EntityInstantiator instantiator = this.instantiators.getInstantiatorFor(entity); S instance = instantiator.createInstance(entity, provider); PersistentPropertyAccessor accessor = new ConvertingPropertyAccessor(entity.getPropertyAccessor(instance), this.conversionService); VaultPersistentProperty idProperty = entity.getIdProperty(); SecretDocumentAccessor documentAccessor = new SecretDocumentAccessor(source); // make sure id property is set before all other properties Object idValue; if (entity.requiresPropertyPopulation()) { if (idProperty != null && !entity.isConstructorArgument(idProperty) && documentAccessor.hasValue(idProperty)) { idValue = readIdValue(idProperty, documentAccessor); accessor.setProperty(idProperty, idValue); } VaultPropertyValueProvider valueProvider = new VaultPropertyValueProvider(documentAccessor); readProperties(entity, accessor, idProperty, documentAccessor, valueProvider); } return instance; }
Example 3
Source File: DefaultDatastoreEntityConverter.java From spring-cloud-gcp with Apache License 2.0 | 4 votes |
@Override @SuppressWarnings("unchecked") public <R> R read(Class<R> aClass, BaseEntity entity) { if (entity == null) { return null; } DatastorePersistentEntity<R> ostensiblePersistentEntity = (DatastorePersistentEntity<R>) this.mappingContext .getPersistentEntity(aClass); if (ostensiblePersistentEntity == null) { throw new DatastoreDataException("Unable to convert Datastore Entity to " + aClass); } EntityPropertyValueProvider propertyValueProvider = new EntityPropertyValueProvider(entity, this.conversions); DatastorePersistentEntity<?> persistentEntity = getDiscriminationPersistentEntity(ostensiblePersistentEntity, propertyValueProvider); ParameterValueProvider<DatastorePersistentProperty> parameterValueProvider = new PersistentEntityParameterValueProvider<>(persistentEntity, propertyValueProvider, null); EntityInstantiator instantiator = this.instantiators.getInstantiatorFor(persistentEntity); Object instance; try { instance = instantiator.createInstance(persistentEntity, parameterValueProvider); PersistentPropertyAccessor accessor = persistentEntity.getPropertyAccessor(instance); persistentEntity.doWithColumnBackedProperties((datastorePersistentProperty) -> { // if a property is a constructor argument, it was already computed on instantiation if (!persistentEntity.isConstructorArgument(datastorePersistentProperty)) { Object value = propertyValueProvider .getPropertyValue(datastorePersistentProperty); accessor.setProperty(datastorePersistentProperty, value); } }); } catch (DatastoreDataException ex) { throw new DatastoreDataException("Unable to read " + persistentEntity.getName() + " entity", ex); } return (R) instance; }
Example 4
Source File: ConverterAwareMappingSpannerEntityReader.java From spring-cloud-gcp with Apache License 2.0 | 4 votes |
/** * Reads a single POJO from a Cloud Spanner row. * @param type the type of POJO * @param source the Cloud Spanner row * @param includeColumns the columns to read. If null then all columns will be read. * @param allowMissingColumns if true, then properties with no corresponding column are * not mapped. If false, then an exception is thrown. * @param <R> the type of the POJO. * @return the POJO */ @SuppressWarnings("unchecked") public <R> R read(Class<R> type, Struct source, Set<String> includeColumns, boolean allowMissingColumns) { boolean readAllColumns = includeColumns == null; SpannerPersistentEntity<R> persistentEntity = (SpannerPersistentEntity<R>) this.spannerMappingContext.getPersistentEntity(type); StructAccessor structAccessor = new StructAccessor(source); StructPropertyValueProvider propertyValueProvider = new StructPropertyValueProvider( structAccessor, this.converter, this, allowMissingColumns); PreferredConstructor<?, SpannerPersistentProperty> persistenceConstructor = persistentEntity .getPersistenceConstructor(); // @formatter:off ParameterValueProvider<SpannerPersistentProperty> parameterValueProvider = new PersistentEntityParameterValueProvider<>(persistentEntity, propertyValueProvider, null); // @formatter:on EntityInstantiator instantiator = this.instantiators.getInstantiatorFor(persistentEntity); R instance = instantiator.createInstance(persistentEntity, parameterValueProvider); PersistentPropertyAccessor accessor = persistentEntity.getPropertyAccessor(instance); persistentEntity.doWithProperties( (PropertyHandler<SpannerPersistentProperty>) (spannerPersistentProperty) -> { if (spannerPersistentProperty.isEmbedded()) { accessor.setProperty(spannerPersistentProperty, read(spannerPersistentProperty.getType(), source, includeColumns, allowMissingColumns)); } else { if (!shouldSkipProperty(structAccessor, spannerPersistentProperty, includeColumns, readAllColumns, allowMissingColumns, persistenceConstructor)) { Object value = propertyValueProvider .getPropertyValue(spannerPersistentProperty); accessor.setProperty(spannerPersistentProperty, value); } } }); return instance; }
Example 5
Source File: TypicalEntityReaderBenchmark.java From spring-data-dev-tools with Apache License 2.0 | 4 votes |
/** * Typical code used to read entities in {@link org.springframework.data.convert.EntityReader}. * * @param data * @param classToRead * @param queryCustomConversions {@literal true} to call {@link CustomConversions#hasCustomReadTarget(Class, Class)}. * @return */ @SuppressWarnings("unchecked") private Object read(Map<String, Object> data, Class<?> classToRead, boolean queryCustomConversions) { if (queryCustomConversions) { customConversions.hasCustomReadTarget(Map.class, classToRead); } MyPersistentEntity<?> persistentEntity = context.getRequiredPersistentEntity(classToRead); PreferredConstructor<?, MyPersistentProperty> constructor = persistentEntity.getPersistenceConstructor(); ParameterValueProvider<MyPersistentProperty> provider = constructor.isNoArgConstructor() // ? NONE // : new ParameterValueProvider<MyPersistentProperty>() { @Override public <T> T getParameterValue(Parameter<T, MyPersistentProperty> parameter) { return (T) getValue(data, parameter.getName(), parameter.getType().getType(), queryCustomConversions); } }; EntityInstantiator instantiator = instantiators.getInstantiatorFor(persistentEntity); Object instance = instantiator.createInstance(persistentEntity, provider); if (!persistentEntity.requiresPropertyPopulation()) { return instance; } PropertyValueProvider<MyPersistentProperty> valueProvider = new PropertyValueProvider<MyPersistentProperty>() { @Override public <T> T getPropertyValue(MyPersistentProperty property) { return (T) getValue(data, property.getName(), property.getType(), queryCustomConversions); } }; PersistentPropertyAccessor<?> accessor = new ConvertingPropertyAccessor<>( persistentEntity.getPropertyAccessor(instance), conversionService); readProperties(data, persistentEntity, valueProvider, accessor); return accessor.getBean(); }
Example 6
Source File: MappingCrateConverter.java From spring-data-crate with Apache License 2.0 | 4 votes |
/** * Read an incoming {@link CrateDocument} into the target entity. * * @param entity the target entity. * @param source the document to convert. * @param parent an optional parent object. * @param <R> the entity type. * @return the converted entity. */ @SuppressWarnings("unchecked") protected <R> R read(final CratePersistentEntity<R> entity, final CrateDocument source, final Object parent) { final DefaultSpELExpressionEvaluator evaluator = new DefaultSpELExpressionEvaluator(source, spELContext); ParameterValueProvider<CratePersistentProperty> provider = getParameterProvider(entity, source, evaluator, parent); EntityInstantiator instantiator = instantiators.getInstantiatorFor(entity); R instance = instantiator.createInstance(entity, provider); final PersistentPropertyAccessor propertyAccessor = entity.getPropertyAccessor(instance); final R result = (R)propertyAccessor.getBean(); final CratePersistentProperty idProperty = entity.getIdProperty(); final CratePersistentProperty versionProperty = entity.getVersionProperty(); if(entity.hasIdProperty()) { Object idValue = getValueInternal(idProperty, source, result); propertyAccessor.setProperty(idProperty, idValue); } if(entity.hasVersionProperty()) { Object versionValue = getValueInternal(versionProperty, source, result); propertyAccessor.setProperty(versionProperty, versionValue); } for(CratePersistentProperty property : entity.getPersistentProperties()) { // skip id and version properties as they may have potentially been set above. if((idProperty != null && idProperty.equals(property)) || (versionProperty != null && versionProperty.equals(property))) { continue; } if(!source.containsKey(property.getFieldName()) || entity.isConstructorArgument(property)) { continue; } propertyAccessor.setProperty(property, getValueInternal(property, source, result)); } entity.doWithAssociations(new AssociationHandler<CratePersistentProperty>() { @Override public void doWithAssociation(final Association<CratePersistentProperty> association) { CratePersistentProperty inverseProp = association.getInverse(); Object obj = getValueInternal(inverseProp, source, result); propertyAccessor.setProperty(inverseProp, obj); } }); return result; }