use of org.graalvm.polyglot.Value in project graal by oracle.
the class ValueHostInteropTest method testNewObject.
@Test
public void testNewObject() {
Value objectClass = context.asValue(Object.class);
Value object = objectClass.newInstance();
assertTrue(object.isHostObject());
}
use of org.graalvm.polyglot.Value in project graal by oracle.
the class ValueHostInteropTest method accessAllProperties.
@Test
public void accessAllProperties() {
Value pojo = context.asValue(new PrivatePOJO());
Map<?, ?> map = pojo.as(Map.class);
int cnt = 0;
for (Map.Entry<?, ?> entry : map.entrySet()) {
Object key = entry.getKey();
Object value = entry.getValue();
assertNotNull(key);
assertNotNull(value);
cnt++;
}
assertEquals("No properties", 0, cnt);
assertEquals("Empty: " + map, 0, map.size());
}
use of org.graalvm.polyglot.Value in project graal by oracle.
the class ValueHostInteropTest method testException2.
@Test
public void testException2() {
Value hashMapClass = context.asValue(HashMap.class);
try {
hashMapClass.newInstance(-1);
fail("expected an exception but none was thrown");
} catch (PolyglotException ex) {
assertTrue("expected HostException but was: " + ex.getClass(), ex.isHostException());
assertThat(ex.asHostException(), CoreMatchers.instanceOf(IllegalArgumentException.class));
}
try {
hashMapClass.newInstance("");
fail("expected an exception but none was thrown");
} catch (IllegalArgumentException ex) {
}
}
use of org.graalvm.polyglot.Value in project graal by oracle.
the class ValueHostInteropTest method testRecursiveListMarshalling.
@Test
public void testRecursiveListMarshalling() {
List<GregorianCalendar> testList = Arrays.asList(new GregorianCalendar());
Value testListValue = context.asValue(testList);
assertTrue(testListValue.isHostObject());
Value calendarValue = testListValue.getArrayElement(0);
assertTrue(calendarValue.isHostObject());
assertValue(context, testListValue);
assertValue(context, calendarValue);
}
use of org.graalvm.polyglot.Value in project graal by oracle.
the class ValueHostInteropTest method accessAllPublicPropertiesDirectly.
@Test
public void accessAllPublicPropertiesDirectly() {
final PublicPOJO orig = new PublicPOJO();
final Value pojo = context.asValue(orig);
Object[] propertyNames = pojo.getMemberKeys().toArray();
assertEquals("One instance field and one method", 2, propertyNames.length);
assertEquals("One field x", "x", propertyNames[0]);
assertEquals("One method to access x", "readX", propertyNames[1]);
Value readX = pojo.getMember("readX");
assertTrue(readX.canExecute());
orig.writeX(10);
assertEquals(10, readX.execute().asInt());
}
Aggregations