Skip to content

Package: InstanceTypeResolver

InstanceTypeResolver

nameinstructionbranchcomplexitylinemethod
InstanceTypeResolver()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$null$0(Set, Object)
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%
lambda$resolveTypes$1(Object, Set, Field)
M: 0 C: 31
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
resolveTypes(Object)
M: 4 C: 34
89%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 0 C: 7
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%

Coverage

1: /**
2: * Copyright (C) 2017 Czech Technical University in Prague
3: * <p>
4: * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
5: * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
6: * version.
7: * <p>
8: * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
10: * details. You should have received a copy of the GNU General Public License along with this program. If not, see
11: * <http://www.gnu.org/licenses/>.
12: */
13: package cz.cvut.kbss.jsonld.serialization.traversal;
14:
15: import cz.cvut.kbss.jsonld.common.BeanAnnotationProcessor;
16: import cz.cvut.kbss.jsonld.common.BeanClassProcessor;
17: import cz.cvut.kbss.jsonld.exception.BeanProcessingException;
18: import cz.cvut.kbss.jsonld.exception.MissingTypeInfoException;
19:
20: import java.lang.reflect.Field;
21: import java.util.Collection;
22: import java.util.Optional;
23: import java.util.Set;
24:
25: /**
26: * Determines the set of types an instance possesses.
27: */
28: class InstanceTypeResolver {
29:
30: /**
31: * Resolves all the types the instance belongs to.
32: * <p>
33: * This includes:
34: * <ul>
35: * <li>{@link cz.cvut.kbss.jopa.model.annotations.OWLClass} values declared on the argument's class
36: * and any of its ancestors.</li>
37: * <li>Value of types field in the instance.</li>
38: * </ul>
39: *
40: * @param instance The instance whose types should be resolved
41: * @return Set of types of the instance
42: */
43: Set<String> resolveTypes(Object instance) {
44:• assert instance != null;
45: final Set<String> declaredTypes = BeanAnnotationProcessor.getOwlClasses(instance);
46: final Optional<Field> typesField = BeanAnnotationProcessor.getTypesField(instance.getClass());
47: typesField.ifPresent(f -> {
48:• if (!Collection.class.isAssignableFrom(f.getType())) {
49: throw new BeanProcessingException("@Types field in object " + instance + " must be a collection.");
50: }
51: final Collection<?> runtimeTypes = (Collection<?>) BeanClassProcessor.getFieldValue(f, instance);
52:• if (runtimeTypes != null) {
53: runtimeTypes.forEach(t -> declaredTypes.add(t.toString()));
54: }
55: });
56:• if (declaredTypes.isEmpty()) {
57: throw new MissingTypeInfoException("No type info found on instance " + instance +
58: ". Either annotate the class with @OWLClass or provide a non-empty @Types field.");
59: }
60: return declaredTypes;
61: }
62: }