use of org.qi4j.api.property.Property in project qi4j-sdk by Qi4j.
the class PropertyMapper method toJavaProperties.
/**
* Create Properties object which is backed by the given Composite.
*
* @param composite the instance
*
* @return properties instance
*/
public static Properties toJavaProperties(final Composite composite) {
return new Properties() {
private static final long serialVersionUID = 3550125427530538865L;
@Override
public Object get(Object o) {
try {
Method propertyMethod = composite.getClass().getMethod(o.toString());
Property<?> property = (Property<?>) propertyMethod.invoke(composite);
return property.get();
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
return null;
}
}
@Override
public Object put(Object o, Object o1) {
Object oldValue = get(o);
try {
Method propertyMethod = composite.getClass().getMethod(o.toString(), Object.class);
propertyMethod.invoke(composite, o1);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
return oldValue;
}
};
}
use of org.qi4j.api.property.Property in project qi4j-sdk by Qi4j.
the class PropertyMapper method map.
/**
* Populate the Composite with properties from the given properties object.
*
* @param props properties object
* @param composite the composite instance
*
* @throws IllegalArgumentException if properties could not be transferred to composite
*/
public static void map(Properties props, Composite composite) throws IllegalArgumentException {
for (Map.Entry<Object, Object> objectObjectEntry : props.entrySet()) {
try {
String methodName = objectObjectEntry.getKey().toString();
Method propertyMethod = composite.getClass().getInterfaces()[0].getMethod(methodName);
propertyMethod.setAccessible(true);
Object value = objectObjectEntry.getValue();
Type propertyType = GenericPropertyInfo.propertyTypeOf(propertyMethod);
value = mapToType(composite, propertyType, value.toString());
@SuppressWarnings("unchecked") Property<Object> property = (Property<Object>) propertyMethod.invoke(composite);
property.set(value);
} catch (NoSuchMethodException e) {
throw new IllegalArgumentException("Could not find any property named " + objectObjectEntry.getKey());
} catch (IllegalAccessException e) {
//noinspection ThrowableInstanceNeverThrown
throw new IllegalArgumentException("Could not populate property named " + objectObjectEntry.getKey(), e);
} catch (InvocationTargetException e) {
//noinspection ThrowableInstanceNeverThrown
String message = "Could not populate property named " + objectObjectEntry.getKey();
throw new IllegalArgumentException(message, e);
}
}
}
use of org.qi4j.api.property.Property 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<>();
for (PropertyModel propertyModel : modelModule.model().state().properties()) {
Property<?> property = new PropertyInstance<>(propertyModel.getBuilderInfo(), propertyModel.initialValue(modelModule.module()));
properties.put(propertyModel.accessor(), property);
}
TransientStateInstance state = new TransientStateInstance(properties);
return new TransientBuilderInstance<>(modelModule, state, UsesInstance.EMPTY_USES);
}
use of org.qi4j.api.property.Property in project qi4j-sdk by Qi4j.
the class PropertyInstance method equals.
/**
* Perform equals with {@code o} argument.
* <p>
* The definition of equals() for the Property is that if both the state and descriptor are equal,
* then the properties are equal.
* </p>
*
* @param o The other object to compare.
* @return Returns a {@code boolean} indicator whether this object is equals the other.
*/
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Property<?> that = (Property<?>) o;
// Unwrap if needed
while (that instanceof PropertyWrapper) {
that = ((PropertyWrapper) that).next();
}
// Descriptor equality
PropertyDescriptor thatDescriptor = (PropertyDescriptor) ((PropertyInstance) that).propertyInfo();
if (!model.equals(thatDescriptor)) {
return false;
}
// State equality
T value = get();
if (value == null) {
return that.get() == null;
}
return value.equals(that.get());
}
use of org.qi4j.api.property.Property in project qi4j-sdk by Qi4j.
the class EntityStateInstance method associationFor.
@Override
@SuppressWarnings("unchecked")
public <T> Association<T> associationFor(AccessibleObject accessor) throws IllegalArgumentException {
Map<AccessibleObject, Object> state = state();
Association<T> association = (Association<T>) state.get(accessor);
if (association == null) {
final AssociationModel associationModel = stateModel.getAssociation(accessor);
association = new AssociationInstance<>(entityState instanceof BuilderEntityState ? associationModel.getBuilderInfo() : associationModel, entityFunction, new Property<EntityReference>() {
@Override
public EntityReference get() {
return entityState.associationValueOf(associationModel.qualifiedName());
}
@Override
public void set(EntityReference newValue) throws IllegalArgumentException, IllegalStateException {
entityState.setAssociationValue(associationModel.qualifiedName(), newValue);
}
});
state.put(accessor, association);
}
return association;
}
Aggregations