From 507eb9498bd73505ba8959e6a911cd6a51b5fe18 Mon Sep 17 00:00:00 2001 From: dreis2211 Date: Sun, 14 Jun 2020 08:50:37 +0200 Subject: [PATCH] Use Class.getDeclaredConstructor().newInstance() See gh-21913 --- .../boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.java | 2 +- .../boot/autoconfigure/web/ServerPropertiesTests.java | 3 ++- .../java/org/springframework/boot/loader/jar/Handler.java | 2 +- .../testsupport/junit/platform/SummaryGeneratingListener.java | 4 ++-- .../java/org/springframework/boot/EnvironmentConverter.java | 4 ++-- .../boot/web/embedded/jetty/JasperInitializer.java | 4 ++-- .../web/embedded/tomcat/TomcatServletWebServerFactory.java | 3 ++- 7 files changed, 12 insertions(+), 10 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.java index b54a8504d7..01a8fa29fd 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.java @@ -204,7 +204,7 @@ class HibernateJpaConfiguration extends JpaBaseConfiguration { private Object getNoJtaPlatformManager() { for (String candidate : NO_JTA_PLATFORM_CLASSES) { try { - return Class.forName(candidate).newInstance(); + return Class.forName(candidate).getDeclaredConstructor().newInstance(); } catch (Exception ex) { // Continue searching diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java index 2f8a68f14e..c479305808 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java @@ -538,7 +538,8 @@ class ServerPropertiesTests { } private AbstractProtocol getDefaultProtocol() throws Exception { - return (AbstractProtocol) Class.forName(TomcatServletWebServerFactory.DEFAULT_PROTOCOL).newInstance(); + return (AbstractProtocol) Class.forName(TomcatServletWebServerFactory.DEFAULT_PROTOCOL) + .getDeclaredConstructor().newInstance(); } private void bind(String name, String value) { diff --git a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/Handler.java b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/Handler.java index 11ff3810ff..54deb09b48 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/Handler.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/Handler.java @@ -132,7 +132,7 @@ public class Handler extends URLStreamHandler { for (String handlerClassName : FALLBACK_HANDLERS) { try { Class handlerClass = Class.forName(handlerClassName); - this.fallbackHandler = (URLStreamHandler) handlerClass.newInstance(); + this.fallbackHandler = (URLStreamHandler) handlerClass.getDeclaredConstructor().newInstance(); return this.fallbackHandler; } catch (Exception ex) { diff --git a/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/junit/platform/SummaryGeneratingListener.java b/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/junit/platform/SummaryGeneratingListener.java index 918df638af..c1b5b01e62 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/junit/platform/SummaryGeneratingListener.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/junit/platform/SummaryGeneratingListener.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,7 +28,7 @@ public class SummaryGeneratingListener extends ReflectiveWrapper { public SummaryGeneratingListener(ClassLoader classLoader) throws Throwable { super(classLoader, "org.junit.platform.launcher.listeners.SummaryGeneratingListener"); - this.instance = this.type.newInstance(); + this.instance = this.type.getDeclaredConstructor().newInstance(); } public TestExecutionSummary getSummary() throws Throwable { diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/EnvironmentConverter.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/EnvironmentConverter.java index 1bd0eb8864..0a79361a08 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/EnvironmentConverter.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/EnvironmentConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -87,7 +87,7 @@ final class EnvironmentConverter { private StandardEnvironment createEnvironment(Class type) { try { - return type.newInstance(); + return type.getDeclaredConstructor().newInstance(); } catch (Exception ex) { return new StandardEnvironment(); diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/JasperInitializer.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/JasperInitializer.java index 3d2cd8e60e..7d80985329 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/JasperInitializer.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/JasperInitializer.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -55,7 +55,7 @@ class JasperInitializer extends AbstractLifeCycle { for (String className : INITIALIZER_CLASSES) { try { Class initializerClass = ClassUtils.forName(className, null); - return (ServletContainerInitializer) initializerClass.newInstance(); + return (ServletContainerInitializer) initializerClass.getDeclaredConstructor().newInstance(); } catch (Exception ex) { // Ignore diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactory.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactory.java index 78fc1974b5..d1ea945452 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactory.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactory.java @@ -288,7 +288,8 @@ public class TomcatServletWebServerFactory extends AbstractServletWebServerFacto private void addJasperInitializer(TomcatEmbeddedContext context) { try { ServletContainerInitializer initializer = (ServletContainerInitializer) ClassUtils - .forName("org.apache.jasper.servlet.JasperInitializer", null).newInstance(); + .forName("org.apache.jasper.servlet.JasperInitializer", null).getDeclaredConstructor() + .newInstance(); context.addServletContainerInitializer(initializer, null); } catch (Exception ex) {