KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > bytecode > JTypeWrapper


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.bytecode;
31
32 import com.caucho.util.CharBuffer;
33
34 import java.lang.reflect.ParameterizedType JavaDoc;
35 import java.lang.reflect.Type JavaDoc;
36
37 /**
38  * Wrapper around the java Class for a JClass.
39  */

40 public class JTypeWrapper implements JType {
41   private JClassLoader _loader;
42   
43   private ParameterizedType JavaDoc _type;
44
45   public JTypeWrapper(JClassLoader loader, ParameterizedType JavaDoc type)
46   {
47     _loader = loader;
48     
49     _type = type;
50   }
51
52   /**
53    * Returns the class name.
54    */

55   public String JavaDoc getName()
56   {
57     return ((Class JavaDoc) _type.getRawType()).getName();
58   }
59
60   /**
61    * Returns the print name.
62    */

63   public String JavaDoc getPrintName()
64   {
65     JType []typeArgs = getActualTypeArguments();
66     
67     if (typeArgs.length == 0)
68       return getRawClass().getPrintName();
69
70     CharBuffer cb = new CharBuffer();
71     cb.append(getRawClass().getPrintName());
72     cb.append('<');
73     for (int i = 0; i < typeArgs.length; i++) {
74       if (i != 0)
75     cb.append(',');
76       
77       cb.append(typeArgs[i].getPrintName());
78     }
79     
80     cb.append('>');
81
82     return cb.toString();
83   }
84
85   /**
86    * Returns the actual type arguments.
87    */

88   public JType []getActualTypeArguments()
89   {
90     Type JavaDoc []rawArgs = _type.getActualTypeArguments();
91
92     JType []args = new JType[rawArgs.length];
93     for (int i = 0; i < args.length; i++) {
94       Type JavaDoc type = rawArgs[i];
95
96       if (type instanceof Class JavaDoc)
97     args[i] = _loader.forName(((Class JavaDoc) type).getName());
98       else if (type instanceof ParameterizedType JavaDoc)
99     args[i] = new JTypeWrapper(_loader, (ParameterizedType JavaDoc) type);
100       else {
101     args[i] = _loader.forName("java.lang.Object");
102     // jpa/0gg0
103
// throw new IllegalStateException(type.toString());
104
}
105     }
106
107     return args;
108   }
109
110   /**
111    * Returns the actual type arguments.
112    */

113   public JClass getRawType()
114   {
115     return _loader.forName(((Class JavaDoc) _type.getRawType()).getName());
116   }
117
118   
119   /**
120    * Returns true for a primitive class.
121    */

122   public boolean isPrimitive()
123   {
124     return getRawClass().isPrimitive();
125   }
126   
127   /**
128    * Returns true for a public class.
129    */

130   public boolean isPublic()
131   {
132     return getRawClass().isPublic();
133   }
134   
135   /**
136    * Returns true for an abstract class
137    */

138   public boolean isAbstract()
139   {
140     return getRawClass().isAbstract();
141   }
142   
143   /**
144    * Returns true for a final class
145    */

146   public boolean isFinal()
147   {
148     return getRawClass().isFinal();
149   }
150   
151   /**
152    * Returns true for an interface
153    */

154   public boolean isInterface()
155   {
156     return getRawClass().isAbstract();
157   }
158
159   /**
160    * Returns the superclass.
161    */

162   public JClass getSuperClass()
163   {
164     return getRawClass().getSuperClass();
165   }
166
167   /**
168    * Returns the interfaces.
169    */

170   public JClass []getInterfaces()
171   {
172     return getRawClass().getInterfaces();
173   }
174
175   /**
176    * Returns true for an array class.
177    */

178   public boolean isArray()
179   {
180     return getRawClass().isArray();
181   }
182
183   /**
184    * Returns the component for a class.
185    */

186   public JClass getComponentType()
187   {
188     return null;
189   }
190
191   /**
192    * Returns true if the jClass is assignable to the class.
193    */

194   public boolean isAssignableTo(Class JavaDoc cl)
195   {
196     return getRawClass().isAssignableTo(cl);
197   }
198
199   /**
200    * Returns true if the jClass is assignable to the class.
201    */

202   public boolean isAssignableFrom(Class JavaDoc cl)
203   {
204     return getRawClass().isAssignableFrom(cl);
205   }
206
207   /**
208    * Returns true if the jClass is assignable to the class.
209    */

210   public boolean isAssignableFrom(JClass cl)
211   {
212     return getRawClass().isAssignableFrom(cl);
213   }
214
215   /**
216    * Returns the declared methods
217    */

218   public JMethod []getDeclaredMethods()
219   {
220     return getRawClass().getDeclaredMethods();
221   }
222
223   /**
224    * Returns the public methods
225    */

226   public JMethod []getMethods()
227   {
228     return getRawClass().getMethods();
229   }
230
231   /**
232    * Returns the matching method.
233    */

234   public JMethod getMethod(String JavaDoc name, JClass []param)
235   {
236     return getRawClass().getMethod(name, param);
237   }
238
239   /**
240    * Returns the declared fields
241    */

242   public JField []getDeclaredFields()
243   {
244     return getRawClass().getDeclaredFields();
245   }
246
247   /**
248    * Returns the fields
249    */

250   public JField []getFields()
251   {
252     return getRawClass().getFields();
253   }
254
255   private JClass getRawClass()
256   {
257     return _loader.forName(((Class JavaDoc) _type.getRawType()).getName());
258   }
259 }
260
Popular Tags