Search in sources :

Example 1 with TypeLiteral

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);
}
Also used : TypeLiteral(org.graalvm.polyglot.TypeLiteral) ValueAssert.assertValue(com.oracle.truffle.tck.tests.ValueAssert.assertValue) Value(org.graalvm.polyglot.Value) AfterClass(org.junit.AfterClass) BeforeClass(org.junit.BeforeClass) Test(org.junit.Test)

Example 2 with TypeLiteral

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());
    }
}
Also used : ProxyObject(org.graalvm.polyglot.proxy.ProxyObject) List(java.util.List) HostAccess(org.graalvm.polyglot.HostAccess) TypeLiteral(org.graalvm.polyglot.TypeLiteral) Value(org.graalvm.polyglot.Value) Map(java.util.Map) ProxyArray(org.graalvm.polyglot.proxy.ProxyArray) Test(org.junit.Test) Context(org.graalvm.polyglot.Context) Assert(org.junit.Assert) Function(java.util.function.Function) Context(org.graalvm.polyglot.Context) HostAccess(org.graalvm.polyglot.HostAccess) Value(org.graalvm.polyglot.Value) ProxyObject(org.graalvm.polyglot.proxy.ProxyObject) Map(java.util.Map) Test(org.junit.Test)

Example 3 with TypeLiteral

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);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) Function(java.util.function.Function) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) TypeLiteral(org.graalvm.polyglot.TypeLiteral) GenericArrayType(java.lang.reflect.GenericArrayType) Map(java.util.Map) Test(org.junit.Test)

Example 4 with TypeLiteral

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]);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeLiteral(org.graalvm.polyglot.TypeLiteral) Test(org.junit.Test)

Example 5 with TypeLiteral

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]);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeLiteral(org.graalvm.polyglot.TypeLiteral) List(java.util.List) GenericArrayType(java.lang.reflect.GenericArrayType) Test(org.junit.Test)

Aggregations

TypeLiteral (org.graalvm.polyglot.TypeLiteral)5 Test (org.junit.Test)5 GenericArrayType (java.lang.reflect.GenericArrayType)3 ParameterizedType (java.lang.reflect.ParameterizedType)3 Type (java.lang.reflect.Type)3 WildcardType (java.lang.reflect.WildcardType)3 List (java.util.List)2 Map (java.util.Map)2 Function (java.util.function.Function)2 Value (org.graalvm.polyglot.Value)2 ValueAssert.assertValue (com.oracle.truffle.tck.tests.ValueAssert.assertValue)1 Context (org.graalvm.polyglot.Context)1 HostAccess (org.graalvm.polyglot.HostAccess)1 ProxyArray (org.graalvm.polyglot.proxy.ProxyArray)1 ProxyObject (org.graalvm.polyglot.proxy.ProxyObject)1 AfterClass (org.junit.AfterClass)1 Assert (org.junit.Assert)1 BeforeClass (org.junit.BeforeClass)1