From f36eba457776124c207a4a12a23d48f2d5caabf8 Mon Sep 17 00:00:00 2001 From: Olivier Lamy Date: Tue, 17 Apr 2018 19:07:20 +1000 Subject: [PATCH] class.newInstance is deprecated #2435 (#2437) * Class.newInstance() is deprecated in Java 9+ #2435 Signed-off-by: olivier lamy --- .../org/eclipse/jetty/ant/JettyRunTask.java | 17 +++++++---------- .../eclipse/jetty/jaas/JAASLoginService.java | 4 ++-- .../jetty/jaas/spi/JDBCLoginModule.java | 12 ++---------- .../org/eclipse/jetty/jmx/ObjectMBean.java | 2 +- .../org/eclipse/jetty/jndi/ContextFactory.java | 4 +++- .../jasper/ContainerTldBundleDiscoverer.java | 3 ++- .../osgi/boot/jasper/JSTLBundleDiscoverer.java | 3 ++- .../utils/BundleClassLoaderHelperFactory.java | 3 ++- .../utils/BundleFileLocatorHelperFactory.java | 3 ++- .../internal/PackageAdminServiceTracker.java | 18 +----------------- .../plus/annotation/ContainerInitializer.java | 2 +- .../jetty/proxy/ProxyServletFailureTest.java | 2 +- .../jetty/proxy/ProxyServletLoadTest.java | 2 +- .../eclipse/jetty/proxy/ProxyServletTest.java | 2 +- .../jetty/security/JDBCLoginService.java | 2 +- .../jetty/server/AsyncContextState.java | 2 +- .../jetty/server/handler/ContextHandler.java | 10 +++------- .../handler/ContextHandlerCollection.java | 2 +- .../jetty/server/handler/RequestLogTest.java | 2 +- .../eclipse/jetty/servlet/FilterHolder.java | 2 +- .../jetty/servlet/ServletContextHandler.java | 2 +- .../eclipse/jetty/servlet/ServletHolder.java | 12 ++++++++++-- .../java/org/eclipse/jetty/util/ajax/JSON.java | 2 +- .../util/ajax/JSONCollectionConvertor.java | 3 ++- .../jetty/util/ajax/JSONPojoConvertor.java | 2 +- .../jetty/util/DecoratedObjectFactory.java | 6 ++++-- .../eclipse/jetty/util/IncludeExcludeSet.java | 10 +++++++--- .../java/org/eclipse/jetty/util/log/Log.java | 2 +- .../eclipse/jetty/webapp/WebAppContext.java | 2 +- .../webapp/WebAppClassLoaderUrlStreamTest.java | 2 +- .../websocket/jsr356/ClientContainer.java | 6 +++--- .../jetty/websocket/jsr356/DecoderFactory.java | 4 ++-- .../jetty/websocket/jsr356/EncoderFactory.java | 4 ++-- .../client/AnnotatedClientEndpointConfig.java | 4 ++-- .../jsr356/endpoints/OnCloseTest.java | 2 +- .../server/AnnotatedServerEndpointConfig.java | 4 ++-- .../server/ContainerDefaultConfigurator.java | 4 ++-- .../WebSocketServerContainerInitializer.java | 4 ++-- .../extensions/WebSocketExtensionFactory.java | 2 +- .../server/NativeWebSocketConfiguration.java | 4 ++-- .../server/WebSocketServerFactory.java | 2 +- .../jetty/test/HttpInputIntegrationTest.java | 4 ++-- .../jetty/DataSourceLoginServiceTest.java | 2 +- .../jetty/DatabaseLoginServiceTestServer.java | 2 +- .../src/main/java/com/acme/Dump.java | 3 ++- 45 files changed, 91 insertions(+), 100 deletions(-) diff --git a/jetty-ant/src/main/java/org/eclipse/jetty/ant/JettyRunTask.java b/jetty-ant/src/main/java/org/eclipse/jetty/ant/JettyRunTask.java index 9e9eab1f038..5da5e947f50 100644 --- a/jetty-ant/src/main/java/org/eclipse/jetty/ant/JettyRunTask.java +++ b/jetty-ant/src/main/java/org/eclipse/jetty/ant/JettyRunTask.java @@ -47,7 +47,7 @@ public class JettyRunTask extends Task private File tempDirectory; /** List of web applications to be deployed. */ - private List webapps = new ArrayList(); + private List webapps = new ArrayList<>(); /** Location of jetty.xml file. */ private File jettyXml; @@ -147,20 +147,17 @@ public class JettyRunTask extends Task { try { - this.requestLog = (RequestLog) Class.forName(className).newInstance(); - } - catch (InstantiationException e) - { - throw new BuildException("Request logger instantiation exception: " + e); - } - catch (IllegalAccessException e) - { - throw new BuildException("Request logger instantiation exception: " + e); + this.requestLog = (RequestLog) Class.forName(className).getDeclaredConstructor().newInstance(); } catch (ClassNotFoundException e) { throw new BuildException("Unknown request logger class: " + className); } + catch (Exception e) + { + throw new BuildException("Request logger instantiation exception: " + e); + } + } public String getRequestLog() diff --git a/jetty-jaas/src/main/java/org/eclipse/jetty/jaas/JAASLoginService.java b/jetty-jaas/src/main/java/org/eclipse/jetty/jaas/JAASLoginService.java index 1f0ea9e90df..e77493ba37d 100644 --- a/jetty-jaas/src/main/java/org/eclipse/jetty/jaas/JAASLoginService.java +++ b/jetty-jaas/src/main/java/org/eclipse/jetty/jaas/JAASLoginService.java @@ -206,7 +206,7 @@ public class JAASLoginService extends AbstractLifeCycle implements LoginService } else if (callback instanceof PasswordCallback) { - ((PasswordCallback)callback).setPassword((char[]) credentials.toString().toCharArray()); + ((PasswordCallback)callback).setPassword(credentials.toString().toCharArray()); } else if (callback instanceof ObjectCallback) { @@ -227,7 +227,7 @@ public class JAASLoginService extends AbstractLifeCycle implements LoginService else { Class clazz = Loader.loadClass(_callbackHandlerClass); - callbackHandler = (CallbackHandler)clazz.newInstance(); + callbackHandler = (CallbackHandler)clazz.getDeclaredConstructor().newInstance(); } //set up the login context //TODO jaspi requires we provide the Configuration parameter diff --git a/jetty-jaas/src/main/java/org/eclipse/jetty/jaas/spi/JDBCLoginModule.java b/jetty-jaas/src/main/java/org/eclipse/jetty/jaas/spi/JDBCLoginModule.java index e98a5824895..03552ec92fd 100644 --- a/jetty-jaas/src/main/java/org/eclipse/jetty/jaas/spi/JDBCLoginModule.java +++ b/jetty-jaas/src/main/java/org/eclipse/jetty/jaas/spi/JDBCLoginModule.java @@ -105,17 +105,9 @@ public class JDBCLoginModule extends AbstractDatabaseLoginModule dbPassword = ""; if (dbDriver != null) - Loader.loadClass(dbDriver).newInstance(); + Loader.loadClass(dbDriver).getDeclaredConstructor().newInstance(); } - catch (ClassNotFoundException e) - { - throw new IllegalStateException (e.toString()); - } - catch (InstantiationException e) - { - throw new IllegalStateException (e.toString()); - } - catch (IllegalAccessException e) + catch (Exception e) { throw new IllegalStateException (e.toString()); } diff --git a/jetty-jmx/src/main/java/org/eclipse/jetty/jmx/ObjectMBean.java b/jetty-jmx/src/main/java/org/eclipse/jetty/jmx/ObjectMBean.java index 7eb773d06aa..e7010719122 100644 --- a/jetty-jmx/src/main/java/org/eclipse/jetty/jmx/ObjectMBean.java +++ b/jetty-jmx/src/main/java/org/eclipse/jetty/jmx/ObjectMBean.java @@ -148,7 +148,7 @@ public class ObjectMBean implements DynamicMBean LOG.ignore(e); if (ModelMBean.class.isAssignableFrom(mClass)) { - mbean = mClass.newInstance(); + mbean = mClass.getDeclaredConstructor().newInstance(); ((ModelMBean)mbean).setManagedResource(o, "objectReference"); } } diff --git a/jetty-jndi/src/main/java/org/eclipse/jetty/jndi/ContextFactory.java b/jetty-jndi/src/main/java/org/eclipse/jetty/jndi/ContextFactory.java index afe82262265..143efa9e5f9 100644 --- a/jetty-jndi/src/main/java/org/eclipse/jetty/jndi/ContextFactory.java +++ b/jetty-jndi/src/main/java/org/eclipse/jetty/jndi/ContextFactory.java @@ -187,7 +187,9 @@ public class ContextFactory implements ObjectFactory Reference ref = (Reference)obj; StringRefAddr parserAddr = (StringRefAddr)ref.get("parser"); String parserClassName = (parserAddr==null?null:(String)parserAddr.getContent()); - NameParser parser = (NameParser)(parserClassName==null?null:loader.loadClass(parserClassName).newInstance()); + NameParser parser = + (NameParser)(parserClassName==null? + null:loader.loadClass(parserClassName).getDeclaredConstructor().newInstance()); return new NamingContext (env, name.get(0), diff --git a/jetty-osgi/jetty-osgi-boot-jsp/src/main/java/org/eclipse/jetty/osgi/boot/jasper/ContainerTldBundleDiscoverer.java b/jetty-osgi/jetty-osgi-boot-jsp/src/main/java/org/eclipse/jetty/osgi/boot/jasper/ContainerTldBundleDiscoverer.java index 4c8ed392b91..dffeb72466a 100644 --- a/jetty-osgi/jetty-osgi-boot-jsp/src/main/java/org/eclipse/jetty/osgi/boot/jasper/ContainerTldBundleDiscoverer.java +++ b/jetty-osgi/jetty-osgi-boot-jsp/src/main/java/org/eclipse/jetty/osgi/boot/jasper/ContainerTldBundleDiscoverer.java @@ -182,7 +182,8 @@ public class ContainerTldBundleDiscoverer implements TldBundleDiscoverer // Class.getForName("org.apache.jasper.runtime.JspFactoryImpl") // however its bundles does not import the jasper package // so it fails. let's help things out: - fact = (JspFactory) JettyBootstrapActivator.class.getClassLoader().loadClass(DEFAULT_JSP_FACTORY_IMPL_CLASS).newInstance(); + fact = (JspFactory) JettyBootstrapActivator.class.getClassLoader() + .loadClass(DEFAULT_JSP_FACTORY_IMPL_CLASS).getDeclaredConstructor().newInstance(); JspFactory.setDefaultFactory(fact); } } diff --git a/jetty-osgi/jetty-osgi-boot-jsp/src/main/java/org/eclipse/jetty/osgi/boot/jasper/JSTLBundleDiscoverer.java b/jetty-osgi/jetty-osgi-boot-jsp/src/main/java/org/eclipse/jetty/osgi/boot/jasper/JSTLBundleDiscoverer.java index 7da3ca590f3..b9670dd06cb 100644 --- a/jetty-osgi/jetty-osgi-boot-jsp/src/main/java/org/eclipse/jetty/osgi/boot/jasper/JSTLBundleDiscoverer.java +++ b/jetty-osgi/jetty-osgi-boot-jsp/src/main/java/org/eclipse/jetty/osgi/boot/jasper/JSTLBundleDiscoverer.java @@ -93,7 +93,8 @@ public class JSTLBundleDiscoverer implements TldBundleDiscoverer // Class.getForName("org.apache.jasper.runtime.JspFactoryImpl") // however its bundles does not import the jasper package // so it fails. let's help things out: - fact = (JspFactory) JettyBootstrapActivator.class.getClassLoader().loadClass(DEFAULT_JSP_FACTORY_IMPL_CLASS).newInstance(); + fact = (JspFactory) JettyBootstrapActivator.class.getClassLoader() + .loadClass(DEFAULT_JSP_FACTORY_IMPL_CLASS).getDeclaredConstructor().newInstance(); JspFactory.setDefaultFactory(fact); } diff --git a/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/utils/BundleClassLoaderHelperFactory.java b/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/utils/BundleClassLoaderHelperFactory.java index 2e3a697b6ea..a3f8213561c 100644 --- a/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/utils/BundleClassLoaderHelperFactory.java +++ b/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/utils/BundleClassLoaderHelperFactory.java @@ -52,7 +52,8 @@ public class BundleClassLoaderHelperFactory try { //if a fragment has not provided their own impl - helper = (BundleClassLoaderHelper) Class.forName(BundleClassLoaderHelper.CLASS_NAME).newInstance(); + helper = (BundleClassLoaderHelper) Class.forName(BundleClassLoaderHelper.CLASS_NAME) + .getDeclaredConstructor().newInstance(); } catch (Throwable t) { diff --git a/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/utils/BundleFileLocatorHelperFactory.java b/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/utils/BundleFileLocatorHelperFactory.java index 0db935896a7..7ed0cec8a62 100644 --- a/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/utils/BundleFileLocatorHelperFactory.java +++ b/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/utils/BundleFileLocatorHelperFactory.java @@ -45,7 +45,8 @@ public class BundleFileLocatorHelperFactory try { //see if a fragment has supplied an alternative - helper = (BundleFileLocatorHelper) Class.forName(BundleFileLocatorHelper.CLASS_NAME).newInstance(); + helper = (BundleFileLocatorHelper) Class.forName(BundleFileLocatorHelper.CLASS_NAME) + .getDeclaredConstructor().newInstance(); } catch (Throwable t) { diff --git a/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/utils/internal/PackageAdminServiceTracker.java b/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/utils/internal/PackageAdminServiceTracker.java index 97b38c37a08..50616767cee 100644 --- a/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/utils/internal/PackageAdminServiceTracker.java +++ b/jetty-osgi/jetty-osgi-boot/src/main/java/org/eclipse/jetty/osgi/boot/utils/internal/PackageAdminServiceTracker.java @@ -280,27 +280,11 @@ public class PackageAdminServiceTracker implements ServiceListener Class c = Class.forName(fragmentActivator); if (c != null) { - BundleActivator bActivator = (BundleActivator) c.newInstance(); + BundleActivator bActivator = (BundleActivator) c.getDeclaredConstructor().newInstance(); bActivator.start(_context); _activatedFragments.add(bActivator); } } - catch (NullPointerException e) - { - // e.printStackTrace(); - } - catch (InstantiationException e) - { - // e.printStackTrace(); - } - catch (IllegalAccessException e) - { - // e.printStackTrace(); - } - catch (ClassNotFoundException e) - { - // e.printStackTrace(); - } catch (Exception e) { e.printStackTrace(); diff --git a/jetty-plus/src/main/java/org/eclipse/jetty/plus/annotation/ContainerInitializer.java b/jetty-plus/src/main/java/org/eclipse/jetty/plus/annotation/ContainerInitializer.java index 553f003b4f6..fd11711372d 100644 --- a/jetty-plus/src/main/java/org/eclipse/jetty/plus/annotation/ContainerInitializer.java +++ b/jetty-plus/src/main/java/org/eclipse/jetty/plus/annotation/ContainerInitializer.java @@ -61,7 +61,7 @@ public class ContainerInitializer try { - _target = (ServletContainerInitializer)loader.loadClass(m.group(1)).newInstance(); + _target = (ServletContainerInitializer)loader.loadClass(m.group(1)).getDeclaredConstructor().newInstance(); String[] interested = StringUtil.arrayFromString(m.group(2)); _interestedTypes = new Class[interested.length]; for (int i=0;i proxyServletClass) throws Exception { - this.proxyServlet = (ProxyServlet)proxyServletClass.newInstance(); + this.proxyServlet = (ProxyServlet)proxyServletClass.getDeclaredConstructor().newInstance(); } private void prepareProxy() throws Exception diff --git a/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/ProxyServletLoadTest.java b/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/ProxyServletLoadTest.java index 9e7256904d1..fe914175eae 100644 --- a/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/ProxyServletLoadTest.java +++ b/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/ProxyServletLoadTest.java @@ -77,7 +77,7 @@ public class ProxyServletLoadTest public ProxyServletLoadTest(Class proxyServletClass) throws Exception { - proxyServlet = (AbstractProxyServlet)proxyServletClass.newInstance(); + proxyServlet = (AbstractProxyServlet)proxyServletClass.getDeclaredConstructor().newInstance(); } private void startServer(HttpServlet servlet) throws Exception diff --git a/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/ProxyServletTest.java b/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/ProxyServletTest.java index f7b455e6fec..fa38db732e8 100644 --- a/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/ProxyServletTest.java +++ b/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/ProxyServletTest.java @@ -126,7 +126,7 @@ public class ProxyServletTest public ProxyServletTest(Class proxyServletClass) throws Exception { - this.proxyServlet = (AbstractProxyServlet)proxyServletClass.newInstance(); + this.proxyServlet = (AbstractProxyServlet)proxyServletClass.getDeclaredConstructor().newInstance(); } private void startServer(HttpServlet servlet) throws Exception diff --git a/jetty-security/src/main/java/org/eclipse/jetty/security/JDBCLoginService.java b/jetty-security/src/main/java/org/eclipse/jetty/security/JDBCLoginService.java index 8f08a6f1d72..8d0b2f9b4e9 100644 --- a/jetty-security/src/main/java/org/eclipse/jetty/security/JDBCLoginService.java +++ b/jetty-security/src/main/java/org/eclipse/jetty/security/JDBCLoginService.java @@ -170,7 +170,7 @@ public class JDBCLoginService extends AbstractLoginService + " = u." + _userRoleTableRoleKey; - Loader.loadClass(_jdbcDriver).newInstance(); + Loader.loadClass(_jdbcDriver).getDeclaredConstructor().newInstance(); super.doStart(); } diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/AsyncContextState.java b/jetty-server/src/main/java/org/eclipse/jetty/server/AsyncContextState.java index 19c6b9a68e7..4498db8f6e4 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/AsyncContextState.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/AsyncContextState.java @@ -107,7 +107,7 @@ public class AsyncContextState implements AsyncContext return contextHandler.getServletContext().createListener(clazz); try { - return clazz.newInstance(); + return clazz.getDeclaredConstructor().newInstance(); } catch (Exception e) { diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java index a43a7480ed4..40dc0af0e0b 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java @@ -2532,7 +2532,7 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu public T createInstance (Class clazz) throws Exception { - T o = clazz.newInstance(); + T o = clazz.getDeclaredConstructor().newInstance(); return o; } @@ -2829,13 +2829,9 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu { try { - return clazz.newInstance(); + return clazz.getDeclaredConstructor().newInstance(); } - catch (InstantiationException e) - { - throw new ServletException(e); - } - catch (IllegalAccessException e) + catch (Exception e) { throw new ServletException(e); } diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandlerCollection.java b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandlerCollection.java index 3697f855255..82bdf32303f 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandlerCollection.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandlerCollection.java @@ -247,7 +247,7 @@ public class ContextHandlerCollection extends HandlerCollection { try { - ContextHandler context = _contextClass.newInstance(); + ContextHandler context = _contextClass.getDeclaredConstructor().newInstance(); context.setContextPath(contextPath); context.setResourceBase(resourceBase); addHandler(context); diff --git a/jetty-server/src/test/java/org/eclipse/jetty/server/handler/RequestLogTest.java b/jetty-server/src/test/java/org/eclipse/jetty/server/handler/RequestLogTest.java index 15ca1e21f3b..7e0f8e7ddde 100644 --- a/jetty-server/src/test/java/org/eclipse/jetty/server/handler/RequestLogTest.java +++ b/jetty-server/src/test/java/org/eclipse/jetty/server/handler/RequestLogTest.java @@ -327,7 +327,7 @@ public class RequestLogTest { try { - throw (Throwable)(Class.forName(value).newInstance()); + throw (Throwable)(Class.forName(value).getDeclaredConstructor().newInstance()); } catch(ServletException | IOException | Error | RuntimeException e) { diff --git a/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/FilterHolder.java b/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/FilterHolder.java index 9fb7462628a..9e214dd7045 100644 --- a/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/FilterHolder.java +++ b/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/FilterHolder.java @@ -120,7 +120,7 @@ public class FilterHolder extends Holder ServletContext context=_servletHandler.getServletContext(); _filter=(context instanceof ServletContextHandler.Context) ?((ServletContextHandler.Context)context).createFilter(getHeldClass()) - :getHeldClass().newInstance(); + :getHeldClass().getDeclaredConstructor().newInstance(); } catch (ServletException se) { diff --git a/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/ServletContextHandler.java b/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/ServletContextHandler.java index 31edea23d58..ec0b6e853d2 100644 --- a/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/ServletContextHandler.java +++ b/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/ServletContextHandler.java @@ -327,7 +327,7 @@ public class ServletContextHandler extends ContextHandler { try { - return (SecurityHandler)_defaultSecurityHandlerClass.newInstance(); + return _defaultSecurityHandlerClass.getDeclaredConstructor().newInstance(); } catch(Exception e) { diff --git a/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/ServletHolder.java b/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/ServletHolder.java index a2aaf7fe9c5..e4681ab1a52 100644 --- a/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/ServletHolder.java +++ b/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/ServletHolder.java @@ -20,6 +20,7 @@ package org.eclipse.jetty.servlet; import java.io.File; import java.io.IOException; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Arrays; @@ -1283,15 +1284,18 @@ public class ServletHolder extends Holder implements UserIdentity.Scope * @throws ServletException if unable to create a new instance * @throws IllegalAccessException if not allowed to create a new instance * @throws InstantiationException if creating new instance resulted in error + * @throws NoSuchMethodException if creating new instance resulted in error + * @throws InvocationTargetException If creating new instance throws an exception */ - protected Servlet newInstance() throws ServletException, IllegalAccessException, InstantiationException + protected Servlet newInstance() throws ServletException, IllegalAccessException, InstantiationException, + NoSuchMethodException, InvocationTargetException { try { ServletContext ctx = getServletHandler().getServletContext(); if (ctx instanceof ServletContextHandler.Context) return ((ServletContextHandler.Context)ctx).createServlet(getHeldClass()); - return getHeldClass().newInstance(); + return getHeldClass().getDeclaredConstructor().newInstance(); } catch (ServletException se) { @@ -1300,6 +1304,10 @@ public class ServletHolder extends Holder implements UserIdentity.Scope throw (InstantiationException)cause; if (cause instanceof IllegalAccessException) throw (IllegalAccessException)cause; + if (cause instanceof NoSuchMethodException) + throw (NoSuchMethodException)cause; + if (cause instanceof InvocationTargetException) + throw (InvocationTargetException)cause; throw se; } } diff --git a/jetty-util-ajax/src/main/java/org/eclipse/jetty/util/ajax/JSON.java b/jetty-util-ajax/src/main/java/org/eclipse/jetty/util/ajax/JSON.java index d3f088e9ab0..f1dad0ed0a8 100644 --- a/jetty-util-ajax/src/main/java/org/eclipse/jetty/util/ajax/JSON.java +++ b/jetty-util-ajax/src/main/java/org/eclipse/jetty/util/ajax/JSON.java @@ -709,7 +709,7 @@ public class JSON { try { - Convertible conv = (Convertible)type.newInstance(); + Convertible conv = (Convertible)type.getDeclaredConstructor().newInstance(); conv.fromJSON(map); return conv; } diff --git a/jetty-util-ajax/src/main/java/org/eclipse/jetty/util/ajax/JSONCollectionConvertor.java b/jetty-util-ajax/src/main/java/org/eclipse/jetty/util/ajax/JSONCollectionConvertor.java index a92b3202261..93c7254005c 100644 --- a/jetty-util-ajax/src/main/java/org/eclipse/jetty/util/ajax/JSONCollectionConvertor.java +++ b/jetty-util-ajax/src/main/java/org/eclipse/jetty/util/ajax/JSONCollectionConvertor.java @@ -38,7 +38,8 @@ public class JSONCollectionConvertor implements JSON.Convertor { try { - Collection result = (Collection)Loader.loadClass((String)object.get("class")).newInstance(); + Collection result = (Collection)Loader.loadClass((String)object.get("class")) + .getDeclaredConstructor().newInstance(); Collections.addAll(result, (Object[])object.get("list")); return result; } diff --git a/jetty-util-ajax/src/main/java/org/eclipse/jetty/util/ajax/JSONPojoConvertor.java b/jetty-util-ajax/src/main/java/org/eclipse/jetty/util/ajax/JSONPojoConvertor.java index a715b1451fb..98c5aff4f38 100644 --- a/jetty-util-ajax/src/main/java/org/eclipse/jetty/util/ajax/JSONPojoConvertor.java +++ b/jetty-util-ajax/src/main/java/org/eclipse/jetty/util/ajax/JSONPojoConvertor.java @@ -182,7 +182,7 @@ public class JSONPojoConvertor implements JSON.Convertor Object obj = null; try { - obj = _pojoClass.newInstance(); + obj = _pojoClass.getDeclaredConstructor().newInstance(); } catch(Exception e) { diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/DecoratedObjectFactory.java b/jetty-util/src/main/java/org/eclipse/jetty/util/DecoratedObjectFactory.java index fd7241bcdb0..963bd1f7a8a 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/DecoratedObjectFactory.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/DecoratedObjectFactory.java @@ -18,6 +18,7 @@ package org.eclipse.jetty.util; +import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; @@ -58,13 +59,14 @@ public class DecoratedObjectFactory implements Iterable this.decorators.clear(); } - public T createInstance(Class clazz) throws InstantiationException, IllegalAccessException + public T createInstance(Class clazz) throws InstantiationException, IllegalAccessException, + NoSuchMethodException, InvocationTargetException { if (LOG.isDebugEnabled()) { LOG.debug("Creating Instance: " + clazz); } - T o = clazz.newInstance(); + T o = clazz.getDeclaredConstructor().newInstance(); return decorate(o); } diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/IncludeExcludeSet.java b/jetty-util/src/main/java/org/eclipse/jetty/util/IncludeExcludeSet.java index b53bc218aa3..73cb2583cfd 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/IncludeExcludeSet.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/IncludeExcludeSet.java @@ -77,8 +77,8 @@ public class IncludeExcludeSet implements Predicate

{ try { - _includes = setClass.newInstance(); - _excludes = setClass.newInstance(); + _includes = setClass.getDeclaredConstructor().newInstance(); + _excludes = setClass.getDeclaredConstructor().newInstance(); if(_includes instanceof Predicate) { @@ -98,7 +98,11 @@ public class IncludeExcludeSet implements Predicate

_excludePredicate = new SetContainsPredicate(_excludes); } } - catch (InstantiationException | IllegalAccessException e) + catch (RuntimeException e) + { + throw e; + } + catch (Exception e) { throw new RuntimeException(e); } diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/log/Log.java b/jetty-util/src/main/java/org/eclipse/jetty/util/log/Log.java index df810437201..5b8c636be6b 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/log/Log.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/log/Log.java @@ -175,7 +175,7 @@ public class Log Class log_class = __logClass==null?null:Loader.loadClass(Log.class,__logClass); if (LOG == null || (log_class!=null && !LOG.getClass().equals(log_class))) { - LOG = (Logger)log_class.newInstance(); + LOG = (Logger)log_class.getDeclaredConstructor().newInstance(); if(announce) { LOG.debug("Logging to {} via {}", LOG, log_class.getName()); diff --git a/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/WebAppContext.java b/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/WebAppContext.java index 001a5f62570..c3594d29bf3 100644 --- a/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/WebAppContext.java +++ b/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/WebAppContext.java @@ -1032,7 +1032,7 @@ public class WebAppContext extends ServletContextHandler implements WebAppClassL _configurationClasses.addAll(Configuration.ClassList.serverDefault(getServer())); } for (String configClass : _configurationClasses) - _configurations.add((Configuration)Loader.loadClass(configClass).newInstance()); + _configurations.add((Configuration)Loader.loadClass(configClass).getDeclaredConstructor().newInstance()); } /* ------------------------------------------------------------ */ diff --git a/jetty-webapp/src/test/java/org/eclipse/jetty/webapp/WebAppClassLoaderUrlStreamTest.java b/jetty-webapp/src/test/java/org/eclipse/jetty/webapp/WebAppClassLoaderUrlStreamTest.java index fc91e67a575..9637585fbfc 100644 --- a/jetty-webapp/src/test/java/org/eclipse/jetty/webapp/WebAppClassLoaderUrlStreamTest.java +++ b/jetty-webapp/src/test/java/org/eclipse/jetty/webapp/WebAppClassLoaderUrlStreamTest.java @@ -61,7 +61,7 @@ public class WebAppClassLoaderUrlStreamTest extends WebAppClassLoaderTest try { Class clazz = Class.forName(className, false, classLoader); - handler = (URLStreamHandler) clazz.newInstance(); + handler = (URLStreamHandler) clazz.getDeclaredConstructor().newInstance(); break; } catch (Exception ignore) diff --git a/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/ClientContainer.java b/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/ClientContainer.java index 17bedc87b06..c3de70d4024 100644 --- a/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/ClientContainer.java +++ b/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/ClientContainer.java @@ -419,11 +419,11 @@ public class ClientContainer extends ContainerLifeCycle implements WebSocketCont { try { - return newClientEndpointInstance(endpointClass.newInstance(),config); + return newClientEndpointInstance(endpointClass.getDeclaredConstructor().newInstance(),config); } - catch (InstantiationException | IllegalAccessException e) + catch (Exception e) { - throw new InvalidWebSocketException("Unable to instantiate websocket: " + endpointClass.getClass()); + throw new InvalidWebSocketException("Unable to instantiate websocket: " + endpointClass.getClass(), e); } } diff --git a/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/DecoderFactory.java b/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/DecoderFactory.java index 1b84276fd04..2846636b8a3 100644 --- a/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/DecoderFactory.java +++ b/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/DecoderFactory.java @@ -194,9 +194,9 @@ public class DecoderFactory implements Configurable Decoder decoder = containerScope.getObjectFactory().createInstance(decoderClass); return new Wrapper(decoder,metadata); } - catch (InstantiationException | IllegalAccessException e) + catch (Exception e) { - throw new IllegalStateException("Unable to instantiate Decoder: " + decoderClass.getName()); + throw new IllegalStateException("Unable to instantiate Decoder: " + decoderClass.getName(), e); } } } diff --git a/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/EncoderFactory.java b/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/EncoderFactory.java index 6ad8a7efd33..3c002d5e2f0 100644 --- a/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/EncoderFactory.java +++ b/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/EncoderFactory.java @@ -181,9 +181,9 @@ public class EncoderFactory implements Configurable Encoder encoder = containerScope.getObjectFactory().createInstance(encoderClass); return new Wrapper(encoder,metadata); } - catch (InstantiationException | IllegalAccessException e) + catch (Exception e) { - throw new IllegalStateException("Unable to instantiate Encoder: " + encoderClass.getName()); + throw new IllegalStateException("Unable to instantiate Encoder: " + encoderClass.getName(),e); } } } diff --git a/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/client/AnnotatedClientEndpointConfig.java b/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/client/AnnotatedClientEndpointConfig.java index 0bbdcfde9ea..5625e238e59 100644 --- a/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/client/AnnotatedClientEndpointConfig.java +++ b/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/client/AnnotatedClientEndpointConfig.java @@ -60,9 +60,9 @@ public class AnnotatedClientEndpointConfig implements ClientEndpointConfig { try { - this.configurator = anno.configurator().newInstance(); + this.configurator = anno.configurator().getDeclaredConstructor( ).newInstance(); } - catch (InstantiationException | IllegalAccessException e) + catch (Exception e) { StringBuilder err = new StringBuilder(); err.append("Unable to instantiate ClientEndpoint.configurator() of "); diff --git a/jetty-websocket/javax-websocket-client-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/endpoints/OnCloseTest.java b/jetty-websocket/javax-websocket-client-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/endpoints/OnCloseTest.java index 446871fbcb8..3b1af127aea 100644 --- a/jetty-websocket/javax-websocket-client-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/endpoints/OnCloseTest.java +++ b/jetty-websocket/javax-websocket-client-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/endpoints/OnCloseTest.java @@ -109,7 +109,7 @@ public class OnCloseTest // Build up EventDriver WebSocketPolicy policy = WebSocketPolicy.newClientPolicy(); ClientEndpointConfig config = metadata.getConfig(); - TrackingSocket endpoint = (TrackingSocket)testcase.closeClass.newInstance(); + TrackingSocket endpoint = (TrackingSocket)testcase.closeClass.getDeclaredConstructor().newInstance(); EndpointInstance ei = new EndpointInstance(endpoint,config,metadata); JsrEvents jsrevents = new JsrEvents<>(metadata); diff --git a/jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/AnnotatedServerEndpointConfig.java b/jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/AnnotatedServerEndpointConfig.java index e58705bac99..2e54ea644c1 100644 --- a/jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/AnnotatedServerEndpointConfig.java +++ b/jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/AnnotatedServerEndpointConfig.java @@ -129,9 +129,9 @@ public class AnnotatedServerEndpointConfig implements ServerEndpointConfig { try { - cfgr = anno.configurator().newInstance(); + cfgr = anno.configurator().getDeclaredConstructor( ).newInstance(); } - catch (InstantiationException | IllegalAccessException e) + catch (Exception e) { StringBuilder err = new StringBuilder(); err.append("Unable to instantiate ClientEndpoint.configurator() of "); diff --git a/jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/ContainerDefaultConfigurator.java b/jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/ContainerDefaultConfigurator.java index 7e6656d401e..a4b7e6052b9 100644 --- a/jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/ContainerDefaultConfigurator.java +++ b/jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/ContainerDefaultConfigurator.java @@ -69,9 +69,9 @@ public final class ContainerDefaultConfigurator extends Configurator { // Since this is started via a ServiceLoader, this class has no Scope or context // that can be used to obtain a ObjectFactory from. - return endpointClass.newInstance(); + return endpointClass.getDeclaredConstructor().newInstance(); } - catch (IllegalAccessException e) + catch (Exception e) { throw new InstantiationException(String.format("%s: %s",e.getClass().getName(),e.getMessage())); } diff --git a/jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/deploy/WebSocketServerContainerInitializer.java b/jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/deploy/WebSocketServerContainerInitializer.java index 209d2af6689..4c8a0d7b172 100644 --- a/jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/deploy/WebSocketServerContainerInitializer.java +++ b/jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/deploy/WebSocketServerContainerInitializer.java @@ -255,7 +255,7 @@ public class WebSocketServerContainerInitializer implements ServletContainerInit } try { - ServerApplicationConfig config = clazz.newInstance(); + ServerApplicationConfig config = clazz.getDeclaredConstructor( ).newInstance(); Set seconfigs = config.getEndpointConfigs(discoveredExtendedEndpoints); if (seconfigs != null) @@ -271,7 +271,7 @@ public class WebSocketServerContainerInitializer implements ServletContainerInit deployableAnnotatedEndpoints.addAll(annotatedClasses); } } - catch (InstantiationException | IllegalAccessException e) + catch (Exception e) { throw new ServletException("Unable to instantiate: " + clazz.getName(),e); } diff --git a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/extensions/WebSocketExtensionFactory.java b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/extensions/WebSocketExtensionFactory.java index 1197b65c92f..1b81f7f7429 100644 --- a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/extensions/WebSocketExtensionFactory.java +++ b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/extensions/WebSocketExtensionFactory.java @@ -66,7 +66,7 @@ public class WebSocketExtensionFactory extends ExtensionFactory } return ext; } - catch (InstantiationException | IllegalAccessException e) + catch (Exception e) { throw new WebSocketException("Cannot instantiate extension: " + extClass,e); } diff --git a/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/NativeWebSocketConfiguration.java b/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/NativeWebSocketConfiguration.java index bdfe0816507..d1acab21594 100644 --- a/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/NativeWebSocketConfiguration.java +++ b/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/NativeWebSocketConfiguration.java @@ -157,9 +157,9 @@ public class NativeWebSocketConfiguration extends ContainerLifeCycle implements { try { - return endpointClass.newInstance(); + return endpointClass.getDeclaredConstructor().newInstance(); } - catch (InstantiationException | IllegalAccessException e) + catch (Exception e) { throw new WebSocketException("Unable to create instance of " + endpointClass.getName(), e); } diff --git a/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketServerFactory.java b/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketServerFactory.java index cd3b9149d6e..19d7fa323f7 100644 --- a/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketServerFactory.java +++ b/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketServerFactory.java @@ -294,7 +294,7 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc { return objectFactory.createInstance(firstClass); } - catch (InstantiationException | IllegalAccessException e) + catch (Exception e) { throw new WebSocketException("Unable to create instance of " + firstClass, e); } diff --git a/tests/test-integration/src/test/java/org/eclipse/jetty/test/HttpInputIntegrationTest.java b/tests/test-integration/src/test/java/org/eclipse/jetty/test/HttpInputIntegrationTest.java index 42a6e2847eb..23732fa4c59 100644 --- a/tests/test-integration/src/test/java/org/eclipse/jetty/test/HttpInputIntegrationTest.java +++ b/tests/test-integration/src/test/java/org/eclipse/jetty/test/HttpInputIntegrationTest.java @@ -329,7 +329,7 @@ public class HttpInputIntegrationTest { System.err.printf("[%d] TEST c=%s, m=%s, delayDispatch=%b delayInFrame=%s content-length:%d expect=%d read=%d content:%s%n",_id,_client.getSimpleName(),_mode,__config.isDelayDispatchUntilContent(),_delay,_length,_status,_read,_send); - TestClient client=_client.newInstance(); + TestClient client=_client.getDeclaredConstructor().newInstance(); String response = client.send("/ctx/test?mode="+_mode,50,_delay,_length,_send); int sum=0; @@ -368,7 +368,7 @@ public class HttpInputIntegrationTest { try { - TestClient client=_client.newInstance(); + TestClient client=_client.getDeclaredConstructor().newInstance(); for (int j=0;j