001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.bytecode;
031:
032: import com.caucho.util.CharBuffer;
033:
034: import java.lang.reflect.ParameterizedType;
035: import java.lang.reflect.Type;
036:
037: /**
038: * Wrapper around the java Class for a JClass.
039: */
040: public class JTypeWrapper implements JType {
041: private JClassLoader _loader;
042:
043: private ParameterizedType _type;
044:
045: public JTypeWrapper(JClassLoader loader, ParameterizedType type) {
046: _loader = loader;
047:
048: _type = type;
049: }
050:
051: /**
052: * Returns the class name.
053: */
054: public String getName() {
055: return ((Class) _type.getRawType()).getName();
056: }
057:
058: /**
059: * Returns the print name.
060: */
061: public String getPrintName() {
062: JType[] typeArgs = getActualTypeArguments();
063:
064: if (typeArgs.length == 0)
065: return getRawClass().getPrintName();
066:
067: CharBuffer cb = new CharBuffer();
068: cb.append(getRawClass().getPrintName());
069: cb.append('<');
070: for (int i = 0; i < typeArgs.length; i++) {
071: if (i != 0)
072: cb.append(',');
073:
074: cb.append(typeArgs[i].getPrintName());
075: }
076:
077: cb.append('>');
078:
079: return cb.toString();
080: }
081:
082: /**
083: * Returns the actual type arguments.
084: */
085: public JType[] getActualTypeArguments() {
086: Type[] rawArgs = _type.getActualTypeArguments();
087:
088: JType[] args = new JType[rawArgs.length];
089: for (int i = 0; i < args.length; i++) {
090: Type type = rawArgs[i];
091:
092: if (type instanceof Class)
093: args[i] = _loader.forName(((Class) type).getName());
094: else if (type instanceof ParameterizedType)
095: args[i] = new JTypeWrapper(_loader,
096: (ParameterizedType) type);
097: else {
098: args[i] = _loader.forName("java.lang.Object");
099: // jpa/0gg0
100: // throw new IllegalStateException(type.toString());
101: }
102: }
103:
104: return args;
105: }
106:
107: /**
108: * Returns the actual type arguments.
109: */
110: public JClass getRawType() {
111: return _loader.forName(((Class) _type.getRawType()).getName());
112: }
113:
114: /**
115: * Returns true for a primitive class.
116: */
117: public boolean isPrimitive() {
118: return getRawClass().isPrimitive();
119: }
120:
121: /**
122: * Returns true for a public class.
123: */
124: public boolean isPublic() {
125: return getRawClass().isPublic();
126: }
127:
128: /**
129: * Returns true for an abstract class
130: */
131: public boolean isAbstract() {
132: return getRawClass().isAbstract();
133: }
134:
135: /**
136: * Returns true for a final class
137: */
138: public boolean isFinal() {
139: return getRawClass().isFinal();
140: }
141:
142: /**
143: * Returns true for an interface
144: */
145: public boolean isInterface() {
146: return getRawClass().isAbstract();
147: }
148:
149: /**
150: * Returns the superclass.
151: */
152: public JClass getSuperClass() {
153: return getRawClass().getSuperClass();
154: }
155:
156: /**
157: * Returns the interfaces.
158: */
159: public JClass[] getInterfaces() {
160: return getRawClass().getInterfaces();
161: }
162:
163: /**
164: * Returns true for an array class.
165: */
166: public boolean isArray() {
167: return getRawClass().isArray();
168: }
169:
170: /**
171: * Returns the component for a class.
172: */
173: public JClass getComponentType() {
174: return null;
175: }
176:
177: /**
178: * Returns true if the jClass is assignable to the class.
179: */
180: public boolean isAssignableTo(Class cl) {
181: return getRawClass().isAssignableTo(cl);
182: }
183:
184: /**
185: * Returns true if the jClass is assignable to the class.
186: */
187: public boolean isAssignableFrom(Class cl) {
188: return getRawClass().isAssignableFrom(cl);
189: }
190:
191: /**
192: * Returns true if the jClass is assignable to the class.
193: */
194: public boolean isAssignableFrom(JClass cl) {
195: return getRawClass().isAssignableFrom(cl);
196: }
197:
198: /**
199: * Returns the declared methods
200: */
201: public JMethod[] getDeclaredMethods() {
202: return getRawClass().getDeclaredMethods();
203: }
204:
205: /**
206: * Returns the public methods
207: */
208: public JMethod[] getMethods() {
209: return getRawClass().getMethods();
210: }
211:
212: /**
213: * Returns the matching method.
214: */
215: public JMethod getMethod(String name, JClass[] param) {
216: return getRawClass().getMethod(name, param);
217: }
218:
219: /**
220: * Returns the declared fields
221: */
222: public JField[] getDeclaredFields() {
223: return getRawClass().getDeclaredFields();
224: }
225:
226: /**
227: * Returns the fields
228: */
229: public JField[] getFields() {
230: return getRawClass().getFields();
231: }
232:
233: private JClass getRawClass() {
234: return _loader.forName(((Class) _type.getRawType()).getName());
235: }
236: }
|