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: 31
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 8
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%
getPrimaryKey(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%
getPrimaryKey(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%
setPrimaryKey(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) 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.utils;
16:
17: import cz.cvut.kbss.jopa.exceptions.OWLPersistenceException;
18: import cz.cvut.kbss.jopa.model.annotations.Transient;
19: import cz.cvut.kbss.jopa.model.metamodel.EntityType;
20: import cz.cvut.kbss.jopa.model.metamodel.FieldSpecification;
21: import cz.cvut.kbss.jopa.model.metamodel.Identifier;
22: import cz.cvut.kbss.jopa.model.metamodel.Metamodel;
23: import cz.cvut.kbss.ontodriver.exception.PrimaryKeyNotSetException;
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 getPrimaryKey(Object entity, Metamodel metamodel) {
56: Objects.requireNonNull(entity);
57: Objects.requireNonNull(metamodel);
58:
59: final EntityType<?> type = metamodel.entity(entity.getClass());
60: return getPrimaryKey(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 primary key 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 getPrimaryKey(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 primary key on the specified entity.
135: *
136: * @param primaryKey The key to set
137: * @param entity Target entity
138: * @param et Entity type
139: */
140: public static <T> void setPrimaryKey(Object primaryKey, T entity, EntityType<T> et) {
141: final Identifier id = et.getIdentifier();
142: final Field idField = id.getJavaField();
143: try {
144: final Object assignablePk = IdentifierTransformer.transformToIdentifier(primaryKey, idField.getType());
145: setFieldValue(idField, entity, assignablePk);
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<>();
171: fields.addAll(Arrays.asList(cls.getDeclaredFields()));
172: Class<?> tmp = cls.getSuperclass();
173:• while (tmp != null) {
174: fields.addAll(Arrays.asList(tmp.getDeclaredFields()));
175: tmp = tmp.getSuperclass();
176: }
177: fields.removeIf(f -> Modifier.isStatic(f.getModifiers()));
178: return fields;
179: }
180:
181: /**
182: * Verifies, that the primary key (identifier) of the specified instance is generated.
183: * <p>
184: * If not, an exception is thrown.
185: *
186: * @param instance The instance to verify
187: * @param entityType Entity type of the instance, as specified by metamodel
188: * @throws PrimaryKeyNotSetException If the identifier is not generated
189: */
190: public static void verifyIdentifierIsGenerated(Object instance, EntityType<?> entityType) {
191:• if (!entityType.getIdentifier().isGenerated()) {
192: throw new PrimaryKeyNotSetException("The id for entity " + instance
193: + " is null and it is not specified as \'generated\' ");
194: }
195: }
196:
197: /**
198: * Checks whether the specified field should not be persisted, i.e. whether it is transient in the persistence
199: * sense.
200: * <p>
201: * A field is transient if it is:
202: * <ul>
203: * <li>static
204: * <li>or final
205: * <li>or transient
206: * <li>or annotated with the {@link Transient} annotation
207: * </ul>
208: *
209: * @param field The field to investigate
210: * @return Whether the field is transient
211: */
212: public static boolean isFieldTransient(Field field) {
213: Objects.requireNonNull(field);
214: final int modifiers = field.getModifiers();
215:• if (Modifier.isStatic(modifiers) || Modifier.isFinal(modifiers) || Modifier.isTransient(modifiers)) {
216: return true;
217: }
218: final Transient transientAnnotation = field.getAnnotation(Transient.class);
219:• return transientAnnotation != null;
220: }
221:
222: /**
223: * Returns true if the specified field is static.
224: *
225: * @param field The field to check
226: * @return True if field is static, false otherwise
227: */
228: public static boolean isFieldStatic(Field field) {
229: return Modifier.isStatic(field.getModifiers());
230: }
231: }