KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > reflect > plugins > MethodInfoImpl


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, 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;
23
24 import java.lang.reflect.Modifier JavaDoc;
25 import java.util.Arrays JavaDoc;
26
27 import org.jboss.reflect.spi.AnnotationValue;
28 import org.jboss.reflect.spi.ClassInfo;
29 import org.jboss.reflect.spi.MethodInfo;
30 import org.jboss.reflect.spi.ParameterInfo;
31 import org.jboss.reflect.spi.TypeInfo;
32 import org.jboss.util.JBossStringBuilder;
33 import org.jboss.util.NotImplementedException;
34
35 /**
36  * Method info
37  *
38  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
39  * @author <a HREF="mailto:adrian@jboss.org">Adrian Brock</a>
40  */

41 public class MethodInfoImpl extends AnnotationHolder implements MethodInfo
42 {
43    /** serialVersionUID */
44    private static final long serialVersionUID = 3257007670035756341L;
45
46    /** The method name */
47    protected String JavaDoc name;
48    
49    /** The declaring class */
50    protected ClassInfo declaringClass;
51    
52    /** The parameter types */
53    protected TypeInfo[] parameterTypes;
54    
55    /** The parameters */
56    protected ParameterInfo[] parameters;
57    
58    /** The exception types */
59    protected ClassInfo[] exceptionTypes;
60    
61    /** The modifiers */
62    protected int modifiers;
63    
64    /** The return type */
65    protected TypeInfo returnType;
66    
67    /** The hash code */
68    protected int hash;
69
70    /**
71     * Create a new method info
72     */

73    public MethodInfoImpl()
74    {
75    }
76
77    /**
78     * Create a new MethodInfo.
79     *
80     * @param annotations the annotations
81     * @param name the method name
82     * @param returnType the return type
83     * @param parameterTypes the parameter types
84     * @param parameterAnnotations the parameter annotations
85     * @param exceptionTypes the exception types
86     * @param modifiers the modifiers
87     * @param declaring the declaring class
88     */

89    public MethodInfoImpl(AnnotationValue[] annotations, String JavaDoc name, TypeInfo returnType, TypeInfo[] parameterTypes, AnnotationValue[][] parameterAnnotations, ClassInfo[] exceptionTypes, int modifiers, ClassInfo declaring)
90    {
91       super(annotations);
92       this.name = name;
93       if (parameterTypes == null)
94       {
95          this.parameterTypes = MethodInfo.NO_PARAMS_TYPES;
96          this.parameters = MethodInfo.NO_PARAMS;
97       }
98       else
99       {
100          this.parameterTypes = parameterTypes;
101          this.parameters = new ParameterInfoImpl[parameterTypes.length];
102          for (int i = 0; i < parameterTypes.length; ++i)
103             this.parameters[i] = new ParameterInfoImpl(parameterAnnotations[i], null, parameterTypes[i]);
104       }
105       if (exceptionTypes == null)
106          this.exceptionTypes = MethodInfo.NO_EXCEPTIONS;
107       else
108          this.exceptionTypes = exceptionTypes;
109       this.modifiers = modifiers;
110       this.declaringClass = declaring;
111       this.returnType = returnType;
112       calculateHash();
113    }
114
115    /**
116     * Create a new MethodInfo.
117     *
118     * @param annotations the annotations
119     * @param name the method name
120     * @param returnType the return type
121     * @param parameters the parameters
122     * @param exceptionTypes the exception types
123     * @param modifiers the modifiers
124     * @param declaring the declaring class
125     */

126    public MethodInfoImpl(AnnotationValue[] annotations, String JavaDoc name, TypeInfo returnType, ParameterInfo[] parameters, ClassInfo[] exceptionTypes, int modifiers, ClassInfo declaring)
127    {
128       super(annotations);
129       this.name = name;
130       if (parameters == null || parameters.length == 0)
131       {
132          this.parameterTypes = MethodInfo.NO_PARAMS_TYPES;
133          this.parameters = MethodInfo.NO_PARAMS;
134       }
135       else
136       {
137          this.parameters = parameters;
138          this.parameterTypes = new TypeInfo[parameters.length];
139          for (int i = 0; i < parameters.length; ++i)
140             this.parameterTypes[i] = parameters[i].getParameterType();
141       }
142       if (exceptionTypes == null || exceptionTypes.length == 0)
143          this.exceptionTypes = MethodInfo.NO_EXCEPTIONS;
144       else
145          this.exceptionTypes = exceptionTypes;
146       this.modifiers = modifiers;
147       this.declaringClass = declaring;
148       this.returnType = returnType;
149       calculateHash();
150    }
151
152    public String JavaDoc getName()
153    {
154       return name;
155    }
156    
157    public ClassInfo getDeclaringClass()
158    {
159       return declaringClass;
160    }
161
162    public TypeInfo[] getParameterTypes()
163    {
164       return parameterTypes;
165    }
166
167    public ParameterInfo[] getParameters()
168    {
169       return parameters;
170    }
171
172    public ClassInfo[] getExceptionTypes()
173    {
174       return exceptionTypes;
175    }
176    
177    public TypeInfo getReturnType()
178    {
179       return returnType;
180    }
181    
182    public int getModifiers()
183    {
184       return modifiers;
185    }
186    
187    public boolean isStatic()
188    {
189       return Modifier.isStatic(modifiers);
190    }
191    
192    public boolean isPublic()
193    {
194       return Modifier.isPublic(modifiers);
195    }
196    
197    public Object JavaDoc invoke(Object JavaDoc target, Object JavaDoc[] args) throws Throwable JavaDoc
198    {
199       throw new NotImplementedException("invoke");
200    }
201
202    protected void toString(JBossStringBuilder buffer)
203    {
204       buffer.append("name=").append(name);
205       buffer.append(Arrays.asList(parameterTypes));
206       buffer.append(" return=").append(returnType);
207    }
208    
209    public void toShortString(JBossStringBuilder buffer)
210    {
211       buffer.append(name);
212    }
213
214    public boolean equals(Object JavaDoc obj)
215    {
216       if (this == obj) return true;
217       if (obj == null || obj instanceof MethodInfo == false)
218          return false;
219
220       final MethodInfo other = (MethodInfo) obj;
221
222       if (name.equals(other.getName()) == false)
223          return false;
224       if (declaringClass.equals(other.getDeclaringClass()) == false)
225          return false;
226       if (returnType.equals(other.getReturnType()) == false)
227          return false;
228       return Arrays.equals(parameterTypes, other.getParameterTypes());
229    }
230
231    public int hashCode()
232    {
233       return hash;
234    }
235
236    /**
237     * Calculate the hash code
238     */

239    protected void calculateHash()
240    {
241       hash = name.hashCode();
242    }
243 }
244
Popular Tags