-
Notifications
You must be signed in to change notification settings - Fork 14
/
ClassFieldMetamodelProcessor.java
218 lines (191 loc) · 10.4 KB
/
ClassFieldMetamodelProcessor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/**
* Copyright (C) 2016 Czech Technical University in Prague
* <p>
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
* version.
* <p>
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.jopa.model.metamodel;
import cz.cvut.kbss.jopa.exception.MetamodelInitializationException;
import cz.cvut.kbss.jopa.exceptions.OWLPersistenceException;
import cz.cvut.kbss.jopa.model.BeanListenerAspect;
import cz.cvut.kbss.jopa.model.IRI;
import cz.cvut.kbss.jopa.model.annotations.*;
import cz.cvut.kbss.jopa.utils.EntityPropertiesUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
class ClassFieldMetamodelProcessor<X> {
private static final Logger LOG = LoggerFactory.getLogger(ClassFieldMetamodelProcessor.class);
private final FieldMappingValidator mappingValidator = new FieldMappingValidator();
private final Class<X> cls;
private final AbstractIdentifiableType<X> et;
private final TypeBuilderContext<X> context;
private final MetamodelBuilder metamodelBuilder;
ClassFieldMetamodelProcessor(TypeBuilderContext<X> context, MetamodelBuilder metamodelBuilder) {
this.cls = context.getType().getJavaType();
this.context = context;
this.et = context.getType();
this.metamodelBuilder = metamodelBuilder;
}
void processField(Field field) {
LOG.trace("processing field: {}", field);
if (EntityPropertiesUtils.isFieldTransient(field)) {
// Do not log static fields
if (!EntityPropertiesUtils.isFieldStatic(field)) {
LOG.trace("Skipping transient field {}", field);
}
return;
}
if (field.getType().isPrimitive()) {
throw new MetamodelInitializationException(
"Primitive types cannot be used for entity fields. Field " + field + " in class " + cls);
}
final Class<?> fieldValueCls = getFieldValueType(field);
field.setAccessible(true);
final InferenceInfo inference = processInferenceInfo(field);
if (isTypesField(field)) {
processTypesField(field, fieldValueCls, inference);
return;
}
if (isPropertiesField(field)) {
processPropertiesField(field, fieldValueCls, inference);
return;
}
final PropertyAttributes propertyAtt = PropertyAttributes.create(field, mappingValidator, context);
propertyAtt.resolve(field, metamodelBuilder, fieldValueCls);
if (propertyAtt.isKnownOwlProperty()) {
createAttribute(field, inference, propertyAtt);
} else if (!isAspectIntegrationField(field)) {
final boolean success = processIdentifierField(field);
if (!success) {
throw new MetamodelInitializationException(
"Unable to process field " + field + ". It is not transient but has no mapping information.");
}
}
}
private static Class<?> getFieldValueType(Field field) {
if (Collection.class.isAssignableFrom(field.getType())) {
return getSetOrListErasureType((ParameterizedType) field.getGenericType());
} else if (field.getType().isArray()) {
throw new MetamodelInitializationException("Array persistent attributes are not supported.");
} else {
return field.getType();
}
}
private static Class<?> getSetOrListErasureType(final ParameterizedType cls) {
final Type[] t = cls.getActualTypeArguments();
if (t.length != 1) {
throw new OWLPersistenceException("Only collections with a single generic parameter are supported.");
}
Type type = t[0];
if (!(type instanceof Class<?>)) {
throw new OWLPersistenceException("Only Classes are valid parameters for generic lists and sets.");
}
return (Class<?>) type;
}
private InferenceInfo processInferenceInfo(Field field) {
final Inferred inferred = field.getAnnotation(Inferred.class);
final InferenceInfo inference = new InferenceInfo(inferred);
if (inference.inferred) {
metamodelBuilder.addInferredClass(cls);
}
return inference;
}
private static boolean isTypesField(Field field) {
return field.getAnnotation(Types.class) != null;
}
private void processTypesField(Field field, Class<?> fieldValueCls, InferenceInfo inference) {
Types tt = field.getAnnotation(Types.class);
mappingValidator.validateTypesField(field);
et.addDirectTypes(new TypesSpecificationImpl<>(et, tt.fetchType(), field, fieldValueCls, inference.inferred));
}
private static boolean isPropertiesField(Field field) {
return field.getAnnotation(Properties.class) != null;
}
private void processPropertiesField(Field field, Class<?> fieldValueCls, InferenceInfo inference) {
Properties properties = field.getAnnotation(Properties.class);
mappingValidator.validatePropertiesField(field);
final PropertiesParametersResolver paramsResolver = new PropertiesParametersResolver(field);
et.addOtherProperties(
PropertiesSpecificationImpl.declaringType(et).fetchType(properties.fetchType()).javaField(field)
.javaType(fieldValueCls).inferred(inference.inferred)
.propertyIdType(paramsResolver.getPropertyIdentifierType())
.propertyValueType(paramsResolver.getPropertyValueType()).build());
}
private void createAttribute(Field field, InferenceInfo inference, PropertyAttributes propertyAttributes) {
final Attribute<X, ?> a;
if (field.getType().isAssignableFrom(List.class)) {
final Sequence os = field.getAnnotation(Sequence.class);
if (os == null) {
throw new MetamodelInitializationException("Expected Sequence annotation.");
}
a = ListAttributeImpl.iri(propertyAttributes.getIri()).name(field.getName()).declaringType(et).field(field)
.elementType(propertyAttributes.getType())
.attributeType(propertyAttributes.getPersistentAttributeType())
.cascadeTypes(propertyAttributes.getCascadeTypes())
.fetchType(propertyAttributes.getFetchType()).inferred(inference.inferred)
.includeExplicit(inference.includeExplicit)
.owlListClass(IRI.create(os.ClassOWLListIRI()))
.hasNextProperty(IRI.create(os.ObjectPropertyHasNextIRI()))
.hasContentsProperty(IRI.create(os.ObjectPropertyHasContentsIRI()))
.sequenceType(os.type())
.participationConstraints(propertyAttributes.getParticipationConstraints())
.nonEmpty(propertyAttributes.isNonEmpty()).build();
} else if (field.getType().isAssignableFrom(Set.class)) {
a = SetAttributeImpl.iri(propertyAttributes.getIri()).name(field.getName()).declaringType(et).field(field)
.elementType(propertyAttributes.getType())
.attributeType(propertyAttributes.getPersistentAttributeType())
.fetchType(propertyAttributes.getFetchType())
.cascadeTypes(propertyAttributes.getCascadeTypes()).inferred(inference.inferred)
.includeExplicit(inference.includeExplicit)
.participationConstraints(propertyAttributes.getParticipationConstraints())
.nonEmpty(propertyAttributes.isNonEmpty()).build();
} else if (field.getType().isAssignableFrom(Map.class)) {
throw new IllegalArgumentException("NOT YET SUPPORTED");
} else {
a = SingularAttributeImpl.iri(propertyAttributes.getIri()).name(field.getName()).identifier(false)
.declaringType(et).type(propertyAttributes.getType()).field(field)
.cascadeTypes(propertyAttributes.getCascadeTypes())
.attributeType(propertyAttributes.getPersistentAttributeType())
.fetchType(propertyAttributes.getFetchType()).inferred(inference.inferred)
.includeExplicit(inference.includeExplicit)
.constraints(propertyAttributes.getParticipationConstraints())
.nonEmpty(propertyAttributes.isNonEmpty()).build();
}
et.addDeclaredAttribute(field.getName(), a);
}
private boolean processIdentifierField(Field field) {
final Id id = field.getAnnotation(Id.class);
if (id == null) {
return false;
}
mappingValidator.validateIdentifierType(field.getType());
et.setIdentifier(new IRIIdentifierImpl<>(et, field, id.generated()));
return true;
}
private static boolean isAspectIntegrationField(Field field) {
// AspectJ integration fields cannot be declared transitive (they're generated by the AJC), so we have to
// skip them manually
return field.getType().equals(BeanListenerAspect.Manageable.class);
}
private static class InferenceInfo {
private final boolean inferred;
private final boolean includeExplicit;
InferenceInfo(Inferred inferredAnnotation) {
this.inferred = inferredAnnotation != null;
this.includeExplicit = inferredAnnotation == null || inferredAnnotation.includeExplicit();
}
}
}