Skip to content
Permalink
Browse files
8264138: Replace uses of Class.newInstance
Reviewed-by: kcr
  • Loading branch information
aghaisas committed May 18, 2021
1 parent 93de584 commit 4619cddfb961ab215cac10c91b828342d10299df
Show file tree
Hide file tree
Showing 25 changed files with 62 additions and 77 deletions.
@@ -32,6 +32,7 @@
package com.javafx.experiments.importers;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.net.URLClassLoader;

@@ -122,7 +123,7 @@ public static Pair<Node,Timeline> loadIncludingAnimation(String fileUrl, boolean
for (String name : names) {
try {
Class<?> clazz = Class.forName(name);
Object obj = clazz.newInstance();
Object obj = clazz.getDeclaredConstructor().newInstance();
if (obj instanceof Importer) {
Importer plugin = (Importer) obj;
if (plugin.isSupported(extension)) {
@@ -131,7 +132,8 @@ public static Pair<Node,Timeline> loadIncludingAnimation(String fileUrl, boolean
break;
}
}
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
| InvocationTargetException | NoSuchMethodException e) {
// FAIL SILENTLY
}
}
@@ -52,7 +52,7 @@ public static Node createApplication(String className) {
// Class appClass = EmbeddedApplication.class.getClassLoader().loadClass(className);
Class appClass = InterpolatorApp.class;
System.out.println("appClass = " + appClass);
Application app = (Application)appClass.newInstance();
Application app = (Application)appClass.getDeclaredConstructor().newInstance();
System.out.println("app = " + app);
app.init();
app.start(TEMP_STAGE);
@@ -191,7 +191,7 @@ public SampleRuntimeInfo buildSampleNode() {
Method play = null;
Method stop = null;
Class clz = Class.forName(appClass);
final Object app = clz.newInstance();
final Object app = clz.getDeclaredConstructor().newInstance();
Parent root = (Parent) clz.getMethod("createContent").invoke(app);

for (Method m : clz.getMethods()) {
@@ -82,7 +82,7 @@ private Node createLauncher(final String name, final Class app) {
@Override public void handle(MouseEvent event) {
Stage stage = new Stage();
try {
((Application) app.newInstance()).start(stage);
((Application) app.getDeclaredConstructor().newInstance()).start(stage);
} catch (Exception e) {
e.printStackTrace();
}
@@ -93,7 +93,7 @@ private Node createLauncher(final String name, final Class app) {
@Override public void handle(MouseEvent event) {
Stage stage = new Stage();
try {
((Application) app.newInstance()).start(stage);
((Application) app.getDeclaredConstructor().newInstance()).start(stage);
} catch (Exception e) {
e.printStackTrace();
}
@@ -187,7 +187,7 @@ public void show(final String name, final Class app, final String info) {
Info.this.setVisible(false);
Stage stage = new Stage();
try {
((Application) app.newInstance()).start(stage);
((Application) app.getDeclaredConstructor().newInstance()).start(stage);
} catch (Exception e) {
e.printStackTrace();
}
@@ -57,8 +57,8 @@ public static Collection data() {
private EventDispatchChain eventDispatchChain;

public EventDispatchChainTest(final Class<EventDispatchChain> chainClass)
throws InstantiationException, IllegalAccessException {
eventDispatchChain = chainClass.newInstance();
throws Exception {
eventDispatchChain = chainClass.getDeclaredConstructor().newInstance();
}

@Test
@@ -93,7 +93,7 @@ public ParameterizedConverterTest(Class<? extends StringConverter> converterClas

@Before public void setup() {
try {
converter = converterClass.newInstance();
converter = converterClass.getDeclaredConstructor().newInstance();
} catch (Exception ex) {
ex.printStackTrace();
}
@@ -79,7 +79,7 @@ public CellTest(Class type) {
}

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

// Empty TableCells can be selected, as long as the row they exist in
// is not empty, so here we set a TableRow to ensure testing works
@@ -1022,18 +1022,16 @@ private static void checkClass(Class someClass) {
String what = someClass.getName();
try {
// should get NoSuchMethodException if ctor is not public
// Constructor ctor = someClass.getConstructor((Class[])null);
Method m = someClass.getMethod("getClassCssMetaData", (Class[]) null);
// Node node = (Node)ctor.newInstance((Object[])null);
Node node = (Node)someClass.newInstance();
Node node = (Node)someClass.getDeclaredConstructor().newInstance();
for (CssMetaData styleable : (List<CssMetaData<? extends Styleable, ?>>) m.invoke(null)) {

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

Object defaultValue = writable.getValue();
Object initialValue = styleable.getInitialValue((Node) someClass.newInstance());
Object initialValue = styleable.getInitialValue((Node) someClass.getDeclaredConstructor().newInstance());

if (defaultValue instanceof Number) {
// 5 and 5.0 are not the same according to equals,
@@ -92,7 +92,7 @@ public EventAnyTest(EventType type, Event event, Class target, boolean matches)

@Test
public void testEventDelivery() throws Exception {
Node n = (Node) target.newInstance();
Node n = (Node) target.getDeclaredConstructor().newInstance();
delivered = false;

n.addEventHandler(type, event1 -> {
@@ -70,7 +70,7 @@ public FireButtonBaseTest(Class type) {
}

@Before public void setup() throws Exception {
btn = (ButtonBase) type.newInstance();
btn = (ButtonBase) type.getDeclaredConstructor().newInstance();
}

@Test public void onActionCalledWhenButtonIsFired() {
@@ -86,7 +86,7 @@ public TextInputControlTest(Class type) {
}

@Before public void setup() throws Exception {
textInput = (TextInputControl) type.newInstance();
textInput = (TextInputControl) type.getDeclaredConstructor().newInstance();
setUncaughtExceptionHandler();
}

@@ -65,16 +65,9 @@ public ParameterisedPrebuiltCellTest(Class<? extends Cell> cellClass) {
this.cellClass = cellClass;
}

@Before public void setup() {
@Before public void setup() throws Exception {
count = 0;

try {
cell = cellClass.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
cell = cellClass.getDeclaredConstructor().newInstance();
}


@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -935,14 +935,12 @@ public void processAttribute(String prefix, String localName, String value)
try {
if (controllerFactory == null) {
ReflectUtil.checkPackageAccess(type);
setController(type.newInstance());
setController(type.getDeclaredConstructor().newInstance());
} else {
setController(controllerFactory.call(type));
}
} catch (InstantiationException exception) {
throw constructLoadException(exception);
} catch (IllegalAccessException exception) {
throw constructLoadException(exception);
} catch (Exception e) {
throw constructLoadException(e);
}
}
} else {
@@ -1018,11 +1016,9 @@ public Object constructValue() throws IOException {
if (value == null) {
try {
ReflectUtil.checkPackageAccess(type);
value = type.newInstance();
} catch (InstantiationException exception) {
throw constructLoadException(exception);
} catch (IllegalAccessException exception) {
throw constructLoadException(exception);
value = type.getDeclaredConstructor().newInstance();
} catch (Exception e) {
throw constructLoadException(e);
}
}
}
@@ -1328,11 +1324,9 @@ public Object constructValue() throws LoadException {
if (value == null) {
try {
ReflectUtil.checkPackageAccess(type);
value = type.newInstance();
} catch (InstantiationException exception) {
throw constructLoadException(exception);
} catch (IllegalAccessException exception) {
throw constructLoadException(exception);
value = type.getDeclaredConstructor().newInstance();
} catch (Exception e) {
throw constructLoadException(e);
}
}
root = value;
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -40,7 +40,7 @@ public static synchronized PlatformFactory getPlatformFactory() {
String factory = "com.sun.glass.ui." + platform.toLowerCase(Locale.ROOT) + "."+ platform + "PlatformFactory";
// System.out.println("Loading Glass Factory " + factory);
Class c = Class.forName(factory);
instance = (PlatformFactory) c.newInstance();
instance = (PlatformFactory) c.getDeclaredConstructor().newInstance();
} catch (Exception e) {
e.printStackTrace();
System.out.println("Failed to load Glass factory class");
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -105,7 +105,7 @@ public static synchronized NativePlatform getNativePlatform() {
throw new IllegalArgumentException("Unrecognized Monocle platform: "
+ factoryClassName);
}
NativePlatformFactory npf = (NativePlatformFactory) clazz.newInstance();
NativePlatformFactory npf = (NativePlatformFactory) clazz.getDeclaredConstructor().newInstance();
if (npf.matches() &&
npf.getMajorVersion() == majorVersion &&
npf.getMinorVersion() == minorVersion) {
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -73,7 +73,7 @@ void addNamedFilter(String filterName) {
+ filterName + "TouchFilter";
}
ClassLoader loader = Thread.currentThread().getContextClassLoader();
addFilter((TouchFilter) loader.loadClass(filterName).newInstance());
addFilter((TouchFilter) loader.loadClass(filterName).getDeclaredConstructor().newInstance());
}
} catch (Exception e) {
System.err.println(
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -258,7 +258,7 @@ public static synchronized Toolkit getToolkit() {
+ forcedToolkit);
}

TOOLKIT = (Toolkit)clz.newInstance();
TOOLKIT = (Toolkit)clz.getDeclaredConstructor().newInstance();
if (TOOLKIT.init()) {
if (printToolkit) {
System.err.println("JavaFX: using " + forcedToolkit);
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -96,7 +96,7 @@ private static class FactoryLoader implements PrivilegedAction<GLFactory> {
public GLFactory run() {
GLFactory factory = null;
try {
factory = (GLFactory) Class.forName(factoryClassName).newInstance();
factory = (GLFactory) Class.forName(factoryClassName).getDeclaredConstructor().newInstance();
} catch (Throwable t) {
System.err.println("GLFactory.static - Platform: "
+ System.getProperty("os.name")
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2009, 2014, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2009, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -450,7 +450,7 @@ private static ShaderSource createShaderSource(String name) {
Class klass = null;
try {
klass = Class.forName(name);
return (ShaderSource)klass.newInstance();
return (ShaderSource)klass.getDeclaredConstructor().newInstance();
} catch (ClassNotFoundException e) {
System.err.println(name + " class not found");
return null;
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2009, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -89,7 +89,7 @@ public synchronized static PSWRenderer createJSWInstance(Screen screen) {
PSWRenderer ret = null;
try {
Class klass = Class.forName(rootPkg + ".impl.sw.java.JSWRendererDelegate");
RendererDelegate delegate = (RendererDelegate)klass.newInstance();
RendererDelegate delegate = (RendererDelegate)klass.getDeclaredConstructor().newInstance();
ret = new PSWRenderer(screen, delegate);
} catch (Throwable e) {}
return ret;
@@ -104,7 +104,7 @@ public synchronized static PSWRenderer createJSWInstance(ResourceFactory factory
PSWRenderer ret = null;
try {
Class klass = Class.forName(rootPkg + ".impl.sw.java.JSWRendererDelegate");
RendererDelegate delegate = (RendererDelegate)klass.newInstance();
RendererDelegate delegate = (RendererDelegate)klass.getDeclaredConstructor().newInstance();
ret = new PSWRenderer(factory, delegate);
} catch (Throwable e) {}
return ret;
@@ -128,7 +128,7 @@ private synchronized static PSWRenderer createSSEInstance(Screen screen) {
PSWRenderer ret = null;
try {
Class klass = Class.forName(rootPkg + ".impl.sw.sse.SSERendererDelegate");
RendererDelegate delegate = (RendererDelegate)klass.newInstance();
RendererDelegate delegate = (RendererDelegate)klass.getDeclaredConstructor().newInstance();
ret = new PSWRenderer(screen, delegate);
} catch (Throwable e) {}
return ret;
@@ -79,21 +79,21 @@ public void testFireOnInvalidate() throws Exception {
sb.setCharAt(0, Character.toUpperCase(propertyName.charAt(0)));

if (clazz.getSuperclass().equals(PathElement.class)) {
PathElement e = (PathElement)clazz.newInstance();
PathElement e = (PathElement)clazz.getDeclaredConstructor().newInstance();
Path path = new Path();
path.getElements().addAll(new MoveTo(0,0), e);
NodeTest.syncNode(path);
getSetter(clazz, sb.toString()).invoke(e, this.inValue);
assertTrue(NodeTest.isDirty(path, (DirtyBits[])this.expectedDirtyBits));
} else if (clazz.getSuperclass().equals(Transform.class)) {
Transform tr = (Transform)clazz.newInstance();
Transform tr = (Transform)clazz.getDeclaredConstructor().newInstance();
Rectangle rect = new Rectangle();
rect.getTransforms().add(tr);
NodeTest.syncNode(rect);
getSetter(clazz, sb.toString()).invoke(tr, this.inValue);
assertTrue(NodeTest.isDirty(rect, (DirtyBits[])this.expectedDirtyBits));
} else {
Node node = (Node)clazz.newInstance();
Node node = (Node)clazz.getDeclaredConstructor().newInstance();
NodeTest.syncNode(node);
getSetter(clazz, sb.toString()).invoke(node, this.inValue);
if (this.expectedDirtyBits instanceof DirtyBits[]) {
@@ -109,7 +109,7 @@ public void testFireOnInvalidate() throws Exception {

private Path getPathNode(Class<? extends PathElement> pathElementClazz) throws Exception {
Path p = new Path();
p.getElements().addAll(new MoveTo(0,0), pathElementClazz.newInstance());
p.getElements().addAll(new MoveTo(0,0), pathElementClazz.getDeclaredConstructor().newInstance());
return p;
}

1 comment on commit 4619cdd

@openjdk-notifier
Copy link

@openjdk-notifier openjdk-notifier bot commented on 4619cdd May 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.