use of org.qi4j.api.composite.CompositeInstance in project qi4j-sdk by Qi4j.
the class ValueSerializerAdapter method serializeValueComposite.
private void serializeValueComposite(Options options, Object object, OutputType output, boolean rootPass) throws Exception {
CompositeInstance valueInstance = Qi4j.FUNCTION_COMPOSITE_INSTANCE_OF.map((ValueComposite) object);
ValueDescriptor descriptor = (ValueDescriptor) valueInstance.descriptor();
AssociationStateHolder state = (AssociationStateHolder) valueInstance.state();
onObjectStart(output);
if (options.getBoolean(Options.INCLUDE_TYPE_INFO) && !rootPass) {
onFieldStart(output, "_type");
onValueStart(output);
onValue(output, first(descriptor.valueType().types()).getName());
onValueEnd(output);
onFieldEnd(output);
}
for (PropertyDescriptor persistentProperty : descriptor.valueType().properties()) {
Property<?> property = state.propertyFor(persistentProperty.accessor());
onFieldStart(output, persistentProperty.qualifiedName().name());
onValueStart(output);
doSerialize(options, property.get(), output, false);
onValueEnd(output);
onFieldEnd(output);
}
for (AssociationDescriptor associationDescriptor : descriptor.valueType().associations()) {
Association<?> association = state.associationFor(associationDescriptor.accessor());
Object instance = association.get();
onFieldStart(output, associationDescriptor.qualifiedName().name());
onValueStart(output);
if (instance == null) {
onValue(output, null);
} else {
onValue(output, ((Identity) instance).identity().get());
}
onValueEnd(output);
onFieldEnd(output);
}
for (AssociationDescriptor associationDescriptor : descriptor.valueType().manyAssociations()) {
ManyAssociation<?> manyAssociation = state.manyAssociationFor(associationDescriptor.accessor());
onFieldStart(output, associationDescriptor.qualifiedName().name());
onValueStart(output);
onArrayStart(output);
for (Object instance : manyAssociation) {
onValueStart(output);
onValue(output, ((Identity) instance).identity().get());
onValueEnd(output);
}
onArrayEnd(output);
onValueEnd(output);
onFieldEnd(output);
}
for (AssociationDescriptor associationDescriptor : descriptor.valueType().namedAssociations()) {
NamedAssociation<?> namedAssociation = state.namedAssociationFor(associationDescriptor.accessor());
onFieldStart(output, associationDescriptor.qualifiedName().name());
onValueStart(output);
onObjectStart(output);
for (String name : namedAssociation) {
onFieldStart(output, name);
onValueStart(output);
onValue(output, ((Identity) namedAssociation.get(name)).identity().get());
onValueEnd(output);
onFieldEnd(output);
}
onObjectEnd(output);
onValueEnd(output);
onFieldEnd(output);
}
onObjectEnd(output);
}
use of org.qi4j.api.composite.CompositeInstance in project qi4j-sdk by Qi4j.
the class MixinModel method newInstance.
public Object newInstance(InjectionContext injectionContext) {
Object mixin;
CompositeInstance compositeInstance = injectionContext.compositeInstance();
mixin = constructorsModel.newInstance(injectionContext);
if (FragmentClassLoader.isGenerated(instantiationClass)) {
try {
instantiationClass.getDeclaredField("_instance").set(mixin, injectionContext.compositeInstance());
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
}
injectedFieldsModel.inject(injectionContext, mixin);
injectedMethodsModel.inject(injectionContext, mixin);
if (mixin instanceof Initializable) {
try {
((Initializable) mixin).initialize();
} catch (InitializationException e) {
List<Class<?>> compositeType = toList(compositeInstance.types());
String message = "Unable to initialize " + mixinClass + " in composite " + compositeType;
throw new ConstructionException(message, e);
}
}
return mixin;
}
use of org.qi4j.api.composite.CompositeInstance in project qi4j-sdk by Qi4j.
the class ManyAssociationFunction method map.
@Override
public ManyAssociation<T> map(Composite entity) {
try {
Object target = entity;
if (traversedAssociation != null) {
target = traversedAssociation.map(entity).get();
}
if (traversedManyAssociation != null) {
throw new IllegalArgumentException("Cannot traverse ManyAssociations");
}
CompositeInstance handler = (CompositeInstance) Proxy.getInvocationHandler(target);
return ((AssociationStateHolder) handler.state()).manyAssociationFor(accessor);
} catch (IllegalArgumentException e) {
throw e;
} catch (Throwable e) {
throw new IllegalArgumentException(e);
}
}
use of org.qi4j.api.composite.CompositeInstance in project qi4j-sdk by Qi4j.
the class TransientBuilderInstance method newInstance.
@Override
public T newInstance() throws ConstructionException {
// Set correct info's (immutable) on the state
for (PropertyDescriptor propertyDescriptor : model.model().state().properties()) {
((PropertyInstance<Object>) state.propertyFor(propertyDescriptor.accessor())).setPropertyInfo((PropertyInfo) propertyDescriptor);
}
model.model().checkConstraints(state);
CompositeInstance compositeInstance = model.model().newInstance(model.module(), uses, state);
return compositeInstance.<T>proxy();
}
use of org.qi4j.api.composite.CompositeInstance in project qi4j-sdk by Qi4j.
the class NamedAssociationFunction method map.
@Override
public NamedAssociation<T> map(Composite entity) {
try {
Object target = entity;
if (traversedAssociation != null) {
target = traversedAssociation.map(entity).get();
}
if (traversedManyAssociation != null) {
throw new IllegalArgumentException("Cannot traverse ManyAssociations");
}
if (traversedNamedAssociation != null) {
throw new IllegalArgumentException("Cannot traverse NamedAssociations");
}
CompositeInstance handler = (CompositeInstance) Proxy.getInvocationHandler(target);
return ((AssociationStateHolder) handler.state()).namedAssociationFor(accessor);
} catch (IllegalArgumentException e) {
throw e;
} catch (Throwable e) {
throw new IllegalArgumentException(e);
}
}
Aggregations