procyon icon indicating copy to clipboard operation
procyon copied to clipboard

Invocation of static generic method of generic class causes "Illegal type in constant pool"

Open BladeWise opened this issue 3 years ago • 2 comments

Trying to invoke the static generic method

Map.<K,V>of(K k1, V v1, K k2, V v2)

produces an "Illegal type in constant pool" error during the lambda class generation.

Below the test case I am using:

    @Test
    void canInvokeGenericStaticMethodOfGenericClass() {
        final var interfaceType = Type.of(Supplier.class).makeGenericType(Types.Map.makeGenericType(Types.String, Types.Integer));
        // Map.of(K k1, V v1, K k2, V v2)
        final var mapOfMethod =
            Types.Map.getMethod("of", Types.Object, Types.Object, Types.Object, Types.Object).makeGenericMethod(Types.String, Types.Integer);
        final var invoke =
            Expression.call(
                mapOfMethod,
                new ExpressionList<>(Expression.constant("k1"), Expression.constant(22), Expression.constant("k2"), Expression.constant(109))
        );
        final var lambda = Expression.lambda(interfaceType, invoke);
        //noinspection unchecked
        final var delegate = (Supplier<Map<String, Integer>>) lambda.compile();
        final var map = delegate.get();
        assertNotNull(map);
        assertEquals(map.get("key2"), 109);
    }

Attached the generated class (zipped). lambda_mapOf.zip

BladeWise avatar Feb 14 '22 17:02 BladeWise

I suspect this is due to Procyon emitting classes in Java 5 format, which doesn't support static interface methods. Emitting newer classfiles is, unfortunately, quite an undertaking.

mstrobel avatar Feb 17 '22 17:02 mstrobel

Have you considered migrating the runtime code generation part to ASM, or maybe make the code generation classes pluggable, so that it is possible to provide a different implementation of TypeBuilder (or emitter)?

I use Procyon mainly for the reflection and expressions features (which are great), the runtime code generation is the cherry on top, but I wouldn't mind if it was delegated to an external library.

BladeWise avatar Feb 17 '22 18:02 BladeWise