Skip to content

Package: AbstractInstanceBuilder

AbstractInstanceBuilder

nameinstructionbranchcomplexitylinemethod
AbstractInstanceBuilder(CloneBuilder, UnitOfWork)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
getDeclaredConstructorFor(Class, Class[])
M: 11 C: 14
56%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 4 C: 6
60%
M: 0 C: 1
100%
logConstructorAccessException(Constructor, Exception)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
populatesAttributes()
M: 0 C: 2
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
static {...}
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%

Coverage

1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.jopa.sessions;
19:
20: import cz.cvut.kbss.jopa.sessions.util.CloneConfiguration;
21: import org.slf4j.Logger;
22: import org.slf4j.LoggerFactory;
23:
24: import java.lang.reflect.Constructor;
25: import java.lang.reflect.Field;
26:
27: abstract class AbstractInstanceBuilder {
28:
29: private static final Logger LOG = LoggerFactory.getLogger(AbstractInstanceBuilder.class);
30:
31: protected final CloneBuilder builder;
32: protected final UnitOfWork uow;
33:
34: AbstractInstanceBuilder(CloneBuilder builder, UnitOfWork uow) {
35: this.builder = builder;
36: this.uow = uow;
37: }
38:
39: /**
40: * Returns true if this builder instances automatically populates the created instance's attribute.
41: *
42: * @return boolean
43: */
44: boolean populatesAttributes() {
45: return false;
46: }
47:
48: /**
49: * Builds new instance from the original. </p>
50: * <p>
51: * For some implementations this may mean creating an empty object, others might choose to initialize it using the
52: * original data.
53: *
54: * @param cloneOwner Instance owning the clone which will be created
55: * @param field Field which will contain the clone
56: * @param original The original object
57: * @param cloneConfiguration Configuration for the cloning process
58: * @return The clone
59: */
60: abstract Object buildClone(Object cloneOwner, Field field, Object original, CloneConfiguration cloneConfiguration);
61:
62: /**
63: * Merges changes from clone to the original.
64: *
65: * @param field The field we are merging
66: * @param target target object on which the values are merged
67: * @param originalValue The original value
68: * @param cloneValue The clone value
69: */
70: abstract void mergeChanges(Field field, Object target, Object originalValue, Object cloneValue);
71:
72: /**
73: * Return the declared constructor for the specified class. If the constructor is not accessible, it is set
74: * accessible. If there is no constructor corresponding to the specified argument list, null is returned.
75: *
76: * @param javaClass The class of the constructor.
77: * @param args An Array of classes, which should take the constructor as parameters.
78: * @return Constructor
79: * @throws SecurityException If the security check denies access to the constructor.
80: */
81: protected static Constructor<?> getDeclaredConstructorFor(final Class<?> javaClass, Class<?>[] args) {
82: Constructor<?> c;
83: try {
84: c = javaClass.getDeclaredConstructor(args);
85:• if (!c.canAccess(null)) {
86: c.setAccessible(true);
87: }
88: } catch (NoSuchMethodException e) {
89: // No constructor matching the argument types
90: return null;
91: } catch (RuntimeException e) {
92: // Constructor cannot be resolved for some other reason
93: LOG.warn("Unable to get constructor for arguments of type {}.", args, e);
94: return null;
95: }
96: return c;
97: }
98:
99: protected static void logConstructorAccessException(Constructor<?> constructor, Exception e) {
100: LOG.warn("Exception caught when invoking constructor {}.", constructor, e);
101: }
102: }