KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtworks > xstream > converters > reflection > PureJavaReflectionProvider


1 package com.thoughtworks.xstream.converters.reflection;
2
3 import java.lang.reflect.Field JavaDoc;
4 import java.lang.reflect.Modifier JavaDoc;
5 import java.util.Iterator JavaDoc;
6
7 /**
8  * Pure Java ObjectFactory that instantiates objects using standard Java reflection, however the types of objects
9  * that can be constructed are limited.
10  * <p/>
11  * Can newInstance: classes with public visibility, outer classes, static inner classes, classes with default constructors.
12  * Cannot newInstance: classes without public visibility, non-static inner classes, classes without default constructors.
13  * Note that any code in the constructor of a class will be executed when the ObjectFactory instantiates the object.
14  * </p>
15  */

16 public class PureJavaReflectionProvider implements ReflectionProvider {
17
18     protected FieldDictionary fieldDictionary = new FieldDictionary();
19
20     public Object JavaDoc newInstance(Class JavaDoc type) {
21         try {
22             return type.newInstance();
23         } catch (InstantiationException JavaDoc e) {
24             throw new ObjectAccessException("Cannot construct " + type.getName(), e);
25         } catch (IllegalAccessException JavaDoc e) {
26             throw new ObjectAccessException("Cannot construct " + type.getName(), e);
27         }
28     }
29
30     public void visitSerializableFields(Object JavaDoc object, ReflectionProvider.Visitor visitor) {
31         for (Iterator JavaDoc iterator = fieldDictionary.serializableFieldsFor(object.getClass()); iterator.hasNext();) {
32             Field JavaDoc field = (Field JavaDoc) iterator.next();
33             if (!fieldModifiersSupported(field)) {
34                 continue;
35             }
36             validateFieldAccess(field);
37             Object JavaDoc value = null;
38             try {
39                 value = field.get(object);
40             } catch (IllegalArgumentException JavaDoc e) {
41                 throw new ObjectAccessException("Could not get field " + field.getClass() + "." + field.getName(), e);
42             } catch (IllegalAccessException JavaDoc e) {
43                 throw new ObjectAccessException("Could not get field " + field.getClass() + "." + field.getName(), e);
44             }
45             visitor.visit(field.getName(), field.getType(), field.getDeclaringClass(), value);
46         }
47     }
48
49     public void writeField(Object JavaDoc object, String JavaDoc fieldName, Object JavaDoc value, Class JavaDoc definedIn) {
50         Field JavaDoc field = fieldDictionary.field(object.getClass(), fieldName, definedIn);
51         validateFieldAccess(field);
52         try {
53             field.set(object, value);
54         } catch (IllegalArgumentException JavaDoc e) {
55             throw new ObjectAccessException("Could not set field " + object.getClass() + "." + field.getName(), e);
56         } catch (IllegalAccessException JavaDoc e) {
57             throw new ObjectAccessException("Could not set field " + object.getClass() + "." + field.getName(), e);
58         }
59     }
60
61     public Class JavaDoc getFieldType(Object JavaDoc object, String JavaDoc fieldName, Class JavaDoc definedIn) {
62         return fieldDictionary.field(object.getClass(), fieldName, definedIn).getType();
63     }
64
65     protected boolean fieldModifiersSupported(Field JavaDoc field) {
66         return !(Modifier.isStatic(field.getModifiers())
67                 || Modifier.isTransient(field.getModifiers()));
68     }
69
70     protected void validateFieldAccess(Field JavaDoc field) {
71         if (Modifier.isFinal(field.getModifiers())) {
72             throw new ObjectAccessException("Invalid final field "
73                     + field.getDeclaringClass().getName() + "." + field.getName());
74         }
75     }
76
77 }
78
Popular Tags