1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.peaseplate.internal.lang.command;
21
22 import java.lang.reflect.Field;
23 import java.lang.reflect.Method;
24 import java.util.Locale;
25 import java.util.Map;
26
27 import org.peaseplate.TemplateRuntimeException;
28 import org.peaseplate.internal.BuildContext;
29 import org.peaseplate.internal.util.ReflectionException;
30 import org.peaseplate.internal.util.ReflectionUtils;
31 import org.peaseplate.locator.TemplateLocator;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 public class InvocationCommand extends AbstractObjectCallCommand {
60
61 private final String identifier;
62
63 public InvocationCommand(TemplateLocator locator, int line, int column, ICommand command, String identifier, ICommand[] parameterCommands) {
64 super(locator, line, column, command, parameterCommands);
65
66 this.identifier = identifier;
67 }
68
69
70
71
72 public Object call(BuildContext context) throws TemplateRuntimeException {
73 Object workingObject = callCommand(context);
74
75 if (workingObject == null)
76 return null;
77
78 return callObject(context, workingObject, identifier);
79 }
80
81
82
83
84
85
86
87
88 @Override
89 protected Object callObject(BuildContext context, Object onObject, Object identifier) throws TemplateRuntimeException {
90 if (onObject == null)
91 return null;
92
93 if (!hasParameters()) {
94 Field field = getField(onObject.getClass(), String.valueOf(identifier).toLowerCase(Locale.getDefault()));
95
96 if (field != null)
97 return callField(onObject, field);
98 }
99
100 Method method = getMethod(
101 onObject.getClass(), String.valueOf(identifier).toLowerCase(Locale.getDefault()), getNumberOfParameters()
102 );
103
104 if (method == null) {
105 if ((onObject instanceof Map) && (identifier instanceof String) && (!hasParameters()))
106 return callMap(context, (Map<?, ?>)onObject, identifier);
107
108 throw new TemplateRuntimeException(
109 getLocator(), getLine(), getColumn(),
110 "No method matches " + ReflectionUtils.formatMethodName(onObject.getClass(), identifier, getNumberOfParameters())
111 );
112 }
113
114 return callMethod(context, onObject, method);
115 }
116
117
118
119
120 @Override
121 protected Field getField(Class<?> clazz, String identifier) throws TemplateRuntimeException {
122 return findField(clazz, identifier);
123 }
124
125
126
127
128 @Override
129 protected Method getMethod(Class<?> clazz, String identifier, int numberOfParameters) throws TemplateRuntimeException {
130
131 try {
132 return ReflectionUtils.findMethod(clazz, identifier, numberOfParameters);
133 }
134 catch (ReflectionException e) {
135 throw new TemplateRuntimeException(
136 getLocator(), getLine(), getColumn(),
137 "Could not find method \"" + identifier + "\" in " + clazz, e
138 );
139 }
140 }
141
142
143
144
145 @Override
146 public String toString() {
147 StringBuilder result = new StringBuilder();
148
149 if (getCommand() != null)
150 result.append(getCommand()).append(".");
151
152 result.append(identifier);
153 result.append(super.toString());
154
155 return result.toString();
156 }
157
158 }