Skip to content

Package: EntityReferenceFactory

EntityReferenceFactory

nameinstructionbranchcomplexitylinemethod
EntityReferenceFactory(MetamodelImpl, UnitOfWork)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
createReferenceProxy(LoadingParameters)
M: 16 C: 51
76%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 2 C: 11
85%
M: 0 C: 1
100%
static {...}
M: 0 C: 1
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: package cz.cvut.kbss.jopa.oom;
2:
3: import cz.cvut.kbss.jopa.exceptions.OWLPersistenceException;
4: import cz.cvut.kbss.jopa.model.MetamodelImpl;
5: import cz.cvut.kbss.jopa.proxy.reference.EntityReferenceProxy;
6: import cz.cvut.kbss.jopa.sessions.UnitOfWork;
7: import cz.cvut.kbss.jopa.sessions.util.LoadingParameters;
8: import cz.cvut.kbss.jopa.utils.EntityPropertiesUtils;
9:
10: import java.lang.reflect.InvocationTargetException;
11:
12: class EntityReferenceFactory {
13:
14: private final MetamodelImpl metamodel;
15:
16: private final UnitOfWork uow;
17:
18: EntityReferenceFactory(MetamodelImpl metamodel, UnitOfWork uow) {
19: this.metamodel = metamodel;
20: this.uow = uow;
21: }
22:
23: /**
24: * Creates entity reference proxy with initialized identifier and associated persistence context.
25: *
26: * @param loadingParameters Parameters for creating the reference
27: * @param <T> Type of the expected instance
28: * @return Entity reference proxy
29: */
30: <T> T createReferenceProxy(LoadingParameters<T> loadingParameters) {
31:• assert loadingParameters != null;
32:
33: final Class<? extends T> referenceProxyClass = metamodel.getEntityReferenceProxy(loadingParameters.getEntityClass());
34: try {
35: final T reference = referenceProxyClass.getDeclaredConstructor().newInstance();
36:• assert reference instanceof EntityReferenceProxy<?>
37: final EntityReferenceProxy<?> referenceProxy = (EntityReferenceProxy<?>) reference;
38: referenceProxy.setIdentifier(loadingParameters.getIdentifier());
39: referenceProxy.setType((Class) loadingParameters.getEntityClass());
40: referenceProxy.setPersistenceContext(uow);
41: referenceProxy.setDescriptor(loadingParameters.getDescriptor());
42: EntityPropertiesUtils.setIdentifier(loadingParameters.getIdentifier(), reference, metamodel.entity(loadingParameters.getEntityClass()));
43: return reference;
44: } catch (InstantiationException | IllegalAccessException | NoSuchMethodException |
45: InvocationTargetException e) {
46: // We do not expect this to happen, as we generated the proxy class
47: throw new OWLPersistenceException("Unable to instantiate entity reference proxy class " + referenceProxyClass, e);
48: }
49: }
50: }