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