Scrivio Interview library
LeadershipAdvanced6 min read · updated 12 November 2026

Your codebase has thousands of @SuppressWarnings("unchecked") annotations and several heap pollution incidents. What policy and tooling do you introduce?

I would make suppressions rare, narrow and explained: each @SuppressWarnings("unchecked") goes on the smallest possible declaration, usually a local variable, with a comment saying why the cast is safe. I would turn on -Xlint:unchecked, or -Xlint:all, and fail CI on new warnings with -Werror, add a checker such as Error Prone for unsafe generic patterns, reduce the need for suppressions with typed helpers, Class tokens and collections instead of arrays, and track the number of suppressions over time as a code health metric.

Why this question is asked

Thousands of suppressions mean the compiler's type checking has been switched off in thousands of places, and heap pollution incidents show that some of those places were wrong. A senior engineer is expected to fix this without stopping feature work or demanding a rewrite. Interviewers ask this to see whether we can combine a clear technical rule with tooling and gradual cleanup. In this answer, we will recap what an unchecked warning really means, set the policy for suppressions, turn on the compiler and CI checks, add static analysis, reduce the need for suppressions at the source, and measure progress.

1What an Unchecked Warning Really Means

Generics are checked at compile time and erased at run time. When code does something the compiler cannot verify, such as casting an Object to List<String> or mixing raw and generic types, javac gives an unchecked warning. It means: from here on, type safety depends on you. If the code is wrong, the result is heap pollution, a List<String> that contains something else, and a ClassCastException later, at a cast the compiler inserted, often in another module.

A suppression is therefore a claim that a human checked the cast. When a codebase has thousands of them, many at class level, nobody checked most of them, and new unsafe code inside those classes gets no warning at all. The incidents are the expected result.

Java
@SuppressWarnings("unchecked") // on the whole class: hides every future mistake too
public class ReportService {
List<Row> load(Object cached) {
return (List<Row>) cached; // was a List<Map<String, Object>> after a cache change
}
}
// Much later, in another module:
// ClassCastException: LinkedHashMap cannot be cast to Row

Notice how the class-level suppression meant the cast in load() was never questioned, and the failure appeared far from it.

2The Policy for Suppressions

The rule is narrowest scope plus a reason. A suppression goes on the smallest declaration that contains the unchecked operation, which is usually a local variable declaration, never a class and rarely a whole method. Next to it, a comment explains why the operation is safe, in terms a reviewer can check. If we cannot write the reason, we do not suppress; we fix the code.

Java
// Before: method-level, no reason
@SuppressWarnings("unchecked")
<T> T[] grow(T[] a) { ... }
// After: local scope, reason stated
<T> T[] grow(T[] a, int n) {
// safe: the new array has the same component type as a
@SuppressWarnings("unchecked")
T[] b = (T[]) Array.newInstance(a.getClass().getComponentType(), n);
System.arraycopy(a, 0, b, 0, a.length);
return b;
}

We put this rule in the coding guidelines and in the review checklist, and reviewers reject suppressions without a reason. A simple custom check, or a grep in CI, can flag any @SuppressWarnings("unchecked") on a type declaration or without a comment on the line before.

3Turning On the Compiler Checks in CI

Many builds never show unchecked warnings because javac only summarises them unless -Xlint:unchecked is given. We enable -Xlint:unchecked, ideally -Xlint:all with a few explicit exclusions, so every warning is printed with its location. Then we make CI fail on warnings with -Werror.

With thousands of existing warnings, failing the build on all of them on day one would block everyone. So we ratchet: the build records the current count per module, fails if the count rises, and lowers the baseline whenever a cleanup brings it down. Modules that reach zero switch to plain -Werror and stay there. New modules start with -Werror from the first commit.

Step throughRatcheting warnings down module by module
  1. Enable -Xlint:all in every modulewarnings become visible
  2. Record the warning count per modulethe baseline
  3. Count higher than baseline?
    YesFail the buildNoPass, lower the baseline if it dropped
  4. Module at zero?
    YesSwitch to -Werror permanentlyNoKeep cleaning in normal work
  5. New modules start at -Werror

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

4Static Analysis for Unsafe Generic Patterns

javac only knows about individual unchecked operations. A static analysis tool can recognise patterns that are unsafe even when they compile cleanly. Error Prone, for example, has bug patterns such as TypeParameterUnusedInFormals, which flags methods like <T> T get(String key) that let any caller pick any return type and so hide an unchecked cast; check its documentation for the full list for your version. IntelliJ inspections and Sonar rules can also flag raw types and unsafe casts. We run the chosen tool as part of the build, with the same ratchet approach, so findings cannot grow.

5Reducing the Need for Suppressions

Policy and tooling stop the problem growing; better code removes it. Most suppressions in a large codebase come from a handful of patterns, and each has a typed replacement. A cache or registry keyed by String that returns Object can take a Class<T> token and use type.cast(value), which checks at run time and fails at the right place. Generic arrays can be replaced by collections. Raw types from old APIs can be wrapped once in a typed adapter. Methods like <T> T get(String key) become <T> T get(Key<T> key), where the key carries the type.

Java
// Before: every caller casts or suppresses
Object v = cache.get("user:42");
User u = (User) v;
// After: a typed helper with a Class token
<T> Optional<T> get(String key, Class<T> type) {
Object v = map.get(key);
return type.isInstance(v) ? Optional.of(type.cast(v)) : Optional.empty(); // checked cast
}
User u = cache.get("user:42", User.class).orElseThrow();

Notice that type.cast is a real, checked cast, so a wrong value fails inside the helper with a clear message instead of polluting the heap. We start cleanup in the modules that had incidents, then move on by count.

6Measuring Progress

Finally we make the problem visible. A small job counts @SuppressWarnings("unchecked") and rawtypes annotations per module, plus compiler warnings, and publishes the trend on the engineering dashboard next to other code health metrics. A target such as halving the count in two quarters gives teams a goal, and incidents traced to unchecked casts are tagged in postmortems so we can see whether the trend in suppressions matches a trend in failures.

7Follow-up questions

Should we ban @SuppressWarnings("unchecked") entirely?

No. Some unchecked casts are genuinely safe and unavoidable, such as inside a generic container built on an Object[] or after Array.newInstance. A ban would push people towards worse workarounds like raw types. The goal is that every remaining suppression is narrow and justified.

How do you get teams to spend time on this?

Tie it to the incidents. Show the postmortems, the hours spent, and the specific suppressions involved. Then keep the cost low: ratchets only block new problems, typed helpers are provided centrally, and cleanup happens when people already touch a file.

Is -Werror risky when upgrading the JDK?

A new JDK can add lint categories and new warnings. We handle it by upgrading in a branch, fixing or explicitly excluding new categories, and only then merging, so the upgrade does not suddenly break every build.

8Conclusion

In this answer, we saw that unchecked warnings mark places where type safety rests on a human, and thousands of broad suppressions mean nobody checked most of them. The policy is the narrowest scope, usually a local variable, with a written reason; the compiler checks are -Xlint:unchecked or -Xlint:all with -Werror, introduced with a ratchet; static analysis such as Error Prone catches unsafe generic patterns; typed helpers with Class tokens, collections instead of arrays and typed APIs remove the need for most suppressions; and the suppression count is tracked over time as a code health metric.

What to say in the interviewAbout 80 seconds aloud

Open with

An unchecked warning means the compiler can no longer guarantee type safety, so a suppression is a claim that someone checked. Thousands of them, often at class level, mean nobody did, and the heap pollution incidents show it.

Then, if they want more

I would introduce a clear policy: a suppression goes on the narrowest scope, normally a local variable declaration, never a class, and it needs a comment saying why the cast is safe; reviewers reject it otherwise. For tooling, I would enable -Xlint:all so warnings are visible, and use -Werror in CI.

Because we start with thousands, I would ratchet per module: fail if the count grows, lower the baseline when it drops, and switch modules to strict -Werror once they reach zero; new modules start strict. I would add Error Prone or similar checks for unsafe generic patterns, such as methods where the caller chooses the return type.

Then I would reduce the need: typed helpers that take a Class token and use type.cast, collections instead of generic arrays, typed adapters around raw legacy APIs, and typed keys. Finally, I would track the number of suppressions per module on a dashboard and link incidents to them in postmortems, so we can show the trend is going down.

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 "Your codebase has thousands of @SuppressWarnings("unchecked") annotations and several...": https://scrivio-interview-library.netlify.app/java/generics/a328d1757c/