KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.db4o.reflect.*;
24
25 public class JdkReflector implements Reflector{
26     
27     private final ClassLoader JavaDoc _classLoader;
28     private Reflector _parent;
29     private ReflectArray _array;
30     
31     public JdkReflector(ClassLoader JavaDoc classLoader){
32         _classLoader = classLoader;
33     }
34     
35     public ReflectArray array(){
36         if(_array == null){
37             _array = new JdkArray(_parent);
38         }
39         return _array;
40     }
41     
42     public boolean constructorCallsSupported(){
43         return true;
44     }
45     
46     public Object JavaDoc deepClone(Object JavaDoc obj) {
47         return new JdkReflector(_classLoader);
48     }
49     
50     public ReflectClass forClass(Class JavaDoc clazz){
51         return new JdkClass(_parent, clazz);
52     }
53     
54     public ReflectClass forName(String JavaDoc className) {
55         try {
56             Class JavaDoc clazz = _classLoader == null ? Class.forName(className) : _classLoader.loadClass(className);
57             if(clazz == null){
58                 return null;
59             }
60             return new JdkClass(_parent, clazz);
61         }
62         catch(Exception JavaDoc exc) {
63         }
64         return null;
65     }
66     
67     public ReflectClass forObject(Object JavaDoc a_object) {
68         if(a_object == null){
69             return null;
70         }
71         return _parent.forClass(a_object.getClass());
72     }
73     
74     public boolean isCollection(ReflectClass candidate) {
75         return false;
76     }
77
78     public boolean methodCallsSupported(){
79         return true;
80     }
81
82     public void setParent(Reflector reflector) {
83         _parent = reflector;
84     }
85
86     public static ReflectClass[] toMeta(Reflector reflector, Class JavaDoc[] clazz){
87         ReflectClass[] claxx = null;
88         if(clazz != null){
89             claxx = new ReflectClass[clazz.length];
90             for (int i = 0; i < clazz.length; i++) {
91                 if(clazz[i] != null){
92                     claxx[i] = reflector.forClass(clazz[i]);
93                 }
94             }
95         }
96         return claxx;
97     }
98     
99     static Class JavaDoc[] toNative(ReflectClass[] claxx){
100         Class JavaDoc[] clazz = null;
101         if(claxx != null){
102             clazz = new Class JavaDoc[claxx.length];
103             for (int i = 0; i < claxx.length; i++) {
104                 clazz[i] = toNative(claxx[i]);
105             }
106         }
107         return clazz;
108     }
109     
110     public static Class JavaDoc toNative(ReflectClass claxx){
111         if(claxx == null){
112             return null;
113         }
114         if(claxx instanceof JdkClass){
115             return ((JdkClass)claxx).getJavaClass();
116         }
117         ReflectClass d = claxx.getDelegate();
118         if(d == claxx){
119             return null;
120         }
121         return toNative(d);
122     }
123
124 }
125
Popular Tags