public final class Value extends Object
host or guest language. Polyglot
values are bound to a context. If the context is closed then all value operations
throw an IllegalStateException.
Polyglot values have one of the following types:
Null: This value represents a null like value. Certain
languages might use a different name or use multiple values to represent null like
values.
Number: This value represents a floating or fixed point number. The
number value may be accessed as byte, short int long, float or double value.
Boolean. This value represents a boolean value. The boolean value can be
accessed using Value.asBoolean().
String: This value represents a string value. The string value can be
accessed using Value.asString().
Host Object: This value represents a value of the host language
(Java). The original Java value can be accessed using Value.asHostObject().
Proxy Object: This value represents a proxy value.
Native Pointer: This value represents a native pointer. The native
pointer value can be accessed using Value.asNativePointer().
Array Elements: This value may contain array elements. The array
indices always start with 0, also if the language uses a different style.
Members: This value may contain members. Members are structural
elements of an object. For example, the members of a Java object are all public methods and
fields. Members are accessible using Value.getMember(String).
Executable: This value can be executed.
This indicates that the value represents an element that can be executed. Guest language examples
for executable elements are functions, methods, closures or promises.
Instantiable: This value can be instantiated. For example, Java classes are instantiable.
In addition to the language agnostic types, the language specific type can be accessed using
Value.getMetaObject(). The identity of value objects is unspecified and should not be relied
upon. For example, multiple calls to Value.getArrayElement(long) with the same index might
return the same or different instances of Value. The equality of
values is based on the identity of the value instance. All values return a human-readable
string for debugging, formatted by the original language.
Polyglot values may be converted to host objects using Value.as(Class). In addition values may
be created from Java values using Context.asValue(Object).
Context,
Engine,
PolyglotException| Modifier and Type | Method and Description |
|---|---|
<T> T |
as(Class<T> targetType)
Maps a polyglot value to a value with a given Java target type.
|
<T> T |
as(TypeLiteral<T> targetType)
Maps a polyglot value to a given Java target type literal.
|
boolean |
asBoolean()
Returns a
boolean representation of this value if it is boolean. |
byte |
asByte()
|
double |
asDouble()
|
float |
asFloat()
|
<T> T |
asHostObject()
Returns the original Java host language object.
|
int |
asInt()
|
long |
asLong()
|
long |
asNativePointer()
Returns the value of the pointer as
long value. |
<T extends Proxy> |
asProxyObject()
Returns the unboxed instance of the
Proxy. |
short |
asShort()
|
String |
asString()
|
static Value |
asValue(Object o)
Converts a Java host value to a polyglot value.
|
boolean |
canExecute()
Returns
true if the value can be executed. |
boolean |
canInstantiate()
Returns
true if the value can be instantiated. |
boolean |
canInvokeMember(String identifier)
Returns
true if the given member exists and can be invoked. |
Value |
execute(Object... arguments)
Executes this value if it
can be executed and returns its result. |
void |
executeVoid(Object... arguments)
Executes this value if it
can be executed. |
boolean |
fitsInByte()
|
boolean |
fitsInDouble()
|
boolean |
fitsInFloat()
|
boolean |
fitsInInt()
|
boolean |
fitsInLong()
|
boolean |
fitsInShort()
|
Value |
getArrayElement(long index)
Returns the array element of a given index.
|
long |
getArraySize()
Returns the array size for values with array elements.
|
Value |
getMember(String identifier)
Returns the member with a given
identifier or null if the member
does not exist. |
Set<String> |
getMemberKeys()
Returns a set of all member keys.
|
Value |
getMetaObject()
Returns the meta representation of this polyglot value.
|
SourceSection |
getSourceLocation()
Returns the declared source location of the value.
|
boolean |
hasArrayElements()
Returns
true if this polyglot value has array elements. |
boolean |
hasMember(String identifier)
Returns
true if such a member exists for a given identifier. |
boolean |
hasMembers()
Returns
true if this value generally supports containing members. |
Value |
invokeMember(String identifier,
Object... arguments)
Invokes the given member of this value.
|
boolean |
isBoolean()
Returns
true if this value represents a boolean value. |
boolean |
isHostObject()
Returns
true if the value originated form the host language Java. |
boolean |
isNativePointer()
Returns
true if this value is a native pointer. |
boolean |
isNull()
Returns
true if this value is a null like. |
boolean |
isNumber()
|
boolean |
isProxyObject()
Returns
true if this value represents a Proxy. |
boolean |
isString()
Returns
true if this value represents a string. |
Value |
newInstance(Object... arguments)
Instantiates this value if it
can be instantiated. |
void |
putMember(String identifier,
Object value)
Sets the value of a member using an identifier.
|
boolean |
removeArrayElement(long index)
Removes an array element at a given index.
|
boolean |
removeMember(String identifier)
Removes a single member from the object.
|
void |
setArrayElement(long index,
Object value)
Sets the value at a given index.
|
String |
toString()
A string representation of the value formatted by the original language.
|
public Value getMetaObject()
value.getMetaObject().toString(). If a
language does not provide any meta object information, null is returned.IllegalStateException - if the context is already closed.public boolean hasArrayElements()
true if this polyglot value has array elements. In this case array
elements can be accessed using Value.getArrayElement(long),
Value.setArrayElement(long, Object), Value.removeArrayElement(long) and the array size
can be queried using Value.getArraySize().IllegalStateException - if the context is already closed.PolyglotException - if a guest language error occurred during execution.public Value getArrayElement(long index)
0,
independent of the guest language. The given array index must be greater or equal to 0.ArrayIndexOutOfBoundsException - if the array index does not exist.UnsupportedOperationException - if the value does not have any
array elements or if the index exists but is not
readable.IllegalStateException - if the context is already closed.PolyglotException - if a guest language error occurred during execution.public void setArrayElement(long index,
Object value)
0, independent
of the guest language. The array element value is subject to polyglot value mapping rules as
described in Context.asValue(Object).ArrayIndexOutOfBoundsException - if the array index does not exist.UnsupportedOperationException - if the value does not have any
array elements or if the index exists but is not
modifiable.IllegalStateException - if the context is already closed.PolyglotException - if a guest language error occurred during execution.public boolean removeArrayElement(long index)
true if the underlying array
element could be removed, otherwise false.ArrayIndexOutOfBoundsException - if the array index does not exist.UnsupportedOperationException - if the value does not have any
array elements or if the index exists but is not
removable.IllegalStateException - if the context is already closed.PolyglotException - if a guest language error occurred during execution.public long getArraySize()
UnsupportedOperationException - if the value does not have any
array elements.IllegalStateException - if the context is already closed.PolyglotException - if a guest language error occurred during execution.public boolean hasMembers()
true if this value generally supports containing members. To check
whether a value has no members use
getMemberKeys().isEmpty()
instead. If polyglot value has members, it may also support Value.getMember(String),
Value.putMember(String, Object) and Value.removeMember(String).IllegalStateException - if the context is already closed.PolyglotException - if a guest language error occurred during execution.To check the existence of members.,
To read members.,
To write members.,
To remove a member.,
For a list of members.public boolean hasMember(String identifier)
true if such a member exists for a given identifier. If the
value has no members then Value.hasMember(String) returns
false.PolyglotException - if a guest language error occurred during execution.NullPointerException - if the identifier is null.public Value getMember(String identifier)
identifier or null if the member
does not exist.UnsupportedOperationException - if the value has no members or
the given identifier exists but is not readable.PolyglotException - if a guest language error occurred during execution.NullPointerException - if the identifier is null.public Set<String> getMemberKeys()
Set.contains(Object) with a string key is
equivalent to calling Value.hasMember(String). Removing an element from the returned set
is equivalent to calling Value.removeMember(String). Adding an element to the set is
equivalent to calling putMember(key, null). If the
value does not support members then an empty unmodifiable set is
returned. If the context gets closed while the returned set is still alive, then the set will
throw an IllegalStateException if any methods except Object methods are invoked.IllegalStateException - if the context is already closed.PolyglotException - if a guest language error occurred during execution.public void putMember(String identifier, Object value)
Context.asValue(Object).IllegalStateException - if the context is already closed.UnsupportedOperationException - if the value does not have any members, the key does not exist and new members cannot be added, or the existing
member is not modifiable.PolyglotException - if a guest language error occurred during execution.NullPointerException - if the identifier is null.public boolean removeMember(String identifier)
true if the member was
successfully removed, false if such a member does not exist.UnsupportedOperationException - if the value does not have any members or if the key exists but cannot be removed.IllegalStateException - if the context is already closed.PolyglotException - if a guest language error occurred during execution.NullPointerException - if the identifier is null.public boolean canExecute()
true if the value can be executed.IllegalStateException - if the underlying context was closed.Value.execute(Object...)public Value execute(Object... arguments)
can be executed and returns its result. If no
result value is expected or needed use Value.executeVoid(Object...) for better
performance. All arguments are subject to polyglot value mapping rules as described in
Context.asValue(Object).IllegalStateException - if the underlying context was closed.IllegalArgumentException - if a wrong number of arguments was provided or one of the
arguments was not applicable.UnsupportedOperationException - if this value cannot be executed.PolyglotException - if a guest language error occurred during execution.NullPointerException - if the arguments array is null.Value.executeVoid(Object...)public void executeVoid(Object... arguments)
can be executed. All arguments are subject to
polyglot value mapping rules as described in Context.asValue(Object).IllegalStateException - if the underlying context was closed.IllegalArgumentException - if a wrong number of arguments was provided or one of the
arguments was not applicable.UnsupportedOperationException - if this value cannot be executed.PolyglotException - if a guest language error occurred during execution.NullPointerException - if the arguments array is null.Value.execute(Object...)public boolean canInstantiate()
true if the value can be instantiated. This indicates that the
Value.newInstance(Object...) can be used with this value.public Value newInstance(Object... arguments)
can be instantiated. All arguments
are subject to polyglot value mapping rules as described in Context.asValue(Object).IllegalStateException - if the underlying context was closed.IllegalArgumentException - if a wrong number of arguments was provided or one of the
arguments was not applicable.UnsupportedOperationException - if this value cannot be instantiated.PolyglotException - if a guest language error occurred during execution.NullPointerException - if the arguments array is null.public boolean canInvokeMember(String identifier)
true if the given member exists and can be invoked. Returns
false if the member does not exist (Value.hasMember(String) returns
false), or is not invocable.identifier - the member identifierIllegalStateException - if the context is already closed.PolyglotException - if a guest language error occurred.For a list of members.,
Value.invokeMember(String, Object...)public Value invokeMember(String identifier, Object... arguments)
Value.execute(Object...), this is an object
oriented execution of a member of an object. To test whether invocation is supported, call
Value.canInvokeMember(String). When object oriented semantics are not supported, use
Value.getMember(String).execute(Object...)
instead.identifier - the member identifier to invokearguments - the invocation argumentsUnsupportedOperationException - if this member cannot be invoked.PolyglotException - if a guest language error occurred during invocation.NullPointerException - if the arguments array is null.Value.canInvokeMember(String)public boolean isString()
true if this value represents a string.PolyglotException - if a guest language error occurred during execution.IllegalStateException - if the underlying context was closed.public String asString()
String value if this value is a string. This method
returns null if this value represents a null value.ClassCastException - if this value could not be converted to string.UnsupportedOperationException - if this value does not represent a string.PolyglotException - if a guest language error occurred during execution.public boolean fitsInInt()
PolyglotException - if a guest language error occurred during execution.IllegalStateException - if the underlying context was closed.Value.asInt()public int asInt()
NullPointerException - if this value represents null.ClassCastException - if this value could not be converted.PolyglotException - if a guest language error occurred during execution.IllegalStateException - if the underlying context was closed.public boolean isBoolean()
true if this value represents a boolean value.PolyglotException - if a guest language error occurred during execution.IllegalStateException - if the underlying context was closed.Value.asBoolean()public boolean asBoolean()
boolean representation of this value if it is boolean.NullPointerException - if this value represents nullClassCastException - if this value could not be converted.PolyglotException - if a guest language error occurred during execution.IllegalStateException - if the underlying context was closed.public boolean isNumber()
true if this value represents a number, else
false. The number value may be accessed as byte,
short int long, float or double value.PolyglotException - if a guest language error occurred during execution.IllegalStateException - if the underlying context was closed.public boolean fitsInLong()
PolyglotException - if a guest language error occurred during execution.IllegalStateException - if the underlying context was closed.Value.asLong()public long asLong()
NullPointerException - if this value represents null.ClassCastException - if this value could not be converted to long.PolyglotException - if a guest language error occurred during execution.IllegalStateException - if the underlying context was closed.public boolean fitsInDouble()
PolyglotException - if a guest language error occurred during execution.IllegalStateException - if the underlying context was closed.Value.asDouble()public double asDouble()
NullPointerException - if this value represents null.ClassCastException - if this value could not be converted.PolyglotException - if a guest language error occurred during execution.IllegalStateException - if the underlying context was closed.public boolean fitsInFloat()
PolyglotException - if a guest language error occurred during execution.IllegalStateException - if the underlying context was closed.Value.asFloat()public float asFloat()
NullPointerException - if this value represents null.ClassCastException - if this value could not be converted.PolyglotException - if a guest language error occurred during execution.IllegalStateException - if the underlying context was closed.public boolean fitsInByte()
PolyglotException - if a guest language error occurred during execution.IllegalStateException - if the underlying context was closed.Value.asByte()public byte asByte()
NullPointerException - if this value represents null.ClassCastException - if this value could not be converted.PolyglotException - if a guest language error occurred during execution.IllegalStateException - if the underlying context was closed.public boolean fitsInShort()
PolyglotException - if a guest language error occurred during execution.IllegalStateException - if the underlying context was closed.Value.asShort()public short asShort()
NullPointerException - if this value represents null.ClassCastException - if this value could not be converted.PolyglotException - if a guest language error occurred during execution.IllegalStateException - if the underlying context was closed.public boolean isNull()
true if this value is a null like.PolyglotException - if a guest language error occurred during execution.IllegalStateException - if the underlying context was closed.public boolean isNativePointer()
true if this value is a native pointer. The value of the pointer can be
accessed using Value.asNativePointer().PolyglotException - if a guest language error occurred during execution.IllegalStateException - if the underlying context was closed.public long asNativePointer()
long value.UnsupportedOperationException - if the value is not a pointer.PolyglotException - if a guest language error occurred during execution.IllegalStateException - if the underlying context was closed.public boolean isHostObject()
true if the value originated form the host language Java. In such a case
the value can be accessed using Value.asHostObject().PolyglotException - if a guest language error occurred during execution.IllegalStateException - if the underlying context was closed.public <T> T asHostObject()
UnsupportedOperationException - if Value.isHostObject() is false.PolyglotException - if a guest language error occurred during execution.IllegalStateException - if the underlying context was closed.public boolean isProxyObject()
true if this value represents a Proxy. The proxy instance can be
unboxed using Value.asProxyObject().PolyglotException - if a guest language error occurred during execution.IllegalStateException - if the underlying context was closed.public <T extends Proxy> T asProxyObject()
Proxy. Proxies are not automatically boxed to
host objects on host language call boundaries (Java methods).UnsupportedOperationException - if a value is not a proxy object.PolyglotException - if a guest language error occurred during execution.IllegalStateException - if the underlying context was closed.public <T> T as(Class<T> targetType) throws ClassCastException, IllegalStateException, PolyglotException
The following target types are supported and interpreted in the following order:
Value.class is always supported and returns this instance.
host object then all classes
implemented or extended by the host object can be used as target type.
String.class is supported if the value is a string, a number, or a boolean. Conversions are
done using the toString method of the respective Java type, and in the case of
floating-point numbers, may result in a slight loss of precision.
Character.class is supported if the value is a string of length one.
Number.class is supported if the value is a number. Byte, Short, Integer, Long, Float and
Double are allowed if they fit without conversion. If a conversion is necessary then
a ClassCastException is thrown. Primitive class literals throw a
NullPointerException if the value represents null. If the target
type is a (boxed) primitive number class and the value is a string, an
attempt will be made to parse the string using the class's parse method (e.g.
Integer.parseInt(String)), which may be lossy in the case of floating-point types; no
white space or type suffix is permitted in the string.
Boolean.class is supported if the value is a boolean. Primitive boolean.class literal is also supported. The primitive
class literal throws a NullPointerException if the value represents null. If the value is a string it will be converted if it equals either
"true" or "false".
host object.
Object.class is always supported. See section Object mapping rules.
Map.class is supported if the value has members or array elements. The returned map can be safely cast
to MapClassCastException is thrown for other unsupported target types.
JavaScript Usage Examples:
Context context = Context.create();
assert context.eval("js", "undefined").as(Object.class) == null;
assert context.eval("js", "'foobar'").as(String.class).equals("foobar");
assert context.eval("js", "42").as(Integer.class) == 42;
assert context.eval("js", "{foo:'bar'}").as(Map.class).get("foo").equals("bar");
assert context.eval("js", "[42]").as(List.class).get(0).equals(42);
assert ((Map)context.eval("js", "[{foo:'bar'}]").as(List.class).get(0)).get("foo").equals("bar");
@FunctionalInterface interface IntFunction { int foo(int value); }
assert context.eval("js", "(function(a){a})").as(IntFunction.class).foo(42) == 42;
@FunctionalInterface interface StringListFunction { int foo(List<String> value); }
assert context.eval("js", "(function(a){a.length})").as(StringListFunction.class)
.foo(new String[]{"42"}) == 1;
Object target mapping is useful to map polyglot values to its closest corresponding standard
JDK type.
The following rules apply when Object is used as a target type:
null then null is returned.
host object then the value is coerced to
host object value.
string then the value is coerced to String
or Character.
boolean then the value is coerced to
Boolean.
number then the value is coerced to Number.
The specific sub type of the Number is not specified. Users need to be prepared for
any Number subclass including BigInteger or BigDecimal. It is recommended to
cast to Number and then convert to a Java primitive like with
Number.longValue().
has members then the result value will implement
Map. If this value has members then all members are accessible
using String keys. The size of the returned Map is equal
to the count of all members. The returned value may also implement Function if the
value can be executed or instantiated.
array elements and it has an
array size that is smaller or equal than
Integer.MAX_VALUE then the result value will implement List. Every array
element of the value maps to one list element. The size of the returned list maps to the
array size of the value. The returned value may also implement Function if the value
can be executed or instantiated.
executed or instantiated then the result value implements Function. By default the
argument of the function will be used as single argument to the function when executed. If a
value of type Object[] is provided then the function will be executed with
those arguments. The returned function may also implement Map if the value has
array elements or members.
Value instance is returned.
host objects, String, Number,
Boolean and null values have unlimited lifetime. Other values will throw
an IllegalStateException for any operation if their originating context was closed.
If a Map element is modified, a List element is modified or a
Function argument is provided then these values are interpreted according to the
host to polyglot value mapping rules.
JavaScript Usage Examples:
Context context = Context.create();
assert context.eval("js", "undefined").as(Object.class) == null;
assert context.eval("js", "'foobar'").as(Object.class) instanceof String;
assert context.eval("js", "42").as(Object.class) instanceof Number;
assert context.eval("js", "[]").as(Object.class) instanceof Map;
assert context.eval("js", "{}").as(Object.class) instanceof Map;
assert ((Map
If polyglot values are mapped as Java primitives such as Boolean, null,
String, Character or Number, then the identity of the polyglot value
is not preserved. All other results can be converted back to a polyglot value
using Context.asValue(Object).
Mapping Example using JavaScript: This example first creates a new JavaScript object
and maps it to a Map. Using the Context.asValue(Object) it is possible to
recreate the polyglot value from the Java map. The JavaScript object identity
is preserved in the process.
Context context = Context.create(); Map
targetType - the target Java type to mapClassCastException - if polyglot value could not be mapped to the target type.PolyglotException - if the conversion triggered a guest language error.IllegalStateException - if the underlying context is already closed.NullPointerException - if the target type is null.to map to generic type signatures.public <T> T as(TypeLiteral<T> targetType)
TypeLiteral.
Usage example:
static final TypeLiteral> STRING_LIST = new TypeLiteral
>() { }; public static void main(String[] args) { Context context = Context.create(); List
javaList = context.eval("js", "['foo', 'bar', 'bazz']").as(STRING_LIST); assert javaList.get(0).equals("foo"); }
NullPointerException - if the target type is null.Value.as(Class)public String toString()
public SourceSection getSourceLocation()
SourceSection or null if unknownpublic static Value asValue(Object o)
Context.asValue(Object) for efficiency instead.o - the object to convertIllegalStateException - if no context is currently entered.Conversion rules.