In an IvyScriptStep variables are auto initialized as soon as they are accessed. The following code will log "bad":

java.util.Date date = null;
if ( date!=null) {
    ivy.log.info("bad");
} else {
    ivy.log.info("good");
}

It looks like the object "date" will never be a java.util.Date, it is an IvyDate. There is also no chance to import java.util.Date because of a conflict.

asked 06.11.2013 at 14:42

Adrian%20Imfeld's gravatar image

Adrian Imfeld
(suspended)
accept rate: 77%

edited 07.11.2013 at 12:12

Reguel%20Wermelinger's gravatar image

Reguel Werme... ♦♦
9.4k31958


IvyScript auto initialise default types like:

  • Strings with an empty String
  • Numbers with zero
  • java.util.Date with a default value of '0001-01-01 00:00:00'


To handle this inside IvyScript the is initialized statement is the recommended operator to check if a variable is null or was auto initialized. Example:

Date myDate = foo.bar.getMyDate();
if (myDate is initialized) {
    ivy.log.info("myDate is not the default value");    
} else {
    ivy.log.error("myDate is auto initialized");    
}


As long as we are working inside IvyScript anything works fine. When we want to use the variable as a parameter, e.g. to call a method in Java, we have to pay attention to the auto initialization. Example:

// getMyDate() returns null
Date myDate = foo.bar.getMyDate();

// below line prints: 'Sat Jan 01 00:00:00 CET 1', 
// because is was auto initialized even getMyDate() returns null
MyJavaClass.printDate(myDate);

// below line prints: 'null', becaue we pass null if the variable was auto initialized
MyJavaClass.printDate(myDate is initialized ? myDate : null);


If we navigating through beans we could also use the #-operator. This operator prevents auto initialization. Example:

if (in.foo.#adressBean == null)
{
   // yes, the field 'adressBean' is null and was not auto initialized
}


When we working with types which could not be initialized (like interfaces), we have to use the #-operator too. Example:

IInterfaceX interfaceX = foo.bar.getInterfaceX();
if (#interfaceX != null)
{
   // call a method on the interface.
}

// below call will fail with an exception, because IvyScript could not auto initialized an interface
interfaceX.getString();


See also the chapter IvyScript & Null Handling for more details.

link
This answer is marked "community wiki".

answered 08.11.2013 at 15:43

Flavio%20Sadeghi's gravatar image

Flavio Sadeghi ♦♦
(suspended)
accept rate: 75%

A simple solution is to transfer the code to a handler class and call the methode from the IvyScript-Step.

If you still want the code in a ScriptStep, you could do the following:

import java.util.concurrent.atomic.AtomicReference;
AtomicReference date = new AtomicReference();
//date.set(new java.util.Date());
if ( date.get()!=null) {
  ivy.log.info("bad");
} else {
  ivy.log.info("good");
}
link

answered 06.11.2013 at 14:45

Adrian%20Imfeld's gravatar image

Adrian Imfeld
(suspended)
accept rate: 77%

edited 06.11.2013 at 14:49

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×33

Asked: 06.11.2013 at 14:42

Seen: 5,755 times

Last updated: 08.11.2013 at 15:43