From 950a1404558d07d80b8eaef48b998b158d689212 Mon Sep 17 00:00:00 2001 From: Matt Pavlovich Date: Wed, 2 Jun 2021 08:30:36 -0500 Subject: [PATCH] [AMQ-8282] Migrate from deprecated .newInstance() calls to .getConstructors().newInstance --- .../java/org/apache/activemq/broker/BrokerService.java | 2 +- .../apache/activemq/security/DefaultAuthorizationMap.java | 2 +- .../java/org/apache/activemq/util/LogWriterFinder.java | 7 ++++++- .../java/org/apache/activemq/util/osgi/Activator.java | 8 +++++++- .../java/org/apache/activemq/TransportLoggerSupport.java | 2 +- .../org/apache/activemq/jndi/JNDIReferenceFactory.java | 2 +- .../main/java/org/apache/activemq/util/FactoryFinder.java | 8 +++++++- .../src/main/java/org/apache/activemq/console/Main.java | 2 +- .../activemq/console/command/AbstractAmqCommand.java | 2 +- .../console/filter/GroupPropertiesViewFilter.java | 2 +- .../activemq/console/filter/PropertiesViewFilter.java | 2 +- .../transport/discovery/http/HTTPDiscoveryAgent.java | 2 +- .../activemq/transport/http/HttpTransportServer.java | 4 ++-- .../store/kahadb/MultiKahaDBPersistenceAdapter.java | 4 ++-- .../activemq/karaf/commands/ActiveMQCommandSupport.java | 2 +- .../apache/activemq/plugin/PropertiesPlaceHolderUtil.java | 2 +- .../org/apache/activemq/tool/AbstractJmsClientSystem.java | 2 +- .../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 741ec4406c..fd329fc2f9 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 e27f8a783e..290f6bc07b 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 fd2cecfa2f..3a735778d2 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 b33c3a5739..8f291d3534 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 80ad17698e..f8a3cff801 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 1619a8e4f6..f9d718b1b7 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 3b2771e5ca..596d15ea00 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 4be67a1252..4e2db4f1e2 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 42d055204e..deeb59d41b 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 adf8a895bc..b77bfb6370 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 7c117a784f..50e3b828a2 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 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(); 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 15f4de6220..7ccc4923f0 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 f0ecefd00f..c25e4b9190 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 0ddae6e84e..d852c15251 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 configuration = new HashMap(); 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 bc3a41c559..12c280bca3 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 16037c18d8..bf02f5f433 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 58efb99958..c9f9058bb0 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 47a32c4777..6d86a20829 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; }