Package: FieldMappingValidator
FieldMappingValidator
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
FieldMappingValidator() |
|
|
|
|
|
||||||||||||||||||||
getBindableType(AbstractAttribute) |
|
|
|
|
|
||||||||||||||||||||
isRawType(Type) |
|
|
|
|
|
||||||||||||||||||||
isValidIdentifierType(Type) |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
||||||||||||||||||||
validateAttributeDoesNotMapRdfType(AbstractAttribute) |
|
|
|
|
|
||||||||||||||||||||
validateAttributeMapping(AbstractAttribute) |
|
|
|
|
|
||||||||||||||||||||
validateIdentifierType(Type) |
|
|
|
|
|
||||||||||||||||||||
validateLexicalFormAttribute(AbstractAttribute) |
|
|
|
|
|
||||||||||||||||||||
validateObjectPropertyEnumMapping(Attribute) |
|
|
|
|
|
||||||||||||||||||||
validatePropertiesField(Field) |
|
|
|
|
|
||||||||||||||||||||
validatePropertiesValueType(Type) |
|
|
|
|
|
||||||||||||||||||||
validateSimpleLiteralField(AbstractAttribute) |
|
|
|
|
|
||||||||||||||||||||
validateTypesField(Field) |
|
|
|
|
|
Coverage
1: /*
2: * Copyright (C) 2023 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.model.annotations.EnumType;
19: import cz.cvut.kbss.jopa.model.annotations.Enumerated;
20: import cz.cvut.kbss.jopa.model.annotations.Types;
21: import cz.cvut.kbss.jopa.utils.IdentifierTransformer;
22: import cz.cvut.kbss.jopa.vocabulary.RDF;
23:
24: import java.lang.reflect.Field;
25: import java.lang.reflect.ParameterizedType;
26: import java.lang.reflect.Type;
27: import java.util.Map;
28: import java.util.Set;
29:
30: import static cz.cvut.kbss.jopa.model.PersistenceProperties.IDENTIFIER_TYPES;
31:
32: /**
33: * Verifies that a field's mapping metadata and declaration are valid.
34: */
35: class FieldMappingValidator {
36:
37: void validatePropertiesField(Field field) {
38:• assert field != null;
39:• if (!Map.class.isAssignableFrom(field.getType())) {
40: throw new InvalidFieldMappingException(
41: "Expected @Properties field to be a map, but it is a " + field.getType());
42: }
43:• if (isRawType(field.getGenericType())) {
44: throw new InvalidFieldMappingException("@Properties field cannot be a raw map.");
45: }
46: final PropertiesParametersResolver parametersResolver = new PropertiesParametersResolver(field);
47:• if (!isValidIdentifierType(parametersResolver.getKeyType())) {
48: throw new InvalidFieldMappingException(
49: "@Properties key type is not a valid identifier type. Expected one of " + IDENTIFIER_TYPES);
50: }
51: validatePropertiesValueType(parametersResolver.getValueType());
52: }
53:
54: private static boolean isRawType(Type type) {
55:• return !(type instanceof ParameterizedType);
56: }
57:
58: private static void validatePropertiesValueType(Type type) {
59:• if (isRawType(type)) {
60: throw new InvalidFieldMappingException(
61: "Expected a java.util.Set as value parameter of the @Properties map, but got " + type);
62: }
63:• if (!((ParameterizedType) type).getRawType().equals(Set.class)) {
64: throw new InvalidFieldMappingException(
65: "Expected a java.util.Set as value parameter of the @Properties map, but got " + type);
66: }
67: }
68:
69: void validateTypesField(Field field) {
70:• if (!Set.class.isAssignableFrom(field.getType())) {
71: throw new InvalidFieldMappingException("Expected @Types field to be a set, but it is a " + field.getType());
72: }
73:• if (isRawType(field.getGenericType())) {
74: throw new InvalidFieldMappingException("@Types field cannot be a raw set.");
75: }
76: final ParameterizedType typeSpec = (ParameterizedType) field.getGenericType();
77:• if (!isValidIdentifierType(typeSpec.getActualTypeArguments()[0])) {
78: throw new InvalidFieldMappingException(
79: "@Types field value is not a valid identifier type. Expected one of " + IDENTIFIER_TYPES);
80: }
81: }
82:
83: void validateIdentifierType(Type type) {
84:• if (!isValidIdentifierType(type)) {
85: throw new InvalidFieldMappingException(type + " is not a valid identifier type.");
86: }
87: }
88:
89: boolean isValidIdentifierType(Type type) {
90:• return type instanceof Class && IdentifierTransformer.isValidIdentifierType((Class<?>) type);
91: }
92:
93: void validateAttributeMapping(AbstractAttribute<?, ?> attribute) {
94: validateAttributeDoesNotMapRdfType(attribute);
95:• switch (attribute.getPersistentAttributeType()) {
96: case OBJECT:
97: validateObjectPropertyEnumMapping(attribute);
98: break;
99: case DATA: // Intentional fall-through
100: case ANNOTATION:
101: validateLexicalFormAttribute(attribute);
102: validateSimpleLiteralField(attribute);
103: break;
104: }
105: }
106:
107: private static void validateAttributeDoesNotMapRdfType(AbstractAttribute<?, ?> att) {
108:• if (RDF.TYPE.equals(att.getIRI().toString())) {
109: throw new InvalidFieldMappingException(
110: att + " - cannot use rdf:type for property mapping. Use a Set field annotated with " + Types.class.getSimpleName());
111: }
112: }
113:
114: private static void validateLexicalFormAttribute(AbstractAttribute<?, ?> attribute) {
115:• if (attribute.isLexicalForm() && !String.class.isAssignableFrom(getBindableType(attribute))) {
116: throw new InvalidFieldMappingException(
117: attribute + " - lexicalForm mapping can be used only on fields of type String.");
118: }
119: }
120:
121: private static void validateSimpleLiteralField(AbstractAttribute<?, ?> attribute) {
122: final Class<?> fieldType = getBindableType(attribute);
123:• if (attribute.isSimpleLiteral() && (!String.class.isAssignableFrom(fieldType) && !Enum.class.isAssignableFrom(
124:• fieldType) && !attribute.getConverter().supportsAxiomValueType(String.class))) {
125: throw new InvalidFieldMappingException(
126: attribute + " - simpleLiteral mapping can only be used on fields of type String or Enum or using a suitable converter.");
127: }
128: }
129:
130: private static Class<?> getBindableType(AbstractAttribute<?, ?> attribute) {
131:• return attribute.isCollection() ? ((AbstractPluralAttribute) attribute).getBindableJavaType() :
132: attribute.getJavaType();
133: }
134:
135: private static void validateObjectPropertyEnumMapping(Attribute<?, ?> attribute) {
136:• if (!attribute.getJavaType().isEnum()) {
137: return;
138: }
139: final Enumerated enumeratedAnn = attribute.getJavaField().getAnnotation(Enumerated.class);
140:• if (enumeratedAnn == null || enumeratedAnn.value() != EnumType.OBJECT_ONE_OF) {
141: throw new InvalidFieldMappingException(
142: "Attribute " + attribute + " maps an enum but is not annotated with " + Enumerated.class + " with " + EnumType.OBJECT_ONE_OF + " value.");
143: }
144: }
145: }