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