Class ObjectModel
java.lang.Object
org.apache.sling.scripting.sightly.render.ObjectModel
The
ObjectModel class provides various static models for object conversion and object property resolution.-
Method Summary
Modifier and TypeMethodDescriptionstatic StringcollectionToString(Collection<?> collection) Converts the passedcollectionto a comma separated valuesStringrepresentation.static MethodfindBeanMethod(Class<?> cls, String baseName) Given a bean class and a base method name, this method will try to find a public method without parameters that is named:baseNameget +BaseNameis +BaseNamestatic Collection<Object>fromIterator(Iterator<Object> iterator) Given aniterator, this method will return aCollection.static ObjectgetEnumValue(Object object, String valueName) Given anobject, this method will return the value of the enum value identified byvalueName.static ObjectGiven anobject, this method will return the value of the public field identified byfieldName.static ObjectGiven an indexableobject(i.e. an array or a collection), this method will return the value available at theindexposition.static ObjectinvokeBeanMethod(Object object, String methodName) Given a beanobject, this method will invoke the public method without parameters identified bymethodNameand return the invocation's result.static booleanisMethodAllowed(Method method) Returnstrueif the method is not one of theObject's class declared methods, with the exception ofObject.toString().static booleanisPrimitive(Object object) Checks if the providedobjectis an instance of a primitive class.static ObjectresolveProperty(Object target, Object property) Given thetargetobject, this method attempts to resolve and return the value of the passedproperty.static booleanConverts the givenobjectto a boolean value, applying the following rules: if theobjectisnullthe returned value isfalseif theobjectis aNumberthe method will returnfalseonly if the number's value is 0 if theStringrepresentation of theobjectis equal irrespective of its casing to "true", the method will returntrueif theobjectis aCollectionor aMap, the method will returntrueonly if the collection / map is not empty if the object is an array, the method will returntrueonly if the array is not emptystatic Collection<Object>toCollection(Object object) Forces the conversion of the passedobjectto an immutable collection, according to the following rules: if theobjectisnullan empty collection will be returned if theobjectis an array a list transformation of the array will be returned if theobjectis aCollectionthe object itself will be returned if theobjectis an instance of aMapthe map's key set will be returned (seeMap.keySet()) if theobjectis an instance of anEnumerationa list transformation will be returned if theobjectis an instance of anIteratororIterablethe result offromIterator(Iterator)will be returned otherwise theobjectis wrapped in a single item liststatic NumberCoerces the passedobjectto a numeric value.static StringConverts the passedobjectto aString.
-
Method Details
-
isPrimitive
Checks if the providedobjectis an instance of a primitive class.- Parameters:
object- theObjectto check- Returns:
trueif theobjectis a primitive,falseotherwise
-
resolveProperty
Given the
targetobject, this method attempts to resolve and return the value of the passedproperty.The property can be either an index or a name:
- index: the property is considered an index if its value is an integer number and in this case the
targetwill be assumed to be either an array or it will be converted to aCollection; a fallback toMapwill be made in case the previous two attempts failed - name: the
propertywill be converted to aString(seetoString(Object)); thetargetwill be assumed to be either aMapor an object; if theMapattempt fails, thepropertywill be used to check if thetargethas a publicly accessible field with this name or a publicly accessible method with no parameters with this name or a combination of the "get" or "is" prefixes plus the capitalised name (seeinvokeBeanMethod(Object, String))
- Parameters:
target- the target objectproperty- the property to be resolved- Returns:
- the value of the property or
null
- index: the property is considered an index if its value is an integer number and in this case the
-
toBoolean
Converts the givenobjectto a boolean value, applying the following rules:- if the
objectisnullthe returned value isfalse - if the
objectis aNumberthe method will returnfalseonly if the number's value is 0 - if the
Stringrepresentation of theobjectis equal irrespective of its casing to "true", the method will returntrue - if the
objectis aCollectionor aMap, the method will returntrueonly if the collection / map is not empty - if the object is an array, the method will return
trueonly if the array is not empty
- Parameters:
object- the target object- Returns:
- the boolean representation of the
objectaccording to the conversion rules
- if the
-
toNumber
Coerces the passedobjectto a numeric value. If the passed value is aStringthe conversion rules are those ofNumberUtils.createNumber(String).- Parameters:
object- the target object- Returns:
- the numeric representation if one can be determined or
null - See Also:
-
NumberUtils.createNumber(String)
-
toString
Converts the passedobjectto aString. The following rules apply:- if the
objectisnullan empty string will be returned - if the
objectis an instance of aStringthe object itself will be returned - if the object is a primitive (see
isPrimitive(Object)), itsStringrepresentation will be returned - if the object is an
Enumits name will be returned (seeEnum.name()) - if the object is a collection an attempt to convert the object to a
Collectionwill be made and then the output ofcollectionToString(Collection)will be returned - otherwise
object.toString()is returned
- Parameters:
object- the target object- Returns:
- the string representation of the object or an empty string
- if the
-
toCollection
Forces the conversion of the passedobjectto an immutable collection, according to the following rules:- if the
objectisnullan empty collection will be returned - if the
objectis an array a list transformation of the array will be returned - if the
objectis aCollectionthe object itself will be returned - if the
objectis an instance of aMapthe map's key set will be returned (seeMap.keySet()) - if the
objectis an instance of anEnumerationa list transformation will be returned - if the
objectis an instance of anIteratororIterablethe result offromIterator(Iterator)will be returned - otherwise the
objectis wrapped in a single item list
- Parameters:
object- the target object- Returns:
- the immutable collection representation of the object
- if the
-
collectionToString
Converts the passedcollectionto a comma separated valuesStringrepresentation.- Parameters:
collection- the collection to be converted to CSV- Returns:
- the CSV; if the
collectionis empty then an empty string will be returned
-
fromIterator
Given aniterator, this method will return aCollection.- Parameters:
iterator- the iterator to be transformed into acollection- Returns:
- an immutable collection with the iterator's elements
-
getIndex
Given an indexableobject(i.e. an array or a collection), this method will return the value available at theindexposition.- Parameters:
object- the indexable objectindex- the index- Returns:
- the value stored at the
indexornull
-
getField
Given anobject, this method will return the value of the public field identified byfieldName.- Parameters:
object- the target objectfieldName- the name of the field- Returns:
- the value of the field or
nullif the field was not found
-
invokeBeanMethod
Given a beanobject, this method will invoke the public method without parameters identified bymethodNameand return the invocation's result.- Parameters:
object- the target objectmethodName- the name of the public method without parameters to invoke- Returns:
- the invocation's result or
nullif such a method cannot be found
-
getEnumValue
Given anobject, this method will return the value of the enum value identified byvalueName.- Parameters:
object- the target objectvalueName- the name of the enum value- Returns:
- the value of the enum or
nullif the enum was not found
-
findBeanMethod
Given a bean class and a base method name, this method will try to find a public method without parameters that is named:baseName- get +
BaseName - is +
BaseName
- Parameters:
cls- the class into which to search for the methodbaseName- the base method name- Returns:
- a method that matches the criteria or
null
-
isMethodAllowed
Returnstrueif the method is not one of theObject's class declared methods, with the exception ofObject.toString().- Parameters:
method- the method to check- Returns:
trueif the method is not one of theObject's class declared methods, with the exception ofObject.toString(),falseotherwise
-