Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix class newinstance deprecation #1439

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion framework/src/play/Play.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import play.classloading.ApplicationClasses.ApplicationClass;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class ContinuationEnhancer extends Enhancer {
Expand Down
4 changes: 2 additions & 2 deletions framework/src/play/data/binding/Unbinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,14 @@ private static void internalUnbind(Map<String, Object> result, Object src, Class
Class<? extends TypeUnbinder<?>> toInstantiate = (Class<? extends TypeUnbinder<?>>) ((As) annotation)
.unbinder();
if (!(toInstantiate.equals(As.DEFAULT.class))) {
TypeUnbinder<?> myInstance = toInstantiate.newInstance();
TypeUnbinder<?> myInstance = toInstantiate.getDeclaredConstructor().newInstance();
isExtendedTypeBinder = myInstance.unBind(result, src, srcClazz, name, annotations);
}else{
// unbinder is default, test if binder handle the unbinder too
Class<? extends TypeBinder<?>> toInstantiateBinder = ((As) annotation).binder();
if (!(toInstantiateBinder.equals(As.DEFAULT.class))
&& TypeUnbinder.class.isAssignableFrom(toInstantiateBinder)) {
TypeUnbinder<?> myInstance = (TypeUnbinder<?>) toInstantiateBinder.newInstance();
TypeUnbinder<?> myInstance = (TypeUnbinder<?>) toInstantiateBinder.getDeclaredConstructor().newInstance();
isExtendedTypeBinder = myInstance.unBind(result, src, srcClazz, name, annotations);
}
}
Expand Down
2 changes: 1 addition & 1 deletion framework/src/play/data/binding/types/BinaryBinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public Object bind(String name, Annotation[] annotations, String value, Class ac
try {
Request req = Request.current();
if (req != null && req.args != null) {
Model.BinaryField b = (Model.BinaryField) actualClass.newInstance();
Model.BinaryField b = (Model.BinaryField) actualClass.getDeclaredConstructor().newInstance();
List<Upload> uploads = (List<Upload>) req.args.get("__UPLOADS");
if(uploads != null){
for (Upload upload : uploads) {
Expand Down
4 changes: 2 additions & 2 deletions framework/src/play/db/DBPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class DBPlugin extends PlayPlugin {
protected DataSourceFactory factory(Configuration dbConfig) {
String dbFactory = dbConfig.getProperty("db.factory", "play.db.hikaricp.HikariDataSourceFactory");
try {
return (DataSourceFactory) Class.forName(dbFactory).newInstance();
return (DataSourceFactory) Class.forName(dbFactory).getDeclaredConstructor().newInstance();
}
catch (Exception e) {
throw new IllegalArgumentException("Expected implementation of " + DataSourceFactory.class.getName() +
Expand Down Expand Up @@ -73,7 +73,7 @@ public void onApplicationStart() {
// Try the driver
String driver = dbConfig.getProperty("db.driver");
try {
Driver d = (Driver) Class.forName(driver, true, Play.classloader).newInstance();
Driver d = (Driver) Class.forName(driver, true, Play.classloader).getDeclaredConstructor().newInstance();
DriverManager.registerDriver(new ProxyDriver(d));
} catch (Exception e) {
throw new Exception("Database [" + dbName + "] Driver not found (" + driver + ")", e);
Expand Down
2 changes: 1 addition & 1 deletion framework/src/play/db/jpa/JPAModelLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ private void initProperties() {
private Object makeCompositeKey(Model model) throws Exception {
initProperties();
Class<?> idClass = getCompositeKeyClass();
Object id = idClass.newInstance();
Object id = idClass.getDeclaredConstructor().newInstance();
PropertyDescriptor[] idProperties = PropertyUtils.getPropertyDescriptors(idClass);
if (idProperties == null || idProperties.length == 0){
throw new UnexpectedException("Composite id has no properties: " + idClass.getName());
Expand Down
2 changes: 1 addition & 1 deletion framework/src/play/libs/Mail.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public static Session getSession() {
if (authenticator != null) {
props.put("mail.smtp.auth", "true");
try {
session = Session.getInstance(props, (Authenticator) Play.classloader.loadClass(authenticator).newInstance());
session = Session.getInstance(props, (Authenticator) Play.classloader.loadClass(authenticator).getDeclaredConstructor().newInstance());
} catch (Exception e) {
Logger.error(e, "Cannot instantiate custom SMTP authenticator (%s)", authenticator);
}
Expand Down
2 changes: 1 addition & 1 deletion framework/src/play/libs/WS.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ private static synchronized void init() {
wsImpl = new WSAsync();
} else {
try {
wsImpl = (WSImpl) Play.classloader.loadClass(implementation).newInstance();
wsImpl = (WSImpl) Play.classloader.loadClass(implementation).getDeclaredConstructor().newInstance();
if (Logger.isTraceEnabled()) {
Logger.trace("Using the class:" + implementation + " for web service");
}
Expand Down
4 changes: 1 addition & 3 deletions framework/src/play/server/HttpServerPipelineFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ protected String getName(String name) {
}

protected ChannelHandler getInstance(String name) throws Exception {

Class<?> clazz = classes.computeIfAbsent(name, className -> {
try {
return Class.forName(className);
Expand All @@ -76,8 +75,7 @@ protected ChannelHandler getInstance(String name) throws Exception {
}
});
if (ChannelHandler.class.isAssignableFrom(clazz))
return (ChannelHandler)clazz.newInstance();
return (ChannelHandler)clazz.getDeclaredConstructor().newInstance();
return null;
}
}

7 changes: 5 additions & 2 deletions framework/src/play/utils/Properties.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
Expand Down Expand Up @@ -105,10 +106,12 @@ public Object getClassInstance(String key) throws IllegalArgumentException {
throw new IllegalArgumentException("Setting " + key + " must be a valid classname : " + key);
}
try {
return Class.forName(s).newInstance();
return Class.forName(s).getDeclaredConstructor().newInstance();
} catch (ClassNotFoundException nfe) {
throw new IllegalArgumentException(s + ": invalid class name for key " + key, nfe);
} catch (InstantiationException | IllegalAccessException e) {
} catch (NoSuchMethodException | SecurityException e) {
throw new IllegalArgumentException(s + ": could not access class constructor for key " + key, e);
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
throw new IllegalArgumentException(s + ": class could not be reflected " + s, e);
}
}
Expand Down
6 changes: 3 additions & 3 deletions framework/src/play/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public static void mergeValueInMap(Map<String, String[]> map, String name, Strin
public static <K, V> Map<K, V> filterMap(Map<K, V> map, String keypattern) {
try {
@SuppressWarnings("unchecked")
Map<K, V> filtered = map.getClass().newInstance();
Map<K, V> filtered = map.getClass().getDeclaredConstructor().newInstance();
for (Map.Entry<K, V> entry : map.entrySet()) {
K key = entry.getKey();
if (key.toString().matches(keypattern)) {
Expand Down Expand Up @@ -169,8 +169,8 @@ public static Map<String, String> filterParams(Map<String, String[]> params, Str
}

public static void kill(String pid) throws Exception {
String command = OS.isWindows() ? "taskkill /F /PID " + pid : "kill " + pid;
Runtime.getRuntime().exec(command).waitFor();
String[] cmdarray = { OS.isWindows() ? "taskkill /F /PID " + pid : "kill " + pid };
Runtime.getRuntime().exec(cmdarray).waitFor();
}

public static class AlternativeDateFormat {
Expand Down