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