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