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%
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) 2016 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: // TODO Add handling of @Properties
37: return fieldMap.get(property);
38: }
39:
40: @Override
41: void setFieldValue(Field field, Object value) {
42:• assert !(instance instanceof Collection);
43:• if (!field.getType().isAssignableFrom(value.getClass())) {
44: boolean success = trySettingReferenceToKnownInstance(field, value);
45:• if (success) {
46: return;
47: }
48: success = tryTypeTransformation(field, value);
49:• if (success) {
50: return;
51: }
52: throw valueTypeMismatch(value, field);
53: }
54: BeanClassProcessor.setFieldValue(field, instance, value);
55: }
56:
57: private JsonLdDeserializationException valueTypeMismatch(Object value, Field field) {
58: return new JsonLdDeserializationException(
59: "Type mismatch. Cannot set value " + value + " of type " + value.getClass() + " on field " + field);
60: }
61:
62: private boolean trySettingReferenceToKnownInstance(Field field, Object value) {
63:• if (!knownInstances.containsKey(value.toString())) {
64: return false;
65: }
66: final Object knownInstance = knownInstances.get(value.toString());
67:• if (!field.getType().isAssignableFrom(knownInstance.getClass())) {
68: // Throw the exception right here so that it contains info about the known instance's type
69: throw valueTypeMismatch(knownInstance, field);
70: }
71: BeanClassProcessor.setFieldValue(field, instance, knownInstance);
72: return true;
73: }
74:
75: private boolean tryTypeTransformation(Field field, Object value) {
76: final Class<?> targetType = field.getType();
77: final Object transformedValue = DataTypeTransformer.transformValue(value, targetType);
78:• if (transformedValue != null) {
79: BeanClassProcessor.setFieldValue(field, instance, transformedValue);
80: return true;
81: }
82: return false;
83: }
84: }