Package org.codehaus.groovy.runtime
Class NullObject
- java.lang.Object
-
- groovy.lang.GroovyObjectSupport
-
- org.codehaus.groovy.runtime.NullObject
-
- All Implemented Interfaces:
GroovyObject
public class NullObject extends GroovyObjectSupport
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
asBoolean()
A null object always coerces to false.Object
asType(Class c)
Type conversion method for null.Object
clone()
Since this is implemented as a singleton, we should avoid the use of the clone methodboolean
equals(Object to)
null is only equal to nullstatic NullObject
getNullObject()
get the NullObject referenceObject
getProperty(String property)
Tries to get a property on null, which will always failint
hashCode()
Object
invokeMethod(String name, Object args)
Tries to invoke a method on null, which will always failboolean
is(Object other)
The method "is" is used to test for equal references.Iterator
iterator()
iterator() method to be able to iterate on null.Object
plus(Object o)
Fallback for null+null.Object
plus(String s)
Allows to add a String to null.void
setProperty(String property, Object newValue)
Tries to set a property on null, which will always failString
toString()
<T> T
with(Closure<T> closure)
Allows the closure to be called for NullObject-
Methods inherited from class groovy.lang.GroovyObjectSupport
getMetaClass, setMetaClass
-
-
-
-
Method Detail
-
getNullObject
public static NullObject getNullObject()
get the NullObject reference- Returns:
- the null object
-
clone
public Object clone()
Since this is implemented as a singleton, we should avoid the use of the clone method
-
getProperty
public Object getProperty(String property)
Tries to get a property on null, which will always fail- Specified by:
getProperty
in interfaceGroovyObject
- Overrides:
getProperty
in classGroovyObjectSupport
- Parameters:
property
- - the property to get- Returns:
- a NPE
-
with
public <T> T with(Closure<T> closure)
Allows the closure to be called for NullObject- Parameters:
closure
- the closure to call on the object- Returns:
- result of calling the closure
-
setProperty
public void setProperty(String property, Object newValue)
Tries to set a property on null, which will always fail- Specified by:
setProperty
in interfaceGroovyObject
- Overrides:
setProperty
in classGroovyObjectSupport
- Parameters:
property
- - the proprty to setnewValue
- - the new value of the property
-
invokeMethod
public Object invokeMethod(String name, Object args)
Tries to invoke a method on null, which will always fail- Specified by:
invokeMethod
in interfaceGroovyObject
- Overrides:
invokeMethod
in classGroovyObjectSupport
- Parameters:
name
- the name of the method to invokeargs
- - arguments to the method- Returns:
- a NPE
-
equals
public boolean equals(Object to)
null is only equal to null
-
iterator
public Iterator iterator()
iterator() method to be able to iterate on null. Note: this part is from Invoker- Returns:
- an iterator for an empty list
-
plus
public Object plus(String s)
Allows to add a String to null. The result is concatenated String of the result of calling toString() on this object and the String in the parameter.- Parameters:
s
- - the String to concatenate- Returns:
- the concatenated string
-
plus
public Object plus(Object o)
Fallback for null+null. The result is always a NPE. The plus(String) version will catch the case of adding a non null String to null.- Parameters:
o
- - the Object- Returns:
- nothing
-
is
public boolean is(Object other)
The method "is" is used to test for equal references. This method will return true only if the given parameter is null- Parameters:
other
- - the object to test- Returns:
- true if other is null
-
asType
public Object asType(Class c)
Type conversion method for null.- Parameters:
c
- - the class to convert to- Returns:
- always null
-
asBoolean
public boolean asBoolean()
A null object always coerces to false.- Returns:
- false
-
-