-
Notifications
You must be signed in to change notification settings - Fork 2
/
BeanAnnotationProcessor.java
93 lines (84 loc) · 3.27 KB
/
BeanAnnotationProcessor.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
package cz.cvut.kbss.jsonld.serialization;
import cz.cvut.kbss.jopa.model.annotations.*;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.*;
public class BeanAnnotationProcessor {
private BeanAnnotationProcessor() {
throw new AssertionError();
}
/**
* Resolves ontological type of the specified object, as specified by the {@link OWLClass} annotation.
*
* @param object The object to resolve
* @return Resolved OWL type or {@code null} if the object's class is not annotated with {@link OWLClass}
*/
public static Set<String> getOwlClasses(Object object) {
Objects.requireNonNull(object);
final Class<?> cls = object.getClass();
final Set<String> classes = new HashSet<>();
getAncestors(cls).forEach(c -> {
final OWLClass owlClass = c.getDeclaredAnnotation(OWLClass.class);
if (owlClass != null) {
classes.add(owlClass.iri());
}
});
return classes;
}
private static List<Class<?>> getAncestors(Class<?> cls) {
final List<Class<?>> classes = new ArrayList<>();
Class<?> current = cls;
while (current != null && !current.equals(Object.class)) {
classes.add(current);
current = current.getSuperclass();
}
return classes;
}
/**
* Returns all fields of the class of the specified object and its superclasses, which can be serialized into
* JSON-LD.
* <p>
* For a field to be serializable, it has to be:
* <pre>
* <ul>
* <li>Non-static</li>
* <li>Annotated with one of the following annotations: {@link Id}, {@link OWLAnnotationProperty}, {@link
* OWLDataProperty}, {@link OWLObjectProperty}</li>
* </ul>
* </pre>
*
* @param object Object whose fields should be discovered
* @return List of discovered fields
*/
public static List<Field> getSerializableFields(Object object) {
Objects.requireNonNull(object);
final Class<?> cls = object.getClass();
final List<Class<?>> classes = getAncestors(cls);
final Set<Field> fields = new HashSet<>();
for (Class<?> c : classes) {
for (Field f : c.getDeclaredFields()) {
if (!isFieldTransient(f)) {
fields.add(f);
}
}
}
return new ArrayList<>(fields);
}
private static boolean isFieldTransient(Field field) {
return Modifier.isStatic(field.getModifiers()) || (
field.getDeclaredAnnotation(OWLAnnotationProperty.class) == null &&
field.getDeclaredAnnotation(OWLDataProperty.class) == null &&
field.getDeclaredAnnotation(OWLObjectProperty.class) == null &&
field.getDeclaredAnnotation(Id.class) == null);
}
/**
* Checks whether the specified field is mapped to an OWL object property.
*
* @param field The field to examine
* @return Whether field has a {@link OWLObjectProperty} annotation
*/
public static boolean isObjectProperty(Field field) {
Objects.requireNonNull(field);
return field.getDeclaredAnnotation(OWLObjectProperty.class) != null;
}
}