Skip to content

Method: FieldMappingValidator()

1: /**
2: * Copyright (C) 2019 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.OWLAnnotationProperty;
17: import cz.cvut.kbss.jopa.model.annotations.OWLDataProperty;
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: import static cz.cvut.kbss.jopa.utils.Constants.SUPPORTED_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 " +
46: SUPPORTED_IDENTIFIER_TYPES);
47: }
48: validatePropertiesValueType(parametersResolver.getValueType());
49: }
50:
51: private static boolean isRawType(Type type) {
52: return !(type instanceof ParameterizedType);
53: }
54:
55: private static void validatePropertiesValueType(Type type) {
56: if (isRawType(type)) {
57: throw new InvalidFieldMappingException(
58: "Expected a java.util.Set as value parameter of the @Properties map, but got " + type);
59: }
60: if (!((ParameterizedType) type).getRawType().equals(Set.class)) {
61: throw new InvalidFieldMappingException(
62: "Expected a java.util.Set as value parameter of the @Properties map, but got " + type);
63: }
64: }
65:
66: void validateTypesField(Field field) {
67: if (!Set.class.isAssignableFrom(field.getType())) {
68: throw new InvalidFieldMappingException("Expected @Types field to be a set, but it is a " + field.getType());
69: }
70: if (isRawType(field.getGenericType())) {
71: throw new InvalidFieldMappingException("@Types field cannot be a raw set.");
72: }
73: final ParameterizedType typeSpec = (ParameterizedType) field.getGenericType();
74: if (!isValidIdentifierType(typeSpec.getActualTypeArguments()[0])) {
75: throw new InvalidFieldMappingException(
76: "@Types field value is not a valid identifier type. Expected one of " + SUPPORTED_IDENTIFIER_TYPES);
77: }
78: }
79:
80: void validateIdentifierType(Type type) {
81: if (!isValidIdentifierType(type)) {
82: throw new InvalidFieldMappingException(type + " is not a valid identifier type.");
83: }
84: }
85:
86: boolean isValidIdentifierType(Type type) {
87: return type instanceof Class && IdentifierTransformer.isValidIdentifierType((Class<?>) type);
88: }
89:
90: void validateAnnotationPropertyField(Field field, OWLAnnotationProperty config) {
91: assert field != null;
92: assert config != null;
93: validateLexicalFormField(field, config.lexicalForm());
94: validateSimpleLiteralField(field, config.simpleLiteral());
95: }
96:
97: void validateDataPropertyField(Field field, OWLDataProperty config) {
98: assert field != null;
99: assert config != null;
100: validateLexicalFormField(field, config.lexicalForm());
101: validateSimpleLiteralField(field, config.simpleLiteral());
102: }
103:
104: private void validateLexicalFormField(Field field, boolean lexicalForm) {
105: if (lexicalForm && !String.class.isAssignableFrom(field.getType())) {
106: throw new InvalidFieldMappingException("lexicalForm mapping can be used only on fields of type String.");
107: }
108: }
109:
110: private void validateSimpleLiteralField(Field field, boolean simpleLiteral) {
111: if (simpleLiteral && !String.class.isAssignableFrom(field.getType())) {
112: throw new InvalidFieldMappingException("simpleLiteral mapping can be used only on fields of type String.");
113: }
114: }
115: }