~ubuntu-branches/ubuntu/wily/libhibernate3-java/wily-proposed

« back to all changes in this revision

Viewing changes to src/org/hibernate/tool/instrument/cglib/InstrumentTask.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2007-10-14 14:43:34 UTC
  • Revision ID: james.westby@ubuntu.com-20071014144334-eamc8i0q10gs1aro
Tags: upstream-3.2.5
ImportĀ upstreamĀ versionĀ 3.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//$Id: InstrumentTask.java 10209 2006-08-03 20:35:26Z steve.ebersole@jboss.com $
 
2
package org.hibernate.tool.instrument.cglib;
 
3
 
 
4
import org.hibernate.bytecode.util.BasicClassFilter;
 
5
import org.hibernate.bytecode.util.ClassDescriptor;
 
6
import org.hibernate.bytecode.cglib.BytecodeProviderImpl;
 
7
import org.hibernate.bytecode.ClassTransformer;
 
8
import org.hibernate.tool.instrument.BasicInstrumentationTask;
 
9
import org.objectweb.asm.ClassReader;
 
10
 
 
11
import java.io.ByteArrayInputStream;
 
12
 
 
13
import net.sf.cglib.core.ClassNameReader;
 
14
import net.sf.cglib.transform.impl.InterceptFieldEnabled;
 
15
 
 
16
/**
 
17
 * An Ant task for instrumenting persistent classes in order to enable
 
18
 * field-level interception using CGLIB.
 
19
 * <p/>
 
20
 * In order to use this task, typically you would define a a taskdef
 
21
 * similiar to:<pre>
 
22
 * <taskdef name="instrument" classname="org.hibernate.tool.instrument.cglib.InstrumentTask">
 
23
 *     <classpath refid="lib.class.path"/>
 
24
 * </taskdef>
 
25
 * </pre>
 
26
 * where <tt>lib.class.path</tt> is an ANT path reference containing all the
 
27
 * required Hibernate and CGLIB libraries.
 
28
 * <p/>
 
29
 * And then use it like:<pre>
 
30
 * <instrument verbose="true">
 
31
 *     <fileset dir="${testclasses.dir}/org/hibernate/test">
 
32
 *         <include name="yadda/yadda/**"/>
 
33
 *         ...
 
34
 *     </fileset>
 
35
 * </instrument>
 
36
 * </pre>
 
37
 * where the nested ANT fileset includes the class you would like to have
 
38
 * instrumented.
 
39
 * <p/>
 
40
 * Optionally you can chose to enable "Extended Instrumentation" if desired
 
41
 * by specifying the extended attriubute on the task:<pre>
 
42
 * <instrument verbose="true" extended="true">
 
43
 *     ...
 
44
 * </instrument>
 
45
 * </pre>
 
46
 * See the Hibernate manual regarding this option.
 
47
 *
 
48
 * @author Gavin King
 
49
 * @author Steve Ebersole
 
50
 */
 
51
public class InstrumentTask extends BasicInstrumentationTask {
 
52
 
 
53
        private static final BasicClassFilter CLASS_FILTER = new BasicClassFilter();
 
54
 
 
55
        private final BytecodeProviderImpl provider = new BytecodeProviderImpl();
 
56
 
 
57
 
 
58
        protected ClassDescriptor getClassDescriptor(byte[] byecode) throws Exception {
 
59
                return new CustomClassDescriptor( byecode );
 
60
        }
 
61
 
 
62
        protected ClassTransformer getClassTransformer(ClassDescriptor descriptor) {
 
63
                if ( descriptor.isInstrumented() ) {
 
64
                        logger.verbose( "class [" + descriptor.getName() + "] already instrumented" );
 
65
                        return null;
 
66
                }
 
67
                else {
 
68
                        return provider.getTransformer( CLASS_FILTER, new CustomFieldFilter( descriptor ) );
 
69
                }
 
70
        }
 
71
 
 
72
        private static class CustomClassDescriptor implements ClassDescriptor {
 
73
                private final byte[] bytecode;
 
74
                private final String name;
 
75
                private final boolean isInstrumented;
 
76
 
 
77
                public CustomClassDescriptor(byte[] bytecode) throws Exception {
 
78
                        this.bytecode = bytecode;
 
79
                        ClassReader reader = new ClassReader( new ByteArrayInputStream( bytecode ) );
 
80
                        String[] names = ClassNameReader.getClassInfo( reader );
 
81
                        this.name = names[0];
 
82
                        boolean instrumented = false;
 
83
                        for ( int i = 1; i < names.length; i++ ) {
 
84
                                if ( InterceptFieldEnabled.class.getName().equals( names[i] ) ) {
 
85
                                        instrumented = true;
 
86
                                        break;
 
87
                                }
 
88
                        }
 
89
                        this.isInstrumented = instrumented;
 
90
                }
 
91
 
 
92
                public String getName() {
 
93
                        return name;
 
94
                }
 
95
 
 
96
                public boolean isInstrumented() {
 
97
                        return isInstrumented;
 
98
                }
 
99
 
 
100
                public byte[] getBytes() {
 
101
                        return bytecode;
 
102
                }
 
103
        }
 
104
 
 
105
 
 
106
}