1 package org.apache.velocity.runtime.parser.node;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.lang.reflect.InvocationTargetException;
23
24 import org.apache.commons.lang.StringUtils;
25 import org.apache.commons.lang.text.StrBuilder;
26 import org.apache.velocity.exception.VelocityException;
27 import org.apache.velocity.runtime.log.Log;
28 import org.apache.velocity.util.introspection.Introspector;
29
30
31
32
33
34
35
36
37
38 public class SetPropertyExecutor
39 extends SetExecutor
40 {
41 private final Introspector introspector;
42
43
44
45
46
47
48
49
50 public SetPropertyExecutor(final Log log, final Introspector introspector,
51 final Class clazz, final String property, final Object arg)
52 {
53 this.log = log;
54 this.introspector = introspector;
55
56
57
58
59 if (StringUtils.isNotEmpty(property))
60 {
61 discover(clazz, property, arg);
62 }
63 }
64
65
66
67
68 protected Introspector getIntrospector()
69 {
70 return this.introspector;
71 }
72
73
74
75
76
77
78 protected void discover(final Class clazz, final String property, final Object arg)
79 {
80 Object [] params = new Object [] { arg };
81
82 try
83 {
84 StrBuilder sb = new StrBuilder("set");
85 sb.append(property);
86
87 setMethod(introspector.getMethod(clazz, sb.toString(), params));
88
89 if (!isAlive())
90 {
91
92
93
94
95 char c = sb.charAt(3);
96
97 if (Character.isLowerCase(c))
98 {
99 sb.setCharAt(3, Character.toUpperCase(c));
100 }
101 else
102 {
103 sb.setCharAt(3, Character.toLowerCase(c));
104 }
105
106 setMethod(introspector.getMethod(clazz, sb.toString(), params));
107 }
108 }
109
110
111
112 catch( RuntimeException e )
113 {
114 throw e;
115 }
116 catch(Exception e)
117 {
118 String msg = "Exception while looking for property setter for '" + property;
119 log.error(msg, e);
120 throw new VelocityException(msg, e);
121 }
122 }
123
124
125
126
127
128
129
130
131
132 public Object execute(final Object o, final Object value)
133 throws IllegalAccessException, InvocationTargetException
134 {
135 Object [] params = new Object [] { value };
136 return isAlive() ? getMethod().invoke(o, params) : null;
137 }
138 }