Scrivio Interview library
LeadershipPrincipal7 min read · updated 12 November 2026

Generic signatures in shared libraries are permanent once published, and badly chosen ones spread through every caller. What review process and rules would you set for generic APIs in your organization's libraries?

Generic signatures are hard to change because erasure hides most changes from the JVM but not from javac, so a release can link fine at run time and still break every caller's build. I would set a few simple rules (PECS wildcards in parameters, no wildcards in return types, as few type parameters as possible, specific types unless the library truly serves many), require an API review with real calling code for every public generic signature, and run a compatibility checker such as japicmp or Revapi in CI on every release.

Why this question is asked

A shared library is a promise. Once fifty services call a method, its signature is effectively frozen, and a generic signature is frozen harder than most because callers write their own types around it: their variables, their lambdas and their helper methods all echo the type parameters we chose. A badly chosen signature does not stay in the library; it spreads into every caller. Interviewers ask this at a senior level to see whether we can turn knowledge of generics into rules and a process that a whole organization can follow. In this answer, we will recap why generic changes are dangerous, set out the design rules, describe the review process, add automated compatibility checks, and finish with how to handle a signature that is already wrong.

1Why Generic Signatures Are So Hard to Change

Type erasure means the compiler checks generic types and then removes most of them from the bytecode. A method declared as void addAll(Collection<? extends T> items) is stored for linking purposes as addAll(Collection). The full generic signature is kept in a separate Signature attribute that javac reads but the JVM ignores when it links calls.

That split creates a trap. Suppose we change a parameter from List<? extends Number> to List<Number>. The erased descriptor is still (List), so already compiled callers keep linking and running; the change is binary compatible. But the next time a caller recompiles, javac reads the new generic signature, and code that passed a List<Integer> no longer compiles. The change is not source compatible. In a company where every service rebuilds against the latest library, a source break is just as painful as a binary one, and it often arrives weeks later in someone else's pipeline.

Java
// Library version 1.0
public static double sumAll(List<? extends Number> values) { ... }
// Library version 1.1: someone "simplifies" the parameter
public static double sumAll(List<Number> values) { ... }
// Erased descriptor in both versions: sumAll(Ljava/util/List;)D
// Caller compiled against 1.0, run with the 1.1 jar
List<Integer> ints = List.of(1, 2);
sumAll(ints); // links and runs: 3.0
// Same caller recompiled against 1.1
sumAll(ints); // error: List<Integer> cannot be converted to List<Number>

Notice that the erased form never changed, so a binary checker that only compares descriptors would call every version compatible. This is why the rules and tools below must look at generic signatures, not only at erased ones.

2The Design Rules We Would Write Down

The first rule is PECS, short for producer extends, consumer super. A parameter we only read from is a producer, so it takes ? extends T; a parameter we only write into is a consumer, so it takes ? super T. Collections.copy(List<? super T> dest, List<? extends T> src) is the classic example. Following PECS in parameters makes a method accept the widest range of callers, so we rarely need to widen it later, which is exactly the kind of change that hurts.

The second rule is no wildcards in return types. A method returning List<? extends Order> forces every caller to deal with a capture type they cannot add to and cannot name easily, and the wildcard then spreads into their code. Return the concrete type, List<Order>, and let the flexibility live in the parameters.

The third rule is minimal type parameters. Every type parameter on a public method or class is another thing callers must understand and that we can never remove. If a type parameter appears only once in a signature, a wildcard usually does the same job: <T> void log(List<T> items) is better written as void log(List<?> items).

The fourth rule is prefer simple, specific types. A Repository<T, ID, Q extends Query<T>, R extends Result<T>> looks powerful, but if the library only ever serves orders and customers, two plain interfaces would be easier to read, easier to evolve and kinder in error messages. We reserve heavy generics for libraries that genuinely serve many types, such as a collection, a cache or a serialization layer.

Rules for public generic signatures
RuleGoodAvoid
Read-only parameter uses extendssum(Collection<? extends Number> c)sum(Collection<Number> c)
Write-only parameter uses superfill(List<? super Integer> out)fill(List<Integer> out)
No wildcard in return typeList<Order> find()List<? extends Order> find()
Type parameter used once -> wildcardvoid log(List<?> items)<T> void log(List<T> items)
Specific unless truly genericOrderStoreStore<T, ID, Q, R>
The rules a reviewer checks first, each with a short example.

3An API Review With Real Calling Code

Rules on a wiki do not stop a bad signature on their own, so I would add a review step. Any pull request that adds or changes a public generic signature in a shared library needs approval from a small group of API owners, typically two or three engineers who know generics well and rotate the duty. We can enforce it with a CODEOWNERS entry on the public API packages, or by having the compatibility report from the next section flag the change.

The key part of the review is that the author must include examples of calling code: a few lines that a typical caller would write, including a lambda, a method reference, and a call with a subtype. Generic signatures look reasonable in isolation and only show their problems at the call site. A reviewer who sees Comparator<? super T> written next to users.sort(byName) can judge it in seconds; a reviewer who sees only the declaration often cannot. The examples then become tests in the library, so later changes that break them fail the build.

4Automated Compatibility Checks in CI

People miss things, so the last line of defence is a tool. japicmp and Revapi both compare the new build of a library against the last released version and report incompatible changes. We configure them to fail the release build on any binary or source incompatible change to the public API unless the version number signals a major release, following semantic versioning.

Check your version of each tool for how thoroughly it reports generic signature changes, since erased descriptors alone hide them; Revapi analyses generic signatures in detail, and japicmp has options for source compatibility. Whichever we pick, it runs on every release, not only when someone remembers, and its report is attached to the release notes so consumers know what changed.

Step throughHow a generic API change reaches a release
  1. Author opens a pull requestincludes calling-code examples
  2. Public generic signature changed?
    YesAPI owners must reviewNoNormal review
  3. Examples become compile testslambda, method reference, subtype call
  4. CI runs japicmp or Revapiagainst the last released version
  5. Incompatible change found?
    YesFail unless this is a major versionNoRelease
  6. Publish with the compatibility report

6 steps in order. Play them, or step through them one at a time.

5When a Signature Is Already Wrong

Sooner or later we find a published method with a bad signature, for example a return type of Map<String, ? extends List<? extends Item>>. We do not change it in place. We add a new method with the right signature under a new name, mark the old one @Deprecated(forRemoval = true, since = "3.4") with a note pointing to the replacement, and give consumers a full major version to move. Internally the old method can delegate to the new one, so there is only one implementation to maintain.

Measuring helps the decision. If a search across the company's repositories shows four callers, we can move them ourselves in an afternoon. If it shows four hundred, the deprecation path is the only realistic choice, and the cost of that path is the strongest argument for getting signatures right at review time.

6Follow-up questions

Is a generic signature change ever safe without a major version?

Yes, some are. Adding a new overload with a different erasure, adding a new method, or widening a parameter from a concrete type to a wildcard that accepts strictly more arguments is usually source compatible. But even widening can break callers who passed method references to overloaded methods, because overload resolution may now find two candidates. That is why the compile tests from the review are the real proof, not our intuition.

Why not just let the compatibility tool decide everything?

Because the tool can only tell us whether a change breaks something; it cannot tell us whether a new signature is a good one. A new method with a confusing return type passes every compatibility check on the day it is added and then becomes permanent. The human review catches design problems, and the tool catches regressions; we need both.

7Conclusion

In this answer, we saw that generic signature changes are dangerous because erasure lets many of them stay binary compatible while still breaking source compatibility, so builds fail later in other teams. We set rules for public signatures: PECS wildcards in parameters, no wildcards in return types, minimal type parameters, and specific types unless a library truly serves many types. We backed the rules with an API review that requires calling-code examples, automated japicmp or Revapi checks on every release, and a deprecation path for signatures that are already wrong.

What to say in the interviewAbout 80 seconds aloud

Open with

The core problem is that erasure hides generic changes from the JVM but not from javac. If I change List<Number> to List<? extends Number>, the erased descriptor is the same, so it is binary compatible, but other signature changes like narrowing a bound break callers the next time they recompile, and in an organization where everyone rebuilds against the latest version that is just as bad.

Then, if they want more

So I would do three things. First, write down a short set of rules: PECS in parameters, so extends for producers and super for consumers; never a wildcard in a return type; no type parameter that appears only once; and prefer specific types unless the library genuinely serves many types.

Second, require an API review by a small rotating group for any public generic signature, and the author has to show real calling code: a lambda, a method reference, a subtype argument. Those examples become compile tests in the library.

Third, run japicmp or Revapi in CI on every release, configured to fail on incompatible changes unless it is a major version. When a published signature is already wrong, I add a correct method next to it, deprecate the old one for removal, and give callers a full major version to migrate.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Generic signatures in shared libraries are permanent once published, and badly chosen...": https://scrivio-interview-library.netlify.app/java/generics/ffc8ae8c6c/