001: //
002: // This file is part of the prose package.
003: //
004: // The contents of this file are subject to the Mozilla Public License
005: // Version 1.1 (the "License"); you may not use this file except in
006: // compliance with the License. You may obtain a copy of the License at
007: // http://www.mozilla.org/MPL/
008: //
009: // Software distributed under the License is distributed on an "AS IS" basis,
010: // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
011: // for the specific language governing rights and limitations under the
012: // License.
013: //
014: // The Original Code is prose.
015: //
016: // The Initial Developers of the Original Code are Angela Nicoara and Gerald Linhofer.
017: // All Rights Reserved.
018: //
019: // Contributor(s):
020: // $Id$
021: // =====================================================================
022: //
023: // (history at end)
024: //
025: package ch.ethz.prose;
026:
027: import java.lang.reflect.Method;
028: import java.util.*;
029:
030: import ch.ethz.prose.crosscut.MethodRedefineCut;
031: import ch.ethz.prose.engine.MethodRedefineRequest;
032: import ch.ethz.inf.iks.jvmai.jvmdi.HotSwapClassWeaver;
033: import ch.ethz.jvmai.*;
034:
035: /**
036: * Weave and unweave method redefine requests.
037: *
038: * @author Angela Nicoara
039: * @author Gerald Linhofer
040: * @version $Revision$
041: */
042: public class HotSwapRedefineWeaver {
043:
044: /**
045: * Mapping from a crosscut (key) to a collection of method redefine requests
046: * (value).
047: */
048: protected Map requests = new HashMap();
049:
050: /**
051: * Weave a method redefinition.
052: *
053: * @param cut crosscut that contains advice method
054: * @param request
055: */
056: public void weaveMethodRedefineCut(MethodRedefineCut cut,
057: MethodRedefineRequest request) {
058: Method advice_method = cut.getMethod();
059: Method target_method = request.getMethod();
060:
061: MethodWeaver weaver = HotSwapClassWeaver
062: .getWeaver(target_method);
063: weaver.setRedefineAdvice(advice_method);
064: getRequests(cut).add(request);
065: }
066:
067: /**
068: * Unweave a method redefinition.
069: *
070: * @param cut crosscut which should be unwoven.
071: */
072: public void unweaveMethodRedefineCut(MethodRedefineCut cut) {
073: Iterator it = getRequests(cut).iterator();
074:
075: while (it.hasNext()) {
076: Method target_method = ((MethodRedefineRequest) it.next())
077: .getMethod();
078: MethodWeaver weaver = HotSwapClassWeaver
079: .getWeaver(target_method);
080:
081: weaver.setRedefineAdvice(null);
082: }
083:
084: requests.remove(cut);
085: }
086:
087: /**
088: * Get requests for `cut'.
089: *
090: * @param cut crosscut whose requests are returned
091: * @return requests for `cut'
092: */
093: protected Collection getRequests(MethodRedefineCut cut) {
094: Collection c = (Collection) requests.get(cut);
095: if (c == null) {
096: c = new LinkedList();
097: requests.put(cut, c);
098: }
099: return c;
100: }
101:
102: }
103:
104: //======================================================================
105: //
106: //$Log$
107: //
|