001 /*
002 * Copyright 2002-2004 The Apache Software Foundation
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.apache.commons.clazz.reflect.common;
017
018 import java.lang.reflect.Method;
019
020 import org.apache.commons.clazz.Clazz;
021 import org.apache.commons.clazz.ClazzFeature;
022 import org.apache.commons.clazz.ClazzLoader;
023 import org.apache.commons.clazz.common.ClazzFeatureSupport;
024
025 /**
026 * A wrapper for a java method.
027 *
028 * @author <a href="mailto:dmitri@apache.org">Dmitri Plotnikov</a>
029 * @version $Id: ReflectedMethodFeatureSupport.java 155436 2005-02-26 13:17:48Z dirkv $
030 */
031 public class ReflectedMethodFeatureSupport extends ClazzFeatureSupport
032 implements ClazzFeature
033 {
034 private Method method;
035 private String signature;
036 private static final Clazz[] EMPTY_CLAZZ_ARRAY = new Clazz[0];
037 private Clazz[] parameterClazzes;
038 private boolean returnClazzCached = false;
039 private Clazz returnClazz;
040
041 /**
042 * Constructor for ReflectedClazzProperty.
043 */
044 public ReflectedMethodFeatureSupport(Clazz declaringClazz, Method method) {
045 super(declaringClazz);
046 this.method = method;
047 }
048
049 public Method getMethod() {
050 return method;
051 }
052
053 public String getName() {
054 return method.getName();
055 }
056
057 /**
058 * @see org.apache.commons.clazz.ClazzOperation#getSignature()
059 */
060 public String getSignature() {
061 if (signature == null) {
062 signature =
063 Clazz.constructSignature(
064 method.getName(),
065 method.getParameterTypes());
066 }
067 return signature;
068 }
069
070 /**
071 * @see org.apache.commons.clazz.ClazzOperation#getParameterClazzes()
072 */
073 public Clazz[] getParameterClazzes() {
074 if (parameterClazzes == null) {
075 Class paramClasses[] = method.getParameterTypes();
076 if (paramClasses == null) {
077 parameterClazzes = EMPTY_CLAZZ_ARRAY;
078 }
079 else {
080 parameterClazzes = new Clazz[paramClasses.length];
081 ClazzLoader loader = getDeclaringClazz().getClazzLoader();
082 for (int i = 0; i < paramClasses.length; i++) {
083 String name = Clazz.getCanonicalClassName(paramClasses[i]);
084 parameterClazzes[i] = loader.getClazzForName(name);
085 }
086 }
087 }
088 return parameterClazzes;
089 }
090
091 /**
092 * @see org.apache.commons.clazz.ClazzOperation#getReturnClazz()
093 */
094 public Clazz getReturnClazz() {
095 if (!returnClazzCached) {
096 returnClazzCached = true;
097 Class returnType = method.getReturnType();
098 if (returnType == null || returnType.equals(Void.TYPE)) {
099 returnClazz = null;
100 }
101 else {
102 ClazzLoader loader = getDeclaringClazz().getClazzLoader();
103 returnClazz = loader.getClazzForName(returnType.getName());
104 }
105 }
106 return returnClazz;
107 }
108
109 public String toString() {
110 return getSignature();
111 }
112 }