Skip to content

Method: getFirstDeclaredConstructorFor(Class)

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