Skip to content

Commit 4619cdd

Browse files
committed
8264138: Replace uses of Class.newInstance
Reviewed-by: kcr
1 parent 93de584 commit 4619cdd

File tree

25 files changed

+62
-77
lines changed

25 files changed

+62
-77
lines changed

apps/samples/3DViewer/src/main/java/com/javafx/experiments/importers/Importer3D.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
package com.javafx.experiments.importers;
3333

3434
import java.io.IOException;
35+
import java.lang.reflect.InvocationTargetException;
3536
import java.net.URL;
3637
import java.net.URLClassLoader;
3738

@@ -122,7 +123,7 @@ public static Pair<Node,Timeline> loadIncludingAnimation(String fileUrl, boolean
122123
for (String name : names) {
123124
try {
124125
Class<?> clazz = Class.forName(name);
125-
Object obj = clazz.newInstance();
126+
Object obj = clazz.getDeclaredConstructor().newInstance();
126127
if (obj instanceof Importer) {
127128
Importer plugin = (Importer) obj;
128129
if (plugin.isSupported(extension)) {
@@ -131,7 +132,8 @@ public static Pair<Node,Timeline> loadIncludingAnimation(String fileUrl, boolean
131132
break;
132133
}
133134
}
134-
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
135+
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
136+
| InvocationTargetException | NoSuchMethodException e) {
135137
// FAIL SILENTLY
136138
}
137139
}

apps/samples/Ensemble8/src/app/java/ensemble/EmbeddedApplication.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public static Node createApplication(String className) {
5252
// Class appClass = EmbeddedApplication.class.getClassLoader().loadClass(className);
5353
Class appClass = InterpolatorApp.class;
5454
System.out.println("appClass = " + appClass);
55-
Application app = (Application)appClass.newInstance();
55+
Application app = (Application)appClass.getDeclaredConstructor().newInstance();
5656
System.out.println("app = " + app);
5757
app.init();
5858
app.start(TEMP_STAGE);

apps/samples/Ensemble8/src/app/java/ensemble/SampleInfo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ public SampleRuntimeInfo buildSampleNode() {
191191
Method play = null;
192192
Method stop = null;
193193
Class clz = Class.forName(appClass);
194-
final Object app = clz.newInstance();
194+
final Object app = clz.getDeclaredConstructor().newInstance();
195195
Parent root = (Parent) clz.getMethod("createContent").invoke(app);
196196

197197
for (Method m : clz.getMethods()) {

apps/toys/DragDrop/src/dragdrop/Main.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ private Node createLauncher(final String name, final Class app) {
8282
@Override public void handle(MouseEvent event) {
8383
Stage stage = new Stage();
8484
try {
85-
((Application) app.newInstance()).start(stage);
85+
((Application) app.getDeclaredConstructor().newInstance()).start(stage);
8686
} catch (Exception e) {
8787
e.printStackTrace();
8888
}

apps/toys/TouchSuite/src/touchsuite/Main.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ private Node createLauncher(final String name, final Class app) {
9393
@Override public void handle(MouseEvent event) {
9494
Stage stage = new Stage();
9595
try {
96-
((Application) app.newInstance()).start(stage);
96+
((Application) app.getDeclaredConstructor().newInstance()).start(stage);
9797
} catch (Exception e) {
9898
e.printStackTrace();
9999
}
@@ -187,7 +187,7 @@ public void show(final String name, final Class app, final String info) {
187187
Info.this.setVisible(false);
188188
Stage stage = new Stage();
189189
try {
190-
((Application) app.newInstance()).start(stage);
190+
((Application) app.getDeclaredConstructor().newInstance()).start(stage);
191191
} catch (Exception e) {
192192
e.printStackTrace();
193193
}

modules/javafx.base/src/test/java/test/com/sun/javafx/event/EventDispatchChainTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ public static Collection data() {
5757
private EventDispatchChain eventDispatchChain;
5858

5959
public EventDispatchChainTest(final Class<EventDispatchChain> chainClass)
60-
throws InstantiationException, IllegalAccessException {
61-
eventDispatchChain = chainClass.newInstance();
60+
throws Exception {
61+
eventDispatchChain = chainClass.getDeclaredConstructor().newInstance();
6262
}
6363

6464
@Test

modules/javafx.base/src/test/java/test/javafx/util/converter/ParameterizedConverterTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public ParameterizedConverterTest(Class<? extends StringConverter> converterClas
9393

9494
@Before public void setup() {
9595
try {
96-
converter = converterClass.newInstance();
96+
converter = converterClass.getDeclaredConstructor().newInstance();
9797
} catch (Exception ex) {
9898
ex.printStackTrace();
9999
}

modules/javafx.controls/src/test/java/test/javafx/scene/control/CellTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public CellTest(Class type) {
7979
}
8080

8181
@Before public void setup() throws Exception {
82-
cell = (Cell<String>) type.newInstance();
82+
cell = (Cell<String>) type.getDeclaredConstructor().newInstance();
8383

8484
// Empty TableCells can be selected, as long as the row they exist in
8585
// is not empty, so here we set a TableRow to ensure testing works

modules/javafx.controls/src/test/java/test/javafx/scene/control/ControlTest.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,18 +1022,16 @@ private static void checkClass(Class someClass) {
10221022
String what = someClass.getName();
10231023
try {
10241024
// should get NoSuchMethodException if ctor is not public
1025-
// Constructor ctor = someClass.getConstructor((Class[])null);
10261025
Method m = someClass.getMethod("getClassCssMetaData", (Class[]) null);
1027-
// Node node = (Node)ctor.newInstance((Object[])null);
1028-
Node node = (Node)someClass.newInstance();
1026+
Node node = (Node)someClass.getDeclaredConstructor().newInstance();
10291027
for (CssMetaData styleable : (List<CssMetaData<? extends Styleable, ?>>) m.invoke(null)) {
10301028

10311029
what = someClass.getName() + " " + styleable.getProperty();
10321030
WritableValue writable = styleable.getStyleableProperty(node);
10331031
assertNotNull(what, writable);
10341032

10351033
Object defaultValue = writable.getValue();
1036-
Object initialValue = styleable.getInitialValue((Node) someClass.newInstance());
1034+
Object initialValue = styleable.getInitialValue((Node) someClass.getDeclaredConstructor().newInstance());
10371035

10381036
if (defaultValue instanceof Number) {
10391037
// 5 and 5.0 are not the same according to equals,

modules/javafx.controls/src/test/java/test/javafx/scene/control/EventAnyTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public EventAnyTest(EventType type, Event event, Class target, boolean matches)
9292

9393
@Test
9494
public void testEventDelivery() throws Exception {
95-
Node n = (Node) target.newInstance();
95+
Node n = (Node) target.getDeclaredConstructor().newInstance();
9696
delivered = false;
9797

9898
n.addEventHandler(type, event1 -> {

0 commit comments

Comments
 (0)