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$resolveTypes$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: 23
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: 26
87%
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: * 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.serialization.traversal;
19:
20: import cz.cvut.kbss.jsonld.common.BeanAnnotationProcessor;
21: import cz.cvut.kbss.jsonld.common.BeanClassProcessor;
22: import cz.cvut.kbss.jsonld.exception.BeanProcessingException;
23: import cz.cvut.kbss.jsonld.exception.MissingTypeInfoException;
24:
25: import java.lang.reflect.Field;
26: import java.util.Collection;
27: import java.util.Optional;
28: import java.util.Set;
29:
30: /**
31: * Determines the set of types an instance possesses.
32: */
33: class InstanceTypeResolver {
34:
35: /**
36: * Resolves all the types the instance belongs to.
37: * <p>
38: * This includes:
39: * <ul>
40: * <li>{@link cz.cvut.kbss.jopa.model.annotations.OWLClass} values declared on the argument's class
41: * and any of its ancestors.</li>
42: * <li>Value of types field in the instance.</li>
43: * </ul>
44: *
45: * @param instance The instance whose types should be resolved
46: * @return Set of types of the instance
47: */
48: Set<String> resolveTypes(Object instance) {
49:• assert instance != null;
50: final Set<String> declaredTypes = BeanAnnotationProcessor.getOwlClasses(instance);
51: final Optional<Field> typesField = BeanAnnotationProcessor.getTypesField(instance.getClass());
52: typesField.ifPresent(f -> {
53:• if (!Collection.class.isAssignableFrom(f.getType())) {
54: throw new BeanProcessingException("@Types field in object " + instance + " must be a collection.");
55: }
56: final Collection<?> runtimeTypes = (Collection<?>) BeanClassProcessor.getFieldValue(f, instance);
57:• if (runtimeTypes != null) {
58: runtimeTypes.forEach(t -> declaredTypes.add(t.toString()));
59: }
60: });
61:• if (declaredTypes.isEmpty()) {
62: throw new MissingTypeInfoException("No type info found on instance " + instance +
63: ". Either annotate the class with @OWLClass or provide a non-empty @Types field. " +
64: "If it is a literal, make sure that the property referencing is not an @OWLObjectProperty");
65: }
66: return declaredTypes;
67: }
68: }