Advanced generics and reflection
On this page 8 topics
Overview
Topics
- Type erasure
- reflection
- annotations
- dynamic proxies
Learn first
2.5 h of practice (estimate)
Learning outcomes
- Explain type erasure and its consequences (no
new T(), bridge methods, heap pollution). - Apply PECS to wildcard signatures.
- Inspect classes, methods, and annotations with reflection and write a custom annotation processed at runtime.
- Explain how Spring uses reflection and dynamic proxies (and why
@Transactionalself-invocation fails).
Lessons
Lab and exercises
Lab
MID-009-LAB1 · Audit annotation
Outcomes: 3–4 (with 1–2 in exercises) · Time: ~2.5 h Create an @Audited annotation and a proxy-based interceptor that logs calls to annotated methods (method name, arguments, duration). Apply it to a Shop service interface. Demonstrate the self-invocation limitation. Acceptance: works through an interface; self-invocation limitation demonstrated.
Exercises (assessment: exercises plus a proxy-behavior quiz)
| ID | Outcome | Task |
|---|---|---|
| MID-009-EX01 | 1 | Show that new ArrayList<String>().getClass() == new ArrayList<Integer>().getClass(); explain, then use a raw list to cause heap pollution and observe where the exception appears. |
| MID-009-EX02 | 1 | Write <T> T create(Class<T>) as the replacement for new T(). |
| MID-009-EX03 | 2 | Write addAll(Collection<? extends T> src, Collection<? super T> dst); explain PECS with Integer, Number, Object. |
| MID-009-EX04 | 3 | Use reflection to list all methods annotated with a custom @Audited on a class. |
| MID-009-EX05 | 4 | Explain why @Transactional on b() does not work when called from a() in the same class, and give two fixes. |
| MID-009-EX06 | 3 | Time 1,000,000 direct vs reflective calls; report the difference and one way to reduce it. |
Show solutions
- EX01 Both classes are
ArrayList(erased). Adding anIntegerthrough a raw reference compiles with a warning;ClassCastExceptionappears later when aStringis read. - EX02
return type.getDeclaredConstructor().newInstance(); - EX03
Collection<Integer>can be copied intoCollection<Number>orCollection<Object>: source extends, destination super. - EX04
Arrays.stream(c.getDeclaredMethods()).filter(m -> m.isAnnotationPresent(Audited.class))withRUNTIMEretention. - EX05 The call is on
this, bypassing the Spring proxy. Moveb()to another bean, or inject the proxy (self-injection with care) or restructure. - EX06 Reflection is slower, mostly warm-up-dependent; cache
Methodobjects or useMethodHandle/direct calls.
Quiz
Outcomes: O1 erasure · O2 PECS · O3 reflection and annotations · O4 proxies and Spring.
A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.
Practical part
Six exercises MID-009-EX01 to EX06 plus the lab. Pass: at least five exercises correct, at least eight of the ten quiz questions right, and the lab showing the self-invocation limitation.
Common mistakes
Reflection in hot paths; raw types from erasure confusion.
Assessment
Exercises plus a proxy-behavior quiz.
Career relevance
Reading framework internals when debugging.