-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
BytecodeEnhancementMetadataPojoImpl.java
270 lines (233 loc) · 8.32 KB
/
BytecodeEnhancementMetadataPojoImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.tuple.entity;
import java.io.Serializable;
import java.util.Set;
import org.hibernate.LockMode;
import org.hibernate.bytecode.enhance.spi.interceptor.BytecodeLazyAttributeInterceptor;
import org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor;
import org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeLoadingInterceptor;
import org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributesMetadata;
import org.hibernate.bytecode.spi.BytecodeEnhancementMetadata;
import org.hibernate.bytecode.spi.NotInstrumentedException;
import org.hibernate.engine.spi.EntityKey;
import org.hibernate.engine.spi.PersistenceContext;
import org.hibernate.engine.spi.PersistentAttributeInterceptable;
import org.hibernate.engine.spi.PersistentAttributeInterceptor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.engine.spi.Status;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.type.CompositeType;
/**
* @author Steve Ebersole
*/
public final class BytecodeEnhancementMetadataPojoImpl implements BytecodeEnhancementMetadata {
/**
* Static constructor
*/
public static BytecodeEnhancementMetadata from(
PersistentClass persistentClass,
Set<String> identifierAttributeNames,
CompositeType nonAggregatedCidMapper,
boolean allowEnhancementAsProxy) {
final Class mappedClass = persistentClass.getMappedClass();
final boolean enhancedForLazyLoading = PersistentAttributeInterceptable.class.isAssignableFrom( mappedClass );
final LazyAttributesMetadata lazyAttributesMetadata = enhancedForLazyLoading
? LazyAttributesMetadata.from( persistentClass, true, allowEnhancementAsProxy )
: LazyAttributesMetadata.nonEnhanced( persistentClass.getEntityName() );
return new BytecodeEnhancementMetadataPojoImpl(
persistentClass.getEntityName(),
mappedClass,
identifierAttributeNames,
nonAggregatedCidMapper,
enhancedForLazyLoading,
lazyAttributesMetadata
);
}
private final String entityName;
private final Class entityClass;
private final Set<String> identifierAttributeNames;
private final CompositeType nonAggregatedCidMapper;
private final boolean enhancedForLazyLoading;
private final LazyAttributesMetadata lazyAttributesMetadata;
@SuppressWarnings("WeakerAccess")
protected BytecodeEnhancementMetadataPojoImpl(
String entityName,
Class entityClass,
Set<String> identifierAttributeNames,
CompositeType nonAggregatedCidMapper,
boolean enhancedForLazyLoading,
LazyAttributesMetadata lazyAttributesMetadata) {
this.nonAggregatedCidMapper = nonAggregatedCidMapper;
assert identifierAttributeNames != null;
assert !identifierAttributeNames.isEmpty();
this.entityName = entityName;
this.entityClass = entityClass;
this.identifierAttributeNames = identifierAttributeNames;
this.enhancedForLazyLoading = enhancedForLazyLoading;
this.lazyAttributesMetadata = lazyAttributesMetadata;
}
@Override
public String getEntityName() {
return entityName;
}
@Override
public boolean isEnhancedForLazyLoading() {
return enhancedForLazyLoading;
}
@Override
public LazyAttributesMetadata getLazyAttributesMetadata() {
return lazyAttributesMetadata;
}
@Override
public boolean hasUnFetchedAttributes(Object entity) {
if ( ! enhancedForLazyLoading ) {
return false;
}
final BytecodeLazyAttributeInterceptor interceptor = extractLazyInterceptor( entity );
if ( interceptor instanceof LazyAttributeLoadingInterceptor ) {
return ( (LazyAttributeLoadingInterceptor) interceptor ).hasAnyUninitializedAttributes();
}
//noinspection RedundantIfStatement
if ( interceptor instanceof EnhancementAsProxyLazinessInterceptor ) {
return true;
}
return false;
}
@Override
public boolean isAttributeLoaded(Object entity, String attributeName) {
if ( ! enhancedForLazyLoading ) {
return true;
}
final BytecodeLazyAttributeInterceptor interceptor = extractLazyInterceptor( entity );
if ( interceptor instanceof LazyAttributeLoadingInterceptor ) {
return ( (LazyAttributeLoadingInterceptor) interceptor ).isAttributeLoaded( attributeName );
}
return true;
}
@Override
public LazyAttributeLoadingInterceptor extractInterceptor(Object entity) throws NotInstrumentedException {
return (LazyAttributeLoadingInterceptor) extractLazyInterceptor( entity );
}
@Override
public PersistentAttributeInterceptable createEnhancedProxy(EntityKey entityKey, boolean addEmptyEntry, SharedSessionContractImplementor session) {
final EntityPersister persister = entityKey.getPersister();
final Serializable identifier = entityKey.getIdentifier();
final PersistenceContext persistenceContext = session.getPersistenceContext();
// first, instantiate the entity instance to use as the proxy
final PersistentAttributeInterceptable entity = (PersistentAttributeInterceptable) persister.getEntityTuplizer().instantiate( identifier, session );
// add the entity (proxy) instance to the PC
persistenceContext.addEnhancedProxy( entityKey, entity );
// if requested, add the "holder entry" to the PC
if ( addEmptyEntry ) {
persistenceContext.addEntry(
entity,
Status.MANAGED,
// loaded state
null,
// row-id
null,
identifier,
// version
null,
LockMode.NONE,
// we assume it exists in db
true,
persister,
true
);
}
// inject the interceptor
persister.getEntityMetamodel()
.getBytecodeEnhancementMetadata()
.injectEnhancedEntityAsProxyInterceptor( entity, entityKey, session );
return entity;
}
@Override
public LazyAttributeLoadingInterceptor injectInterceptor(
Object entity,
Object identifier,
SharedSessionContractImplementor session) {
if ( !enhancedForLazyLoading ) {
throw new NotInstrumentedException( "Entity class [" + entityClass.getName() + "] is not enhanced for lazy loading" );
}
if ( !entityClass.isInstance( entity ) ) {
throw new IllegalArgumentException(
String.format(
"Passed entity instance [%s] is not of expected type [%s]",
entity,
getEntityName()
)
);
}
final LazyAttributeLoadingInterceptor interceptor = new LazyAttributeLoadingInterceptor(
getEntityName(),
identifier,
lazyAttributesMetadata.getLazyAttributeNames(),
session
);
injectInterceptor( entity, interceptor, session );
return interceptor;
}
@Override
public void injectEnhancedEntityAsProxyInterceptor(
Object entity,
EntityKey entityKey,
SharedSessionContractImplementor session) {
injectInterceptor(
entity,
new EnhancementAsProxyLazinessInterceptor(
entityName,
identifierAttributeNames,
nonAggregatedCidMapper,
entityKey,
session
),
session
);
}
@Override
public void injectInterceptor(
Object entity,
PersistentAttributeInterceptor interceptor,
SharedSessionContractImplementor session) {
if ( !enhancedForLazyLoading ) {
throw new NotInstrumentedException( "Entity class [" + entityClass.getName() + "] is not enhanced for lazy loading" );
}
if ( !entityClass.isInstance( entity ) ) {
throw new IllegalArgumentException(
String.format(
"Passed entity instance [%s] is not of expected type [%s]",
entity,
getEntityName()
)
);
}
( (PersistentAttributeInterceptable) entity ).$$_hibernate_setInterceptor( interceptor );
}
@Override
public BytecodeLazyAttributeInterceptor extractLazyInterceptor(Object entity) throws NotInstrumentedException {
if ( !enhancedForLazyLoading ) {
throw new NotInstrumentedException( "Entity class [" + entityClass.getName() + "] is not enhanced for lazy loading" );
}
if ( !entityClass.isInstance( entity ) ) {
throw new IllegalArgumentException(
String.format(
"Passed entity instance [%s] is not of expected type [%s]",
entity,
getEntityName()
)
);
}
final PersistentAttributeInterceptor interceptor = ( (PersistentAttributeInterceptable) entity ).$$_hibernate_getInterceptor();
if ( interceptor == null ) {
return null;
}
return (BytecodeLazyAttributeInterceptor) interceptor;
}
}