Merge pull request #665 from mattrpav/AMQ-8282

[AMQ-8282] Migrate from deprecated .newInstance() calls to .getConstr…
This commit is contained in:
Jean-Baptiste Onofré 2021-06-04 06:18:27 +02:00 committed by GitHub
commit ae777e9571
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 37 additions and 20 deletions

View File

@ -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;

View File

@ -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++) {

View File

@ -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());
}
}
/**

View File

@ -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());
}
}
// ================================================================

View File

@ -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;
}

View File

@ -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();) {

View File

@ -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 {

View File

@ -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();
}

View File

@ -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);
}

View File

@ -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();

View File

@ -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>();

View File

@ -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() {

View File

@ -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);
}

View File

@ -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) {

View File

@ -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);

View File

@ -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));

View File

@ -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;

View File

@ -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;
}