KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > test > reflect > GenericObjects


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.test.reflect;
22
23 import java.util.*;
24
25 import com.db4o.reflect.*;
26 import com.db4o.reflect.generic.*;
27 import com.db4o.reflect.jdk.*;
28
29 public class GenericObjects extends Test {
30
31     private GenericReflector _reflector;
32     private final GenericClass _objectIClass;
33
34     private GenericClass _iClass;
35     
36     public GenericObjects() throws ClassNotFoundException JavaDoc {
37         _reflector = new GenericReflector(null, new JdkReflector(Thread.currentThread().getContextClassLoader()));
38         _objectIClass = (GenericClass)_reflector.forClass(Object JavaDoc.class);
39     }
40
41     public void test() throws ClassNotFoundException JavaDoc {
42         _reflector.register(acmeDataClass());
43         _iClass = (GenericClass)_reflector.forName("com.acme.Person");
44         _assert(_iClass.getName().equals("com.acme.Person"));
45         _assert(_iClass.getSuperclass() == _objectIClass);
46         
47         _assert(_iClass.isAssignableFrom(subclass()));
48         _assert(!_iClass.isAssignableFrom(otherDataClass()));
49         _assert(!_iClass.isAssignableFrom(_objectIClass));
50     
51         _assert(_iClass.isInstance(_iClass.newInstance()));
52         _assert(_iClass.isInstance(subclass().newInstance()));
53         _assert(!_iClass.isInstance(otherDataClass().newInstance()));
54         _assert(!_iClass.isInstance("whatever"));
55         
56         _assert(_reflector.forObject(_iClass.newInstance()) == _iClass);
57         
58         tstFields();
59         tstReflectionDelegation();
60         
61
62     }
63
64     private void tstReflectionDelegation() throws ClassNotFoundException JavaDoc {
65         Reflection test = new Reflection(new GenericReflector(null, new JdkReflector(Thread.currentThread().getContextClassLoader())));
66         test.tstEverything();
67     }
68
69     private GenericClass otherDataClass() {
70         return new GenericClass(_reflector, null, "anyName", _objectIClass);
71     }
72
73     private GenericClass subclass() {
74         return new GenericClass(_reflector, null, "anyName", _iClass);
75     }
76
77     private void tstFields() {
78         ReflectField surname = _iClass.getDeclaredField("surname");
79         ReflectField birthdate = _iClass.getDeclaredField("birthdate");
80         ReflectField[] fields = _iClass.getDeclaredFields();
81         _assert(fields.length == 3);
82         _assert(fields[0] == surname);
83         _assert(fields[1] == birthdate);
84         
85         Object JavaDoc person = _iClass.newInstance();
86         _assert(birthdate.get(person) == null);
87         surname.set(person, "Cleese");
88         _assert(surname.get(person).equals("Cleese"));
89     }
90
91     private GenericClass acmeDataClass() {
92         GenericClass result = new GenericClass(_reflector, null, "com.acme.Person", _objectIClass);
93         result.initFields(fields(result));
94         return result;
95     }
96
97     private GenericField[] fields(ReflectClass personClass) {
98         return new GenericField[] {
99                 new GenericField("surname", _reflector.forClass(String JavaDoc.class), false, false, false),
100                 new GenericField("birthdate", _reflector.forClass(Date.class), false, false, false),
101                 new GenericField("bestFriend", personClass, false, false, false)
102         };
103     }
104
105 }
106
Popular Tags