KickJava   Java API By Example, From Geeks To Geeks.

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


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

40 public abstract class OptimizedBehaviourInvocations extends OptimizedInvocations
41 {
42    protected static void addSetArguments(ClassPool pool, CtClass invocation, CtClass[] params)throws NotFoundException, CannotCompileException
43    {
44       if (params == null || params.length == 0) return;
45       CtClass methodInvocation = pool.get("org.jboss.aop.joinpoint.MethodInvocation");
46       CtMethod template = methodInvocation.getDeclaredMethod("setArguments");
47    
48       String JavaDoc code =
49               "public void setArguments(java.lang.Object[] args)" +
50               "{ ";
51       code += " arguments = args; ";
52       for (int i = 0; i < params.length; i++)
53       {
54          if (params[i].isPrimitive())
55          {
56             CtPrimitiveType primitive = (CtPrimitiveType) params[i];
57             code += " arg" + i + " = ((" + primitive.getWrapperName() + ")args[" + i + "])." + primitive.getGetMethodName() + "(); ";
58          }
59          else
60          {
61             code += " Object warg" + i + " = args[" + i + "]; ";
62             code += " arg" + i + " = (" + params[i].getName() + ")warg" + i + "; ";
63          }
64       }
65       code += "}";
66       CtMethod setArguments = CtNewMethod.make(code, invocation);
67       setArguments.setModifiers(template.getModifiers());
68       invocation.addMethod(setArguments);
69    }
70
71    public static void addGetArguments(ClassPool pool, CtClass invocation, CtClass[] params) throws CannotCompileException
72    {
73       addGetArguments(pool, invocation, params, false);
74    }
75
76    public static void addGetArguments(ClassPool pool, CtClass invocation, CtClass[] params, boolean hasMarshalledArguments) throws CannotCompileException
77    {
78       if (params == null || params.length == 0) return;
79       try {
80          CtClass superInvocation = invocation.getSuperclass();
81          CtMethod template = superInvocation.getDeclaredMethod("getArguments");
82    
83          StringBuffer JavaDoc code = new StringBuffer JavaDoc();
84          code.append("public Object[] getArguments()");
85          code.append("{ ");
86          
87          if (hasMarshalledArguments)
88          {
89             code.append(" if (super.marshalledArguments != null)");
90             code.append(" {");
91             code.append(" Object[] args = super.getArguments();");
92             code.append(" setArguments(args);");
93             code.append(" return args;");
94             code.append(" }");
95          }
96          
97          code.append(" if (arguments != (Object[])null) { return (Object[])arguments; } ");
98          code.append(" arguments = new Object[" + params.length + "]; ");
99          for (int i = 0; i < params.length; i++)
100          {
101             code.append(" arguments[" + i + "] = ($w)arg" + i + "; ");
102          }
103    
104          code.append(" return arguments; }");
105          CtMethod getArguments = CtNewMethod.make(code.toString(), invocation);
106          getArguments.setModifiers(template.getModifiers());
107          invocation.addMethod(getArguments);
108       } catch (NotFoundException e) {
109         //Field invocations don't have a getArguments() method, that's fine
110
}
111    }
112
113    protected static String JavaDoc setArguments(int length)
114    {
115       return setArguments("invocation", length, 0);
116    }
117
118    protected static String JavaDoc setArguments(String JavaDoc inv, int length, int offset)
119    {
120       StringBuffer JavaDoc sb = new StringBuffer JavaDoc("");
121       for (int i = 0 ; i < length ; i++)
122       {
123          sb.append(inv + ".arg" + (i) + " = $" + (i + 1 + offset) + "; ");
124       }
125       return sb.toString();
126    }
127
128    /** Adds fields arg0, arg1 etc. to the invocation class for storing the parameters for a method
129     *
130     * @param invocation The invocation we want to add
131     * @param params Array of the types of the parameters
132     * @throws CannotCompileException
133     */

134    public static void addArgumentFieldsToInvocation(CtClass invocation, CtClass[] params)throws CannotCompileException
135    {
136       for (int i = 0 ; i < params.length ; i++)
137       {
138          CtField field = new CtField(params[i], "arg" + i, invocation);
139          field.setModifiers(Modifier.PUBLIC);
140          invocation.addField(field);
141       }
142    }
143 }
144
Popular Tags