Quick revision
The short answer
We start by measuring: compiler warnings from -Xlint, counts of @SuppressWarnings("unchecked"), and static analysis findings per repository. We fix shared libraries first, because their generic signatures shape the code of every caller, and use automated refactoring such as OpenRewrite recipes and IDE migrations for mechanical fixes like raw types. A CI ratchet forbids new warnings while allowing existing ones, and the baseline is lowered over time. We accept that some legacy code is not worth fixing and prioritise by how often code changes and how often it causes incidents.
What to remember
In this answer, we built a program to raise type safety across hundreds of repositories without stopping delivery. We measured warnings, suppressions and static analysis findings per repository, fixed shared libraries first because their signatures shape every caller, used OpenRewrite recipes and IDE migrations for mechanical fixes, and added a CI ratchet that blocks new warnings and lowers the baseline over time. We also accepted that some stable legacy code is not worth fixing and prioritised by change frequency and incident history.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We start by measuring: compiler warnings from -Xlint, counts of @SuppressWarnings("unchecked"), and static analysis findings per repository. We fix shared libraries first, because their generic signatures shape the code of every caller, and use automated refactoring such as OpenRewrite recipes and IDE migrations for mechanical fixes like raw types. A CI ratchet forbids new warnings while allowing existing ones, and the baseline is lowered over time. We accept that some legacy code is not worth fixing and prioritise by how often code changes and how often it causes incidents.
Why this question is asked
A large organization's code is layered history: pre-Java 5 code with raw types, generics adopted unevenly, casts papered over with @SuppressWarnings, and newer services written carefully. Unchecked casts and raw types are not just ugly; they hide ClassCastExceptions and make refactoring dangerous. But no leader can stop delivery across hundreds of repositories to clean them up. Interviewers ask this to see whether we can run a long technical improvement program with measurement, sequencing, automation and judgement. In this answer, we will recap where unsafety comes from, then go through measuring, choosing where to start, automating, ratcheting, and deciding what to leave alone.
1A Quick Recap of Where Type Unsafety Comes From
A raw type is a generic type used without type arguments, such as List instead of List<String>. Raw types exist so that pre-generics code still compiles, and using them switches off generic checking for that expression. An unchecked cast, such as (List<Order>) obj, cannot be verified at run time because of erasure, so the compiler warns and the cast succeeds even when the list holds something else. The failure then appears later, wherever an element is read.
Both produce compiler warnings, which many builds ignore or hide. @SuppressWarnings("unchecked") silences them, sometimes justifiably, often not. So the amount of unsafety in a codebase is visible, if we choose to look.
2Measure Before Changing Anything
We need a baseline per repository so we can decide where to work and later show progress. Compiling with javac -Xlint:rawtypes,unchecked lists every warning. Counting @SuppressWarnings("unchecked") and ("rawtypes") annotations shows how much has been hidden. Static analysis tools such as Error Prone, SpotBugs or Sonar add findings of their own.
Notice that the numbers are per repository and per kind. A central dashboard collecting them from CI turns a vague worry into a picture: perhaps 80 percent of the warnings sit in 20 repositories, and a handful of shared libraries account for many of them.
3Fix Shared Libraries First
A shared library's generic signatures shape every caller. If a common utility returns a raw List, every one of its hundreds of callers has to cast, and each cast is a warning in the caller's repository. Fixing the library's signature to return List<Order> removes those casts at the source. The reverse is not true: fixing callers first leaves them casting around a raw API.
Library changes need care, because they are API changes. Changing a raw return type to a parameterized one is binary compatible, since the erasure is the same, but it can produce new warnings or errors in callers that relied on the raw type. So we release it as a minor version with clear notes, and run the change against a sample of consumer repositories before publishing.
- Measure warnings per repository-Xlint, suppressions, static analysis
- Fix shared libraries' signaturescallers' casts disappear at the source
- Run automated recipesraw types and mechanical fixes in bulk
- Turn on the CI ratchetno new warnings; baseline only goes down
- Prioritise the restby change frequency and incident history
- Review progress quarterlydashboard of counts per repository
6 steps in order. Play them, or step through them one at a time.
Each step makes the next one cheaper.
4Automate the Mechanical Fixes
Most raw-type fixes are mechanical: add the obvious type arguments, add the diamond operator, replace a raw Iterator loop with an enhanced for. Doing this by hand across hundreds of repositories is slow and error-prone. OpenRewrite, an automated refactoring tool, runs recipes that make such changes and opens pull requests; IDE migrations such as IntelliJ's infer generics refactoring do the same at smaller scale. A platform team can run a recipe across every repository, and teams only review and merge.
Automation should handle only the cases where the right answer is clear. An unchecked cast in a deserialization helper often needs a design change, such as passing a Class<T> or a type token, and that is a human decision. So we use automation to clear the bulk, and put engineers on the hard cases.
5Ratchet in CI
Without a guard, new warnings arrive as fast as old ones are fixed. A ratchet is a CI check that records the current warning count for each repository as a baseline and fails the build if the count goes up. Existing warnings are tolerated, so no team is blocked, but no new ones can be added. When a team fixes warnings, the baseline is lowered automatically, so it can never climb back.
This makes the program sustainable without heroics. Each team improves at its own pace, and the trend line is visible on the dashboard. For new repositories, the baseline is zero, and the build turns these warnings into errors with -Werror from the start.
6Decide What Not to Fix
Some code is not worth touching. A billing module that has not changed in four years and has never produced a ClassCastException incident may have 300 raw types, and fixing them risks breaking something stable for no measurable gain. So we prioritise by two signals: change frequency, because code that changes often is where unsafety turns into new bugs, and incident history, because code that has already failed tells us where the risk really is. A repository with many warnings, weekly changes and two ClassCastException incidents last year goes to the top. A frozen one with no incidents stays under the ratchet and is left alone, and we say so openly rather than pretending the program will reach 100 percent.
7Follow-up questions
How do you keep teams from just adding @SuppressWarnings to pass the ratchet?
Count suppressions in the same baseline, so adding one raises the number just like a warning. Allow them only with a comment explaining why the cast is safe, and keep the scope narrow, on a single local variable rather than a whole class. Code review checks the justification.
How do you show leadership that the program is worth the effort?
Track a few numbers over time: warnings per repository, ClassCastException incidents in production, and time spent on related bugs. A falling trend in warnings alongside fewer incidents in the targeted repositories is a clear story, and it also shows where the effort has stopped paying off.
Should this be combined with a Java version upgrade?
It often helps. Teams moving to Java 21 or 25 are already touching build files and code, and recipes for upgrades and generics can run together. But keep the changes in separate commits, so a regression can be traced to one or the other.
8Conclusion
In this answer, we built a program to raise type safety across hundreds of repositories without stopping delivery. We measured warnings, suppressions and static analysis findings per repository, fixed shared libraries first because their signatures shape every caller, used OpenRewrite recipes and IDE migrations for mechanical fixes, and added a CI ratchet that blocks new warnings and lowers the baseline over time. We also accepted that some stable legacy code is not worth fixing and prioritised by change frequency and incident history.
Open with
I would treat it as a long program with measurement, not a cleanup sprint. First I would measure: turn on -Xlint for raw types and unchecked warnings in every build, count @SuppressWarnings for unchecked and rawtypes, and collect static analysis findings, all per repository, on a dashboard.
Then, if they want more
That usually shows the problem is concentrated. Second, I would fix shared libraries first, because their generic signatures shape every caller; a utility returning a raw List forces casts in hundreds of places, and fixing the signature removes them at the source. That is a released API change, so I would test it against consumer repositories first.
Third, I would automate the mechanical work: OpenRewrite recipes or IDE migrations can add type arguments and diamonds across repositories and open pull requests, so teams just review, while engineers handle the cases that need real design, like unchecked casts in deserialization.
Fourth, I would add a ratchet in CI: the build fails if a repository's warning or suppression count goes up, existing ones are tolerated, and the baseline only goes down, with new repositories starting at zero. And finally I would be honest that some legacy code is not worth fixing.
I would prioritise by how often the code changes and by its incident history, and leave stable, frozen modules under the ratchet.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 21, Java 5
- 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.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
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 "Over twenty years your organization's codebases have mixed raw types, unchecked casts...": https://scrivio-interview-library.netlify.app/java/generics/192c0fdbc0/