{"record":{"id":"f4e17bd0a0b10a61","repo":"quarkusio/quarkus","slug":"cannot-compare-f4e17b","errorCode":null,"errorMessage":"Cannot compare ","messagePattern":"Cannot compare ","errorType":"exception","errorClass":"ClassCastException","httpStatus":null,"severity":"warning","filePath":"independent-projects/resteasy-reactive/client/runtime/src/main/java/org/jboss/resteasy/reactive/client/impl/multipart/QuarkusInternalAttribute.java","lineNumber":90,"sourceCode":"\n    @Override\n    public int hashCode() {\n        return getName().hashCode();\n    }\n\n    @Override\n    public boolean equals(Object o) {\n        if (!(o instanceof QuarkusInternalAttribute)) {\n            return false;\n        }\n        QuarkusInternalAttribute attribute = (QuarkusInternalAttribute) o;\n        return getName().equalsIgnoreCase(attribute.getName());\n    }\n\n    @Override\n    public int compareTo(InterfaceHttpData o) {\n        if (!(o instanceof QuarkusInternalAttribute)) {\n            throw new ClassCastException(\"Cannot compare \" + getHttpDataType() +\n                    \" with \" + o.getHttpDataType());\n        }\n        return compareTo((QuarkusInternalAttribute) o);\n    }\n\n    public int compareTo(QuarkusInternalAttribute o) {\n        return getName().compareToIgnoreCase(o.getName());\n    }\n\n    @Override\n    public String toString() {\n        StringBuilder result = new StringBuilder();\n        for (ByteBuf elt : value) {\n            result.append(elt.toString(charset));\n        }\n        return result.toString();\n    }\n","sourceCodeStart":72,"sourceCodeEnd":108,"githubUrl":"https://github.com/quarkusio/quarkus/blob/e1c734241f34c7919086ceb4c9262b4a58f6de44/independent-projects/resteasy-reactive/client/runtime/src/main/java/org/jboss/resteasy/reactive/client/impl/multipart/QuarkusInternalAttribute.java#L72-L108","documentation":"QuarkusInternalAttribute.compareTo(InterfaceHttpData) implements Comparable by first checking the argument is a QuarkusInternalAttribute. If a different InterfaceHttpData implementation is passed, it throws ClassCastException, because attributes can only be ordered against other attributes. It is effectively a defensive type check rather than an unexpected JVM cast failure.","triggerScenarios":"Sorting or comparing a collection of InterfaceHttpData where QuarkusInternalAttribute instances are mixed with other multipart data types (e.g. FileUpload implementations), causing compareTo to be called with a non-attribute object.","commonSituations":"Sorting a List<InterfaceHttpData> built from both form attributes and file uploads (e.g. Collections.sort before encoding a multipart body); generic utilities that compare InterfaceHttpData regardless of concrete type.","solutions":["Sort/filter the collection so it contains only QuarkusInternalAttribute instances before comparing","Use a Comparator that compares by getName() across different InterfaceHttpData types instead of relying on natural ordering","Separate attributes and file uploads into distinct lists and sort each independently"],"exampleFix":"// before\nCollections.sort(httpDataList); // mixes attributes and file uploads\n// after\nCollections.sort(httpDataList, Comparator.comparing(InterfaceHttpData::getName));","handlingStrategy":"type-guard","validationCode":"if (!(data instanceof QuarkusInternalAttribute)) {\n    // compare by name instead of natural ordering\n    Comparator<InterfaceHttpData> byName = Comparator.comparing(InterfaceHttpData::getName, String.CASE_INSENSITIVE_ORDER);\n}","typeGuard":"boolean isComparableAttribute(InterfaceHttpData d) {\n    return d instanceof QuarkusInternalAttribute;\n}","tryCatchPattern":"try {\n    Collections.sort(httpDataList);\n} catch (ClassCastException e) {\n    httpDataList.sort(Comparator.comparing(InterfaceHttpData::getName, String.CASE_INSENSITIVE_ORDER));\n}","preventionTips":["Keep attributes and file uploads in separate lists","Sort with an explicit Comparator by name instead of natural ordering","Only rely on Comparable within homogeneous collections"],"tags":["java","classcastexception","multipart","sorting"],"backgroundTag":"incompatible-compare-types","analyzedSha":"e1c734241f34c7919086ceb4c9262b4a58f6de44","analyzedAt":"2026-09-05T17:01:29.979Z","contentChangedAt":"2026-09-05T17:01:29.979Z","schemaVersion":2},"datasetVersion":"2026-09-12T22:17:10.623Z"}