001: /*
002: * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.es.wrapper;
030:
031: import java.beans.IntrospectionException;
032: import java.beans.MethodDescriptor;
033: import java.lang.reflect.Method;
034: import java.lang.reflect.Modifier;
035:
036: /**
037: * Describes a method from a JavaScript perspective.
038: */
039: public class ESMethodDescriptor extends MethodDescriptor {
040: String name;
041: boolean overwrite;
042: boolean staticVirtual;
043: Class declaringClass;
044: String classJVMName;
045: Class[] parameterTypes;
046:
047: /**
048: * Create a new method descriptor.
049: *
050: * @param method the underlying java method.
051: * @param overwrite true if this method should overwrite the standard one.
052: * @param staticVirtual true if this is a "static-virtual" method.
053: */
054: public ESMethodDescriptor(Method method, boolean overwrite,
055: boolean staticVirtual) throws IntrospectionException {
056: super (method);
057:
058: this .name = method.getName();
059: if (this .name == null)
060: throw new RuntimeException();
061: this .overwrite = overwrite;
062: this .staticVirtual = staticVirtual;
063: }
064:
065: public ESMethodDescriptor(ESMethodDescriptor md)
066: throws IntrospectionException {
067: super (md.getMethod());
068:
069: this .name = md.getName();
070: this .overwrite = md.overwrite;
071: this .staticVirtual = md.staticVirtual;
072: this .declaringClass = md.declaringClass;
073: this .parameterTypes = md.parameterTypes;
074: this .classJVMName = md.classJVMName;
075: }
076:
077: public String getName() {
078: return name;
079: }
080:
081: public void setName(String name) {
082: this .name = name;
083: }
084:
085: /**
086: * True if this overwrites the standard method.
087: */
088: boolean isOverwrite() {
089: return overwrite;
090: }
091:
092: public boolean isStaticVirtual() {
093: return staticVirtual;
094: }
095:
096: /**
097: * True if this is a static method.
098: */
099: public boolean isStatic() {
100: return !staticVirtual
101: && Modifier.isStatic(getMethod().getModifiers());
102: }
103:
104: /**
105: * True if this method should replace md.
106: */
107: public boolean overwrites(ESMethodDescriptor md) {
108: if (!isStatic() && md.isStatic())
109: return true;
110: else if (isOverwrite() && !md.isOverwrite())
111: return true;
112: else
113: return false;
114: }
115:
116: /**
117: * Returns the method's parameter types.
118: */
119: public Class[] getParameterTypes() {
120: if (parameterTypes != null)
121: return parameterTypes;
122:
123: if (!isStaticVirtual())
124: parameterTypes = getMethod().getParameterTypes();
125: else {
126: Class[] realParam = getMethod().getParameterTypes();
127:
128: parameterTypes = new Class[realParam.length - 1];
129:
130: for (int i = 0; i < parameterTypes.length; i++)
131: parameterTypes[i] = realParam[i + 1];
132: }
133:
134: return parameterTypes;
135: }
136:
137: /**
138: * Returns the declaring class for the method.
139: */
140: public Class getDeclaringClass() {
141: if (declaringClass == null) {
142: if (staticVirtual)
143: declaringClass = getMethod().getParameterTypes()[0];
144: else
145: declaringClass = getMethod().getDeclaringClass();
146: }
147:
148: return declaringClass;
149: }
150:
151: /**
152: * Returns the return type of the method.
153: */
154: public Class getReturnType() {
155: return getMethod().getReturnType();
156: }
157:
158: /**
159: * Returns the class name for the method, i.e. the class we should
160: * cast to get the right method.
161: */
162: public String getMethodClassName() {
163: return getMethod().getDeclaringClass().getName();
164: }
165:
166: String getObjectClassName() {
167: return getDeclaringClass().getName();
168: }
169:
170: String getClassJVMName() {
171: if (classJVMName == null)
172: classJVMName = getDeclaringClass().getName().replace('.',
173: '/');
174:
175: return classJVMName;
176: }
177:
178: public String toString() {
179: Class[] param = getParameterTypes();
180:
181: if (param == null || param.length == 0)
182: return ("[ESMethodDescriptor "
183: + getDeclaringClass().getName() + "." + getName() + "]");
184: else
185: return ("[ESMethodDescriptor "
186: + getDeclaringClass().getName() + "." + getName()
187: + " " + param[0] + "]");
188: }
189: }
|