aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/processing/ComputedAttributesHandler.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/org/traccar/processing/ComputedAttributesHandler.java')
-rw-r--r--src/org/traccar/processing/ComputedAttributesHandler.java34
1 files changed, 31 insertions, 3 deletions
diff --git a/src/org/traccar/processing/ComputedAttributesHandler.java b/src/org/traccar/processing/ComputedAttributesHandler.java
index ea7c0aa1d..8689c5a58 100644
--- a/src/org/traccar/processing/ComputedAttributesHandler.java
+++ b/src/org/traccar/processing/ComputedAttributesHandler.java
@@ -16,7 +16,13 @@
*/
package org.traccar.processing;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.Arrays;
import java.util.Collection;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
import org.apache.commons.jexl2.JexlEngine;
import org.apache.commons.jexl2.JexlException;
@@ -36,10 +42,32 @@ public class ComputedAttributesHandler extends BaseDataHandler {
engine.setStrict(true);
}
+ private MapContext prepareContext(Position position) {
+ MapContext result = new MapContext();
+ Set<Method> methods = new HashSet<>(Arrays.asList(position.getClass().getMethods()));
+ methods.removeAll(Arrays.asList(Object.class.getMethods()));
+ for (Method method : methods) {
+ if (method.getName().startsWith("get") && method.getParameterTypes().length == 0) {
+ String name = Character.toLowerCase(method.getName().charAt(3)) + method.getName().substring(4);
+
+ try {
+ if (!method.getReturnType().equals(Map.class)) {
+ result.set(name, method.invoke(position));
+ } else {
+ for (Object key : ((Map) method.invoke(position)).keySet()) {
+ result.set((String) key, ((Map) method.invoke(position)).get(key));
+ }
+ }
+ } catch (IllegalAccessException | InvocationTargetException error) {
+ Log.warning(error);
+ }
+ }
+ }
+ return result;
+ }
+
public Object computeAttribute(Attribute attribute, Position position) throws JexlException {
- MapContext expressionContext = new MapContext();
- expressionContext.set("position", position);
- return engine.createExpression(attribute.getExpression()).evaluate(expressionContext);
+ return engine.createExpression(attribute.getExpression()).evaluate(prepareContext(position));
}
@Override