use of org.graalvm.polyglot.TypeLiteral in project graal by oracle.
the class ValueAPITest method testNullError.
@Test
public void testNullError() {
Value members = context.asValue(new MembersAndArrayAndExecutableAndInstantiable());
assertFails(() -> members.putMember(null, "value"), NullPointerException.class, "identifier");
assertFails(() -> members.getMember(null), NullPointerException.class, "identifier");
assertFails(() -> members.hasMember(null), NullPointerException.class, "identifier");
assertFails(() -> members.removeMember(null), NullPointerException.class, "identifier");
assertFails(() -> members.execute((Object[]) null), NullPointerException.class, null);
assertFails(() -> members.executeVoid((Object[]) null), NullPointerException.class, null);
assertFails(() -> members.newInstance((Object[]) null), NullPointerException.class, null);
assertFails(() -> members.as((Class<?>) null), NullPointerException.class, null);
assertFails(() -> members.as((TypeLiteral<?>) null), NullPointerException.class, null);
}
use of org.graalvm.polyglot.TypeLiteral in project graal by oracle.
the class ArrayToObjectMappingTest method testArrayAsMapLong.
@Test
public void testArrayAsMapLong() {
HostAccess hostAccess = HostAccess.newBuilder(HostAccess.ALL).targetTypeMapping(Value.class, Object.class, v -> v.hasMembers() && v.hasArrayElements(), v -> v.as(new TypeLiteral<Map<Long, Object>>() {
})).build();
try (Context c = Context.newBuilder("js").allowHostAccess(hostAccess).build()) {
Value fn = c.asValue((Function<Object, Object>) f -> {
if (f instanceof Map) {
Assert.assertEquals(4, ((Map<?, ?>) f).size());
return "Map";
}
return f.getClass().getName();
});
Value result = fn.execute(new ArrayAndMembers("a", "b", "c", "d"));
Assert.assertEquals("Map", result.asString());
}
}
use of org.graalvm.polyglot.TypeLiteral in project graal by oracle.
the class TypeLiteralTest method testComplexLiteral.
@Test
public void testComplexLiteral() {
TypeLiteral<?> literal = new TypeLiteral<List<Map<Integer, Map<String, Map<? extends Number, Function<Long, ?>>[]>>>>() {
};
Type type = literal.getType();
Class<?> rawType = literal.getRawType();
assertSame(List.class, rawType);
assertThat(type, instanceOf(ParameterizedType.class));
ParameterizedType list = (ParameterizedType) literal.getType();
assertThat(list.getActualTypeArguments()[0], instanceOf(ParameterizedType.class));
ParameterizedType map1 = (ParameterizedType) list.getActualTypeArguments()[0];
assertSame(Map.class, map1.getRawType());
assertSame(Integer.class, map1.getActualTypeArguments()[0]);
assertThat(map1.getActualTypeArguments()[1], instanceOf(ParameterizedType.class));
ParameterizedType map2 = (ParameterizedType) map1.getActualTypeArguments()[1];
assertSame(Map.class, map2.getRawType());
assertSame(String.class, map2.getActualTypeArguments()[0]);
assertThat(map2.getActualTypeArguments()[1], instanceOf(GenericArrayType.class));
GenericArrayType arr = (GenericArrayType) map2.getActualTypeArguments()[1];
assertThat(arr.getGenericComponentType(), instanceOf(ParameterizedType.class));
ParameterizedType map3 = (ParameterizedType) arr.getGenericComponentType();
assertSame(Map.class, map3.getRawType());
assertThat(map3.getActualTypeArguments()[0], instanceOf(WildcardType.class));
WildcardType key3 = (WildcardType) map3.getActualTypeArguments()[0];
assertSame(Number.class, key3.getUpperBounds()[0]);
assertEquals(0, key3.getLowerBounds().length);
assertThat(map3.getActualTypeArguments()[1], instanceOf(ParameterizedType.class));
ParameterizedType function = (ParameterizedType) map3.getActualTypeArguments()[1];
assertSame(Function.class, function.getRawType());
assertSame(Long.class, function.getActualTypeArguments()[0]);
assertThat(function.getActualTypeArguments()[1], instanceOf(WildcardType.class));
WildcardType wc = (WildcardType) function.getActualTypeArguments()[1];
assertSame(Object.class, wc.getUpperBounds()[0]);
assertEquals(0, wc.getLowerBounds().length);
}
use of org.graalvm.polyglot.TypeLiteral in project graal by oracle.
the class TypeLiteralTest method testSimpleMap.
@Test
public void testSimpleMap() {
TypeLiteral<?> literal = new TypeLiteral<Map<String, Object[]>>() {
};
Type type = literal.getType();
Class<?> rawType = literal.getRawType();
assertSame(Map.class, rawType);
assertThat(type, instanceOf(ParameterizedType.class));
ParameterizedType map = (ParameterizedType) type;
assertSame(String.class, map.getActualTypeArguments()[0]);
assertSame(Object[].class, map.getActualTypeArguments()[1]);
}
use of org.graalvm.polyglot.TypeLiteral in project graal by oracle.
the class TypeLiteralTest method testSimpleArray.
@Test
public void testSimpleArray() {
TypeLiteral<?> literal = new TypeLiteral<List<String>[]>() {
};
Type type = literal.getType();
Class<?> rawType = literal.getRawType();
assertSame(List[].class, rawType);
assertThat(type, instanceOf(GenericArrayType.class));
GenericArrayType arr = (GenericArrayType) type;
assertThat(arr.getGenericComponentType(), instanceOf(ParameterizedType.class));
ParameterizedType list = (ParameterizedType) arr.getGenericComponentType();
assertSame(String.class, list.getActualTypeArguments()[0]);
}
Aggregations