KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.L10N;
38
39 import com.caucho.java.JavaWriter;
40
41 import com.caucho.java.gen.BaseMethod;
42
43 /**
44  * Basic method generation.
45  */

46 public class AopMethod extends BaseMethod {
47   private static final L10N L = new L10N(AopMethod.class);
48
49   private String _fieldName;
50   
51   /**
52    * Creates the base method
53    */

54   public AopMethod(JMethod apiMethod, JMethod implMethod)
55   {
56     super(apiMethod, implMethod);
57   }
58   
59   /**
60    * Generates the code for the class.
61    *
62    * @param out the writer to the output stream.
63    */

64   public void generate(JavaWriter out)
65     throws IOException
66   {
67     _fieldName = "__caucho_aop_" + out.generateId();
68
69     generateInterceptors(out);
70     
71     super.generate(out);
72   }
73
74   private void generateInterceptors(JavaWriter out)
75     throws IOException
76   {
77     out.println();
78     out.println("private static class " + _fieldName + " extends com.caucho.aop.MethodInvocationImpl {");
79     out.pushDepth();
80     out.print("private static java.lang.reflect.Method _method = getMethod(");
81     out.print(getMethod().getDeclaringClass().getName() + ".class");
82
83     out.print(", \"" + getMethodName() + "\", new Class[] {");
84     JClass []types = getParameterTypes();
85     for (int i = 0; i < types.length; i++) {
86       if (i != 0)
87     out.print(", ");
88       out.print(types[i].getPrintName());
89       out.print(".class");
90     }
91     out.println("});");
92     out.print("private static java.lang.reflect.Method _superMethod = getMethod(");
93     out.print(getMethod().getDeclaringClass().getName() + ".class");
94
95     out.print(", \"" + getMethodName() + "__super\", new Class[] {");
96     for (int i = 0; i < types.length; i++) {
97       if (i != 0)
98     out.print(", ");
99       out.print(types[i].getPrintName());
100       out.print(".class");
101     }
102     out.println("});");
103     out.println();
104     out.println("static final org.aopalliance.intercept.MethodInterceptor _interceptor = com.caucho.aop.MethodInterceptorBuilder.create(_method);");
105     
106     out.println();
107     out.print(_fieldName + "(");
108
109     out.print("Object t");
110     
111     JClass []parameterTypes = getParameterTypes();
112     for (int i = 0; i < parameterTypes.length; i++) {
113       out.print(", ");
114       
115       out.print(parameterTypes[i].getPrintName());
116       
117       out.print(" a" + i);
118     }
119     
120     out.println(")");
121     out.println("{");
122     out.pushDepth();
123
124     out.print("super(t, new Object[] { ");
125     
126     for (int i = 0; i < parameterTypes.length; i++) {
127       if (i != 0)
128     out.print(", ");
129       
130       JClass type = parameterTypes[i];
131
132       if ("boolean".equals(type.getName()))
133     out.print("new Boolean(a" + i + ")");
134       else if ("byte".equals(type.getName()))
135     out.print("new Byte(a" + i + ")");
136       else if ("short".equals(type.getName()))
137     out.print("new Short(a" + i + ")");
138       else if ("int".equals(type.getName()))
139     out.print("new Integer(a" + i + ")");
140       else if ("long".equals(type.getName()))
141     out.print("new Long(a" + i + ")");
142       else if ("float".equals(type.getName()))
143     out.print("new Float(a" + i + ")");
144       else if ("double".equals(type.getName()))
145     out.print("new Double(a" + i + ")");
146       else if ("char".equals(type.getName()))
147     out.print("new Character(a" + i + ")");
148       else
149     out.print("a" + i);
150     }
151     out.println("});");
152     
153     out.popDepth();
154     out.println("}");
155
156     out.println();
157     out.println("public java.lang.reflect.Method getMethod() { return _method; }");
158     out.println("protected java.lang.reflect.Method getSuperMethod() { return _superMethod; }");
159     
160     out.popDepth();
161     out.println("}");
162   }
163   
164   /**
165    * Generates the code for the call.
166    *
167    * @param out the writer to the output stream.
168    * @param args the arguments
169    */

170   protected void generateCall(JavaWriter out, String []args)
171     throws IOException
172   {
173     super.generateCall(out, args);
174   }
175 }
176
Popular Tags