1 package com.opensymphony.xwork2.ognl;
2
3 import com.opensymphony.xwork2.inject.Inject;
4 import com.opensymphony.xwork2.util.reflection.ReflectionException;
5 import com.opensymphony.xwork2.util.reflection.ReflectionProvider;
6 import ognl.Ognl;
7 import ognl.OgnlException;
8 import ognl.OgnlRuntime;
9
10 import java.beans.IntrospectionException;
11 import java.beans.PropertyDescriptor;
12 import java.lang.reflect.Field;
13 import java.lang.reflect.Method;
14 import java.util.Collection;
15 import java.util.Map;
16
17 public class OgnlReflectionProvider implements ReflectionProvider {
18
19 private OgnlUtil ognlUtil;
20
21 @Inject
22 public void setOgnlUtil(OgnlUtil ognlUtil) {
23 this.ognlUtil = ognlUtil;
24 }
25
26 public Field getField(Class inClass, String name) {
27 return OgnlRuntime.getField(inClass, name);
28 }
29
30 public Method getGetMethod(Class targetClass, String propertyName)
31 throws IntrospectionException, ReflectionException {
32 try {
33 return OgnlRuntime.getGetMethod(null, targetClass, propertyName);
34 } catch (OgnlException e) {
35 throw new ReflectionException(e);
36 }
37 }
38
39 public Method getSetMethod(Class targetClass, String propertyName)
40 throws IntrospectionException, ReflectionException {
41 try {
42 return OgnlRuntime.getSetMethod(null, targetClass, propertyName);
43 } catch (OgnlException e) {
44 throw new ReflectionException(e);
45 }
46 }
47
48 public void setProperties(Map<String, String> props, Object o, Map<String, Object> context) {
49 ognlUtil.setProperties(props, o, context);
50 }
51
52 public void setProperties(Map<String, String> props, Object o, Map<String, Object> context,
53 boolean throwPropertyExceptions) throws ReflectionException{
54 ognlUtil.setProperties(props, o, context, throwPropertyExceptions);
55
56 }
57
58 public void setProperties(Map<String, String> properties, Object o) {
59 ognlUtil.setProperties(properties, o);
60 }
61
62 public PropertyDescriptor getPropertyDescriptor(Class targetClass,
63 String propertyName) throws IntrospectionException,
64 ReflectionException {
65 try {
66 return OgnlRuntime.getPropertyDescriptor(targetClass, propertyName);
67 } catch (OgnlException e) {
68 throw new ReflectionException(e);
69 }
70 }
71
72 public void copy(Object from, Object to, Map<String, Object> context,
73 Collection<String> exclusions, Collection<String> inclusions) {
74 ognlUtil.copy(from, to, context, exclusions, inclusions);
75 }
76
77 public Object getRealTarget(String property, Map<String, Object> context, Object root)
78 throws ReflectionException {
79 try {
80 return ognlUtil.getRealTarget(property, context, root);
81 } catch (OgnlException e) {
82 throw new ReflectionException(e);
83 }
84 }
85
86 public void setProperty(String name, Object value, Object o, Map<String, Object> context) {
87 ognlUtil.setProperty(name, value, o, context);
88 }
89
90 public void setProperty(String name, Object value, Object o, Map<String, Object> context, boolean throwPropertyExceptions) {
91 ognlUtil.setProperty(name, value, o, context, throwPropertyExceptions);
92 }
93
94 public Map getBeanMap(Object source) throws IntrospectionException,
95 ReflectionException {
96 try {
97 return ognlUtil.getBeanMap(source);
98 } catch (OgnlException e) {
99 throw new ReflectionException(e);
100 }
101 }
102
103 public Object getValue(String expression, Map<String, Object> context, Object root)
104 throws ReflectionException {
105 try {
106 return ognlUtil.getValue(expression, context, root);
107 } catch (OgnlException e) {
108 throw new ReflectionException(e);
109 }
110 }
111
112 public void setValue(String expression, Map<String, Object> context, Object root,
113 Object value) throws ReflectionException {
114 try {
115 Ognl.setValue(expression, context, root, value);
116 } catch (OgnlException e) {
117 throw new ReflectionException(e);
118 }
119 }
120
121 public PropertyDescriptor[] getPropertyDescriptors(Object source)
122 throws IntrospectionException {
123 return ognlUtil.getPropertyDescriptors(source);
124 }
125
126 }