1

we have the following problem with code. Our code must make lots of decision according to the fields of some object, and sometimes the fields are accessed through a complex path:

public void perform(OurBean bean) {
  if (bean != null 
    && bean.getWaybill() != null
    && bean.getWaybill().getTransaction() != null
    && bean.getWaybill().getTransaction().getGuid() != null) {
     // Do some action with the guid - a string
   }
}

What I would like to have is to do something like this:

public void perform(OurBean bean) {
  if (notEmpty(bean, "waybill.transaction.guid")) {
     // Do some action with the guid - a string
   }
}

Right now we have such a function implemented on our own, using a Reflection mechanism. Is there a better way to do it? JSP EL has exactly what we need - expressions using getter and setter methods. But how can I use that inside a Java code, not a JSP page, for some object? Could not find any good samples so far.

SPIRiT_1984
  • 2,717
  • 3
  • 29
  • 46
  • Wrap the `bean.getWaybill().getTransaction().getGuid()` in a try catch for `NullPointerException`, which you then ignore if it doesn't matter which object is null. – Sotirios Delimanolis Jun 06 '13 at 17:19
  • 4
    No! Do not do that! Related/duplicate: http://stackoverflow.com/questions/271526/avoiding-null-statements-in-java – BalusC Jun 06 '13 at 17:24
  • @BalusC Assuming the method doesn't care which object in the chain is null, what does it matter if we catch an NPE? We just don't `perform` if we can't find the last object. – Sotirios Delimanolis Jun 06 '13 at 17:34
  • No, using exceptions like this is wrong. Besides, I asked mainly about the compact form "waybill.transaction.guid" - bean.getWaybill().getTransaction().getGuid() is a little bit overloaded with "noise". – SPIRiT_1984 Jun 06 '13 at 18:09
  • @BalusC this not a duplicate. I asked about using something like EL parser to make the code shorter, the question you relate to is about avoiding unnecessary if's. – SPIRiT_1984 Jun 06 '13 at 18:11
  • It's at least related. You've a design problem which is answered over there. So if you intend to fix the design problem, it's a duplicate. If you don't, then it's not, but you still have a design problem :) – BalusC Jun 06 '13 at 18:12

2 Answers2

0

See the java.beans package

Example:

   if (notEmpty(new Expression(bean, "waybill.transaction.guid", null).getValue()) {
     // Do some action with the guid - a string
   }

This is just an example and it would probably need more love to make it work as you want, but you can reuse a lot of useful stuff from that package.

Jiri Kremser
  • 12,471
  • 7
  • 45
  • 72
  • According to the [javadoc for java.beans.Expression](http://docs.oracle.com/javase/7/docs/api/java/beans/Expression.html#Expression%28java.lang.Object,%20java.lang.String,%20java.lang.Object[]%29), that won't work. The String parameter is the name of a method in the bean's class, not a dot-expression. In fact I don't think the java.beans package has any classes that can do what EL does. – VGR Jun 06 '13 at 17:37
  • Right, javax.el is the right package to look into. Here is some tutorial for using EL outside the servlet container http://illegalargumentexception.blogspot.cz/2008/04/java-using-el-outside-j2ee.html – Jiri Kremser Jun 06 '13 at 21:46
0

If you have control over the bean classes, and if the bean classes are not auto-generated, add convenience methods to them:

public class ProgramLogic {
    public void perform(OurBean bean) {
        if (bean != null && bean.getWaybillTransactionId() != null) {
            // Do some action
        }
    }
}

public class OurBean {
    public String getWaybillTransactionId() {
        return waybill == null ? null : waybill.getTransactionGuid();
    }
}

public class Waybill {
    public String getTransactionGuid() {
        return transaction == null ? null : transaction.getGuid();
    }
}
VGR
  • 40,506
  • 4
  • 48
  • 63
  • That's a good thing, but this will just transfer the code from the procedure to the bean class, not making it any less. Besides, I was wondering more about how-to use EL parser inside Java for such a problem – SPIRiT_1984 Jun 06 '13 at 18:10
  • 1
    @SPIRiT_1984 The EL parser is most likely just doing what you were trying to do with reflection. – Sotirios Delimanolis Jun 06 '13 at 18:23