Db4o reflector can be used in your application just like normal java reflector. Let's create a new database with a couple of cars in it:
01private static void setCars() 02
{ 03
new File(DB4O_FILE_NAME).delete(); 04
ObjectContainer container=Db4o.openFile(DB4O_FILE_NAME); 05
try { 06
Car car1 = new Car("BMW"); 07
container.set(car1); 08
Car car2 = new Car("Ferrari"); 09
container.set(car2); 10
11
System.out.println("Saved:"); 12
Query query = container.query(); 13
query.constrain(Car.class); 14
ObjectSet results = query.execute(); 15
listResult(results); 16
} finally { 17
container.close(); 18
} 19
}
We can check, what information is available for db4o reflector:
01private static void getReflectorInfo() 02
{ 03
ObjectContainer container=Db4o.openFile(DB4O_FILE_NAME); 04
try { 05
System.out.println("Reflector in use: " + container.ext().reflector()); 06
System.out.println("Reflector delegate" +container.ext().reflector().getDelegate()); 07
ReflectClass[] knownClasses = container.ext().reflector().knownClasses(); 08
int count = knownClasses.length; 09
System.out.println("Known classes: " + count); 10
for (int i=0; i <knownClasses.length; i++){ 11
System.out.println(knownClasses[i]); 12
} 13
} finally { 14
container.close(); 15
} 16
}
All the information about Car class can also be retrieved through reflector:
01private static void getCarInfo() 02
{ 03
ObjectContainer container=Db4o.openFile(DB4O_FILE_NAME); 04
try { 05
GenericReflector reflector = new GenericReflector(null,container.ext().reflector()); 06
ReflectClass carClass = reflector.forName(Car.class.getName()); 07
System.out.println("Reflected class "+carClass); 08
// public fields 09
System.out.println("FIELDS:"); 10
ReflectField[] fields = carClass.getDeclaredFields(); 11
for (int i = 0; i < fields.length; i++) 12
System.out.println(fields[i].getName()); 13
14
// constructors 15
System.out.println("CONSTRUCTORS:"); 16
ReflectConstructor[] cons = carClass.getDeclaredConstructors(); 17
for (int i = 0; i < cons.length; i++) 18
System.out.println( cons[i]); 19
20
// public methods 21
System.out.println("METHODS:"); 22
ReflectMethod method = carClass.getMethod("getPilot",null); 23
System.out.println(method.getClass()); 24
25
} finally { 26
container.close(); 27
} 28
}