/sitebricks/src/test/java/com/google/sitebricks/rendering/DynTypedMvelEvaluatorCompiler.java

http://github.com/dhanji/sitebricks · Java · 71 lines · 49 code · 16 blank · 6 comment · 2 complexity · 5de2f7f4891aae4d76bac8481c50cc9c MD5 · raw file

  1. package com.google.sitebricks.rendering;
  2. import com.google.sitebricks.Evaluator;
  3. import com.google.sitebricks.compiler.EvaluatorCompiler;
  4. import com.google.sitebricks.compiler.ExpressionCompileException;
  5. import com.google.sitebricks.compiler.Parsing;
  6. import com.google.sitebricks.compiler.Token;
  7. import org.jetbrains.annotations.Nullable;
  8. import org.mvel2.MVEL;
  9. import java.io.Serializable;
  10. import java.util.Collection;
  11. import java.util.List;
  12. import java.util.Map;
  13. import java.util.concurrent.ConcurrentHashMap;
  14. import java.util.concurrent.ConcurrentMap;
  15. /**
  16. * Temporary class to enable dynamic typing for collection projections (since we don't have
  17. * a good mechanism in MVEL to reflect on parametric types yet)
  18. *
  19. * @author Dhanji R. Prasanna (dhanji@gmail.com)
  20. */
  21. public class DynTypedMvelEvaluatorCompiler implements EvaluatorCompiler {
  22. public DynTypedMvelEvaluatorCompiler(Map<String, Class<?>> map) {
  23. }
  24. public Evaluator compile(final String expression) throws ExpressionCompileException {
  25. return new Evaluator() {
  26. private final ConcurrentMap<String, Serializable> map = new ConcurrentHashMap<String, Serializable>();
  27. @Nullable
  28. public Object evaluate(String ___expr, Object bean) {
  29. Serializable serializable = map.get(expression);
  30. if (null == serializable) {
  31. serializable = MVEL.compileExpression(expression);
  32. map.put(expression, serializable);
  33. }
  34. return MVEL.executeExpression(serializable, bean);
  35. }
  36. public void write(String expr, Object bean, Object value) {
  37. }
  38. public Object read(String property, Object contextObject) {
  39. return MVEL.getProperty(property, contextObject);
  40. }
  41. };
  42. }
  43. public Class<?> resolveCollectionTypeParameter(String expression) throws ExpressionCompileException {
  44. return Object.class;
  45. }
  46. public List<Token> tokenizeAndCompile(String template) throws ExpressionCompileException {
  47. return Parsing.tokenize(template, this);
  48. }
  49. public Class<?> resolveEgressType(String expression) throws ExpressionCompileException {
  50. return Collection.class;
  51. }
  52. public boolean isWritable(String property) throws ExpressionCompileException {
  53. return true;
  54. }
  55. }