Skip to content

Package: EntityPropertiesUtils

EntityPropertiesUtils

nameinstructionbranchcomplexitylinemethod
EntityPropertiesUtils()
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getAllFields(Class)
M: 0 C: 28
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
getAttributeValue(FieldSpecification, Object)
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
getFieldValue(Field, Object)
M: 7 C: 13
65%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 2 C: 4
67%
M: 0 C: 1
100%
getIdentifier(Object, EntityType)
M: 7 C: 13
65%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 2 C: 4
67%
M: 0 C: 1
100%
getIdentifier(Object, Metamodel)
M: 0 C: 15
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
getValueAsURI(Object)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
isFieldStatic(Field)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
isFieldTransient(Field)
M: 0 C: 28
100%
M: 0 C: 8
100%
M: 0 C: 5
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
lambda$getAllFields$0(Field)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
setFieldValue(Field, Object, Object)
M: 7 C: 15
68%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 2 C: 6
75%
M: 0 C: 1
100%
setIdentifier(Object, Object, EntityType)
M: 6 C: 17
74%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 6
75%
M: 0 C: 1
100%
verifyIdentifierIsGenerated(Object, EntityType)
M: 0 C: 19
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%

Coverage

1: /**
2: * Copyright (C) 2020 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.utils;
16:
17: import cz.cvut.kbss.jopa.exception.IdentifierNotSetException;
18: import cz.cvut.kbss.jopa.exceptions.OWLPersistenceException;
19: import cz.cvut.kbss.jopa.model.annotations.Transient;
20: import cz.cvut.kbss.jopa.model.metamodel.EntityType;
21: import cz.cvut.kbss.jopa.model.metamodel.FieldSpecification;
22: import cz.cvut.kbss.jopa.model.metamodel.Identifier;
23: import cz.cvut.kbss.jopa.model.metamodel.Metamodel;
24: import cz.cvut.kbss.ontodriver.exception.UnassignableIdentifierException;
25:
26: import java.lang.reflect.Field;
27: import java.lang.reflect.Modifier;
28: import java.net.URI;
29: import java.util.ArrayList;
30: import java.util.Arrays;
31: import java.util.List;
32: import java.util.Objects;
33:
34: /**
35: * Utility class for entity properties.
36: */
37: public class EntityPropertiesUtils {
38:
39: /**
40: * Private constructor
41: */
42: private EntityPropertiesUtils() {
43: throw new AssertionError("I am not for instantiation.");
44: }
45:
46: /**
47: * Extracts primary key from the specified {@code entity} and returns it.
48: *
49: * @param entity The entity to extract primary key from
50: * @param metamodel Metamodel
51: * @return IRI of the entity or null if it is not set
52: * @throws NullPointerException If {@code entity} or {@code metamodel} is null
53: * @throws OWLPersistenceException If {@code entity} is not an entity or if the identifier is of an unknown type
54: */
55: public static URI getIdentifier(Object entity, Metamodel metamodel) {
56: Objects.requireNonNull(entity);
57: Objects.requireNonNull(metamodel);
58:
59: final EntityType<?> type = metamodel.entity(entity.getClass());
60: return getIdentifier(entity, type);
61: }
62:
63: /**
64: * Sets value of the specified field.
65: *
66: * @param field Field to set value on
67: * @param instance Target instance (may be {@code null} for static fields)
68: * @param value The value to set (may be {@code null})
69: */
70: public static void setFieldValue(Field field, Object instance, Object value) {
71: Objects.requireNonNull(field);
72:• if (!field.isAccessible()) {
73: field.setAccessible(true);
74: }
75: try {
76: field.set(instance, value);
77: } catch (IllegalAccessException e) {
78: throw new OWLPersistenceException("Unable to set field value.", e);
79: }
80: }
81:
82: /**
83: * Gets value of the specified field from the specified instance.
84: *
85: * @param field Field to get value of
86: * @param instance Instance that contains the field
87: * @return Field value
88: */
89: public static Object getFieldValue(Field field, Object instance) {
90: Objects.requireNonNull(field);
91:• if (!field.isAccessible()) {
92: field.setAccessible(true);
93: }
94: try {
95: return field.get(instance);
96: } catch (IllegalAccessException e) {
97: throw new OWLPersistenceException("Unable to extract field value.", e);
98: }
99: }
100:
101: /**
102: * Gets value of the specified attribute.
103: *
104: * @param attribute Attribute to extract value of
105: * @param instance Instance from which value will be extracted
106: * @return Attribute value
107: */
108: public static Object getAttributeValue(FieldSpecification<?, ?> attribute, Object instance) {
109: Objects.requireNonNull(attribute);
110: final Field field = attribute.getJavaField();
111: return getFieldValue(field, instance);
112: }
113:
114: /**
115: * Extracts entity's identifier according to the specified entity type.
116: *
117: * @param entity Entity
118: * @param et Entity type
119: * @return Primary key, possibly null
120: */
121: public static <T> URI getIdentifier(T entity, EntityType<?> et) {
122: try {
123: final Object id = getFieldValue(et.getIdentifier().getJavaField(), entity);
124:• if (id == null) {
125: return null;
126: }
127: return getValueAsURI(id);
128: } catch (IllegalArgumentException e) {
129: throw new OWLPersistenceException("Unable to extract entity identifier.", e);
130: }
131: }
132:
133: /**
134: * Sets the specified identifier on the specified entity.
135: *
136: * @param identifier The identifier to set
137: * @param entity Target entity
138: * @param et Entity type
139: */
140: public static <T> void setIdentifier(Object identifier, T entity, EntityType<T> et) {
141: final Identifier id = et.getIdentifier();
142: final Field idField = id.getJavaField();
143: try {
144: final Object assignableId = IdentifierTransformer.transformToIdentifier(identifier, idField.getType());
145: setFieldValue(idField, entity, assignableId);
146: } catch (IllegalArgumentException e) {
147: throw new UnassignableIdentifierException(e);
148: }
149: }
150:
151: /**
152: * Transforms the specified value to URI (if possible).
153: *
154: * @param value The value to transform
155: * @return {@code URI}
156: * @throws NullPointerException If {@code value} is {@code null}
157: * @throws IllegalArgumentException If {@code value} cannot be transformed to URI
158: */
159: public static URI getValueAsURI(Object value) {
160: return IdentifierTransformer.valueAsUri(Objects.requireNonNull(value));
161: }
162:
163: /**
164: * Gets all instance fields of the specified class, including inherited ones.
165: *
166: * @param cls The class to search
167: * @return List of declared fields
168: */
169: public static List<Field> getAllFields(Class<?> cls) {
170: final List<Field> fields = new ArrayList<>(Arrays.asList(cls.getDeclaredFields()));
171: Class<?> tmp = cls.getSuperclass();
172:• while (tmp != null) {
173: fields.addAll(Arrays.asList(tmp.getDeclaredFields()));
174: tmp = tmp.getSuperclass();
175: }
176: fields.removeIf(f -> Modifier.isStatic(f.getModifiers()));
177: return fields;
178: }
179:
180: /**
181: * Verifies, that the primary key (identifier) of the specified instance is generated.
182: * <p>
183: * If not, an exception is thrown.
184: *
185: * @param instance The instance to verify
186: * @param entityType Entity type of the instance, as specified by metamodel
187: * @throws cz.cvut.kbss.jopa.exception.IdentifierNotSetException If the identifier is not generated
188: */
189: public static void verifyIdentifierIsGenerated(Object instance, EntityType<?> entityType) {
190:• if (!entityType.getIdentifier().isGenerated()) {
191: throw new IdentifierNotSetException(
192: "The id for entity " + instance + " is null and it is not specified as \'generated\' ");
193: }
194: }
195:
196: /**
197: * Checks whether the specified field should not be persisted, i.e. whether it is transient in the persistence
198: * sense.
199: * <p>
200: * A field is transient if it is:
201: * <ul>
202: * <li>static
203: * <li>or final
204: * <li>or transient
205: * <li>or annotated with the {@link Transient} annotation
206: * </ul>
207: *
208: * @param field The field to investigate
209: * @return Whether the field is transient
210: */
211: public static boolean isFieldTransient(Field field) {
212: Objects.requireNonNull(field);
213: final int modifiers = field.getModifiers();
214:• if (Modifier.isStatic(modifiers) || Modifier.isFinal(modifiers) || Modifier.isTransient(modifiers)) {
215: return true;
216: }
217: final Transient transientAnnotation = field.getAnnotation(Transient.class);
218:• return transientAnnotation != null;
219: }
220:
221: /**
222: * Returns true if the specified field is static.
223: *
224: * @param field The field to check
225: * @return True if field is static, false otherwise
226: */
227: public static boolean isFieldStatic(Field field) {
228: return Modifier.isStatic(field.getModifiers());
229: }
230: }