001: /*
002: * Copyright (c) 1998-2008 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: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.bytecode;
031:
032: import java.lang.annotation.Annotation;
033:
034: import java.lang.reflect.Method;
035: import java.lang.reflect.Modifier;
036: import java.lang.reflect.ParameterizedType;
037: import java.lang.reflect.Type;
038:
039: /**
040: * Wrapper around the Java Method for a JMethod.
041: */
042: public class JMethodWrapper extends JMethod {
043: private JClassLoader _loader;
044:
045: private Method _method;
046:
047: public JMethodWrapper(Method method) {
048: this (method, JClassLoaderWrapper.create());
049: }
050:
051: public JMethodWrapper(Method method, JClassLoader loader) {
052: if (loader == null)
053: throw new NullPointerException();
054:
055: _method = method;
056: _loader = loader;
057: }
058:
059: /**
060: * Returns the method name.
061: */
062: public String getName() {
063: return _method.getName();
064: }
065:
066: /**
067: * Returns true for a static method.
068: */
069: public boolean isStatic() {
070: return Modifier.isStatic(_method.getModifiers());
071: }
072:
073: /**
074: * Returns true for a private method
075: */
076: public boolean isPrivate() {
077: return Modifier.isPrivate(_method.getModifiers());
078: }
079:
080: /**
081: * Returns true for a public method.
082: */
083: public boolean isPublic() {
084: return Modifier.isPublic(_method.getModifiers());
085: }
086:
087: /**
088: * Returns true for a protected method.
089: */
090: public boolean isProtected() {
091: return Modifier.isProtected(_method.getModifiers());
092: }
093:
094: /**
095: * Returns true for a final method.
096: */
097: public boolean isFinal() {
098: return Modifier.isFinal(_method.getModifiers());
099: }
100:
101: /**
102: * Returns true for an abstract method.
103: */
104: public boolean isAbstract() {
105: return Modifier.isAbstract(_method.getModifiers());
106: }
107:
108: /**
109: * Returns the declaring type.
110: */
111: public JClass getDeclaringClass() {
112: return _loader.forName(_method.getDeclaringClass().getName());
113: }
114:
115: /**
116: * Returns the return type.
117: */
118: public JClass getReturnType() {
119: return _loader.forName(_method.getReturnType().getName());
120: }
121:
122: /**
123: * Returns the return type.
124: */
125: public JType getGenericReturnType() {
126: try {
127: Type retType = _method.getGenericReturnType();
128:
129: if (retType instanceof Class)
130: return _loader.forName(((Class) retType).getName());
131: else
132: return new JTypeWrapper(_loader,
133: (ParameterizedType) retType);
134: } catch (NoSuchMethodError e) {
135: return getReturnType();
136: }
137:
138: }
139:
140: /**
141: * Returns the parameter types.
142: */
143: public JClass[] getParameterTypes() {
144: Class[] types = _method.getParameterTypes();
145:
146: JClass[] jTypes = new JClass[types.length];
147:
148: for (int i = 0; i < types.length; i++) {
149: jTypes[i] = _loader.forName(types[i].getName());
150: }
151:
152: return jTypes;
153: }
154:
155: /**
156: * Returns the exception types.
157: */
158: public JClass[] getExceptionTypes() {
159: Class[] types = _method.getExceptionTypes();
160:
161: JClass[] jTypes = new JClass[types.length];
162:
163: for (int i = 0; i < types.length; i++) {
164: jTypes[i] = _loader.forName(types[i].getName());
165: }
166:
167: return jTypes;
168: }
169:
170: /**
171: * Returns the annotation.
172: */
173: @Override
174: public JAnnotation[] getDeclaredAnnotations() {
175: Annotation[] ann = _method.getAnnotations();
176:
177: JAnnotation[] jAnn = new JAnnotation[ann.length];
178:
179: for (int i = 0; i < ann.length; i++) {
180: jAnn[i] = new JAnnotationWrapper(ann[i]);
181: }
182:
183: return jAnn;
184: }
185: }
|