01: /**************************************************************************************
02: * Copyright (c) Jonas Bonér, Alexandre Vasseur. All rights reserved. *
03: * http://aspectwerkz.codehaus.org *
04: * ---------------------------------------------------------------------------------- *
05: * The software in this package is published under the terms of the LGPL license *
06: * a copy of which has been included with this distribution in the license.txt file. *
07: **************************************************************************************/package org.codehaus.aspectwerkz.hook;
08:
09: import org.codehaus.aspectwerkz.transform.inlining.deployer.Redefiner;
10: import org.codehaus.aspectwerkz.transform.inlining.deployer.ChangeSet;
11: import org.codehaus.aspectwerkz.transform.inlining.compiler.JoinPointFactory;
12: import org.codehaus.aspectwerkz.exception.WrappedRuntimeException;
13:
14: import java.util.Iterator;
15: import java.lang.instrument.ClassDefinition;
16:
17: /**
18: * Redefines classes using Java 5 HotSwap.
19: *
20: * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
21: */
22: public class JVMTIRedefiner implements Redefiner {
23:
24: public JVMTIRedefiner() {
25: if (!Agent.getInstrumentation().isRedefineClassesSupported()) {
26: throw new UnsupportedOperationException(
27: "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[] changes = new ClassDefinition[changeSet
42: .getElements().size()];
43: int index = 0;
44: for (Iterator it = changeSet.getElements().iterator(); it
45: .hasNext(); index++) {
46: ChangeSet.Element changeSetElement = (ChangeSet.Element) it
47: .next();
48: final byte[] bytecode = JoinPointFactory
49: .redefineJoinPoint(changeSetElement
50: .getCompilationInfo());
51: changes[index] = new ClassDefinition(changeSetElement
52: .getJoinPointInfo().getJoinPointClass(), bytecode);
53: }
54: try {
55: Agent.getInstrumentation().redefineClasses(changes);
56: } catch (Exception e) {
57: throw new WrappedRuntimeException(e);
58: }
59: }
60: }
|