Skip to content

Package: AbstractInstanceBuilder

AbstractInstanceBuilder

nameinstructionbranchcomplexitylinemethod
AbstractInstanceBuilder(CloneBuilderImpl, UnitOfWork)
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
getDeclaredConstructorFor(Class, Class[])
M: 2 C: 18
90%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 1 C: 8
89%
M: 0 C: 1
100%
getFirstDeclaredConstructorFor(Class)
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%
logConstructorAccessException(Constructor, Exception)
M: 15 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
logPrivilegedConstructorAccessException(Constructor, PrivilegedActionException)
M: 15 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: 3
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: * 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.sessions;
16:
17: import org.slf4j.Logger;
18: import org.slf4j.LoggerFactory;
19:
20: import java.lang.reflect.Constructor;
21: import java.lang.reflect.Field;
22: import java.security.PrivilegedActionException;
23:
24: abstract class AbstractInstanceBuilder {
25:
26: private static final Logger LOG = LoggerFactory.getLogger(AbstractInstanceBuilder.class);
27:
28: protected boolean populates;
29: protected final CloneBuilderImpl builder;
30: protected final UnitOfWork uow;
31:
32: AbstractInstanceBuilder(CloneBuilderImpl builder, UnitOfWork uow) {
33: this.builder = builder;
34: this.uow = uow;
35: this.populates = false;
36: }
37:
38: /**
39: * Returns true if this builder instances automatically populates the created instance's attribute.
40: *
41: * @return boolean
42: */
43: boolean populatesAttributes() {
44: return populates;
45: }
46:
47: /**
48: * Builds new instance from the original. </p>
49: * <p>
50: * For some implementations this may mean creating an empty object, others might choose to initialize it using the
51: * original data.
52: *
53: * @param cloneOwner Instance owning the clone which will be created
54: * @param field Field which will contain the clone
55: * @param original The original object
56: * @param cloneConfiguration Configuration for the cloning process
57: * @return The clone
58: */
59: abstract Object buildClone(Object cloneOwner, Field field, Object original, CloneConfiguration cloneConfiguration);
60:
61: /**
62: * Merges changes from clone to the original.
63: *
64: * @param field The field we are merging
65: * @param target target object on which the values are merged
66: * @param originalValue The original value
67: * @param cloneValue The clone value
68: */
69: abstract void mergeChanges(Field field, Object target, Object originalValue, Object cloneValue);
70:
71: /**
72: * Return the declared constructor for the specified class. If the constructor is not accessible, it is set
73: * accessible. If there is no constructor corresponding to the specified argument list, null is returned.
74: *
75: * @param javaClass The class of the constructor.
76: * @param args An Array of classes, which should take the constructor as parameters.
77: * @return Constructor
78: * @throws SecurityException If the security check denies access to the constructor.
79: */
80: protected static Constructor<?> getDeclaredConstructorFor(final Class<?> javaClass,
81: Class<?>[] args) throws SecurityException {
82: Constructor<?> c;
83: try {
84: c = javaClass.getDeclaredConstructor(args);
85:• if (c == null) {
86: return null;
87: }
88:• if (!c.isAccessible()) {
89: c.setAccessible(true);
90: }
91: } catch (NoSuchMethodException e) {
92: // No constructor matching the argument types
93: return null;
94: }
95: return c;
96: }
97:
98: /**
99: * This helper method returns the first declared constructor of the specified class. It may be used only in cases
100: * when the caller knows exactly which constructor is the first one declared by the class. A use case may be a class
101: * with only one declared constructor, which is not a zero argument one.
102: *
103: * @param javaClass The class whose constructors should be searched.
104: * @return The first declared constructor of the specified class.
105: */
106: protected static Constructor<?> getFirstDeclaredConstructorFor(Class<?> javaClass) {
107: Constructor<?>[] constructors = javaClass.getDeclaredConstructors();
108: return constructors[0];
109: }
110:
111: protected static void logConstructorAccessException(Constructor<?> constructor, Exception e) {
112: LOG.warn("Exception caught when invoking constructor " + constructor + ". Exception: " + e);
113: }
114:
115: protected static void logPrivilegedConstructorAccessException(Constructor<?> constructor,
116: PrivilegedActionException e) {
117: LOG.warn("Exception caught on privileged invocation of constructor " + constructor + ". Exception: " + e);
118: }
119: }