الأنواع العامة المتقدمة والانعكاس
في هذه الصفحة 8 موضوعات
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
نظرة عامة
الموضوعات
- محو الأنواع (Type Erasure)
- الانعكاس (Reflection)
- التعليقات التوضيحية (Annotations)
- الوكلاء الديناميكيون (Dynamic Proxies)
تعلّم أولًا
2.5 ساعة تدريب (تقدير)
مخرجات التعلم
- 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).
الدروس
المختبر والتمارين
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. |
أظهر الحلول
- 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.
الاختبار
Outcomes: O1 erasure · O2 PECS · O3 reflection and annotations · O4 proxies and Spring.
اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.
الجزء العملي
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.
أخطاء شائعة
Reflection in hot paths; raw types from erasure confusion.
التقييم
Exercises plus a proxy-behavior quiz.
الصلة بالمسار الوظيفي
Reading framework internals when debugging.