Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1878 | jmachado | 1 | package com.owlike.genson.reflect; |
2 | |||
3 | import java.lang.annotation.Annotation; |
||
4 | import java.lang.reflect.Field; |
||
5 | import java.lang.reflect.InvocationTargetException; |
||
6 | import java.lang.reflect.Method; |
||
7 | import java.lang.reflect.Type; |
||
8 | |||
9 | import com.owlike.genson.*; |
||
10 | import com.owlike.genson.stream.JsonWriter; |
||
11 | import com.owlike.genson.stream.ObjectWriter; |
||
12 | |||
13 | public abstract class PropertyAccessor extends BeanProperty implements Comparable<PropertyAccessor> { |
||
14 | Serializer<Object> propertySerializer; |
||
15 | private final char[] escapedName; |
||
16 | |||
17 | protected PropertyAccessor(1.5.0/docs/api/java/lang/String.html">String name, 1.5.0/docs/api/java/lang/reflect/Type.html">Type type, Class<?> declaringClass, Class<?> concreteClass, |
||
18 | 5+0%2Fdocs%2Fapi+Annotation">Annotation[] annotations, int modifiers) { |
||
19 | super(name, type, declaringClass, concreteClass, annotations, modifiers); |
||
20 | escapedName = JsonWriter.escapeString(name); |
||
21 | } |
||
22 | |||
23 | public void serialize(5+0%2Fdocs%2Fapi+Object">Object propertySource, ObjectWriter writer, 5+0%2Fdocs%2Fapi+Context">Context ctx) { |
||
24 | 5+0%2Fdocs%2Fapi+Object">Object propertyValue = access(propertySource); |
||
25 | writer.writeEscapedName(escapedName); |
||
26 | try { |
||
27 | propertySerializer.serialize(propertyValue, writer, ctx); |
||
28 | } catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable th) { |
||
29 | throw couldNotSerialize(th); |
||
30 | } |
||
31 | } |
||
32 | |||
33 | public abstract 5+0%2Fdocs%2Fapi+Object">Object access(final 5+0%2Fdocs%2Fapi+Object">Object target); |
||
34 | |||
35 | public int compareTo(PropertyAccessor o) { |
||
36 | return o.priority() - priority(); |
||
37 | } |
||
38 | |||
39 | protected JsonBindingException couldNotAccess(1.5.0/docs/api/java/lang/Exception.html">Exception e) { |
||
40 | return new JsonBindingException("Could not access value of property named '" |
||
41 | + name + "' using accessor " + signature() + " from class " |
||
42 | + declaringClass.getName(), e); |
||
43 | } |
||
44 | |||
45 | protected JsonBindingException couldNotSerialize(1.5.0/docs/api/java/lang/Throwable.html">Throwable e) { |
||
46 | return new JsonBindingException("Could not serialize property '" + name |
||
47 | + "' from class " + declaringClass.getName(), e); |
||
48 | } |
||
49 | |||
50 | public static class MethodAccessor extends PropertyAccessor { |
||
51 | protected final 1.5.0/docs/api/java/lang/reflect/Method.html">Method _getter; |
||
52 | |||
53 | public MethodAccessor(1.5.0/docs/api/java/lang/String.html">String name, 1.5.0/docs/api/java/lang/reflect/Method.html">Method getter, 1.5.0/docs/api/java/lang/reflect/Type.html">Type type, Class<?> concreteClass) { |
||
54 | super(name, type, getter.getDeclaringClass(), concreteClass, getter.getAnnotations(), getter.getModifiers()); |
||
55 | this._getter = getter; |
||
56 | if (!_getter.isAccessible()) { |
||
57 | _getter.setAccessible(true); |
||
58 | } |
||
59 | } |
||
60 | |||
61 | @1.5.0/docs/api/java/lang/Override.html">Override |
||
62 | public 5+0%2Fdocs%2Fapi+Object">Object access(final 5+0%2Fdocs%2Fapi+Object">Object target) { |
||
63 | try { |
||
64 | return _getter.invoke(target); |
||
65 | } catch (1.5.0/docs/api/java/lang/IllegalArgumentException.html">IllegalArgumentException e) { |
||
66 | throw couldNotAccess(e); |
||
67 | } catch (1.5.0/docs/api/java/lang/IllegalAccessException.html">IllegalAccessException e) { |
||
68 | throw couldNotAccess(e); |
||
69 | } catch (1.5.0/docs/api/java/lang/reflect/InvocationTargetException.html">InvocationTargetException e) { |
||
70 | throw couldNotAccess(e); |
||
71 | } |
||
72 | } |
||
73 | |||
74 | @1.5.0/docs/api/java/lang/Override.html">Override |
||
75 | 1.5.0/docs/api/java/lang/String.html">String signature() { |
||
76 | return _getter.toGenericString(); |
||
77 | } |
||
78 | |||
79 | @1.5.0/docs/api/java/lang/Override.html">Override |
||
80 | int priority() { |
||
81 | return 100; |
||
82 | } |
||
83 | } |
||
84 | |||
85 | public static class FieldAccessor extends PropertyAccessor { |
||
86 | protected final 1.5.0/docs/api/java/lang/reflect/Field.html">Field _field; |
||
87 | |||
88 | public FieldAccessor(1.5.0/docs/api/java/lang/String.html">String name, 1.5.0/docs/api/java/lang/reflect/Field.html">Field field, 1.5.0/docs/api/java/lang/reflect/Type.html">Type type, Class<?> concreteClass) { |
||
89 | super(name, type, field.getDeclaringClass(), concreteClass, field.getAnnotations(), field.getModifiers()); |
||
90 | this._field = field; |
||
91 | if (!_field.isAccessible()) { |
||
92 | _field.setAccessible(true); |
||
93 | } |
||
94 | } |
||
95 | |||
96 | @1.5.0/docs/api/java/lang/Override.html">Override |
||
97 | public 5+0%2Fdocs%2Fapi+Object">Object access(final 5+0%2Fdocs%2Fapi+Object">Object target) { |
||
98 | try { |
||
99 | return _field.get(target); |
||
100 | } catch (1.5.0/docs/api/java/lang/IllegalArgumentException.html">IllegalArgumentException e) { |
||
101 | throw couldNotAccess(e); |
||
102 | } catch (1.5.0/docs/api/java/lang/IllegalAccessException.html">IllegalAccessException e) { |
||
103 | throw couldNotAccess(e); |
||
104 | } |
||
105 | } |
||
106 | |||
107 | @1.5.0/docs/api/java/lang/Override.html">Override |
||
108 | public 1.5.0/docs/api/java/lang/String.html">String signature() { |
||
109 | return _field.toGenericString(); |
||
110 | } |
||
111 | |||
112 | @1.5.0/docs/api/java/lang/Override.html">Override |
||
113 | public int priority() { |
||
114 | return 50; |
||
115 | } |
||
116 | } |
||
117 | } |