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%
getLiteralFieldType(AbstractAttribute)
M: 0 C: 10
100%
M: 0 C: 2
100%
M: 0 C: 2
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%
validateAttributeDoesNotMapRdfType(AbstractAttribute)
M: 0 C: 22
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
validateAttributeMapping(AbstractAttribute)
M: 0 C: 15
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 6
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%
validateLexicalFormAttribute(AbstractAttribute)
M: 0 C: 21
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
validatePropertiesField(Field)
M: 4 C: 56
93%
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: 22
65%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 1 C: 4
80%
M: 0 C: 1
100%
validateSimpleLiteralField(AbstractAttribute)
M: 0 C: 32
100%
M: 0 C: 8
100%
M: 0 C: 5
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
validateTypesField(Field)
M: 0 C: 51
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) 2022 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.jopa.model.metamodel;
14:
15: import cz.cvut.kbss.jopa.exception.InvalidFieldMappingException;
16: import cz.cvut.kbss.jopa.model.annotations.Types;
17: import cz.cvut.kbss.jopa.utils.IdentifierTransformer;
18: import cz.cvut.kbss.jopa.vocabulary.RDF;
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: import static cz.cvut.kbss.jopa.model.PersistenceProperties.IDENTIFIER_TYPES;
27:
28: /**
29: * Verifies that a field's mapping metadata and declaration are valid.
30: */
31: class FieldMappingValidator {
32:
33: void validatePropertiesField(Field field) {
34:• assert field != null;
35:• if (!Map.class.isAssignableFrom(field.getType())) {
36: throw new InvalidFieldMappingException(
37: "Expected @Properties field to be a map, but it is a " + field.getType());
38: }
39:• if (isRawType(field.getGenericType())) {
40: throw new InvalidFieldMappingException("@Properties field cannot be a raw map.");
41: }
42: final PropertiesParametersResolver parametersResolver = new PropertiesParametersResolver(field);
43:• if (!isValidIdentifierType(parametersResolver.getKeyType())) {
44: throw new InvalidFieldMappingException(
45: "@Properties key type is not a valid identifier type. Expected one of " + IDENTIFIER_TYPES);
46: }
47: validatePropertiesValueType(parametersResolver.getValueType());
48: }
49:
50: private static boolean isRawType(Type type) {
51:• return !(type instanceof ParameterizedType);
52: }
53:
54: private static void validatePropertiesValueType(Type type) {
55:• if (isRawType(type)) {
56: throw new InvalidFieldMappingException(
57: "Expected a java.util.Set as value parameter of the @Properties map, but got " + type);
58: }
59:• if (!((ParameterizedType) type).getRawType().equals(Set.class)) {
60: throw new InvalidFieldMappingException(
61: "Expected a java.util.Set as value parameter of the @Properties map, but got " + type);
62: }
63: }
64:
65: void validateTypesField(Field field) {
66:• if (!Set.class.isAssignableFrom(field.getType())) {
67: throw new InvalidFieldMappingException("Expected @Types field to be a set, but it is a " + field.getType());
68: }
69:• if (isRawType(field.getGenericType())) {
70: throw new InvalidFieldMappingException("@Types field cannot be a raw set.");
71: }
72: final ParameterizedType typeSpec = (ParameterizedType) field.getGenericType();
73:• if (!isValidIdentifierType(typeSpec.getActualTypeArguments()[0])) {
74: throw new InvalidFieldMappingException(
75: "@Types field value is not a valid identifier type. Expected one of " + IDENTIFIER_TYPES);
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:
89: void validateAttributeMapping(AbstractAttribute<?, ?> attribute) {
90: validateAttributeDoesNotMapRdfType(attribute);
91:• if (attribute.getPersistentAttributeType() == Attribute.PersistentAttributeType.DATA
92:• || attribute.getPersistentAttributeType() == Attribute.PersistentAttributeType.ANNOTATION) {
93: validateLexicalFormAttribute(attribute);
94: validateSimpleLiteralField(attribute);
95: }
96: }
97:
98: private static void validateAttributeDoesNotMapRdfType(AbstractAttribute<?, ?> att) {
99:• if (RDF.TYPE.equals(att.getIRI().toString())) {
100: throw new InvalidFieldMappingException(
101: att + " - cannot use rdf:type for property mapping. Use a Set field annotated with " + Types.class.getSimpleName());
102: }
103: }
104:
105: private static void validateLexicalFormAttribute(AbstractAttribute<?, ?> attribute) {
106:• if (attribute.isLexicalForm() && !String.class.isAssignableFrom(getLiteralFieldType(attribute))) {
107: throw new InvalidFieldMappingException(
108: attribute + " - lexicalForm mapping can be used only on fields of type String.");
109: }
110: }
111:
112: private static void validateSimpleLiteralField(AbstractAttribute<?, ?> attribute) {
113: final Class<?> fieldType = getLiteralFieldType(attribute);
114:• if (attribute.isSimpleLiteral() && (!String.class.isAssignableFrom(fieldType) && !Enum.class.isAssignableFrom(
115:• fieldType) && !attribute.getConverter().supportsAxiomValueType(String.class))) {
116: throw new InvalidFieldMappingException(
117: attribute + " - simpleLiteral mapping can only be used on fields of type String or Enum or using a suitable converter.");
118: }
119: }
120:
121: private static Class<?> getLiteralFieldType(AbstractAttribute<?, ?> attribute) {
122:• return attribute.isCollection() ? ((AbstractPluralAttribute) attribute).getBindableJavaType() : attribute.getJavaType();
123: }
124: }