KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > JDKReflect


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;
22
23 import java.io.ByteArrayInputStream JavaDoc;
24 import java.io.ByteArrayOutputStream JavaDoc;
25 import java.io.ObjectInputStream JavaDoc;
26 import java.io.ObjectOutputStream JavaDoc;
27 import java.lang.reflect.*;
28 import java.text.SimpleDateFormat JavaDoc;
29 import java.util.Date JavaDoc;
30
31 import com.db4o.reflect.Reflector;
32 import com.db4o.reflect.generic.*;
33 import com.db4o.reflect.jdk.JdkReflector;
34
35 /**
36  *
37  * package and class name are hard-referenced in JavaOnly#jdk()
38  *
39  * TODO: may need to use this on instead of JDK on .NET. Check!
40  *
41  */

42 class JDKReflect extends JDK {
43     Class JavaDoc constructorClass(){
44         return Constructor.class;
45     }
46     
47     Object JavaDoc deserialize(byte[] bytes) {
48         try {
49             return new ObjectInputStream JavaDoc(new ByteArrayInputStream JavaDoc(bytes)).readObject();
50         } catch (Exception JavaDoc e) {
51         }
52         return null;
53     }
54     
55     String JavaDoc format(Date JavaDoc date, boolean showTime) {
56         String JavaDoc fmt = "yyyy-MM-dd";
57         if (showTime) {
58             fmt += " HH:mm:ss";
59         }
60         return new SimpleDateFormat JavaDoc(fmt).format(date);
61     }
62     
63     /**
64      * use for system classes only, since not ClassLoader
65      * or Reflector-aware
66      */

67     final boolean methodIsAvailable(
68         String JavaDoc className,
69         String JavaDoc methodName,
70         Class JavaDoc[] params) {
71         
72         try {
73             
74             Class JavaDoc clazz = Class.forName(className);
75             if (clazz.getMethod(methodName, params) !=null) {
76                 return true;
77             }
78             return false;
79         } catch (Throwable JavaDoc t) {
80         }
81         return false;
82     }
83     
84     public static Object JavaDoc invoke (Class JavaDoc clazz, String JavaDoc methodName, Class JavaDoc[] paramClasses, Object JavaDoc[] params){
85         return invoke(clazz.getName(), methodName, paramClasses, params, null);
86     }
87     
88     
89     /**
90      * use for system classes only, since not ClassLoader
91      * or Reflector-aware
92      */

93     public static Object JavaDoc invoke (Object JavaDoc obj, String JavaDoc methodName, Class JavaDoc[] paramClasses, Object JavaDoc[] params){
94         return invoke(obj.getClass().getName(), methodName, paramClasses, params, obj );
95     }
96     
97     /**
98      * use for system classes only, since not ClassLoader
99      * or Reflector-aware
100      */

101     public static Object JavaDoc invoke (String JavaDoc className, String JavaDoc methodName, Class JavaDoc[] paramClasses, Object JavaDoc[] params, Object JavaDoc onObject){
102         try {
103                 Method method = getMethod(className, methodName, paramClasses);
104                 return method.invoke(onObject, params);
105             } catch (Throwable JavaDoc t) {
106             }
107         return null;
108     }
109
110     /**
111      * calling this "method" will break C# conversion with the old converter
112      *
113      * use for system classes only, since not ClassLoader
114      * or Reflector-aware
115      */

116     public static Method getMethod(String JavaDoc className, String JavaDoc methodName, Class JavaDoc[] paramClasses) {
117         try {
118             Class JavaDoc clazz = Class.forName(className);
119             Method method = clazz.getMethod(methodName, paramClasses);
120             return method;
121         } catch (Throwable JavaDoc t) {
122         }
123         return null;
124     }
125     
126     public void registerCollections(GenericReflector reflector) {
127         if(! Deploy.csharp){
128             reflector.registerCollection(java.util.Vector JavaDoc.class);
129             reflector.registerCollection(java.util.Hashtable JavaDoc.class);
130             reflector.registerCollectionUpdateDepth(java.util.Hashtable JavaDoc.class, 3);
131         }
132     }
133     
134     byte[] serialize(Object JavaDoc obj) throws Exception JavaDoc{
135         ByteArrayOutputStream JavaDoc byteStream = new ByteArrayOutputStream JavaDoc();
136         new ObjectOutputStream JavaDoc(byteStream).writeObject(obj);
137         return byteStream.toByteArray();
138     }
139
140     public Reflector createReflector(Object JavaDoc classLoader) {
141         if(classLoader==null) {
142             classLoader=getContextClassLoader();
143             
144             // FIXME: The new reflector does not like the ContextCloader at all.
145
// Resolve hierarchies.
146

147             // if (cl == null || classloaderName.indexOf("eclipse") >= 0) {
148
classLoader= Db4o.class.getClassLoader();
149             // }
150
}
151         return new JdkReflector((ClassLoader JavaDoc)classLoader);
152     }
153 }
154
Popular Tags