Skip to content

Package: FieldMappingValidator

FieldMappingValidator

nameinstructionbranchcomplexitylinemethod
FieldMappingValidator()
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%
initIdClasses()
M: 0 C: 15
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
isRawType(Type)
M: 0 C: 7
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
isValidIdentifierType(Type)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
static {...}
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%
validatePropertiesField(Field)
M: 4 C: 58
94%
M: 1 C: 7
88%
M: 1 C: 4
80%
M: 0 C: 11
100%
M: 0 C: 1
100%
validatePropertiesValueType(Type)
M: 12 C: 23
66%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 1 C: 4
80%
M: 0 C: 1
100%
validateTypesField(Field)
M: 0 C: 52
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 8
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.jopa.model.metamodel;
16:
17: import cz.cvut.kbss.jopa.exception.InvalidFieldMappingException;
18:
19: import java.lang.reflect.*;
20: import java.lang.reflect.Type;
21: import java.net.URI;
22: import java.util.*;
23:
24: /**
25: * Verifies that a field's mapping metadata and declaration are valid.
26: */
27: class FieldMappingValidator {
28:
29: private static final Set<Type> VALID_ID_TYPES = initIdClasses();
30:
31: private static Set<Type> initIdClasses() {
32: final Set<Type> set = new HashSet<>(4);
33: set.add(String.class);
34: set.add(URI.class);
35: return set;
36: }
37:
38: void validatePropertiesField(Field field) {
39:• assert field != null;
40:• if (!Map.class.isAssignableFrom(field.getType())) {
41: throw new InvalidFieldMappingException(
42: "Expected @Properties field to be a map, but it is a " + field.getType());
43: }
44:• if (isRawType(field.getGenericType())) {
45: throw new InvalidFieldMappingException("@Properties field cannot be a raw map.");
46: }
47: final PropertiesParametersResolver parametersResolver = new PropertiesParametersResolver(field);
48:• if (!isValidIdentifierType(parametersResolver.getKeyType())) {
49: throw new InvalidFieldMappingException(
50: "@Properties key type is not a valid identifier type. Expected one of " + VALID_ID_TYPES);
51: }
52: validatePropertiesValueType(parametersResolver.getValueType());
53: }
54:
55: private boolean isRawType(Type type) {
56:• return !(type instanceof ParameterizedType);
57: }
58:
59: private void validatePropertiesValueType(Type type) {
60:• if (isRawType(type)) {
61: throw new InvalidFieldMappingException(
62: "Expected a java.util.Set as value parameter of the @Properties map, but got " + type);
63: }
64:• if (!((ParameterizedType) type).getRawType().equals(Set.class)) {
65: throw new InvalidFieldMappingException(
66: "Expected a java.util.Set as value parameter of the @Properties map, but got " + type);
67: }
68: }
69:
70: void validateTypesField(Field field) {
71:• if (!Set.class.isAssignableFrom(field.getType())) {
72: throw new InvalidFieldMappingException("Expected @Types field to be a set, but it is a " + field.getType());
73: }
74:• if (isRawType(field.getGenericType())) {
75: throw new InvalidFieldMappingException("@Types field cannot be a raw set.");
76: }
77: final ParameterizedType typeSpec = (ParameterizedType) field.getGenericType();
78:• if (!isValidIdentifierType(typeSpec.getActualTypeArguments()[0])) {
79: throw new InvalidFieldMappingException(
80: "@Types field value is not a valid identifier type. Expected one of " + VALID_ID_TYPES);
81: }
82: }
83:
84: boolean isValidIdentifierType(Type type) {
85: return VALID_ID_TYPES.contains(type);
86: }
87: }