Skip to content

Method: validatePropertiesField(Field)

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 ParameterizedType typeSpec = (ParameterizedType) field.getGenericType();
48:• assert typeSpec.getActualTypeArguments().length == 2;
49:• if (!isValidIdentifierType(typeSpec.getActualTypeArguments()[0])) {
50: throw new InvalidFieldMappingException(
51: "@Properties key type is not a valid identifier type. Expected one of " + VALID_ID_TYPES);
52: }
53: validatePropertiesValueType(typeSpec.getActualTypeArguments()[1]);
54: }
55:
56: private boolean isRawType(Type type) {
57: return !(type instanceof ParameterizedType);
58: }
59:
60: private void validatePropertiesValueType(Type type) {
61: if (isRawType(type)) {
62: throw new InvalidFieldMappingException(
63: "Expected a java.util.Set as value parameter of the @Properties map, but got " + type);
64: }
65: if (!((ParameterizedType) type).getRawType().equals(Set.class)) {
66: throw new InvalidFieldMappingException(
67: "Expected a java.util.Set as value parameter of the @Properties map, but got " + type);
68: }
69: }
70:
71: void validateTypesField(Field field) {
72: if (!Set.class.isAssignableFrom(field.getType())) {
73: throw new InvalidFieldMappingException("Expected @Types field to be a set, but it is a " + field.getType());
74: }
75: if (isRawType(field.getGenericType())) {
76: throw new InvalidFieldMappingException("@Types field cannot be a raw set.");
77: }
78: final ParameterizedType typeSpec = (ParameterizedType) field.getGenericType();
79: if (!isValidIdentifierType(typeSpec.getActualTypeArguments()[0])) {
80: throw new InvalidFieldMappingException(
81: "@Types field value is not a valid identifier type. Expected one of " + VALID_ID_TYPES);
82: }
83: }
84:
85: boolean isValidIdentifierType(Type type) {
86: return VALID_ID_TYPES.contains(type);
87: }
88: }