activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jbono...@apache.org
Subject [activemq] branch main updated: [AMQ-8282] Migrate from deprecated .newInstance() calls to .getConstructors().newInstance
Date Fri, 04 Jun 2021 04:18:33 GMT
This is an automated email from the ASF dual-hosted git repository.

jbonofre pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/activemq.git


The following commit(s) were added to refs/heads/main by this push:
     new 950a140  [AMQ-8282] Migrate from deprecated .newInstance() calls to .getConstructors().newInstance
     new ae777e9  Merge pull request #665 from mattrpav/AMQ-8282
950a140 is described below

commit 950a1404558d07d80b8eaef48b998b158d689212
Author: Matt Pavlovich <matt@hyte.io>
AuthorDate: Wed Jun 2 08:30:36 2021 -0500

    [AMQ-8282] Migrate from deprecated .newInstance() calls to .getConstructors().newInstance
---
 .../src/main/java/org/apache/activemq/broker/BrokerService.java   | 2 +-
 .../org/apache/activemq/security/DefaultAuthorizationMap.java     | 2 +-
 .../src/main/java/org/apache/activemq/util/LogWriterFinder.java   | 7 ++++++-
 .../src/main/java/org/apache/activemq/util/osgi/Activator.java    | 8 +++++++-
 .../src/main/java/org/apache/activemq/TransportLoggerSupport.java | 2 +-
 .../main/java/org/apache/activemq/jndi/JNDIReferenceFactory.java  | 2 +-
 .../src/main/java/org/apache/activemq/util/FactoryFinder.java     | 8 +++++++-
 .../src/main/java/org/apache/activemq/console/Main.java           | 2 +-
 .../org/apache/activemq/console/command/AbstractAmqCommand.java   | 2 +-
 .../apache/activemq/console/filter/GroupPropertiesViewFilter.java | 2 +-
 .../org/apache/activemq/console/filter/PropertiesViewFilter.java  | 2 +-
 .../activemq/transport/discovery/http/HTTPDiscoveryAgent.java     | 2 +-
 .../org/apache/activemq/transport/http/HttpTransportServer.java   | 4 ++--
 .../activemq/store/kahadb/MultiKahaDBPersistenceAdapter.java      | 4 ++--
 .../apache/activemq/karaf/commands/ActiveMQCommandSupport.java    | 2 +-
 .../org/apache/activemq/plugin/PropertiesPlaceHolderUtil.java     | 2 +-
 .../java/org/apache/activemq/tool/AbstractJmsClientSystem.java    | 2 +-
 .../apache/activemq/tool/spi/ReflectionSPIConnectionFactory.java  | 2 +-
 18 files changed, 37 insertions(+), 20 deletions(-)

diff --git a/activemq-broker/src/main/java/org/apache/activemq/broker/BrokerService.java b/activemq-broker/src/main/java/org/apache/activemq/broker/BrokerService.java
index 741ec44..fd329fc 100644
--- a/activemq-broker/src/main/java/org/apache/activemq/broker/BrokerService.java
+++ b/activemq-broker/src/main/java/org/apache/activemq/broker/BrokerService.java
@@ -2477,7 +2477,7 @@ public class BrokerService implements Service {
             } else {
                 try {
                     String clazz = "org.apache.activemq.store.kahadb.KahaDBPersistenceAdapter";
-                    PersistenceAdapter adaptor = (PersistenceAdapter)getClass().getClassLoader().loadClass(clazz).newInstance();
+                    PersistenceAdapter adaptor = (PersistenceAdapter)getClass().getClassLoader().loadClass(clazz).getConstructor().newInstance();
                     File dir = new File(getBrokerDataDirectory(),"KahaDB");
                     adaptor.setDirectory(dir);
                     return adaptor;
diff --git a/activemq-broker/src/main/java/org/apache/activemq/security/DefaultAuthorizationMap.java
b/activemq-broker/src/main/java/org/apache/activemq/security/DefaultAuthorizationMap.java
index e27f8a7..290f6bc 100644
--- a/activemq-broker/src/main/java/org/apache/activemq/security/DefaultAuthorizationMap.java
+++ b/activemq-broker/src/main/java/org/apache/activemq/security/DefaultAuthorizationMap.java
@@ -246,7 +246,7 @@ public class DefaultAuthorizationMap extends DestinationMap implements
Authoriza
         if (i < constructors.length) {
             instance = constructors[i].newInstance(param);
         } else {
-            instance = cls.newInstance();
+            instance = cls.getConstructor().newInstance();
             Method[] methods = cls.getMethods();
             i = 0;
             for (i = 0; i < methods.length; i++) {
diff --git a/activemq-broker/src/main/java/org/apache/activemq/util/LogWriterFinder.java b/activemq-broker/src/main/java/org/apache/activemq/util/LogWriterFinder.java
index fd2cecf..3a73577 100644
--- a/activemq-broker/src/main/java/org/apache/activemq/util/LogWriterFinder.java
+++ b/activemq-broker/src/main/java/org/apache/activemq/util/LogWriterFinder.java
@@ -19,6 +19,7 @@ package org.apache.activemq.util;
 import java.io.BufferedInputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.lang.reflect.InvocationTargetException;
 import java.util.Properties;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
@@ -74,7 +75,11 @@ public class LogWriterFinder {
             clazz = newInstance(doFindLogWriterProperties(logWriterName));
             classMap.put(logWriterName, clazz);
         }
-        return (LogWriter)clazz.newInstance();
+        try {
+            return LogWriter.class.cast(clazz.getConstructor().newInstance());
+        } catch (InvocationTargetException | NoSuchMethodException e) {
+        	throw new InstantiationException(e.getMessage());
+        }
     }
 
     /**
diff --git a/activemq-broker/src/main/java/org/apache/activemq/util/osgi/Activator.java b/activemq-broker/src/main/java/org/apache/activemq/util/osgi/Activator.java
index b33c3a5..8f291d3 100644
--- a/activemq-broker/src/main/java/org/apache/activemq/util/osgi/Activator.java
+++ b/activemq-broker/src/main/java/org/apache/activemq/util/osgi/Activator.java
@@ -22,6 +22,7 @@ import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
+import java.lang.reflect.InvocationTargetException;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.HashSet;
@@ -214,7 +215,12 @@ public class Activator implements BundleActivator, SynchronousBundleListener,
Ob
                 throw new IOException(msg);
             }
         }
-        return clazz.newInstance();
+        
+        try {
+            return clazz.getConstructor().newInstance();
+        } catch (InvocationTargetException | NoSuchMethodException e) {
+        	throw new InstantiationException(e.getMessage());
+        }
     }
 
     // ================================================================
diff --git a/activemq-client/src/main/java/org/apache/activemq/TransportLoggerSupport.java
b/activemq-client/src/main/java/org/apache/activemq/TransportLoggerSupport.java
index 80ad176..f8a3cff 100644
--- a/activemq-client/src/main/java/org/apache/activemq/TransportLoggerSupport.java
+++ b/activemq-client/src/main/java/org/apache/activemq/TransportLoggerSupport.java
@@ -42,7 +42,7 @@ public class TransportLoggerSupport {
     static {
         SPI temp;
         try {
-            temp = (SPI) TransportLoggerSupport.class.getClassLoader().loadClass("org.apache.activemq.transport.TransportLoggerFactorySPI").newInstance();
+            temp = (SPI) TransportLoggerSupport.class.getClassLoader().loadClass("org.apache.activemq.transport.TransportLoggerFactorySPI").getConstructor().newInstance();
         } catch (Throwable e) {
             temp = null;
         }
diff --git a/activemq-client/src/main/java/org/apache/activemq/jndi/JNDIReferenceFactory.java
b/activemq-client/src/main/java/org/apache/activemq/jndi/JNDIReferenceFactory.java
index 1619a8e..f9d718b 100644
--- a/activemq-client/src/main/java/org/apache/activemq/jndi/JNDIReferenceFactory.java
+++ b/activemq-client/src/main/java/org/apache/activemq/jndi/JNDIReferenceFactory.java
@@ -62,7 +62,7 @@ public class JNDIReferenceFactory implements ObjectFactory {
             Class theClass = loadClass(this, reference.getClassName());
             if (JNDIStorableInterface.class.isAssignableFrom(theClass)) {
 
-                JNDIStorableInterface store = (JNDIStorableInterface)theClass.newInstance();
+                JNDIStorableInterface store = JNDIStorableInterface.class.cast(theClass.getConstructor().newInstance());
                 Properties properties = new Properties();
                 for (Enumeration iter = reference.getAll(); iter.hasMoreElements();) {
 
diff --git a/activemq-client/src/main/java/org/apache/activemq/util/FactoryFinder.java b/activemq-client/src/main/java/org/apache/activemq/util/FactoryFinder.java
index 3b2771e..596d15e 100644
--- a/activemq-client/src/main/java/org/apache/activemq/util/FactoryFinder.java
+++ b/activemq-client/src/main/java/org/apache/activemq/util/FactoryFinder.java
@@ -19,6 +19,7 @@ package org.apache.activemq.util;
 import java.io.BufferedInputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.lang.reflect.InvocationTargetException;
 import java.util.Properties;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
@@ -60,7 +61,12 @@ public class FactoryFinder {
                 clazz = loadClass(loadProperties(path));
                 classMap.put(path, clazz);
             }
-            return clazz.newInstance();
+            
+            try {
+               return clazz.getConstructor().newInstance();
+            } catch (NoSuchMethodException | InvocationTargetException e) {
+               throw new InstantiationException(e.getMessage());
+            }
         }
 
         static public Class loadClass(Properties properties) throws ClassNotFoundException,
IOException {
diff --git a/activemq-console/src/main/java/org/apache/activemq/console/Main.java b/activemq-console/src/main/java/org/apache/activemq/console/Main.java
index 4be67a1..4e2db4f 100644
--- a/activemq-console/src/main/java/org/apache/activemq/console/Main.java
+++ b/activemq-console/src/main/java/org/apache/activemq/console/Main.java
@@ -259,7 +259,7 @@ public class Main {
             Method runTask = task.getMethod("main", new Class[] {
                 String[].class, InputStream.class, PrintStream.class
             });
-            return (int)runTask.invoke(task.newInstance(), args, System.in, System.out);
+            return (int)runTask.invoke(task.getConstructor().newInstance(), args, System.in,
System.out);
         } catch (InvocationTargetException e) {
             throw e.getCause();
         }
diff --git a/activemq-console/src/main/java/org/apache/activemq/console/command/AbstractAmqCommand.java
b/activemq-console/src/main/java/org/apache/activemq/console/command/AbstractAmqCommand.java
index 42d0552..deeb59d 100644
--- a/activemq-console/src/main/java/org/apache/activemq/console/command/AbstractAmqCommand.java
+++ b/activemq-console/src/main/java/org/apache/activemq/console/command/AbstractAmqCommand.java
@@ -250,7 +250,7 @@ public abstract class AbstractAmqCommand extends AbstractCommand {
         if (passwordFactory == null && passwordFactoryClassString != null) {
             try {
                 Class klass = Class.forName(passwordFactoryClassString);
-                passwordFactory = (PasswordFactory) klass.newInstance();
+                passwordFactory = PasswordFactory.class.cast(klass.getConstructor().newInstance());
             } catch (Exception e) {
                 throw new RuntimeException(e);
             }
diff --git a/activemq-console/src/main/java/org/apache/activemq/console/filter/GroupPropertiesViewFilter.java
b/activemq-console/src/main/java/org/apache/activemq/console/filter/GroupPropertiesViewFilter.java
index adf8a89..b77bfb6 100644
--- a/activemq-console/src/main/java/org/apache/activemq/console/filter/GroupPropertiesViewFilter.java
+++ b/activemq-console/src/main/java/org/apache/activemq/console/filter/GroupPropertiesViewFilter.java
@@ -59,7 +59,7 @@ public class GroupPropertiesViewFilter extends PropertiesViewFilter {
         Map newData;
         try {
             // Lets try to use the same class as the original
-            newData = data.getClass().newInstance();
+            newData = data.getClass().getConstructor().newInstance();
         } catch (Exception e) {
             // Lets use a default HashMap
             newData = new HashMap();
diff --git a/activemq-console/src/main/java/org/apache/activemq/console/filter/PropertiesViewFilter.java
b/activemq-console/src/main/java/org/apache/activemq/console/filter/PropertiesViewFilter.java
index 7c117a7..50e3b82 100644
--- a/activemq-console/src/main/java/org/apache/activemq/console/filter/PropertiesViewFilter.java
+++ b/activemq-console/src/main/java/org/apache/activemq/console/filter/PropertiesViewFilter.java
@@ -96,7 +96,7 @@ public class PropertiesViewFilter implements QueryFilter {
         Map<Object, Object> newData;
         try {
             // Lets try to use the same class as the original
-            newData = new LinkedHashMap(data.getClass().newInstance());
+            newData = new LinkedHashMap(data.getClass().getConstructor().newInstance());
         } catch (Exception e) {
             // Lets use a default HashMap
             newData = new LinkedHashMap<Object, Object>();
diff --git a/activemq-http/src/main/java/org/apache/activemq/transport/discovery/http/HTTPDiscoveryAgent.java
b/activemq-http/src/main/java/org/apache/activemq/transport/discovery/http/HTTPDiscoveryAgent.java
index 15f4de6..7ccc492 100644
--- a/activemq-http/src/main/java/org/apache/activemq/transport/discovery/http/HTTPDiscoveryAgent.java
+++ b/activemq-http/src/main/java/org/apache/activemq/transport/discovery/http/HTTPDiscoveryAgent.java
@@ -276,7 +276,7 @@ public class HTTPDiscoveryAgent implements DiscoveryAgent, Suspendable
{
      */
     private Service createEmbeddedJettyServer() throws Exception {
         Class<?> clazz = HTTPDiscoveryAgent.class.getClassLoader().loadClass("org.apache.activemq.transport.discovery.http.EmbeddedJettyServer");
-        return (Service) clazz.newInstance();
+        return Service.class.cast(clazz.getConstructor().newInstance());
     }
 
     private void update() {
diff --git a/activemq-http/src/main/java/org/apache/activemq/transport/http/HttpTransportServer.java
b/activemq-http/src/main/java/org/apache/activemq/transport/http/HttpTransportServer.java
index f0ecefd..c25e4b9 100644
--- a/activemq-http/src/main/java/org/apache/activemq/transport/http/HttpTransportServer.java
+++ b/activemq-http/src/main/java/org/apache/activemq/transport/http/HttpTransportServer.java
@@ -137,9 +137,9 @@ public class HttpTransportServer extends WebTransportServerSupport {
     private void addGzipHandler(ServletContextHandler contextHandler) throws Exception {
         HandlerWrapper handler = null;
         try {
-            handler = (HandlerWrapper) forName("org.eclipse.jetty.servlets.gzip.GzipHandler").newInstance();
+            handler = (HandlerWrapper) forName("org.eclipse.jetty.servlets.gzip.GzipHandler").getConstructor().newInstance();
         } catch (Throwable t) {
-            handler = (HandlerWrapper) forName("org.eclipse.jetty.server.handler.gzip.GzipHandler").newInstance();
+            handler = (HandlerWrapper) forName("org.eclipse.jetty.server.handler.gzip.GzipHandler").getConstructor().newInstance();
         }
         contextHandler.insertHandler(handler);
     }
diff --git a/activemq-kahadb-store/src/main/java/org/apache/activemq/store/kahadb/MultiKahaDBPersistenceAdapter.java
b/activemq-kahadb-store/src/main/java/org/apache/activemq/store/kahadb/MultiKahaDBPersistenceAdapter.java
index 0ddae6e..d852c15 100644
--- a/activemq-kahadb-store/src/main/java/org/apache/activemq/store/kahadb/MultiKahaDBPersistenceAdapter.java
+++ b/activemq-kahadb-store/src/main/java/org/apache/activemq/store/kahadb/MultiKahaDBPersistenceAdapter.java
@@ -440,7 +440,7 @@ public class MultiKahaDBPersistenceAdapter extends LockableServiceSupport
implem
         File directory = null;
         File defaultDir = DEFAULT_DIRECTORY;
         try {
-            defaultDir = adapter.getClass().newInstance().getDirectory();
+            defaultDir = adapter.getClass().getConstructor().newInstance().getDirectory();
         } catch (Exception e) {
         }
         if (defaultDir.equals(adapter.getDirectory())) {
@@ -480,7 +480,7 @@ public class MultiKahaDBPersistenceAdapter extends LockableServiceSupport
implem
         try {
             Map<String, Object> configuration = new HashMap<String, Object>();
             IntrospectionSupport.getProperties(template, configuration, null);
-            PersistenceAdapter adapter = template.getClass().newInstance();
+            PersistenceAdapter adapter = template.getClass().getConstructor().newInstance();
             IntrospectionSupport.setProperties(adapter, configuration);
             return adapter;
         } catch (Exception e) {
diff --git a/activemq-karaf/src/main/java/org/apache/activemq/karaf/commands/ActiveMQCommandSupport.java
b/activemq-karaf/src/main/java/org/apache/activemq/karaf/commands/ActiveMQCommandSupport.java
index bc3a41c..12c280b 100644
--- a/activemq-karaf/src/main/java/org/apache/activemq/karaf/commands/ActiveMQCommandSupport.java
+++ b/activemq-karaf/src/main/java/org/apache/activemq/karaf/commands/ActiveMQCommandSupport.java
@@ -38,7 +38,7 @@ public class ActiveMQCommandSupport extends OsgiCommandSupport {
     protected Object doExecute() throws Exception {
         CommandContext context2 = new CommandContext();
         context2.setFormatter(new CommandShellOutputFormatter(System.out));
-        Command currentCommand = command.getClass().newInstance();
+        Command currentCommand = command.getClass().getConstructor().newInstance();
 
         try {
             currentCommand.setCommandContext(context2);
diff --git a/activemq-runtime-config/src/main/java/org/apache/activemq/plugin/PropertiesPlaceHolderUtil.java
b/activemq-runtime-config/src/main/java/org/apache/activemq/plugin/PropertiesPlaceHolderUtil.java
index 16037c1..bf02f5f 100644
--- a/activemq-runtime-config/src/main/java/org/apache/activemq/plugin/PropertiesPlaceHolderUtil.java
+++ b/activemq-runtime-config/src/main/java/org/apache/activemq/plugin/PropertiesPlaceHolderUtil.java
@@ -137,7 +137,7 @@ public class PropertiesPlaceHolderUtil {
         }
         for (String value : propertiesClazzes) {
             try {
-                Object springBean = getClass().getClassLoader().loadClass(value).newInstance();
+                Object springBean = getClass().getClassLoader().loadClass(value).getConstructor().newInstance();
                 if (springBean instanceof FactoryBean) {
                     // can't access the factory or created properties from spring context
so we got to recreate
                     initialProperties.putAll((Properties) FactoryBean.class.getMethod("getObject",
(Class<?>[]) null).invoke(springBean));
diff --git a/activemq-tooling/activemq-perf-maven-plugin/src/main/java/org/apache/activemq/tool/AbstractJmsClientSystem.java
b/activemq-tooling/activemq-perf-maven-plugin/src/main/java/org/apache/activemq/tool/AbstractJmsClientSystem.java
index 58efb99..c9f9058 100644
--- a/activemq-tooling/activemq-perf-maven-plugin/src/main/java/org/apache/activemq/tool/AbstractJmsClientSystem.java
+++ b/activemq-tooling/activemq-perf-maven-plugin/src/main/java/org/apache/activemq/tool/AbstractJmsClientSystem.java
@@ -237,7 +237,7 @@ public abstract class AbstractJmsClientSystem extends AbstractObjectProperties
{
     protected ConnectionFactory loadJmsFactory(String spiClass, Properties factorySettings)
throws JMSException {
         try {
             Class<?> spi = Class.forName(spiClass);
-            SPIConnectionFactory spiFactory = (SPIConnectionFactory)spi.newInstance();
+            SPIConnectionFactory spiFactory = SPIConnectionFactory.class.cast(spi.getConstructor().newInstance());
             ConnectionFactory jmsFactory = spiFactory.createConnectionFactory(factorySettings);
             LOG.info("Created: " + jmsFactory.getClass().getName() + " using SPIConnectionFactory:
" + spiFactory.getClass().getName());
             return jmsFactory;
diff --git a/activemq-tooling/activemq-perf-maven-plugin/src/main/java/org/apache/activemq/tool/spi/ReflectionSPIConnectionFactory.java
b/activemq-tooling/activemq-perf-maven-plugin/src/main/java/org/apache/activemq/tool/spi/ReflectionSPIConnectionFactory.java
index 47a32c4..6d86a20 100644
--- a/activemq-tooling/activemq-perf-maven-plugin/src/main/java/org/apache/activemq/tool/spi/ReflectionSPIConnectionFactory.java
+++ b/activemq-tooling/activemq-perf-maven-plugin/src/main/java/org/apache/activemq/tool/spi/ReflectionSPIConnectionFactory.java
@@ -26,7 +26,7 @@ public abstract class ReflectionSPIConnectionFactory extends ClassLoaderSPIConne
 
     public ConnectionFactory instantiateConnectionFactory(Properties settings) throws Exception
{
         Class factoryClass = Thread.currentThread().getContextClassLoader().loadClass(getClassName());
-        ConnectionFactory factory = (ConnectionFactory)factoryClass.newInstance();
+        ConnectionFactory factory = ConnectionFactory.class.cast(factoryClass.getConstructor().newInstance());
         configureConnectionFactory(factory, settings);
         return factory;
     }

Mime
View raw message