Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 342359

Summary: [Xtend] evaluation of complex boolean expressions in generated Java code
Product: [Modeling] TMF Reporter: Maxim Frolov <maxim.frolov>
Component: XtextAssignee: Project Inbox <tmf.xtext-inbox>
Status: CLOSED DUPLICATE QA Contact:
Severity: major    
Priority: P3 CC: maxim.frolov, sebastian.zarnekow, sven.efftinge
Version: 2.0.0Flags: sebastian.zarnekow: indigo+
Target Milestone: M7   
Hardware: All   
OS: All   
Whiteboard:

Description Maxim Frolov CLA 2011-04-09 21:12:32 EDT
Both in Java and many other programming languages the boolean expressions are evaluated 'lazily'. If, for example, in expression '(a && b)', a is false, then b is not evaluated. This lets write less code in many cases. Compare for example: 
if (file != null) {
   if (file.exist)
      return true;
}
with
return (file != null && file.exist)


In Xtend2 all parts of complex boolean expressions are evaluated. 

The following Xtend function:
boolean fileExists (File file) {
 file != null && file.exists
}

generates the following Java code:
public boolean fileExists(final File file) {
    final File typeConverted_file = (File)file;
    boolean _operator_notEquals = ObjectExtensions.operator_notEquals(typeConverted_file, null);
    boolean _exists = file.exists();
    boolean _operator_and = BooleanExtensions.operator_and(_operator_notEquals, _exists);
    return _operator_and;
  }

At runtime a NPE is thrown if the input argument file is null.
Comment 1 Sven Efftinge CLA 2011-04-11 08:26:00 EDT

*** This bug has been marked as a duplicate of bug 342424 ***