This article may be useful for those who once had to make hundreds or thousands of lines of code using reflection masticating vs centrifugal juicer to access attributes, navigate maps and scroll through lists in Java. Now if we add to these lists or maps may have more of the same or even more complex objects which contain lists or maps.
Apache JEXL (Java Expression Language) was inspired by Apache masticating vs centrifugal juicer Velocity and the definition masticating vs centrifugal juicer of JSTL and Expression Language masticating vs centrifugal juicer for JSP 2.0. Also in version 2.0 were added functionality Unified EL.
The problem is to obtain a value from the DTO using reflection, ie, the entity that runs the parameter extraction of a priori knowledge of the definition of that class. JEXL This is where using a simple language and we can direct you to where to access. In that how and what we will call expression. public class {private static final JexlEvaluator JexlEngine masticating vs centrifugal juicer JexlEngine JEXL = new (); static {jexl.setCache (512); jexl.setLenient (false); jexl.setSilent (false);} public Object getValue (String expression, Object object) { Expression e = jexl.createExpression (expression); JexlContext MapContext context = new (); context.set ("fua", object) return e.evaluate (context);}}
The GetValue method receives as parameters, the expression and the object to be evaluated. Within masticating vs centrifugal juicer the method creates the context of Apache JEXL (very similar to a HashMap) which is passed the object masticating vs centrifugal juicer to inspect and then evaluates the expression.
Here is the unit test and output: public class JexlEvaluatorTest {private static Log logger = LogFactory.getLog (JexlEvaluatorTest.class); @ Test public void testJexl () {JexlEvaluator JexlEvaluator masticating vs centrifugal juicer je = new () / * expression to evaluate * / String expression = "fua.dataBag.hola2"; Map Object> String, String, HashMap map = new Object> () map.put ("hello", "Hello MAP 1") Map <String, Object> map2 = new HashMap String, Object> (); map2.put ("hola2", masticating vs centrifugal juicer "Hello 2 MAP"); map.put masticating vs centrifugal juicer ("hola2" map2); TestDTO TestDTO testDTO = new (); testDTO . setDataBag (map2); testDTO.setName ("DTO Interesting"); Object result = je.getValue (expression, testDTO) logger.info (result.toString ());}}
Please enable JavaScript to view the comments powered by Disqus. Recent Posts Make your mark in life Javassist A Software Architect ProxyFactory SpringFramework: Java Dynamic Proxy ProxyFactoryBean Latest Tweets masticating vs centrifugal juicer
No comments:
Post a Comment