JetBrains/intellij-community · error · GenerateCodeException
Error in Velocity code generator
Error message
Error in Velocity code generator
What it means
GenerationUtil wraps any non-cancellation exception thrown while evaluating a Velocity template (toString/equals/hashCode/code generators built on Velocity) into GenerateCodeException('Error in Velocity code generator'). ProcessCanceledException is deliberately rethrown untouched; everything else — template syntax errors, missing context objects, ClassCastException on 'autoImportPackages', IO errors — is bundled with the original cause for higher layers to report.
Source
Thrown at java/java-impl/src/org/jetbrains/java/generate/GenerationUtil.java:247
if (LOG.isDebugEnabled()) LOG.debug("Velocity Macro:\n" + templateMacro);
// velocity
VelocityEngine velocity = VelocityFactory.getVelocityEngine();
LOG.debug("Executing velocity +++ START +++");
velocity.evaluate(vc, sw, GenerateToStringWorker.class.getName(), templateMacro);
LOG.debug("Executing velocity +++ END +++");
// any additional packages to import returned from velocity?
if (vc.get("autoImportPackages") != null) {
params.put("autoImportPackages", (String)vc.get("autoImportPackages"));
}
}
catch (ProcessCanceledException e) {
throw e;
}
catch (Exception e) {
throw new GenerateCodeException("Error in Velocity code generator", e);
}
return StringUtil.convertLineSeparators(sw.getBuffer().toString());
}
}View on GitHub (pinned to be881553f2)
Solutions
- Inspect the wrapped cause in the stack trace (GenerateCodeException.getCause()) — it names the real template failure.
- Reset customized toString/equals/hashCode templates to defaults (Settings > Editor > Live Templates/File Templates) and retry.
- Fix the VTL: verify every $variable referenced exists in the generator's context; check #if/#foreach syntax and quoting.
- Ensure 'autoImportPackages', if set by the template, is set to a String (it is cast to String on extraction).
- If a plugin supplies the macro, align it with the context populated by GenerateToStringWorker/GenerationUtil for your IDE version.
Example fix
## before (template references missing context var): #foreach($f in $fieldsOfUnknownName)$f.myName#end ## after: #foreach($field in $fields)$field.name#end
Defensive patterns
Strategy: try-catch
Try / catch
try {
GenerationUtil.velocityEvalCode(...);
} catch (GenerateCodeException e) {
Throwable cause = e.getCause(); // the real Velocity/template failure
// log cause, fall back to the default template, inform the user
} Prevention
- Test customized Velocity templates after every IDE/plugin upgrade.
- Reference only context variables documented for the generator; keep 'autoImportPackages' a String.
- Never swallow ProcessCanceledException separately — it is rethrown by design; handle GenerateCodeException as one failure unit.
When it happens
Trigger: Generating toString()/equals()/hashCode() with a custom or damaged Velocity template macro; template referencing a context variable that was never put into the VelocityContext; vc.get("autoImportPackages") returning a non-String, causing ClassCastException; malformed VTL syntax.
Common situations: Users with customized templates under Settings > Editor > File and Code Templates or plugin-provided templates that mismatch the expected context after an IDE/plugin upgrade; templates calling Java methods that now throw; encoding issues in template files; plugin authors injecting their own template macros without populating all referenced context entries.
Related errors
- No JDK specified
- Home directory is not specified for JDK
- {2, choice, 1#flag|1<flags} ''{0}'' not allowed in ''{1}''
- unexpected character ''{0}'' in ''{1}''
- duplicate flag ''{0}'' in ''{1}''
AI-assisted analysis of JetBrains/intellij-community@be881553f2 (2026-08-14).
Data as JSON: /api/errors/e75c728cc6719841.
Report an issue: GitHub.