KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > reflect > jdk > JdkClass


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.reflect.jdk;
22
23 import java.lang.reflect.*;
24
25 import com.db4o.*;
26 import com.db4o.reflect.*;
27
28 /**
29  * Reflection implementation for Class to map to JDK reflection.
30  */

31 public class JdkClass implements ReflectClass{
32     
33     private final Reflector _reflector;
34     private final Class JavaDoc _clazz;
35     private ReflectConstructor _constructor;
36     private Object JavaDoc[] _constructorParams;
37     
38     public JdkClass(Reflector reflector, Class JavaDoc clazz) {
39         if(reflector == null){
40             throw new NullPointerException JavaDoc();
41         }
42         _reflector = reflector;
43         _clazz = clazz;
44     }
45     
46     public ReflectClass getComponentType() {
47         return _reflector.forClass(_clazz.getComponentType());
48     }
49
50     public ReflectConstructor[] getDeclaredConstructors(){
51         Constructor[] constructors = _clazz.getDeclaredConstructors();
52         ReflectConstructor[] reflectors = new ReflectConstructor[constructors.length];
53         for (int i = 0; i < constructors.length; i++) {
54             reflectors[i] = new JdkConstructor(_reflector, constructors[i]);
55         }
56         return reflectors;
57     }
58     
59     public ReflectField getDeclaredField(String JavaDoc name){
60         try {
61             return new JdkField(_reflector, _clazz.getDeclaredField(name));
62         } catch (Exception JavaDoc e) {
63             return null;
64         }
65     }
66     
67     public ReflectField[] getDeclaredFields(){
68         Field[] fields = _clazz.getDeclaredFields();
69         ReflectField[] reflectors = new ReflectField[fields.length];
70         for (int i = 0; i < reflectors.length; i++) {
71             reflectors[i] = new JdkField(_reflector, fields[i]);
72         }
73         return reflectors;
74     }
75     
76     public ReflectClass getDelegate(){
77         return this;
78     }
79     
80     public ReflectMethod getMethod(String JavaDoc methodName, ReflectClass[] paramClasses){
81         try {
82             Method method = _clazz.getMethod(methodName, JdkReflector.toNative(paramClasses));
83             if(method == null){
84                 return null;
85             }
86             return new JdkMethod(method, reflector());
87         } catch (Exception JavaDoc e) {
88             return null;
89         }
90     }
91     
92     public String JavaDoc getName(){
93         return _clazz.getName();
94     }
95     
96     public ReflectClass getSuperclass() {
97         return _reflector.forClass(_clazz.getSuperclass());
98     }
99     
100     public boolean isAbstract(){
101         return Modifier.isAbstract(_clazz.getModifiers());
102     }
103     
104     public boolean isArray() {
105         return _clazz.isArray();
106     }
107
108     public boolean isAssignableFrom(ReflectClass type) {
109         if(!(type instanceof JdkClass)) {
110             return false;
111         }
112         return _clazz.isAssignableFrom(JdkReflector.toNative(type));
113     }
114     
115     public boolean isCollection() {
116         return _reflector.isCollection(this);
117     }
118     
119     public boolean isInstance(Object JavaDoc obj) {
120         return _clazz.isInstance(obj);
121     }
122     
123     public boolean isInterface(){
124         return _clazz.isInterface();
125     }
126     
127     public boolean isPrimitive() {
128         return _clazz.isPrimitive();
129     }
130     
131     public boolean isSecondClass() {
132         
133         return isPrimitive();
134         
135         // TODO: Internal SecondClass needs testing with many test cases first.
136
// Not sure if the following could break Entry class.
137

138         // return isPrimitive()||SecondClass.class.isAssignableFrom(_clazz);
139
}
140     
141     public Object JavaDoc newInstance(){
142         try {
143             if(_constructor == null){
144                 return _clazz.newInstance();
145             }
146             return _constructor.newInstance(_constructorParams);
147         } catch (Throwable JavaDoc t) {
148         }
149         return null;
150     }
151     
152     Class JavaDoc getJavaClass(){
153         return _clazz;
154     }
155     
156     public Reflector reflector() {
157         return _reflector;
158     }
159     
160     public boolean skipConstructor(boolean flag){
161         if(flag){
162             Constructor constructor = Platform4.jdk().serializableConstructor(_clazz);
163             if(constructor != null){
164                 try{
165                     Object JavaDoc o = constructor.newInstance((Object JavaDoc[])null);
166                     if(o != null){
167                         useConstructor(new JdkConstructor(_reflector, constructor), null);
168                         return true;
169                     }
170                 }catch(Throwable JavaDoc t){
171                     
172                 }
173             }
174         }
175         useConstructor(null, null);
176         return false;
177     }
178     
179     public void useConstructor(ReflectConstructor constructor, Object JavaDoc[] params){
180         this._constructor = constructor;
181         _constructorParams = params;
182     }
183
184     public Object JavaDoc[] toArray(Object JavaDoc obj){
185         return null;
186     }
187 }
188
Popular Tags