Skip to contentMethod: getFieldForProperty(String)
1: /*
2: * JB4JSON-LD
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.jsonld.deserialization;
19:
20: import cz.cvut.kbss.jopa.datatype.exception.DatatypeMappingException;
21: import cz.cvut.kbss.jsonld.common.BeanAnnotationProcessor;
22: import cz.cvut.kbss.jsonld.common.BeanClassProcessor;
23: import cz.cvut.kbss.jsonld.exception.JsonLdDeserializationException;
24:
25: import java.lang.reflect.Field;
26: import java.util.Collection;
27: import java.util.Map;
28:
29: class SingularObjectContext<T> extends InstanceContext<T> {
30:
31: private final Map<String, Field> fieldMap;
32:
33: SingularObjectContext(T instance, Map<String, Field> fieldMap, Map<String, Object> knownInstances) {
34: super(instance, knownInstances);
35: this.fieldMap = fieldMap;
36: }
37:
38: @Override
39: Field getFieldForProperty(String property) {
40: return fieldMap.get(property);
41: }
42:
43: @Override
44: void setFieldValue(Field field, Object value) {
45: assert !(instance instanceof Collection);
46: try {
47: final Object toSet = resolveAssignableValue(field.getType(), value);
48: BeanClassProcessor.setFieldValue(field, instance, toSet);
49: } catch (DatatypeMappingException e) {
50: throw new JsonLdDeserializationException("Type mismatch when setting value " + value + " on field " + field + ".", e);
51: }
52: }
53:
54: @Override
55: boolean isPropertyMapped(String property) {
56: return fieldMap.containsKey(property) || hasPropertiesField();
57: }
58:
59: @Override
60: boolean supports(String property) {
61: if (!isPropertyMapped(property)) {
62: return false;
63: }
64: return !fieldMap.containsKey(property) || BeanAnnotationProcessor.isWriteable(fieldMap.get(property));
65: }
66: }