KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > instrument > Codifier


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.aop.instrument;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.Iterator JavaDoc;
27
28 import javassist.CannotCompileException;
29 import javassist.CtMethod;
30
31 /**
32  * Register pending codes, so that they can be added
33  * to a class bytecode in a later moment.
34  * @author Flavia Rainone
35  */

36 public class Codifier
37 {
38
39    private Collection JavaDoc pendingCodes;
40    
41    /**
42     * Constructor.
43     */

44    public Codifier()
45    {
46       this.pendingCodes = new ArrayList JavaDoc();
47    }
48    
49    /**
50     * Register the body of <code>method</code> as a pending code.
51     * @param method the method whose body will be <code>coded</code> in a later moment.
52     * @param body the future body of <code>method</code>.
53     */

54    public synchronized void addPendingCode(CtMethod method, String JavaDoc body)
55    {
56       PendingCode pendingCode = new PendingCode(method, body);
57       this.pendingCodes.add(pendingCode);
58    }
59    
60    /**
61     * Deploys pending code. In other words: changes the body of methods to their
62     * pending bodies, which must have been registered through <code>addPendingCode</code>
63     * method.
64     * @throws CannotCompileException thrown if javassist cannot compile a pending code,
65     * registered through <code>addPendingCode</code> method.
66     */

67    public synchronized void codifyPending() throws CannotCompileException
68    {
69       for (Iterator JavaDoc iterator = pendingCodes.iterator(); iterator.hasNext();)
70       {
71          PendingCode pendingCode = (PendingCode) iterator.next();
72          pendingCode.method.setBody(pendingCode.body);
73       }
74       pendingCodes.clear();
75    }
76    
77    /**
78     * Represents a pending code, to be applied to a method body in the appropriate moment.
79     */

80    private class PendingCode
81    {
82       CtMethod method;
83       String JavaDoc body;
84       
85       public PendingCode(CtMethod method, String JavaDoc body)
86       {
87          this.method = method;
88          this.body = body;
89       }
90    }
91 }
Popular Tags