{"record":{"id":"8d1b31eece585b38","repo":"apache/flink","slug":"could-not-initialize-basic-comparator","errorCode":null,"errorMessage":"Could not initialize basic comparator {}","messagePattern":"Could not initialize basic comparator (.+?)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"flink-core/src/main/java/org/apache/flink/api/common/typeinfo/BasicTypeInfo.java","lineNumber":309,"sourceCode":"    @PublicEvolving\n    public static <X> BasicTypeInfo<X> getInfoFor(Class<X> type) {\n        if (type == null) {\n            throw new NullPointerException();\n        }\n\n        @SuppressWarnings(\"unchecked\")\n        BasicTypeInfo<X> info = (BasicTypeInfo<X>) TYPES.get(type);\n        return info;\n    }\n\n    private static <X> TypeComparator<X> instantiateComparator(\n            Class<? extends TypeComparator<X>> comparatorClass, boolean ascendingOrder) {\n        try {\n            Constructor<? extends TypeComparator<X>> constructor =\n                    comparatorClass.getConstructor(boolean.class);\n            return constructor.newInstance(ascendingOrder);\n        } catch (Exception e) {\n            throw new RuntimeException(\n                    \"Could not initialize basic comparator \" + comparatorClass.getName(), e);\n        }\n    }\n\n    private static final Map<Class<?>, BasicTypeInfo<?>> TYPES =\n            new HashMap<Class<?>, BasicTypeInfo<?>>();\n\n    static {\n        TYPES.put(String.class, STRING_TYPE_INFO);\n        TYPES.put(Boolean.class, BOOLEAN_TYPE_INFO);\n        TYPES.put(boolean.class, BOOLEAN_TYPE_INFO);\n        TYPES.put(Byte.class, BYTE_TYPE_INFO);\n        TYPES.put(byte.class, BYTE_TYPE_INFO);\n        TYPES.put(Short.class, SHORT_TYPE_INFO);\n        TYPES.put(short.class, SHORT_TYPE_INFO);\n        TYPES.put(Integer.class, INT_TYPE_INFO);\n        TYPES.put(int.class, INT_TYPE_INFO);\n        TYPES.put(Long.class, LONG_TYPE_INFO);","sourceCodeStart":291,"sourceCodeEnd":327,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-core/src/main/java/org/apache/flink/api/common/typeinfo/BasicTypeInfo.java#L291-L327","documentation":"BasicTypeInfo.instantiateComparator reflectively constructs the comparatorClass via comparatorClass.getConstructor(boolean.class).newInstance(ascending). If the comparator class lacks a public single-boolean constructor, or instantiation throws (security, missing constructor, abstract class), the failure is wrapped in a RuntimeException naming the comparator class. This is an internal contract violation: all basic comparators must have that constructor.","triggerScenarios":"A custom BasicTypeInfo registered with a comparatorClass that does not expose a public (boolean) constructor; reflective instantiation hitting an InstantiationException because the class is abstract or an interface; a security manager blocking reflective access.","commonSituations":"User-defined basic type info with a comparator that breaks the boolean-constructor contract; shaded or relocated comparator classes whose constructor signature changed; JVM module/access restrictions on reflective construction in newer JDKs.","solutions":["Ensure any custom comparator class passed to BasicTypeInfo has a public constructor accepting a single boolean (sort order).","Prefer using the built-in basic types rather than registering a custom BasicTypeInfo with a bespoke comparator.","If running on a restricted JDK, open the comparator package to reflective access or avoid custom comparators."],"exampleFix":"// before\nclass MyComparator implements TypeComparator<X> {\n    public MyComparator() {} // missing boolean arg -> throws\n}\n\n// after\nclass MyComparator implements TypeComparator<X> {\n    public MyComparator(boolean ascending) { ... }\n}","handlingStrategy":"type-guard","validationCode":"// Verify the comparator class has the required constructor before relying on it\ntry {\n    comparatorClass.getConstructor(boolean.class);\n} catch (NoSuchMethodException e) {\n    throw new IllegalStateException(\n        comparatorClass + \" must have a public (boolean) constructor\", e);\n}","typeGuard":"static boolean hasBooleanCtor(Class<? extends TypeComparator<?>> c) {\n    try { c.getConstructor(boolean.class); return true; }\n    catch (NoSuchMethodException e) { return false; }\n}","tryCatchPattern":"try {\n    return BasicTypeInfo.instantiateComparator(clazz, asc);\n} catch (RuntimeException e) {\n    throw new IllegalStateException(\n        \"Comparator \" + clazz + \" is misconfigured; check its constructor\", e);\n}","preventionTips":["Do not register custom BasicTypeInfo with a comparator missing a public (boolean) constructor.","Prefer the built-in basic types and their comparators.","If forking, add a reflection test for every comparator class."],"tags":["type-information","comparator","reflection","basic-type"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}