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.velocity.exception.VelocityException;
26 import org.apache.velocity.runtime.RuntimeLogger;
27 import org.apache.velocity.runtime.log.Log;
28 import org.apache.velocity.runtime.log.RuntimeLoggerLog;
29 import org.apache.velocity.util.introspection.Introspector;
30
31
32
33
34 public class PropertyExecutor extends AbstractExecutor
35 {
36 private final Introspector introspector;
37
38
39
40
41
42
43
44
45 public PropertyExecutor(final Log log, final Introspector introspector,
46 final Class clazz, final String property)
47 {
48 this.log = log;
49 this.introspector = introspector;
50
51
52
53
54 if (StringUtils.isNotEmpty(property))
55 {
56 discover(clazz, property);
57 }
58 }
59
60
61
62
63
64
65
66
67 public PropertyExecutor(final RuntimeLogger r, final Introspector introspector,
68 final Class clazz, final String property)
69 {
70 this(new RuntimeLoggerLog(r), introspector, clazz, property);
71 }
72
73
74
75
76
77 protected Introspector getIntrospector()
78 {
79 return this.introspector;
80 }
81
82
83
84
85
86 protected void discover(final Class clazz, final String property)
87 {
88
89
90
91
92 try
93 {
94 Object [] params = {};
95
96 StringBuffer sb = new StringBuffer("get");
97 sb.append(property);
98
99 setMethod(introspector.getMethod(clazz, sb.toString(), params));
100
101 if (!isAlive())
102 {
103
104
105
106
107 char c = sb.charAt(3);
108
109 if (Character.isLowerCase(c))
110 {
111 sb.setCharAt(3, Character.toUpperCase(c));
112 }
113 else
114 {
115 sb.setCharAt(3, Character.toLowerCase(c));
116 }
117
118 setMethod(introspector.getMethod(clazz, sb.toString(), params));
119 }
120 }
121
122
123
124 catch( RuntimeException e )
125 {
126 throw e;
127 }
128 catch(Exception e)
129 {
130 String msg = "Exception while looking for property getter for '" + property;
131 log.error(msg, e);
132 throw new VelocityException(msg, e);
133 }
134 }
135
136
137
138
139 public Object execute(Object o)
140 throws IllegalAccessException, InvocationTargetException
141 {
142 return isAlive() ? getMethod().invoke(o, ((Object []) null)) : null;
143 }
144 }