Skip to content

Package: ClassFieldMetamodelProcessor$InferenceInfo

ClassFieldMetamodelProcessor$InferenceInfo

nameinstructionbranchcomplexitylinemethod
ClassFieldMetamodelProcessor.InferenceInfo(Inferred)
M: 1 C: 19
95%
M: 1 C: 5
83%
M: 1 C: 3
75%
M: 0 C: 4
100%
M: 0 C: 1
100%

Coverage

1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: * <p>
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: * <p>
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.MetamodelInitializationException;
18: import cz.cvut.kbss.jopa.exceptions.OWLPersistenceException;
19: import cz.cvut.kbss.jopa.model.*;
20: import cz.cvut.kbss.jopa.model.annotations.*;
21: import cz.cvut.kbss.jopa.utils.EntityPropertiesUtils;
22: import org.slf4j.Logger;
23: import org.slf4j.LoggerFactory;
24:
25: import java.lang.reflect.Field;
26: import java.lang.reflect.ParameterizedType;
27: import java.lang.reflect.Type;
28: import java.util.Collection;
29: import java.util.List;
30: import java.util.Map;
31: import java.util.Set;
32:
33: class ClassFieldMetamodelProcessor<X> {
34:
35: private static final Logger LOG = LoggerFactory.getLogger(ClassFieldMetamodelProcessor.class);
36:
37: private final FieldMappingValidator mappingValidator = new FieldMappingValidator();
38:
39: private final Class<X> cls;
40: private final AbstractIdentifiableType<X> et;
41: private final MetamodelBuilder metamodelBuilder;
42:
43: ClassFieldMetamodelProcessor(Class<X> cls, AbstractIdentifiableType<X> et, MetamodelBuilder metamodelBuilder) {
44: this.cls = cls;
45: this.et = et;
46: this.metamodelBuilder = metamodelBuilder;
47: }
48:
49: void processField(Field field) {
50: LOG.trace("processing field: {}", field);
51: if (EntityPropertiesUtils.isFieldTransient(field)) {
52: // Do not log static fields
53: if (!EntityPropertiesUtils.isFieldStatic(field)) {
54: LOG.trace("Skipping transient field {}", field);
55: }
56: return;
57: }
58: if (field.getType().isPrimitive()) {
59: throw new MetamodelInitializationException(
60: "Primitive types cannot be used for entity fields. Field " + field + " in class " + cls);
61: }
62:
63: final Class<?> fieldValueCls = getFieldValueType(field);
64: field.setAccessible(true);
65: final InferenceInfo inference = processInferenceInfo(field);
66:
67: if (isTypesField(field)) {
68: processTypesField(field, fieldValueCls, inference);
69: return;
70: }
71: if (isPropertiesField(field)) {
72: processPropertiesField(field, fieldValueCls, inference);
73: return;
74: }
75:
76: final PropertyAttributes propertyAtt = PropertyAttributes.create(field, mappingValidator);
77: propertyAtt.resolve(field, metamodelBuilder, fieldValueCls);
78:
79: if (propertyAtt.isKnownOwlProperty()) {
80: createAttribute(field, inference, propertyAtt);
81: } else {
82: final boolean success = processIdentifierField(field);
83: if (!success) {
84: throw new MetamodelInitializationException(
85: "Unable to process field " + field + ". It is not transient but has no mapping information.");
86: }
87: }
88: }
89:
90: private Class<?> getFieldValueType(Field field) {
91: if (Collection.class.isAssignableFrom(field.getType())) {
92: return getSetOrListErasureType((ParameterizedType) field.getGenericType());
93: } else if (field.getType().isArray()) {
94: throw new MetamodelInitializationException("Array persistent attributes are not supported.");
95: } else {
96: return field.getType();
97: }
98: }
99:
100: private static Class<?> getSetOrListErasureType(final ParameterizedType cls) {
101: final Type[] t = cls.getActualTypeArguments();
102:
103: if (t.length != 1) {
104: throw new OWLPersistenceException("Only collections with a single generic parameter are supported.");
105: }
106: Type type = t[0];
107: if (!(type instanceof Class<?>)) {
108: throw new OWLPersistenceException("Only Classes are valid parameters for generic lists and sets.");
109: }
110: return (Class<?>) type;
111: }
112:
113: private InferenceInfo processInferenceInfo(Field field) {
114: final Inferred inferred = field.getAnnotation(Inferred.class);
115:
116: final InferenceInfo inference = new InferenceInfo(inferred);
117: if (inference.inferred) {
118: metamodelBuilder.addInferredClass(cls);
119: }
120: return inference;
121: }
122:
123: private boolean isTypesField(Field field) {
124: return field.getAnnotation(Types.class) != null;
125: }
126:
127: private void processTypesField(Field field, Class<?> fieldValueCls, InferenceInfo inference) {
128: Types tt = field.getAnnotation(Types.class);
129: mappingValidator.validateTypesField(field);
130: et.addDirectTypes(new TypesSpecificationImpl<>(et, tt.fetchType(), field, fieldValueCls, inference.inferred));
131: }
132:
133: private boolean isPropertiesField(Field field) {
134: return field.getAnnotation(Properties.class) != null;
135: }
136:
137: private void processPropertiesField(Field field, Class<?> fieldValueCls, InferenceInfo inference) {
138: Properties properties = field.getAnnotation(Properties.class);
139: mappingValidator.validatePropertiesField(field);
140: final PropertiesParametersResolver paramsResolver = new PropertiesParametersResolver(field);
141: et.addOtherProperties(
142: PropertiesSpecificationImpl.declaringType(et).fetchType(properties.fetchType()).javaField(field)
143: .javaType(fieldValueCls).inferred(inference.inferred)
144: .propertyIdType(paramsResolver.getPropertyIdentifierType())
145: .propertyValueType(paramsResolver.getPropertyValueType()).build());
146: }
147:
148: private void createAttribute(Field field, InferenceInfo inference, PropertyAttributes propertyAttributes) {
149: final Attribute<X, ?> a;
150: if (field.getType().isAssignableFrom(List.class)) {
151: final Sequence os = field.getAnnotation(Sequence.class);
152:
153: if (os == null) {
154: throw new MetamodelInitializationException("Expected Sequence annotation.");
155: }
156: a = ListAttributeImpl.iri(propertyAttributes.getIri()).declaringType(et).field(field)
157: .elementType(propertyAttributes.getType())
158: .attributeType(propertyAttributes.getPersistentAttributeType())
159: .cascadeTypes(propertyAttributes.getCascadeTypes())
160: .fetchType(propertyAttributes.getFetchType()).inferred(inference.inferred)
161: .includeExplicit(inference.includeExplicit)
162: .owlListClass(IRI.create(os.ClassOWLListIRI()))
163: .hasNextProperty(IRI.create(os.ObjectPropertyHasNextIRI()))
164: .hasContentsProperty(IRI.create(os.ObjectPropertyHasContentsIRI()))
165: .sequenceType(os.type())
166: .participationConstraints(propertyAttributes.getParticipationConstraints())
167: .nonEmpty(propertyAttributes.isNonEmpty()).build();
168: } else if (field.getType().isAssignableFrom(Set.class)) {
169: a = SetAttributeImpl.iri(propertyAttributes.getIri()).declaringType(et).field(field)
170: .elementType(propertyAttributes.getType())
171: .attributeType(propertyAttributes.getPersistentAttributeType())
172: .fetchType(propertyAttributes.getFetchType())
173: .cascadeTypes(propertyAttributes.getCascadeTypes()).inferred(inference.inferred)
174: .includeExplicit(inference.includeExplicit)
175: .participationConstraints(propertyAttributes.getParticipationConstraints())
176: .nonEmpty(propertyAttributes.isNonEmpty()).build();
177: } else if (field.getType().isAssignableFrom(Map.class)) {
178: throw new IllegalArgumentException("NOT YET SUPPORTED");
179: } else {
180: a = SingularAttributeImpl.iri(propertyAttributes.getIri()).name(field.getName()).identifier(false)
181: .declaringType(et).type(propertyAttributes.getType()).field(field)
182: .cascadeTypes(propertyAttributes.getCascadeTypes())
183: .attributeType(propertyAttributes.getPersistentAttributeType())
184: .fetchType(propertyAttributes.getFetchType()).inferred(inference.inferred)
185: .includeExplicit(inference.includeExplicit)
186: .constraints(propertyAttributes.getParticipationConstraints())
187: .nonEmpty(propertyAttributes.isNonEmpty()).build();
188: }
189: et.addDeclaredAttribute(field.getName(), a);
190: }
191:
192: private boolean processIdentifierField(Field field) {
193: final Id id = field.getAnnotation(Id.class);
194: if (id == null) {
195: return false;
196: }
197: mappingValidator.validateIdentifierType(field.getType());
198: et.setIdentifier(new IRIIdentifierImpl(field, id.generated()));
199: return true;
200: }
201:
202: private static class InferenceInfo {
203: private final boolean inferred;
204: private final boolean includeExplicit;
205:
206: InferenceInfo(Inferred inferredAnnotation) {
207:• this.inferred = inferredAnnotation != null;
208:• this.includeExplicit = inferredAnnotation == null || inferredAnnotation.includeExplicit();
209: }
210: }
211: }