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