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