Skip to content

Commit a68f8e8

Browse files
Copilotbrunoborges
andauthored
Add constructor reflection pattern
Co-authored-by: brunoborges <129743+brunoborges@users.noreply.github.com>
1 parent 93d5b27 commit a68f8e8

19 files changed

Lines changed: 365 additions & 2 deletions

content/language/anonymous-classes-to-lambdas.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ whyModernWins:
3333
support:
3434
state: "available"
3535
description: "Available since JDK 8 (March 2014)."
36-
prev: "tooling/junit6-with-jspecify"
36+
prev: "tooling/class-newinstance-to-constructor"
3737
next: "language/default-interface-methods"
3838
related:
3939
- "language/default-interface-methods"
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
id: da4d528e-40f5-4a65-be86-ed01776ce458
3+
slug: "class-newinstance-to-constructor"
4+
title: "Class.newInstance() to constructor reflection"
5+
category: "tooling"
6+
difficulty: "intermediate"
7+
jdkVersion: "9"
8+
oldLabel: "Deprecated Class.newInstance()"
9+
modernLabel: "Constructor reflection"
10+
oldApproach: "Class.newInstance()"
11+
modernApproach: "Explicit constructor lookup"
12+
oldCode: |-
13+
Plugin plugin = pluginClass.newInstance();
14+
modernCode: |-
15+
Plugin plugin = pluginClass
16+
.getDeclaredConstructor()
17+
.newInstance();
18+
summary: "Replace deprecated Class.newInstance() with explicit constructor lookup and invocation."
19+
explanation: "Class.newInstance() is deprecated because it can bypass compile-time checking for exceptions thrown by the constructor and only supports a zero-argument constructor. Explicit Constructor lookup makes the selected signature visible and reports reflective invocation failures through the appropriate reflection exceptions. Module and access rules still apply; this pattern does not recommend bypassing encapsulation."
20+
whyModernWins:
21+
- icon: ""
22+
title: "Supported API"
23+
desc: "Removes use of deprecated Class.newInstance()."
24+
- icon: "🎯"
25+
title: "Explicit constructor"
26+
desc: "The requested signature is visible and can accept arguments."
27+
- icon: "🛡"
28+
title: "Honest failures"
29+
desc: "Constructor exceptions are represented through InvocationTargetException."
30+
support:
31+
state: "available"
32+
description: "Recommended since Class.newInstance() was deprecated in JDK 9 (September 2017)."
33+
prev: "tooling/junit6-with-jspecify"
34+
next: "language/anonymous-classes-to-lambdas"
35+
related:
36+
- "tooling/class-file-api"
37+
- "io/url-constructors-to-uri"
38+
- "language/compact-canonical-constructor"
39+
tags:
40+
- tooling
41+
- migration
42+
- constructors
43+
- exceptions
44+
docs:
45+
- title: "Class.getDeclaredConstructor()"
46+
href: "https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/Class.html#getDeclaredConstructor(java.lang.Class...)"
47+
- title: "Constructor.newInstance()"
48+
href: "https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/reflect/Constructor.html#newInstance(java.lang.Object...)"

content/tooling/junit6-with-jspecify.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ support:
8888
state: "available"
8989
description: "Available since JUnit 6.0 (October 2025, requires Java 17+)"
9090
prev: "tooling/aot-class-preloading"
91-
next: "language/anonymous-classes-to-lambdas"
91+
next: "tooling/class-newinstance-to-constructor"
9292
related:
9393
- "enterprise/spring-null-safety-jspecify"
9494
- "errors/helpful-npe"
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
///usr/bin/env jbang "$0" "$@" ; exit $?
2+
//JAVA 25+
3+
4+
/// Proof: class-newinstance-to-constructor
5+
/// Source: content/tooling/class-newinstance-to-constructor.yaml
6+
interface Plugin {}
7+
8+
static final class ExamplePlugin implements Plugin {}
9+
10+
void main() throws ReflectiveOperationException {
11+
Class<? extends Plugin> pluginClass = ExamplePlugin.class;
12+
13+
Plugin plugin = pluginClass
14+
.getDeclaredConstructor()
15+
.newInstance();
16+
17+
IO.println(plugin.getClass().getSimpleName());
18+
}

social/queue.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,4 @@ tooling/stack-walker
126126
collections/map-compute-and-merge
127127
io/http-websocket-client
128128
security/security-manager-migration
129+
tooling/class-newinstance-to-constructor

social/tweets.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,4 +1277,14 @@ security/security-manager-migration: |-
12771277
12781278
🔗 https://javaevolved.github.io/security/security-manager-migration.html
12791279
1280+
#Java #JavaEvolved
1281+
tooling/class-newinstance-to-constructor: |-
1282+
☕ Class.newInstance() to constructor reflection
1283+
1284+
Replace deprecated Class.newInstance() with explicit constructor looku…
1285+
1286+
Class.newInstance() → Explicit constructor lookup (JDK 9+)
1287+
1288+
🔗 https://javaevolved.github.io/tooling/class-newinstance-to-constructor.html
1289+
12801290
#Java #JavaEvolved
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
title: الانتقال من Class.newInstance() إلى انعكاس المُنشئ
2+
oldApproach: Class.newInstance()
3+
modernApproach: البحث الصريح عن المُنشئ
4+
summary: استبدل Class.newInstance() المهجورة بالبحث الصريح عن المُنشئ واستدعائه.
5+
explanation: أُهملت Class.newInstance() لأنها قد تتجاوز التحقق وقت الترجمة من الاستثناءات
6+
التي يرميها المُنشئ، ولا تدعم سوى مُنشئ بلا معاملات. يجعل البحث الصريح عن Constructor
7+
التوقيع المحدد مرئيًا ويبلّغ عن إخفاقات الاستدعاء الانعكاسي عبر استثناءات الانعكاس
8+
المناسبة. تظل قواعد الوحدات والوصول سارية؛ ولا يوصي هذا النمط بتجاوز التغليف.
9+
whyModernWins:
10+
- icon:
11+
title: واجهة مدعومة
12+
desc: تزيل استخدام Class.newInstance() المهجورة.
13+
- icon: 🎯
14+
title: مُنشئ صريح
15+
desc: يكون التوقيع المطلوب مرئيًا ويمكنه قبول معاملات.
16+
- icon: 🛡
17+
title: إخفاقات واضحة
18+
desc: تُمثّل استثناءات المُنشئ عبر InvocationTargetException.
19+
support:
20+
description: موصى بها منذ إهمال Class.newInstance() في JDK 9 (سبتمبر 2017).
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
title: Class.newInstance() থেকে কনস্ট্রাক্টর রিফ্লেকশন
2+
oldApproach: Class.newInstance()
3+
modernApproach: স্পষ্ট কনস্ট্রাক্টর অনুসন্ধান
4+
summary: অবচিত Class.newInstance()-কে স্পষ্ট কনস্ট্রাক্টর অনুসন্ধান ও আহ্বান দিয়ে প্রতিস্থাপন
5+
করুন।
6+
explanation: Class.newInstance() অবচিত, কারণ এটি কনস্ট্রাক্টর থেকে নিক্ষিপ্ত এক্সসেপশনের
7+
কম্পাইল-টাইম পরীক্ষা এড়াতে পারে এবং শুধু শূন্য-আর্গুমেন্ট কনস্ট্রাক্টর সমর্থন করে।
8+
স্পষ্ট Constructor অনুসন্ধান নির্বাচিত সিগনেচার দৃশ্যমান করে এবং উপযুক্ত রিফ্লেকশন
9+
এক্সসেপশনের মাধ্যমে রিফ্লেকটিভ আহ্বানের ব্যর্থতা জানায়। মডিউল ও অ্যাক্সেসের নিয়ম
10+
এখনও প্রযোজ্য; এই প্যাটার্ন এনক্যাপসুলেশন এড়ানোর পরামর্শ দেয় না।
11+
whyModernWins:
12+
- icon:
13+
title: সমর্থিত API
14+
desc: অবচিত Class.newInstance()-এর ব্যবহার সরিয়ে দেয়।
15+
- icon: 🎯
16+
title: স্পষ্ট কনস্ট্রাক্টর
17+
desc: অনুরোধ করা সিগনেচার দৃশ্যমান থাকে এবং আর্গুমেন্ট নিতে পারে।
18+
- icon: 🛡
19+
title: সঠিক ব্যর্থতা
20+
desc: কনস্ট্রাক্টরের এক্সসেপশন InvocationTargetException-এর মাধ্যমে প্রকাশিত হয়।
21+
support:
22+
description: JDK 9-এ (সেপ্টেম্বর 2017) Class.newInstance() অবচিত হওয়ার পর থেকে প্রস্তাবিত।
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
title: Class.newInstance() durch Konstruktorreflexion ersetzen
2+
oldApproach: Class.newInstance()
3+
modernApproach: Explizite Konstruktorsuche
4+
summary: Das veraltete Class.newInstance() durch explizite Konstruktorsuche und
5+
-aufruf ersetzen.
6+
explanation: Class.newInstance() ist veraltet, weil es die Prüfung der vom Konstruktor
7+
ausgelösten Ausnahmen zur Kompilierzeit umgehen kann und nur parameterlose Konstruktoren
8+
unterstützt. Die explizite Konstruktorsuche macht die gewählte Signatur sichtbar
9+
und meldet Fehler beim reflektiven Aufruf über die passenden Reflexionsausnahmen.
10+
Modul- und Zugriffsregeln gelten weiterhin; dieses Muster empfiehlt nicht, die Kapselung
11+
zu umgehen.
12+
whyModernWins:
13+
- icon:
14+
title: Unterstützte API
15+
desc: Vermeidet das veraltete Class.newInstance().
16+
- icon: 🎯
17+
title: Expliziter Konstruktor
18+
desc: Die gewünschte Signatur ist sichtbar und kann Argumente annehmen.
19+
- icon: 🛡
20+
title: Ehrliche Fehler
21+
desc: Konstruktorausnahmen werden durch InvocationTargetException dargestellt.
22+
support:
23+
description: Empfohlen, seit Class.newInstance() in JDK 9 (September 2017) als veraltet
24+
markiert wurde.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
title: De Class.newInstance() a reflexión de constructores
2+
oldApproach: Class.newInstance()
3+
modernApproach: Búsqueda explícita del constructor
4+
summary: Reemplaza Class.newInstance(), obsoleto, por la búsqueda e invocación explícitas
5+
del constructor.
6+
explanation: Class.newInstance() está obsoleto porque puede eludir la comprobación en
7+
compilación de las excepciones lanzadas por el constructor y solo admite un constructor
8+
sin argumentos. La búsqueda explícita del constructor hace visible la firma elegida
9+
e informa de los fallos de invocación reflexiva mediante las excepciones de reflexión
10+
adecuadas. Las reglas de módulos y acceso siguen aplicándose; este patrón no recomienda
11+
eludir la encapsulación.
12+
whyModernWins:
13+
- icon:
14+
title: API compatible
15+
desc: Elimina el uso de Class.newInstance(), que está obsoleto.
16+
- icon: 🎯
17+
title: Constructor explícito
18+
desc: La firma solicitada es visible y puede aceptar argumentos.
19+
- icon: 🛡
20+
title: Fallos transparentes
21+
desc: Las excepciones del constructor se representan mediante InvocationTargetException.
22+
support:
23+
description: Recomendado desde que Class.newInstance() quedó obsoleto en JDK 9 (septiembre
24+
de 2017).

0 commit comments

Comments
 (0)