~raginggoblin/infolog/infolog

« back to all changes in this revision

Viewing changes to InfologServer/lib/hibernate-distribution-3.3.2.GA/project/core/src/main/java/org/hibernate/bytecode/buildtime/CGLIBInstrumenter.java

  • Committer: Raging Goblin
  • Date: 2013-11-16 16:51:32 UTC
  • Revision ID: raging_goblin-20131116165132-weujnptzc88uy4ah
Mavenized the project, now using shared project InfologSync

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Hibernate, Relational Persistence for Idiomatic Java
3
 
 *
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.
8
 
 *
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.
12
 
 *
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
16
 
 * for more details.
17
 
 *
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
23
 
 */
24
 
package org.hibernate.bytecode.buildtime;
25
 
 
26
 
import java.util.Set;
27
 
import java.io.ByteArrayInputStream;
28
 
 
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;
36
 
 
37
 
/**
38
 
 * Strategy for performing build-time instrumentation of persistent classes in order to enable
39
 
 * field-level interception using CGLIB.
40
 
 *
41
 
 * @author Steve Ebersole
42
 
 * @author Gavin King
43
 
 */
44
 
public class CGLIBInstrumenter extends AbstractInstrumenter {
45
 
        private static final BasicClassFilter CLASS_FILTER = new BasicClassFilter();
46
 
 
47
 
        private final BytecodeProviderImpl provider = new BytecodeProviderImpl();
48
 
 
49
 
        public CGLIBInstrumenter(Logger logger, Options options) {
50
 
                super( logger, options );
51
 
        }
52
 
 
53
 
        protected ClassDescriptor getClassDescriptor(byte[] byecode) throws Exception {
54
 
                return new CustomClassDescriptor( byecode );
55
 
        }
56
 
 
57
 
        protected ClassTransformer getClassTransformer(ClassDescriptor descriptor, Set classNames) {
58
 
                if ( descriptor.isInstrumented() ) {
59
 
                        logger.debug( "class [" + descriptor.getName() + "] already instrumented" );
60
 
                        return null;
61
 
                }
62
 
                else {
63
 
                        return provider.getTransformer( CLASS_FILTER, new CustomFieldFilter( descriptor, classNames ) );
64
 
                }
65
 
        }
66
 
 
67
 
        private static class CustomClassDescriptor implements ClassDescriptor {
68
 
                private final byte[] bytecode;
69
 
                private final String name;
70
 
                private final boolean isInstrumented;
71
 
 
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 );
76
 
                        this.name = names[0];
77
 
                        boolean instrumented = false;
78
 
                        for ( int i = 1; i < names.length; i++ ) {
79
 
                                if ( InterceptFieldEnabled.class.getName().equals( names[i] ) ) {
80
 
                                        instrumented = true;
81
 
                                        break;
82
 
                                }
83
 
                        }
84
 
                        this.isInstrumented = instrumented;
85
 
                }
86
 
 
87
 
                public String getName() {
88
 
                        return name;
89
 
                }
90
 
 
91
 
                public boolean isInstrumented() {
92
 
                        return isInstrumented;
93
 
                }
94
 
 
95
 
                public byte[] getBytes() {
96
 
                        return bytecode;
97
 
                }
98
 
        }
99
 
 
100
 
}