2
* Hibernate, Relational Persistence for Idiomatic Java
4
* Copyright (c) 2009, Red Hat Middleware LLC or third-party contributors as
5
* indicated by the @author tags or express copyright attribution
6
* statements applied by the authors. All third-party contributions are
7
* distributed under license by Red Hat Middleware LLC.
9
* This copyrighted material is made available to anyone wishing to use, modify,
10
* copy, or redistribute it subject to the terms and conditions of the GNU
11
* Lesser General Public License, as published by the Free Software Foundation.
13
* This program is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
18
* You should have received a copy of the GNU Lesser General Public License
19
* along with this distribution; if not, write to:
20
* Free Software Foundation, Inc.
21
* 51 Franklin Street, Fifth Floor
22
* Boston, MA 02110-1301 USA
24
package org.hibernate.bytecode.buildtime;
27
import java.io.ByteArrayInputStream;
29
import org.hibernate.bytecode.util.ClassDescriptor;
30
import org.hibernate.bytecode.util.BasicClassFilter;
31
import org.hibernate.bytecode.ClassTransformer;
32
import org.hibernate.bytecode.cglib.BytecodeProviderImpl;
33
import org.objectweb.asm.ClassReader;
34
import net.sf.cglib.core.ClassNameReader;
35
import net.sf.cglib.transform.impl.InterceptFieldEnabled;
38
* Strategy for performing build-time instrumentation of persistent classes in order to enable
39
* field-level interception using CGLIB.
41
* @author Steve Ebersole
44
public class CGLIBInstrumenter extends AbstractInstrumenter {
45
private static final BasicClassFilter CLASS_FILTER = new BasicClassFilter();
47
private final BytecodeProviderImpl provider = new BytecodeProviderImpl();
49
public CGLIBInstrumenter(Logger logger, Options options) {
50
super( logger, options );
53
protected ClassDescriptor getClassDescriptor(byte[] byecode) throws Exception {
54
return new CustomClassDescriptor( byecode );
57
protected ClassTransformer getClassTransformer(ClassDescriptor descriptor, Set classNames) {
58
if ( descriptor.isInstrumented() ) {
59
logger.debug( "class [" + descriptor.getName() + "] already instrumented" );
63
return provider.getTransformer( CLASS_FILTER, new CustomFieldFilter( descriptor, classNames ) );
67
private static class CustomClassDescriptor implements ClassDescriptor {
68
private final byte[] bytecode;
69
private final String name;
70
private final boolean isInstrumented;
72
public CustomClassDescriptor(byte[] bytecode) throws Exception {
73
this.bytecode = bytecode;
74
ClassReader reader = new ClassReader( new ByteArrayInputStream( bytecode ) );
75
String[] names = ClassNameReader.getClassInfo( reader );
77
boolean instrumented = false;
78
for ( int i = 1; i < names.length; i++ ) {
79
if ( InterceptFieldEnabled.class.getName().equals( names[i] ) ) {
84
this.isInstrumented = instrumented;
87
public String getName() {
91
public boolean isInstrumented() {
92
return isInstrumented;
95
public byte[] getBytes() {