1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package com.hack23.cia.service.data.impl.util;
20
21 import java.beans.PropertyDescriptor;
22 import java.lang.reflect.InvocationTargetException;
23 import java.lang.reflect.UndeclaredThrowableException;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.IdentityHashMap;
27 import java.util.Set;
28
29 import org.apache.commons.beanutils.PropertyUtils;
30 import org.hibernate.Hibernate;
31 import org.hibernate.collection.spi.PersistentCollection;
32 import org.hibernate.proxy.HibernateProxy;
33
34
35
36
37 public final class LoadHelper {
38
39
40
41
42 private LoadHelper() {
43 super();
44 }
45
46
47
48
49
50
51
52 private static void handleReflectionException(final Exception ex) {
53 if (ex instanceof NoSuchMethodException) {
54 throw new IllegalStateException("Method not found: " + ex.getMessage());
55 }
56 if (ex instanceof IllegalAccessException) {
57 throw new IllegalStateException("Could not access method: " + ex.getMessage());
58 }
59 if (ex instanceof RuntimeException) {
60 throw (RuntimeException) ex;
61 }
62 throw new UndeclaredThrowableException(ex);
63 }
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79 private static void recursiveInitialize(final Object obj, final Set<Object> dejaVu)
80 throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
81 if (dejaVu.contains(obj)) {
82 return;
83 } else {
84 dejaVu.add(obj);
85
86 if (!Hibernate.isInitialized(obj)) {
87 Hibernate.initialize(obj);
88 }
89
90 if (obj instanceof HibernateProxy || obj instanceof PersistentCollection) {
91
92 initProxyAndCollections(obj, PropertyUtils.getPropertyDescriptors(obj), dejaVu);
93 }
94 }
95 }
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113 private static void initProxyAndCollections(final Object obj, final PropertyDescriptor[] properties,
114 final Set<Object> dejaVu) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
115 for (final PropertyDescriptor propertyDescriptor : properties) {
116 if (PropertyUtils.getReadMethod(propertyDescriptor) != null) {
117 initProxies(dejaVu, PropertyUtils.getProperty(obj, propertyDescriptor.getName()));
118 }
119 }
120 }
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136 private static void initProxies(final Set<Object> dejaVu, final Object origProp)
137 throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
138 if (origProp != null) {
139 recursiveInitialize(origProp, dejaVu);
140 }
141 if (origProp instanceof Collection) {
142 for (final Object item : (Collection<?>) origProp) {
143 recursiveInitialize(item, dejaVu);
144 }
145 }
146 }
147
148
149
150
151
152
153
154
155
156
157 public static <T> T recursiveInitialize(final T obj) {
158 if (obj != null) {
159
160 final Set<Object> dejaVu = Collections.newSetFromMap(new IdentityHashMap<Object, Boolean>());
161 try {
162 recursiveInitialize(obj, dejaVu);
163 } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
164 handleReflectionException(e);
165 }
166 }
167 return obj;
168 }
169
170 }