{"record":{"id":"deae223c6cc0a7b9","repo":"elastic/elasticsearch","slug":"cannot-apply-operation-to-type","errorCode":null,"errorMessage":"Cannot apply [~] operation to type [{}].","messagePattern":"Cannot apply \\[~\\] operation to type \\[(.+?)\\]\\.","errorType":"exception","errorClass":"ClassCastException","httpStatus":null,"severity":"error","filePath":"modules/lang-painless/src/main/java/org/elasticsearch/painless/DefMath.java","lineNumber":68,"sourceCode":"\n    private static boolean not(boolean v) {\n        throw new ClassCastException(\"Cannot apply not [~] to type [boolean]\");\n    }\n\n    private static Object not(Object unary) {\n        if (unary instanceof Long) {\n            return ~(Long) unary;\n        } else if (unary instanceof Integer) {\n            return ~(Integer) unary;\n        } else if (unary instanceof Short) {\n            return ~(Short) unary;\n        } else if (unary instanceof Character) {\n            return ~(Character) unary;\n        } else if (unary instanceof Byte) {\n            return ~(Byte) unary;\n        }\n\n        throw new ClassCastException(\"Cannot apply [~] operation to type \" + \"[\" + unary.getClass().getCanonicalName() + \"].\");\n    }\n\n    // unary negation and plus: applicable to all numeric types\n\n    private static int neg(int v) {\n        return -v;\n    }\n\n    private static long neg(long v) {\n        return -v;\n    }\n\n    private static float neg(float v) {\n        return -v;\n    }\n\n    private static double neg(double v) {\n        return -v;","sourceCodeStart":50,"sourceCodeEnd":86,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/modules/lang-painless/src/main/java/org/elasticsearch/painless/DefMath.java#L50-L86","documentation":"DefMath.not(Object) is the catch-all overload for bitwise NOT on def values whose runtime type is a boxed Number but not one of the supported integral wrappers (Long, Integer, Short, Character, Byte). After all instanceof checks fail, the method throws a ClassCastException naming the actual type.","triggerScenarios":"A Painless script applies ~ to a def variable whose runtime type is an unsupported numeric type such as BigInteger, BigDecimal, or AtomicLong. Example: `def x = new BigInteger('255'); def y = ~x;`","commonSituations":"Interfacing with Java libraries from a Painless script that return BigInteger or BigDecimal. Processing values from third-party ingest plugins that store non-standard numeric types in document fields.","solutions":["Convert the value to a supported integral type before applying ~: `def y = ~(long) x;`","Check with instanceof and branch: only apply ~ to Long, Integer, Short, Character, or Byte.","Avoid def; declare the variable as int or long so the type is known at compile time."],"exampleFix":"// before\ndef x = doc['big_value'].value;\ndef y = ~x;\n\n// after\ndef x = doc['big_value'].value;\ndef y = ~(long) x;","handlingStrategy":"type-guard","validationCode":"// Painless: convert unsupported numeric types to long before ~\ndef x = doc['value'].value;\nif (x instanceof Number) {\n    def y = ~(long) x;\n}","typeGuard":"// Painless type guard for supported integral types\ndef isSupportedIntegral(def value) {\n    return value instanceof Long || value instanceof Integer || value instanceof Short || value instanceof Character || value instanceof Byte;\n}","tryCatchPattern":null,"preventionTips":["Cast BigInteger/BigDecimal values to long or int before applying bitwise operators.","Use explicit types (int, long) instead of def for variables used in bitwise operations.","Check instanceof against the five supported integral wrappers before applying ~."],"tags":["painless","def-type","bitwise","type-mismatch","arithmetic"],"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-12T08:17:17.861Z"}