File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Have you even wanted to do this in Java: ${x.a.b.c}

 
Jeff Albertson
Ranch Hand
Posts: 1780
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In JSP and JSF you can write expressions like ${x.a.b.c} or #{x.a.b.c} which translates to roughly x.getA().getB().getC(), except that if x or any method in that chain return null, the whole expression is null. Have you ever wanted that in Java? Right now I'm writing code that does a lot of drill down into structures, with short circuits at null. So tedious

I've love to be able to write either of the following in Java (note clever new syntax)

x..a..b..c

x..getA()..getB()..getC() //I can live without the first's syntactic sugar.

What do you think? or
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That could be handy, I agree.

People have argued that returning "null" should be avoided, in general, and this kind of mess is one of the reasons. Google for the "Null Object Pattern" to get an idea of some of the alternatives.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dig the Null Object pattern . Unfortunately, the object I'm drilling into is from a third party library (Apache XMLBeans)
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then the other solution -- following the Law of Demeter -- is also right out.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
-- following the Law of Demeter --

I learned that back in C++. That was a confusing time for me: ... don't talk to strangers ... friends can touch your privates ...
 
Alan Moore
Ranch Hand
Posts: 262
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you looked at [url=http://jakarta.apache.org/commons/jxpath/]JXPath[/ur]? I know it doesn't use the syntax you're talking about, but it might be helpful.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic