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

  1. Inspect the wrapped cause in the stack trace (GenerateCodeException.getCause()) — it names the real template failure.
  2. Reset customized toString/equals/hashCode templates to defaults (Settings > Editor > Live Templates/File Templates) and retry.
  3. Fix the VTL: verify every $variable referenced exists in the generator's context; check #if/#foreach syntax and quoting.
  4. Ensure 'autoImportPackages', if set by the template, is set to a String (it is cast to String on extraction).
  5. 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

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


AI-assisted analysis of JetBrains/intellij-community@be881553f2 (2026-08-14). Data as JSON: /api/errors/e75c728cc6719841. Report an issue: GitHub.