{"record":{"id":"e75c728cc6719841","repo":"JetBrains/intellij-community","slug":"error-in-velocity-code-generator","errorCode":null,"errorMessage":"Error in Velocity code generator","messagePattern":"Error in Velocity code generator","errorType":"exception","errorClass":"GenerateCodeException","httpStatus":null,"severity":"error","filePath":"java/java-impl/src/org/jetbrains/java/generate/GenerationUtil.java","lineNumber":247,"sourceCode":"\n      if (LOG.isDebugEnabled()) LOG.debug(\"Velocity Macro:\\n\" + templateMacro);\n\n      // velocity\n      VelocityEngine velocity = VelocityFactory.getVelocityEngine();\n      LOG.debug(\"Executing velocity +++ START +++\");\n      velocity.evaluate(vc, sw, GenerateToStringWorker.class.getName(), templateMacro);\n      LOG.debug(\"Executing velocity +++ END +++\");\n\n      // any additional packages to import returned from velocity?\n      if (vc.get(\"autoImportPackages\") != null) {\n        params.put(\"autoImportPackages\", (String)vc.get(\"autoImportPackages\"));\n      }\n    }\n    catch (ProcessCanceledException e) {\n      throw e;\n    }\n    catch (Exception e) {\n      throw new GenerateCodeException(\"Error in Velocity code generator\", e);\n    }\n\n    return StringUtil.convertLineSeparators(sw.getBuffer().toString());\n  }\n}","sourceCodeStart":229,"sourceCodeEnd":252,"githubUrl":"https://github.com/JetBrains/intellij-community/blob/be881553f2a76ac8b4ea53950d93f0de78295c19/java/java-impl/src/org/jetbrains/java/generate/GenerationUtil.java#L229-L252","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"## before (template references missing context var):\n#foreach($f in $fieldsOfUnknownName)$f.myName#end\n\n## after:\n#foreach($field in $fields)$field.name#end","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  GenerationUtil.velocityEvalCode(...);\n} catch (GenerateCodeException e) {\n  Throwable cause = e.getCause(); // the real Velocity/template failure\n  // log cause, fall back to the default template, inform the user\n}","preventionTips":["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."],"tags":["java","intellij","velocity","code-generation","tostring","template"],"backgroundTag":null,"analyzedSha":"be881553f2a76ac8b4ea53950d93f0de78295c19","analyzedAt":"2026-08-14T14:13:06.425Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}