KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4odoc > f1 > reflections > ReflectorExample


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com */
2
3 package com.db4odoc.f1.reflections;
4
5
6 import java.io.File JavaDoc;
7 import java.io.IOException JavaDoc;
8
9 import com.db4o.Db4o;
10 import com.db4o.ObjectContainer;
11 import com.db4o.ObjectSet;
12 import com.db4o.query.Query;
13 import com.db4o.reflect.ReflectClass;
14 import com.db4o.reflect.ReflectConstructor;
15 import com.db4o.reflect.ReflectField;
16 import com.db4o.reflect.ReflectMethod;
17 import com.db4o.reflect.generic.GenericReflector;
18
19 public class ReflectorExample {
20     public final static String JavaDoc YAPFILENAME="formula1.yap";
21     public static void main(String JavaDoc[] args) throws IOException JavaDoc {
22         setCars();
23         getReflectorInfo();
24         getCars();
25         getCarInfo();
26     }
27     // end main
28

29     public static void setCars()
30     {
31          new File JavaDoc(YAPFILENAME).delete();
32          ObjectContainer db=Db4o.openFile(YAPFILENAME);
33         try {
34             Car car1 = new Car("BMW");
35             db.set(car1);
36             Car car2 = new Car("Ferrari");
37             db.set(car2);
38             
39             System.out.println("Saved:");
40             Query query = db.query();
41             query.constrain(Car.class);
42             ObjectSet results = query.execute();
43             listResult(results);
44         } finally {
45             db.close();
46         }
47     }
48     // end setCars
49

50     public static void getCars()
51     {
52         ObjectContainer db=Db4o.openFile(YAPFILENAME);
53         try {
54             GenericReflector reflector = new GenericReflector(null,db.ext().reflector());
55             ReflectClass carClass = reflector.forName(Car.class.getName());
56             System.out.println("Reflected class "+carClass);
57             System.out.println("Retrieved with reflector:");
58             Query query = db.query();
59             query.constrain(carClass);
60             ObjectSet results = query.execute();
61             listResult(results);
62         } finally {
63             db.close();
64         }
65     }
66     // end getCars
67

68     public static void getCarInfo()
69     {
70         ObjectContainer db=Db4o.openFile(YAPFILENAME);
71         try {
72             GenericReflector reflector = new GenericReflector(null,db.ext().reflector());
73             ReflectClass carClass = reflector.forName(Car.class.getName());
74             System.out.println("Reflected class "+carClass);
75              // public fields
76
System.out.println("FIELDS:");
77             ReflectField[] fields = carClass.getDeclaredFields();
78             for (int i = 0; i < fields.length; i++)
79                 System.out.println(fields[i].getName());
80             
81             // constructors
82
System.out.println("CONSTRUCTORS:");
83             ReflectConstructor[] cons = carClass.getDeclaredConstructors();
84             for (int i = 0; i < cons.length; i++)
85                 System.out.println( cons[i]);
86             
87             // public methods
88
System.out.println("METHODS:");
89             ReflectMethod method = carClass.getMethod("getPilot",null);
90             System.out.println(method.getClass());
91
92         } finally {
93             db.close();
94         }
95     }
96     // end getCarInfo
97

98     public static void getReflectorInfo()
99     {
100         ObjectContainer db=Db4o.openFile(YAPFILENAME);
101         try {
102             System.out.println("Reflector in use: " + db.ext().reflector());
103             System.out.println("Reflector delegate" +db.ext().reflector().getDelegate());
104             ReflectClass[] knownClasses = db.ext().reflector().knownClasses();
105             int count = knownClasses.length;
106             System.out.println("Known classes: " + count);
107             for (int i=0; i <knownClasses.length; i++){
108                 System.out.println(knownClasses[i]);
109             }
110         } finally {
111             db.close();
112         }
113     }
114     // end getReflectorInfo
115

116     public static void testReflector()
117     {
118         LoggingReflector logger = new LoggingReflector();
119         Db4o.configure().reflectWith(logger);
120         ObjectContainer db=Db4o.openFile(YAPFILENAME);
121         try {
122             ReflectClass rc = db.ext().reflector().forName(Car.class.getName());
123             System.out.println("Reflected class: " + rc);
124         } finally {
125             db.close();
126         }
127     }
128     // end testReflector
129

130     public static void listResult(ObjectSet result) {
131         System.out.println(result.size());
132         while(result.hasNext()) {
133             System.out.println(result.next());
134         }
135     }
136     // end listResult
137
}
138
Popular Tags