Skip to content

Package: SingularObjectContext

SingularObjectContext

nameinstructionbranchcomplexitylinemethod
SingularObjectContext(Object, Map, Map)
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
getFieldForProperty(String)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
isPropertyMapped(String)
M: 0 C: 12
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
setFieldValue(Field, Object)
M: 4 C: 38
90%
M: 1 C: 7
88%
M: 1 C: 4
80%
M: 0 C: 11
100%
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%
trySettingReferenceToKnownInstance(Field, Object)
M: 0 C: 32
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
tryTypeTransformation(Field, Object)
M: 0 C: 18
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
valueTypeMismatch(Object, Field)
M: 0 C: 21
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%

Coverage

1: /**
2: * Copyright (C) 2017 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jsonld.deserialization;
16:
17: import cz.cvut.kbss.jsonld.common.BeanClassProcessor;
18: import cz.cvut.kbss.jsonld.deserialization.util.DataTypeTransformer;
19: import cz.cvut.kbss.jsonld.exception.JsonLdDeserializationException;
20:
21: import java.lang.reflect.Field;
22: import java.util.Collection;
23: import java.util.Map;
24:
25: class SingularObjectContext<T> extends InstanceContext<T> {
26:
27: private final Map<String, Field> fieldMap;
28:
29: SingularObjectContext(T instance, Map<String, Field> fieldMap, Map<String, Object> knownInstances) {
30: super(instance, knownInstances);
31: this.fieldMap = fieldMap;
32: }
33:
34: @Override
35: Field getFieldForProperty(String property) {
36: return fieldMap.get(property);
37: }
38:
39: @Override
40: void setFieldValue(Field field, Object value) {
41:• assert !(instance instanceof Collection);
42:• if (!field.getType().isAssignableFrom(value.getClass())) {
43: boolean success = trySettingReferenceToKnownInstance(field, value);
44:• if (success) {
45: return;
46: }
47: success = tryTypeTransformation(field, value);
48:• if (success) {
49: return;
50: }
51: throw valueTypeMismatch(value, field);
52: }
53: BeanClassProcessor.setFieldValue(field, instance, value);
54: }
55:
56: private JsonLdDeserializationException valueTypeMismatch(Object value, Field field) {
57: return new JsonLdDeserializationException(
58: "Type mismatch. Cannot set value " + value + " of type " + value.getClass() + " on field " + field);
59: }
60:
61: private boolean trySettingReferenceToKnownInstance(Field field, Object value) {
62:• if (!knownInstances.containsKey(value.toString())) {
63: return false;
64: }
65: final Object knownInstance = knownInstances.get(value.toString());
66:• if (!field.getType().isAssignableFrom(knownInstance.getClass())) {
67: // Throw the exception right here so that it contains info about the known instance's type
68: throw valueTypeMismatch(knownInstance, field);
69: }
70: BeanClassProcessor.setFieldValue(field, instance, knownInstance);
71: return true;
72: }
73:
74: private boolean tryTypeTransformation(Field field, Object value) {
75: final Class<?> targetType = field.getType();
76: final Object transformedValue = DataTypeTransformer.transformValue(value, targetType);
77:• if (transformedValue != null) {
78: BeanClassProcessor.setFieldValue(field, instance, transformedValue);
79: return true;
80: }
81: return false;
82: }
83:
84: @Override
85: boolean isPropertyMapped(String property) {
86:• return fieldMap.containsKey(property) || hasPropertiesField();
87: }
88: }