{"record":{"id":"851932f1024f0a9d","repo":"apache/cassandra","slug":"check-your-source-to-not-define-additional-java-me","errorCode":null,"errorMessage":"Check your source to not define additional Java methods or constructors","messagePattern":"Check your source to not define additional Java methods or constructors","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/functions/JavaBasedUDFunction.java","lineNumber":353,"sourceCode":"            try\n            {\n                thread.setContextClassLoader(UDFunction.udfClassLoader);\n                // Execute UDF intiialization from UDF class loader\n\n                Class cls = Class.forName(targetClassName, false, targetClassLoader);\n\n                // Count only non-synthetic methods, so code coverage instrumentation doesn't cause a miscount\n                int nonSyntheticMethodCount = 0;\n                for (Method m : cls.getDeclaredMethods())\n                {\n                    if (!m.isSynthetic())\n                    {\n                        nonSyntheticMethodCount += 1;\n                    }\n                }\n\n                if (nonSyntheticMethodCount != 3 || cls.getDeclaredConstructors().length != 1)\n                    throw new InvalidRequestException(\"Check your source to not define additional Java methods or constructors\");\n                MethodType methodType = MethodType.methodType(void.class)\n                                                  .appendParameterTypes(UDFDataType.class, UDFContext.class);\n                MethodHandle ctor = MethodHandles.lookup().findConstructor(cls, methodType);\n                this.javaUDF = (JavaUDF) ctor.invokeWithArguments(resultType, udfContext);\n            }\n            finally\n            {\n                thread.setContextClassLoader(orig);\n            }\n        }\n        catch (InvocationTargetException e)\n        {\n            // in case of an ITE, use the cause\n            logger.error(String.format(\"Could not compile function '%s' from Java source:%n%s\", name, javaSource), e);\n            throw new InvalidRequestException(String.format(\"Could not compile function '%s' from Java source: %s\", name, e.getCause()));\n        }\n        catch (InvalidRequestException | VirtualMachineError e)\n        {","sourceCodeStart":335,"sourceCodeEnd":371,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/functions/JavaBasedUDFunction.java#L335-L371","documentation":"When creating a Java UDF, Cassandra compiles the user's Java source and reflects on the resulting class. It expects exactly 3 non-synthetic methods plus exactly one declared constructor (the JavaUDF constructor taking UDFDataType and UDFContext). If the user's source declares any extra method or constructor, this InvalidRequestException is thrown because additional code paths would bypass the UDF sandbox contract.","triggerScenarios":"CREATE FUNCTION with a Java source body that defines helper methods, overloaded constructors, static methods, or any method beyond the ones generated/allowed by the UDF wrapper.","commonSituations":"Developers pasting utility/helper methods into the UDF source for reuse; porting UDFs from other engines that allow arbitrary class members; copying a Java class into the function source rather than only the needed body.","solutions":["Remove all methods and constructors from the Java source except what the UDF framework generates; keep logic inline in the single function body","Move helper logic into a separate class in an external JAR added to the classpath, and reference it from the UDF body","If sharing code is essential, use a native-code UDF (via the code parameter) or a scalar function implemented in Java server-side instead of source-based UDF"],"exampleFix":"// before\ncREATE FUNCTION f(x int) RETURNS int LANGUAGE java AS '\n  private int helper(int v) { return v * 2; }\n  return helper(x);\n';\n// after\ncREATE FUNCTION f(x int) RETURNS int LANGUAGE java AS '\n  return x * 2;\n';","handlingStrategy":"validation","validationCode":"// Before CREATE FUNCTION, ensure the Java source contains only the single UDF body:\nString src = javaSource;\nif (src.contains(\"public \") && (src.matches(\"(?s).*\\\\b(void|int|long|double|float|boolean|String)\\\\s+\\\\w+\\\\s*\\\\(.*\")))\n    throw new IllegalArgumentException(\"UDF source must not define extra methods or constructors\");","typeGuard":null,"tryCatchPattern":"try { session.execute(createFunctionStmt); }\ncatch (InvalidRequestException e) {\n  if (e.getMessage().contains(\"Check your source\")) { /* strip extra methods and retry */ }\n  else throw e;\n}","preventionTips":["Keep UDF Java source to a single return-expression/body with no member declarations","Factor shared logic into an external JAR on the classpath, not into the UDF source","Review the source for 'private'/'public' method or constructor declarations before CREATE FUNCTION"],"tags":["udf","java","invalid-request","reflection"],"backgroundTag":"invalid-udf-source","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}