KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > aop > AopVarComponent


1 /*
2  * Copyright (c) 1998-2004 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.aop;
31
32 import java.io.IOException;
33
34 import com.caucho.bytecode.JClass;
35 import com.caucho.bytecode.JMethod;
36
37 import com.caucho.java.JavaWriter;
38
39 import com.caucho.java.gen.ClassComponent;
40
41 import com.caucho.util.L10N;
42
43 /**
44  * Creates the pre-method variables for aop.
45  */

46 public class AopVarComponent extends ClassComponent {
47   private static final L10N L = new L10N(AopVarComponent.class);
48
49   private String _fieldName;
50   private JMethod _apiMethod;
51   private JMethod _implMethod;
52   private boolean _isStatic = true;
53   
54   /**
55    * Creates the base method
56    */

57   public AopVarComponent(JMethod apiMethod, JMethod implMethod)
58   {
59     _apiMethod = apiMethod;
60     _implMethod = implMethod;
61   }
62
63   /**
64    * Sets true for a static instance.
65    */

66   public void setStatic(boolean isStatic)
67   {
68     _isStatic = isStatic;
69   }
70
71   /**
72    * Returns the field name.
73    */

74   public String getFieldName()
75   {
76     return _fieldName;
77   }
78
79   /**
80    * Returns the method.
81    */

82   public JMethod getMethod()
83   {
84     return _apiMethod;
85   }
86   
87   /**
88    * Generates the code for the class.
89    *
90    * @param out the writer to the output stream.
91    */

92   public void generate(JavaWriter out)
93     throws IOException
94   {
95     _fieldName = "__caucho_aop_" + out.generateId();
96
97     generateInterceptors(out);
98   }
99
100   private void generateInterceptors(JavaWriter out)
101     throws IOException
102   {
103     JMethod jMethod = getMethod();
104     
105     out.println();
106     out.println("private static class " + _fieldName + " extends com.caucho.aop.MethodInvocationImpl {");
107     out.pushDepth();
108     out.print("private static java.lang.reflect.Method _method = getMethod(");
109     out.print(jMethod.getDeclaringClass().getName() + ".class");
110
111     out.print(", \"" + jMethod.getName() + "\", new Class[] {");
112     JClass []types = jMethod.getParameterTypes();
113     for (int i = 0; i < types.length; i++) {
114       if (i != 0)
115     out.print(", ");
116       out.print(types[i].getPrintName());
117       out.print(".class");
118     }
119     out.println("});");
120     out.print("private static java.lang.reflect.Method _superMethod = getMethod(");
121     out.print(jMethod.getDeclaringClass().getName() + ".class");
122
123     out.print(", \"" + jMethod.getName() + "__super\", new Class[] {");
124     for (int i = 0; i < types.length; i++) {
125       if (i != 0)
126     out.print(", ");
127       out.print(types[i].getPrintName());
128       out.print(".class");
129     }
130     out.println("});");
131     
132     out.println();
133     if (_isStatic)
134       out.print("static ");
135     else
136       out.print("transient ");
137
138     out.println("final org.aopalliance.intercept.MethodInterceptor _interceptor = com.caucho.aop.MethodInterceptorBuilder.create(_method);");
139     
140     out.println();
141     out.print(_fieldName + "(");
142
143     out.print("Object t");
144     
145     JClass []parameterTypes = jMethod.getParameterTypes();
146     for (int i = 0; i < parameterTypes.length; i++) {
147       out.print(", ");
148       
149       out.print(parameterTypes[i].getPrintName());
150       
151       out.print(" a" + i);
152     }
153     
154     out.println(")");
155     out.println("{");
156     out.pushDepth();
157
158     out.print("super(t, new Object[] { ");
159     
160     for (int i = 0; i < parameterTypes.length; i++) {
161       if (i != 0)
162     out.print(", ");
163       
164       JClass type = parameterTypes[i];
165
166       if ("boolean".equals(type.getName()))
167     out.print("new Boolean(a" + i + ")");
168       else if ("byte".equals(type.getName()))
169     out.print("new Byte(a" + i + ")");
170       else if ("short".equals(type.getName()))
171     out.print("new Short(a" + i + ")");
172       else if ("int".equals(type.getName()))
173     out.print("new Integer(a" + i + ")");
174       else if ("long".equals(type.getName()))
175     out.print("new Long(a" + i + ")");
176       else if ("float".equals(type.getName()))
177     out.print("new Float(a" + i + ")");
178       else if ("double".equals(type.getName()))
179     out.print("new Double(a" + i + ")");
180       else if ("char".equals(type.getName()))
181     out.print("new Character(a" + i + ")");
182       else
183     out.print("a" + i);
184     }
185     out.println("});");
186     
187     out.popDepth();
188     out.println("}");
189
190     out.println();
191     out.println("public java.lang.reflect.Method getMethod() { return _method; }");
192     out.println("protected java.lang.reflect.Method getSuperMethod() { return _superMethod; }");
193     
194     out.popDepth();
195     out.println("}");
196   }
197 }
198
Popular Tags