Skip to content

Generic record pattern not working in EJC #4002

@schosin

Description

@schosin

I've run into an issue with the EJC in a Java 21 project. The below example does not compile within Eclipse (4.35.0), but does with javac (JDK 21, JDK 24):

Cannot infer record pattern types for ComponentType.Wildcard(var bound)

The problem is line 33 and happens when Wildcard implements ComponentType with anything but ComponentType<T>.

import java.util.List;

public class Example {

    private static boolean matches(ComponentType<?> type, ComponentType.RegularComponentType<?> eventType) {
        return switch (type) {
            case ComponentType.RegularComponentType<?> regular -> switch (regular) {
                case ComponentType.ClassType(var clazz) -> switch (eventType) {
                    case ComponentType.ClassType(var eventClazz) -> clazz == eventClazz;
                };
            };
            /* Workaround: 
            case ComponentType.Wildcard<?> wildcard -> switch (eventType) {
                case ComponentType.ClassType(var eventClazz) -> wildcard.bound().isAssignableFrom(eventClazz);
            }; 
            /**/
            case ComponentType.Wildcard(var bound) -> switch (eventType) {
                case ComponentType.ClassType(var eventClazz) -> bound.isAssignableFrom(eventClazz);
            };
            /**/
        };
    }

    sealed interface ComponentType<T> {

        sealed interface RegularComponentType<T> extends ComponentType<T> {
        }

        record ClassType<T>(Class<T> clazz) implements RegularComponentType<T> {
        }

        // "implements ComponentType<T>" works, javac (JDK 21 & JDK 24) accepts both
        record Wildcard<T>(Class<T> bound) implements ComponentType<List<? extends T>> {
        }

    }

}

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions