From d5c066839b854c8fa99aaed96dd2653c99816dc1 Mon Sep 17 00:00:00 2001 From: Chris Webb Date: Mon, 9 Jan 2023 13:38:16 +0000 Subject: [PATCH 1/3] Fixed class newInstance deprecation --- framework/src/play/Play.java | 1 - .../classloading/enhancers/ContinuationEnhancer.java | 1 - framework/src/play/data/binding/Unbinder.java | 4 ++-- framework/src/play/data/binding/types/BinaryBinder.java | 2 +- framework/src/play/db/DBPlugin.java | 4 ++-- framework/src/play/db/jpa/JPAModelLoader.java | 2 +- framework/src/play/libs/Mail.java | 2 +- framework/src/play/libs/WS.java | 2 +- framework/src/play/server/HttpServerPipelineFactory.java | 6 ++---- framework/src/play/test/FunctionalTest.java | 4 +--- framework/src/play/utils/Properties.java | 9 ++++++--- framework/src/play/utils/Utils.java | 6 +++--- 12 files changed, 20 insertions(+), 23 deletions(-) diff --git a/framework/src/play/Play.java b/framework/src/play/Play.java index 1241fdac71..aa1d66ea85 100644 --- a/framework/src/play/Play.java +++ b/framework/src/play/Play.java @@ -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; diff --git a/framework/src/play/classloading/enhancers/ContinuationEnhancer.java b/framework/src/play/classloading/enhancers/ContinuationEnhancer.java index 456f28704e..dddfcf26e2 100644 --- a/framework/src/play/classloading/enhancers/ContinuationEnhancer.java +++ b/framework/src/play/classloading/enhancers/ContinuationEnhancer.java @@ -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 { diff --git a/framework/src/play/data/binding/Unbinder.java b/framework/src/play/data/binding/Unbinder.java index 577826560a..1d70ccfa15 100644 --- a/framework/src/play/data/binding/Unbinder.java +++ b/framework/src/play/data/binding/Unbinder.java @@ -121,14 +121,14 @@ private static void internalUnbind(Map result, Object src, Class Class> toInstantiate = (Class>) ((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> 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); } } diff --git a/framework/src/play/data/binding/types/BinaryBinder.java b/framework/src/play/data/binding/types/BinaryBinder.java index 50bfaa67b8..54296c7ecf 100644 --- a/framework/src/play/data/binding/types/BinaryBinder.java +++ b/framework/src/play/data/binding/types/BinaryBinder.java @@ -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 uploads = (List) req.args.get("__UPLOADS"); if(uploads != null){ for (Upload upload : uploads) { diff --git a/framework/src/play/db/DBPlugin.java b/framework/src/play/db/DBPlugin.java index 31a73df31c..8f0363a939 100644 --- a/framework/src/play/db/DBPlugin.java +++ b/framework/src/play/db/DBPlugin.java @@ -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() + @@ -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); diff --git a/framework/src/play/db/jpa/JPAModelLoader.java b/framework/src/play/db/jpa/JPAModelLoader.java index 4040854404..e8a052c1e2 100644 --- a/framework/src/play/db/jpa/JPAModelLoader.java +++ b/framework/src/play/db/jpa/JPAModelLoader.java @@ -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()); diff --git a/framework/src/play/libs/Mail.java b/framework/src/play/libs/Mail.java index 5eff4b485d..c311bb51bb 100644 --- a/framework/src/play/libs/Mail.java +++ b/framework/src/play/libs/Mail.java @@ -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); } diff --git a/framework/src/play/libs/WS.java b/framework/src/play/libs/WS.java index f9af9d49fb..c482c99a7f 100644 --- a/framework/src/play/libs/WS.java +++ b/framework/src/play/libs/WS.java @@ -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"); } diff --git a/framework/src/play/server/HttpServerPipelineFactory.java b/framework/src/play/server/HttpServerPipelineFactory.java index ca006c307c..78570c3c66 100644 --- a/framework/src/play/server/HttpServerPipelineFactory.java +++ b/framework/src/play/server/HttpServerPipelineFactory.java @@ -67,8 +67,7 @@ protected String getName(String name) { } protected ChannelHandler getInstance(String name) throws Exception { - - Class clazz = classes.computeIfAbsent(name, className -> { + Class clazz = classes.computeIfAbsent(name, className -> { try { return Class.forName(className); } catch (ClassNotFoundException e) { @@ -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; } } - diff --git a/framework/src/play/test/FunctionalTest.java b/framework/src/play/test/FunctionalTest.java index 53e3825f36..d429fbd15b 100644 --- a/framework/src/play/test/FunctionalTest.java +++ b/framework/src/play/test/FunctionalTest.java @@ -6,7 +6,6 @@ import com.ning.http.client.multipart.MultipartUtils; import com.ning.http.client.multipart.Part; import com.ning.http.client.multipart.StringPart; -import org.apache.commons.lang.ArrayUtils; import org.junit.Before; import play.Invoker; import play.Invoker.InvocationContext; @@ -222,8 +221,7 @@ public static Response POST(Request request, Object url, Map par _ByteArrayOutputStream baos; try { requestEntity = MultipartUtils.newMultipartBody(parts, new FluentCaseInsensitiveStringsMap()); - request.headers.putAll(ArrayUtils - .toMap(new Object[][] { { "content-type", new Http.Header("content-type", requestEntity.getContentType()) } })); + request.headers.put("content-type", new Http.Header("content-type", requestEntity.getContentType())); long contentLength = requestEntity.getContentLength(); if (contentLength < Integer.MIN_VALUE || contentLength > Integer.MAX_VALUE) { throw new IllegalArgumentException(contentLength + " cannot be cast to int without changing its value."); diff --git a/framework/src/play/utils/Properties.java b/framework/src/play/utils/Properties.java index d718a9ee7a..a12fd9d9b1 100644 --- a/framework/src/play/utils/Properties.java +++ b/framework/src/play/utils/Properties.java @@ -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; @@ -105,12 +106,14 @@ 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); - } + } } public Object getClassInstance(String key, Object defaultinstance) diff --git a/framework/src/play/utils/Utils.java b/framework/src/play/utils/Utils.java index a5b0f7ba84..0c61e72cfb 100644 --- a/framework/src/play/utils/Utils.java +++ b/framework/src/play/utils/Utils.java @@ -115,7 +115,7 @@ public static void mergeValueInMap(Map map, String name, Strin public static Map filterMap(Map map, String keypattern) { try { @SuppressWarnings("unchecked") - Map filtered = map.getClass().newInstance(); + Map filtered = map.getClass().getDeclaredConstructor().newInstance(); for (Map.Entry entry : map.entrySet()) { K key = entry.getKey(); if (key.toString().matches(keypattern)) { @@ -169,8 +169,8 @@ public static Map filterParams(Map 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 { From a771a77ce56e8bfe34c6a7eb642b934aec01efef Mon Sep 17 00:00:00 2001 From: Chris Webb Date: Mon, 9 Jan 2023 14:06:09 +0000 Subject: [PATCH 2/3] Revert change to FunctionalTest --- framework/src/play/test/FunctionalTest.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/framework/src/play/test/FunctionalTest.java b/framework/src/play/test/FunctionalTest.java index d429fbd15b..53e3825f36 100644 --- a/framework/src/play/test/FunctionalTest.java +++ b/framework/src/play/test/FunctionalTest.java @@ -6,6 +6,7 @@ import com.ning.http.client.multipart.MultipartUtils; import com.ning.http.client.multipart.Part; import com.ning.http.client.multipart.StringPart; +import org.apache.commons.lang.ArrayUtils; import org.junit.Before; import play.Invoker; import play.Invoker.InvocationContext; @@ -221,7 +222,8 @@ public static Response POST(Request request, Object url, Map par _ByteArrayOutputStream baos; try { requestEntity = MultipartUtils.newMultipartBody(parts, new FluentCaseInsensitiveStringsMap()); - request.headers.put("content-type", new Http.Header("content-type", requestEntity.getContentType())); + request.headers.putAll(ArrayUtils + .toMap(new Object[][] { { "content-type", new Http.Header("content-type", requestEntity.getContentType()) } })); long contentLength = requestEntity.getContentLength(); if (contentLength < Integer.MIN_VALUE || contentLength > Integer.MAX_VALUE) { throw new IllegalArgumentException(contentLength + " cannot be cast to int without changing its value."); From d372ccef2b82742e994a9c026367a7132bbdcc32 Mon Sep 17 00:00:00 2001 From: Chris Webb Date: Fri, 14 Jul 2023 09:04:17 +0100 Subject: [PATCH 3/3] Fixed formatting --- framework/src/play/utils/Properties.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/framework/src/play/utils/Properties.java b/framework/src/play/utils/Properties.java index a12fd9d9b1..fd1c00ef82 100644 --- a/framework/src/play/utils/Properties.java +++ b/framework/src/play/utils/Properties.java @@ -110,10 +110,10 @@ public Object getClassInstance(String key) throws IllegalArgumentException { } catch (ClassNotFoundException nfe) { throw new IllegalArgumentException(s + ": invalid class name for key " + key, nfe); } 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 + ": 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); - } + } } public Object getClassInstance(String key, Object defaultinstance)