There is another option that you can use, which may be useful to you if you use Spring.
If you are not using Spring, you need to add an additional Spel to your project, you can do:
ExpressionParser parser = new SpelExpressionParser(); StandardEvaluationContext context = new StandardEvaluationContext(dao.getCar()); Power power = parser.parseExpression("engine?.power").getValue(context, Power.class);
The engine?.power uses a safe navigation operator. In case the engine is null , then the whole expression will evaluate to null .
This solution will work in Java 6.
source share