KickJava   Java API By Example, From Geeks To Geeks.

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


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
25 import javassist.CannotCompileException;
26 import javassist.CtClass;
27 import javassist.CtConstructor;
28 import javassist.CtNewConstructor;
29 import javassist.NotFoundException;
30
31 import org.jboss.aop.classpool.AOPClassPool;
32
33 /**
34  * Comment
35  *
36  * @author <a HREF="mailto:kabir.khan@jboss.org">Kabir Khan</a>
37  * @version $Revision$
38  */

39 public abstract class OptimizedInvocations
40 {
41    public static void defrostClassIfExists(AOPClassPool pool, String JavaDoc className)
42    {
43       try
44       {
45          //In some cases we get a class frozen exception. I guess if the invocation class
46
//existed, method was unwrapped and the wrapped again
47
CtClass existing = pool.get(className);
48          existing.defrost();
49       }
50       catch (NotFoundException e)
51       {
52          //Ignore, we are creating the class the first time
53
}
54    }
55
56    /**
57     *
58     * @param pool The AOPClassPool to create the optimized invocation class in
59     * @param makeInnerClass If true creates the new class as an inner class of className
60     * @param outerClass The class to create the invocation class as an inner class of if makeInnerClass==true
61     * @param className The full class name (including package info) of the invocation class to be created
62     * @param superInvocation The super class of this invocation
63     * @return The created invocation class
64     */

65    public static CtClass makeInvocationClass(AOPClassPool pool,
66          boolean makeInnerClass,
67          CtClass outerClass,
68          String JavaDoc className,
69          CtClass superInvocation)throws CannotCompileException, NotFoundException
70    {
71    
72       CtClass invocation = makeInvocationClassNoCtors(pool, makeInnerClass, outerClass, className, superInvocation);
73    
74       //Add the invocation constructor
75
CtConstructor[] cons = superInvocation.getDeclaredConstructors();
76       for (int i = 0; i < cons.length; i++)
77       {
78          CtConstructor conTemplate = superInvocation.getDeclaredConstructors()[i];
79          CtConstructor icon = CtNewConstructor.make(conTemplate.getParameterTypes(),
80                   conTemplate.getExceptionTypes(),
81                   invocation);
82          invocation.addConstructor(icon);
83       }
84    
85       return invocation;
86    }
87    
88    public static CtClass makeInvocationClassNoCtors(AOPClassPool pool,
89          boolean makeInnerClass,
90          CtClass outerClass,
91          String JavaDoc className,
92          CtClass superInvocation)throws CannotCompileException, NotFoundException
93    {
94       CtClass untransformable = pool.get("org.jboss.aop.instrument.Untransformable");
95    
96       CtClass invocation;
97       if (makeInnerClass)
98       {
99          //Strip away the package from classname
100
String JavaDoc innerClassName = className.substring(className.lastIndexOf('.') + 1);
101          defrostClassIfExists(pool, outerClass.getName() + "$" + innerClassName);
102       
103          //Only static nested classes are supported
104
boolean classStatic = true;
105          invocation = TransformerCommon.makeNestedClass(outerClass, innerClassName, classStatic);
106          invocation.setSuperclass(superInvocation);
107       }
108       else
109       {
110          defrostClassIfExists(pool, className);
111          invocation = TransformerCommon.makeClass(pool, className, superInvocation);
112       }
113       
114       invocation.addInterface(untransformable);
115       return invocation;
116    }
117 }
118
Popular Tags