KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > aspectwerkz > hook > JVMTIRedefiner


1 /**************************************************************************************
2  * Copyright (c) Jonas Bonér, Alexandre Vasseur. All rights reserved. *
3  * http://aspectwerkz.codehaus.org *
4  * ---------------------------------------------------------------------------------- *
5  * The software in this package is published under the terms of the LGPL license *
6  * a copy of which has been included with this distribution in the license.txt file. *
7  **************************************************************************************/

8 package org.codehaus.aspectwerkz.hook;
9
10 import org.codehaus.aspectwerkz.transform.inlining.deployer.Redefiner;
11 import org.codehaus.aspectwerkz.transform.inlining.deployer.ChangeSet;
12 import org.codehaus.aspectwerkz.transform.inlining.compiler.JoinPointFactory;
13 import org.codehaus.aspectwerkz.exception.WrappedRuntimeException;
14
15 import java.util.Iterator JavaDoc;
16 import java.lang.instrument.ClassDefinition JavaDoc;
17
18 /**
19  * Redefines classes using Java 5 HotSwap.
20  *
21  * @author <a HREF="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
22  */

23 public class JVMTIRedefiner implements Redefiner {
24
25     public JVMTIRedefiner() {
26         if (!Agent.getInstrumentation().isRedefineClassesSupported()) {
27             throw new UnsupportedOperationException JavaDoc("This Java 5 does not support JVMTI redefine()");
28         }
29     }
30
31     /**
32      * Redefines all classes affected by the change set according to the rules defined in the change set.
33      *
34      * @param changeSet
35      */

36     public void redefine(final ChangeSet changeSet) {
37         if (! Agent.getInstrumentation().isRedefineClassesSupported()) {
38             //TODO - should we fail ?
39
return;
40         }
41         ClassDefinition JavaDoc[] changes = new ClassDefinition JavaDoc[changeSet.getElements().size()];
42         int index = 0;
43         for (Iterator JavaDoc it = changeSet.getElements().iterator(); it.hasNext(); index++) {
44             ChangeSet.Element changeSetElement = (ChangeSet.Element) it.next();
45             final byte[] bytecode = JoinPointFactory.redefineJoinPoint(changeSetElement.getCompilationInfo());
46             changes[index] = new ClassDefinition JavaDoc(changeSetElement.getJoinPointInfo().getJoinPointClass(), bytecode);
47         }
48         try {
49             Agent.getInstrumentation().redefineClasses(changes);
50         } catch (Exception JavaDoc e) {
51             throw new WrappedRuntimeException(e);
52         }
53     }
54 }
55
Popular Tags