Skip to content

Refactor duplicate JRE implementations into shared BaseJRE struct #1264

Closed

Description

@stokpop

Currently the Go buildpack has 6 nearly identical JRE implementations (~1300 lines total): OpenJDK, SapMachine, Zulu, IBM, Oracle, GraalVM. Each has its own copy of Supply(), Finalize(), MemoryCalculatorCommand() etc. with only names/strings differing.

In the Ruby buildpack, this was solved cleanly via inheritance — e.g. ZuluJRE < OpenJDKLike was 3 lines.

Proposed fix

Introduce a shared BaseJRE struct with Go embedding:

type BaseJRE struct {
    ctx        *common.Context
    jreName    string
    envVarName string
    memoryCalc *MemoryCalculator
    ...
}

// Supply(), Finalize(), MemoryCalculatorCommand() implemented once on BaseJRE

type ZuluJRE struct { BaseJRE }
type SapMachineJRE struct { BaseJRE }
// etc.

This would reduce ~1300 lines to ~200 lines of shared logic + ~5 lines per vendor.

Context

Discovered while fixing #1257 — the LoadConfig() bug (reading only JBP_CONFIG_OPEN_JDK_JRE for all JREs) was a direct consequence of the duplicated structure. A minimal fix was applied to pass jreName to NewMemoryCalculator. The base struct refactor is the structural follow-up.

Activity

  1. stokpop commented on May 20, 2026

    @stokpop
    ContributorAuthor

    After analyzing the refactor in detail, the complexity tradeoff is worth discussing:

    What the refactor saves: 6 standard JRE files × ~220 lines = ~1320 lines of near-identical code, reduced to a base_jre.go (~150 lines) + 6 thin constructors (~10 lines each) = ~210 lines. Zing stays separate as it is genuinely different. More importantly, any bug fix or improvement to Supply()/Finalize() currently needs to be applied in all 6 files — and it is easy to miss one.

    What it costs: A BaseJRE struct with config/function-field hooks introduces indirection — instead of reading ibm.go top-to-bottom, you jump between the constructor and base_jre.go to follow the flow. Go has no virtual dispatch, so method-override approaches have a 'forgot to call base' footgun; the safer alternative is injecting variation points as function fields, which adds its own complexity.

    The variation is already there: Zing has no memory calculator or jvmkill. IBM needs extra JVM opts. GraalVM has a different error message. Oracle and OpenJDK share the same findJavaHome prefix. More special cases will likely follow.

    Conclusion: The files are self-contained and easy to read as-is, but the duplication is a real maintenance risk. The refactor pays off most when bugs need to be fixed consistently across all JREs. If the JRE set is stable and rarely changes, the added indirection may not be worth it.

    Suggest closing this issue if there is no real benefit.

  2. ramonskie commented on May 20, 2026

    @ramonskie
    Contributor

    i am always for making things more readable and dedepluication.
    so if anyone is interested in picking this up. i will review it

  3. stokpop commented on May 20, 2026

    @stokpop
    ContributorAuthor

    @ramonskie I created a PR for this #1288 : if you can review it, thanks

  4. added a commit that references this issue on Jun 5, 2026
    9a7f22d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions