apache/cassandra · error · InvalidRequestException
Check your source to not define additional Java methods or c
Error message
Check your source to not define additional Java methods or constructors
What it means
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.
Source
Thrown at src/java/org/apache/cassandra/cql3/functions/JavaBasedUDFunction.java:353
try
{
thread.setContextClassLoader(UDFunction.udfClassLoader);
// Execute UDF intiialization from UDF class loader
Class cls = Class.forName(targetClassName, false, targetClassLoader);
// Count only non-synthetic methods, so code coverage instrumentation doesn't cause a miscount
int nonSyntheticMethodCount = 0;
for (Method m : cls.getDeclaredMethods())
{
if (!m.isSynthetic())
{
nonSyntheticMethodCount += 1;
}
}
if (nonSyntheticMethodCount != 3 || cls.getDeclaredConstructors().length != 1)
throw new InvalidRequestException("Check your source to not define additional Java methods or constructors");
MethodType methodType = MethodType.methodType(void.class)
.appendParameterTypes(UDFDataType.class, UDFContext.class);
MethodHandle ctor = MethodHandles.lookup().findConstructor(cls, methodType);
this.javaUDF = (JavaUDF) ctor.invokeWithArguments(resultType, udfContext);
}
finally
{
thread.setContextClassLoader(orig);
}
}
catch (InvocationTargetException e)
{
// in case of an ITE, use the cause
logger.error(String.format("Could not compile function '%s' from Java source:%n%s", name, javaSource), e);
throw new InvalidRequestException(String.format("Could not compile function '%s' from Java source: %s", name, e.getCause()));
}
catch (InvalidRequestException | VirtualMachineError e)
{View on GitHub (pinned to 88fd0f6a0e)
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
Example fix
// before
cREATE FUNCTION f(x int) RETURNS int LANGUAGE java AS '
private int helper(int v) { return v * 2; }
return helper(x);
';
// after
cREATE FUNCTION f(x int) RETURNS int LANGUAGE java AS '
return x * 2;
'; Defensive patterns
Strategy: validation
Validate before calling
// Before CREATE FUNCTION, ensure the Java source contains only the single UDF body:
String src = javaSource;
if (src.contains("public ") && (src.matches("(?s).*\\b(void|int|long|double|float|boolean|String)\\s+\\w+\\s*\\(.*")))
throw new IllegalArgumentException("UDF source must not define extra methods or constructors"); Try / catch
try { session.execute(createFunctionStmt); }
catch (InvalidRequestException e) {
if (e.getMessage().contains("Check your source")) { /* strip extra methods and retry */ }
else throw e;
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Java source compilation failed:
- Java source compilation failed:\n
- Could not compile function '%s' from Java source: %s
- Function does not declare an argument named '${argName}'
- Invalid value for CQL type ${toDataType().getName()}
AI-assisted analysis of apache/cassandra@88fd0f6a0e (2026-09-10).
Data as JSON: /api/errors/851932f1024f0a9d.
Report an issue: GitHub.