This is an automated email from the ASF dual-hosted git repository.
tombentley pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/kafka.git
The following commit(s) were added to refs/heads/trunk by this push:
new 91b3be4 MINOR: replace deprecated Class.newInstance() to new one
(#10610)
91b3be4 is described below
commit 91b3be44a246a49084bce0c957127aa759c616cc
Author: Luke Chen <[email protected]>
AuthorDate: Fri May 7 21:16:58 2021 +0800
MINOR: replace deprecated Class.newInstance() to new one (#10610)
* replace deprecated Class.newInstance() to
class.getDeclaredConstructor().newInstance()
* throw ReflectiveOperationException to cover all other exceptions
Reviewers: Tom Bentley <[email protected]>
---
.../connect/runtime/isolation/DelegatingClassLoader.java | 15 ++++++++-------
core/src/main/scala/kafka/tools/ConsoleConsumer.scala | 2 +-
2 files changed, 9 insertions(+), 8 deletions(-)
diff --git
a/connect/runtime/src/main/java/org/apache/kafka/connect/runtime/isolation/DelegatingClassLoader.java
b/connect/runtime/src/main/java/org/apache/kafka/connect/runtime/isolation/DelegatingClassLoader.java
index c894dd6..43ceba3 100644
---
a/connect/runtime/src/main/java/org/apache/kafka/connect/runtime/isolation/DelegatingClassLoader.java
+++
b/connect/runtime/src/main/java/org/apache/kafka/connect/runtime/isolation/DelegatingClassLoader.java
@@ -236,13 +236,13 @@ public class DelegatingClassLoader extends URLClassLoader
{
log.error("Invalid path in plugin path: {}. Ignoring.", path, e);
} catch (IOException e) {
log.error("Could not get listing for plugin path: {}. Ignoring.",
path, e);
- } catch (InstantiationException | IllegalAccessException e) {
+ } catch (ReflectiveOperationException e) {
log.error("Could not instantiate plugins in: {}. Ignoring: {}",
path, e);
}
}
private void registerPlugin(Path pluginLocation)
- throws InstantiationException, IllegalAccessException, IOException
{
+ throws IOException, ReflectiveOperationException {
log.info("Loading plugin from: {}", pluginLocation);
List<URL> pluginUrls = new ArrayList<>();
for (Path path : PluginUtils.pluginUrls(pluginLocation)) {
@@ -264,7 +264,7 @@ public class DelegatingClassLoader extends URLClassLoader {
ClassLoader loader,
URL[] urls,
Path pluginLocation
- ) throws InstantiationException, IllegalAccessException {
+ ) throws ReflectiveOperationException {
PluginScanResult plugins = scanPluginPath(loader, urls);
log.info("Registered loader: {}", loader);
if (!plugins.isEmpty()) {
@@ -322,7 +322,7 @@ public class DelegatingClassLoader extends URLClassLoader {
private PluginScanResult scanPluginPath(
ClassLoader loader,
URL[] urls
- ) throws InstantiationException, IllegalAccessException {
+ ) throws ReflectiveOperationException {
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setClassLoaders(new ClassLoader[]{loader});
builder.addUrls(urls);
@@ -346,7 +346,7 @@ public class DelegatingClassLoader extends URLClassLoader {
Reflections reflections,
Class<T> klass,
ClassLoader loader
- ) throws InstantiationException, IllegalAccessException {
+ ) throws ReflectiveOperationException {
Set<Class<? extends T>> plugins;
try {
plugins = reflections.getSubTypesOf(klass);
@@ -387,9 +387,10 @@ public class DelegatingClassLoader extends URLClassLoader {
return pluginImpl instanceof Versioned ? ((Versioned)
pluginImpl).version() : UNDEFINED_VERSION;
}
- private static <T> String versionFor(Class<? extends T> pluginKlass)
throws IllegalAccessException, InstantiationException {
+ private static <T> String versionFor(Class<? extends T> pluginKlass)
throws ReflectiveOperationException {
// Temporary workaround until all the plugins are versioned.
- return Connector.class.isAssignableFrom(pluginKlass) ?
versionFor(pluginKlass.newInstance()) : UNDEFINED_VERSION;
+ return Connector.class.isAssignableFrom(pluginKlass) ?
+ versionFor(pluginKlass.getDeclaredConstructor().newInstance()) :
UNDEFINED_VERSION;
}
@Override
diff --git a/core/src/main/scala/kafka/tools/ConsoleConsumer.scala
b/core/src/main/scala/kafka/tools/ConsoleConsumer.scala
index a40c041..5e450c2 100755
--- a/core/src/main/scala/kafka/tools/ConsoleConsumer.scala
+++ b/core/src/main/scala/kafka/tools/ConsoleConsumer.scala
@@ -578,7 +578,7 @@ class DefaultMessageFormatter extends MessageFormatter {
}
private def getDeserializerProperty(isKey: Boolean)(configs: Map[String, _],
propertyName: String): Deserializer[_] = {
- val deserializer =
Class.forName(configs.get(propertyName).asInstanceOf[String]).newInstance().asInstanceOf[Deserializer[_]]
+ val deserializer =
Class.forName(configs.get(propertyName).asInstanceOf[String]).getDeclaredConstructor().newInstance().asInstanceOf[Deserializer[_]]
val deserializerConfig = propertiesWithKeyPrefixStripped(propertyName +
".", configs)
.asScala
.asJava