KickJava   Java API By Example, From Geeks To Geeks.

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


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.CtClass;
28 import javassist.CtConstructor;
29 import javassist.NotFoundException;
30
31 import org.jboss.reflect.plugins.AnnotationHelper;
32 import org.jboss.reflect.spi.AnnotationValue;
33 import org.jboss.reflect.spi.ClassInfo;
34 import org.jboss.reflect.spi.ConstructorInfo;
35 import org.jboss.reflect.spi.ParameterInfo;
36 import org.jboss.reflect.spi.TypeInfo;
37 import org.jboss.util.JBossStringBuilder;
38
39 /**
40  * JavassistConstructor.
41  *
42  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
43  * @version $Revision: 57133 $
44  */

45 public class JavassistConstructorInfo extends JavassistAnnotatedParameterInfo implements ConstructorInfo
46 {
47    /** The reflection factory */
48    private static final JavassistReflectionFactory reflectionFactory = new JavassistReflectionFactory(true);
49  
50    /** The constructor */
51    private CtConstructor ctConstructor;
52    
53    /** The constructor implementation */
54    private transient JavassistConstructor constructor;
55
56    /**
57     * Create a new JavassistConstructor.
58     *
59     * @param annotationHelper the annotation helper
60     * @param typeInfo the type ifo
61     * @param ctConstructor the constructor
62     */

63    public JavassistConstructorInfo(AnnotationHelper annotationHelper, JavassistTypeInfo typeInfo, CtConstructor ctConstructor)
64    {
65       super(annotationHelper);
66       this.typeInfo = typeInfo;
67       this.ctConstructor = ctConstructor;
68       
69    }
70
71    public int getModifiers()
72    {
73       return ctConstructor.getModifiers();
74    }
75
76    public boolean isPublic()
77    {
78       return Modifier.isPublic(getModifiers());
79    }
80
81    public boolean isStatic()
82    {
83       return Modifier.isStatic(getModifiers());
84    }
85
86    public ClassInfo getDeclaringClass()
87    {
88       return typeInfo;
89    }
90
91    public ClassInfo[] getExceptionTypes()
92    {
93       if (exceptionTypes == null)
94       {
95          try
96          {
97             CtClass[] types = ctConstructor.getExceptionTypes();
98             exceptionTypes = new ClassInfo[types.length];
99             for (int i = 0; i < types.length; ++i)
100                exceptionTypes[i] = (ClassInfo) typeInfo.getFactory().getTypeInfo(types[i]);
101          }
102          catch (NotFoundException e)
103          {
104             throw JavassistTypeInfoFactoryImpl.raiseClassNotFound("for exception types of constructor", e);
105          }
106       }
107       return exceptionTypes;
108    }
109
110    public ParameterInfo[] getParameters()
111    {
112       if (parameters == null)
113          generateParameters();
114       return parameters;
115    }
116
117    public TypeInfo[] getParameterTypes()
118    {
119       if (parameterTypes == null)
120          generateParameters();
121       return parameterTypes;
122    }
123
124    public Object JavaDoc newInstance(Object JavaDoc[] args) throws Throwable JavaDoc
125    {
126       if (constructor == null)
127          constructor = reflectionFactory.createConstructor(ctConstructor);
128       return constructor.newInstance(args);
129    }
130
131    protected int getHashCode()
132    {
133       int result = getDeclaringClass().hashCode();
134       generateParameters();
135       if (parameterTypes != null)
136       {
137          for (int i = 0; i < parameterTypes.length; i++)
138             result = 29 * result + parameterTypes[i].hashCode();
139       }
140       return result;
141    }
142
143    public boolean equals(Object JavaDoc obj)
144    {
145       if (this == obj)
146          return true;
147       if (obj == null || obj instanceof ConstructorInfo == false)
148          return false;
149
150       final ConstructorInfo other = (ConstructorInfo) obj;
151       
152       if (getDeclaringClass().equals(other.getDeclaringClass()) == false)
153          return false;
154       return (Arrays.equals(getParameterTypes(), other.getParameterTypes()));
155    }
156
157
158    protected void toString(JBossStringBuilder buffer)
159    {
160       buffer.append(Arrays.asList(getParameterTypes()));
161       super.toString(buffer);
162    }
163    
164    /**
165     * Generate parameters
166     */

167    protected void generateParameters()
168    {
169       try
170       {
171          CtClass[] types = ctConstructor.getParameterTypes();
172          parameterTypes = new TypeInfo[types.length];
173          for (int i = 0; i < types.length; ++i)
174             parameterTypes[i] = typeInfo.getFactory().getTypeInfo(types[i]);
175          parameters = new ParameterInfo[types.length];
176          for (int i = 0; i < types.length; ++i)
177             parameters[i] = new JavassistParameterInfo(annotationHelper, this, i, parameterTypes[i]);
178       }
179       catch (NotFoundException e)
180       {
181          throw JavassistTypeInfoFactoryImpl.raiseClassNotFound("for parameters of constructor", e);
182       }
183    }
184
185    public AnnotationValue[] getAnnotations()
186    {
187       return getAnnotations(ctConstructor);
188    }
189
190    protected void createParameterAnnotations()
191    {
192       try
193       {
194          Object JavaDoc[][] parameterAnnotations = ctConstructor.getParameterAnnotations();
195          super.setupParameterAnnotations(parameterAnnotations);
196       }
197       catch (ClassNotFoundException JavaDoc e)
198       {
199          // AutoGenerated
200
throw new RuntimeException JavaDoc(e);
201       }
202    }
203 }
204
Popular Tags