KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > reflect > plugins > javassist > JavassistMethodInfo


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2006, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.reflect.plugins.javassist;
23
24 import java.lang.reflect.Modifier JavaDoc;
25 import java.util.Arrays JavaDoc;
26
27 import javassist.CtBehavior;
28 import javassist.CtClass;
29 import javassist.CtMethod;
30 import javassist.NotFoundException;
31
32 import org.jboss.reflect.plugins.AnnotationHelper;
33 import org.jboss.reflect.spi.AnnotationValue;
34 import org.jboss.reflect.spi.ClassInfo;
35 import org.jboss.reflect.spi.MethodInfo;
36 import org.jboss.reflect.spi.ParameterInfo;
37 import org.jboss.reflect.spi.TypeInfo;
38 import org.jboss.util.JBossStringBuilder;
39
40 public class JavassistMethodInfo extends JavassistAnnotatedParameterInfo implements MethodInfo
41 {
42    /** The reflection factory */
43    private static final JavassistReflectionFactory reflectionFactory = new JavassistReflectionFactory(true);
44    
45    /** The key */
46    private SignatureKey key;
47    
48    /** The method */
49    private CtMethod ctMethod;
50    
51    /** The method implementation */
52    private transient JavassistMethod method;
53    
54    /** The return type */
55    private transient TypeInfo returnType;
56    
57    /**
58     * Create a new JavassistMethodInfo.
59     *
60     * @param annotationHelper the annotation helper
61     * @param typeInfo the type info
62     * @param key the key
63     * @param ctMethod the method
64     */

65    public JavassistMethodInfo(AnnotationHelper annotationHelper, JavassistTypeInfo typeInfo, SignatureKey key, CtMethod ctMethod)
66    {
67       super(annotationHelper);
68       this.typeInfo = typeInfo;
69       this.key = key;
70       this.ctMethod = ctMethod;
71    }
72
73    public String JavaDoc getName()
74    {
75       return key.name;
76    }
77
78    public ClassInfo getDeclaringClass()
79    {
80       return typeInfo;
81    }
82
83    public int getModifiers()
84    {
85       return ctMethod.getModifiers();
86    }
87
88    public boolean isPublic()
89    {
90       return Modifier.isPublic(getModifiers());
91    }
92
93    public boolean isStatic()
94    {
95       return Modifier.isStatic(getModifiers());
96    }
97
98    public ClassInfo[] getExceptionTypes()
99    {
100       if (exceptionTypes == null)
101       {
102          try
103          {
104             CtClass[] types = ctMethod.getExceptionTypes();
105             exceptionTypes = new ClassInfo[types.length];
106             for (int i = 0; i < types.length; ++i)
107                exceptionTypes[i] = (ClassInfo) typeInfo.getFactory().getTypeInfo(types[i]);
108          }
109          catch (NotFoundException e)
110          {
111             throw JavassistTypeInfoFactoryImpl.raiseClassNotFound("for exception types of method " + getName(), e);
112          }
113       }
114       return exceptionTypes;
115    }
116
117    public TypeInfo getReturnType()
118    {
119       if (returnType != null)
120          return returnType;
121       try
122       {
123          CtClass clazz = ctMethod.getReturnType();
124          returnType = typeInfo.getFactory().getTypeInfo(clazz);
125          return returnType;
126       }
127       catch (NotFoundException e)
128       {
129          throw JavassistTypeInfoFactoryImpl.raiseClassNotFound("for return type of method " + getName(), e);
130       }
131    }
132
133    public ParameterInfo[] getParameters()
134    {
135       if (parameters == null)
136          generateParameters();
137       return parameters;
138    }
139
140    public TypeInfo[] getParameterTypes()
141    {
142       if (parameterTypes == null)
143          generateParameters();
144       return parameterTypes;
145    }
146
147    public Object JavaDoc invoke(Object JavaDoc target, Object JavaDoc[] args) throws Throwable JavaDoc
148    {
149       if (method == null)
150          method = reflectionFactory.createMethod(ctMethod);
151       return method.invoke(target, args);
152    }
153
154    protected int getHashCode()
155    {
156       return getName().hashCode();
157    }
158
159    public boolean equals(Object JavaDoc obj)
160    {
161       if (obj == this)
162          return true;
163       if (obj == null || obj instanceof MethodInfo == false)
164          return false;
165
166       final MethodInfo other = (MethodInfo) obj;
167
168       if (getName().equals(other.getName()) == false)
169          return false;
170       if (getDeclaringClass().equals(other.getDeclaringClass()) == false)
171          return false;
172       if (getReturnType().equals(other.getReturnType()) == false)
173          return false;
174       return Arrays.equals(getParameterTypes(), other.getParameterTypes());
175    }
176
177    public void toShortString(JBossStringBuilder buffer)
178    {
179       buffer.append(getName());
180    }
181
182    protected void toString(JBossStringBuilder buffer)
183    {
184       buffer.append("name=").append(getName());
185       super.toString(buffer);
186    }
187    
188    /**
189     * Generate parameters
190     */

191    protected void generateParameters()
192    {
193       try
194       {
195          CtClass[] types = ctMethod.getParameterTypes();
196          parameterTypes = new TypeInfo[types.length];
197          for (int i = 0; i < types.length; ++i)
198             parameterTypes[i] = typeInfo.getFactory().getTypeInfo(types[i]);
199          parameters = new ParameterInfo[types.length];
200          for (int i = 0; i < types.length; ++i)
201             parameters[i] = new JavassistParameterInfo(annotationHelper, this, i, parameterTypes[i]);
202       }
203       catch (NotFoundException e)
204       {
205          throw JavassistTypeInfoFactoryImpl.raiseClassNotFound("for parameters of " + getName(), e);
206       }
207    }
208    
209    public AnnotationValue[] getAnnotations()
210    {
211       return getAnnotations(ctMethod);
212    }
213
214    protected CtBehavior getParameterizedObject()
215    {
216       return ctMethod;
217    }
218
219    protected void createParameterAnnotations()
220    {
221       try
222       {
223          Object JavaDoc[][] parameterAnnotations = ctMethod.getParameterAnnotations();
224          super.setupParameterAnnotations(parameterAnnotations);
225       }
226       catch (ClassNotFoundException JavaDoc e)
227       {
228          // AutoGenerated
229
throw new RuntimeException JavaDoc(e);
230       }
231    }
232
233 }
234
Popular Tags