KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > es > wrapper > ESMethodDescriptor


1 /*
2  * Copyright (c) 1998-2006 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  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.es.wrapper;
30
31 import java.beans.IntrospectionException JavaDoc;
32 import java.beans.MethodDescriptor JavaDoc;
33 import java.lang.reflect.Method JavaDoc;
34 import java.lang.reflect.Modifier JavaDoc;
35
36 /**
37  * Describes a method from a JavaScript perspective.
38  */

39 public class ESMethodDescriptor extends MethodDescriptor JavaDoc {
40   String JavaDoc name;
41   boolean overwrite;
42   boolean staticVirtual;
43   Class JavaDoc declaringClass;
44   String JavaDoc classJVMName;
45   Class JavaDoc []parameterTypes;
46
47   /**
48    * Create a new method descriptor.
49    *
50    * @param method the underlying java method.
51    * @param overwrite true if this method should overwrite the standard one.
52    * @param staticVirtual true if this is a "static-virtual" method.
53    */

54   public ESMethodDescriptor(Method JavaDoc method, boolean overwrite,
55                 boolean staticVirtual)
56     throws IntrospectionException JavaDoc
57   {
58     super(method);
59
60     this.name = method.getName();
61     if (this.name == null)
62       throw new RuntimeException JavaDoc();
63     this.overwrite = overwrite;
64     this.staticVirtual = staticVirtual;
65   }
66
67   public ESMethodDescriptor(ESMethodDescriptor md)
68     throws IntrospectionException JavaDoc
69   {
70     super(md.getMethod());
71
72     this.name = md.getName();
73     this.overwrite = md.overwrite;
74     this.staticVirtual = md.staticVirtual;
75     this.declaringClass = md.declaringClass;
76     this.parameterTypes = md.parameterTypes;
77     this.classJVMName = md.classJVMName;
78   }
79
80   public String JavaDoc getName()
81   {
82     return name;
83   }
84
85   public void setName(String JavaDoc name)
86   {
87     this.name = name;
88   }
89
90   /**
91    * True if this overwrites the standard method.
92    */

93   boolean isOverwrite()
94   {
95     return overwrite;
96   }
97
98   public boolean isStaticVirtual()
99   {
100     return staticVirtual;
101   }
102
103   /**
104    * True if this is a static method.
105    */

106   public boolean isStatic()
107   {
108     return ! staticVirtual && Modifier.isStatic(getMethod().getModifiers());
109   }
110
111   /**
112    * True if this method should replace md.
113    */

114   public boolean overwrites(ESMethodDescriptor md)
115   {
116     if (! isStatic() && md.isStatic())
117       return true;
118     else if (isOverwrite() && ! md.isOverwrite())
119       return true;
120     else
121       return false;
122   }
123
124   /**
125    * Returns the method's parameter types.
126    */

127   public Class JavaDoc []getParameterTypes()
128   {
129     if (parameterTypes != null)
130       return parameterTypes;
131
132     if (! isStaticVirtual())
133       parameterTypes = getMethod().getParameterTypes();
134     else {
135       Class JavaDoc []realParam = getMethod().getParameterTypes();
136
137       parameterTypes = new Class JavaDoc[realParam.length - 1];
138       
139       for (int i = 0; i < parameterTypes.length; i++)
140         parameterTypes[i] = realParam[i + 1];
141     }
142
143     return parameterTypes;
144   }
145   
146   /**
147    * Returns the declaring class for the method.
148    */

149   public Class JavaDoc getDeclaringClass()
150   {
151     if (declaringClass == null) {
152       if (staticVirtual)
153     declaringClass = getMethod().getParameterTypes()[0];
154       else
155     declaringClass = getMethod().getDeclaringClass();
156     }
157
158     return declaringClass;
159   }
160
161   /**
162    * Returns the return type of the method.
163    */

164   public Class JavaDoc getReturnType()
165   {
166     return getMethod().getReturnType();
167   }
168
169   /**
170    * Returns the class name for the method, i.e. the class we should
171    * cast to get the right method.
172    */

173   public String JavaDoc getMethodClassName()
174   {
175     return getMethod().getDeclaringClass().getName();
176   }
177
178   String JavaDoc getObjectClassName()
179   {
180     return getDeclaringClass().getName();
181   }
182
183   String JavaDoc getClassJVMName()
184   {
185     if (classJVMName == null)
186       classJVMName = getDeclaringClass().getName().replace('.', '/');
187
188     return classJVMName;
189   }
190
191   public String JavaDoc toString()
192   {
193     Class JavaDoc []param = getParameterTypes();
194
195     if (param == null || param.length == 0)
196       return ("[ESMethodDescriptor " + getDeclaringClass().getName() +
197               "." + getName() + "]");
198     else
199       return ("[ESMethodDescriptor " + getDeclaringClass().getName() +
200               "." + getName() + " " + param[0] + "]");
201   }
202 }
203
204
205
Popular Tags