diff --git a/httpclient/src/main/java/org/apache/http/impl/client/CloseableHttpResponseProxy.java b/httpclient/src/main/java/org/apache/http/impl/client/CloseableHttpResponseProxy.java index 0b2623703..7149a16e2 100644 --- a/httpclient/src/main/java/org/apache/http/impl/client/CloseableHttpResponseProxy.java +++ b/httpclient/src/main/java/org/apache/http/impl/client/CloseableHttpResponseProxy.java @@ -28,6 +28,7 @@ package org.apache.http.impl.client; import java.io.IOException; +import java.lang.reflect.Constructor; import java.lang.reflect.InvocationHandler; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -45,6 +46,17 @@ import org.apache.http.util.EntityUtils; @NotThreadSafe class CloseableHttpResponseProxy implements InvocationHandler { + private final static Constructor CONSTRUCTOR; + + static { + try { + CONSTRUCTOR = Proxy.getProxyClass(CloseableHttpResponseProxy.class.getClassLoader(), + new Class[] { CloseableHttpResponse.class }).getConstructor(new Class[] { InvocationHandler.class }); + } catch (NoSuchMethodException ex) { + throw new IllegalStateException(ex); + } + } + private final HttpResponse original; CloseableHttpResponseProxy(final HttpResponse original) { @@ -79,10 +91,15 @@ class CloseableHttpResponseProxy implements InvocationHandler { } public static CloseableHttpResponse newProxy(final HttpResponse original) { - return (CloseableHttpResponse) Proxy.newProxyInstance( - CloseableHttpResponseProxy.class.getClassLoader(), - new Class[] { CloseableHttpResponse.class }, - new CloseableHttpResponseProxy(original)); + try { + return (CloseableHttpResponse) CONSTRUCTOR.newInstance(new CloseableHttpResponseProxy(original)); + } catch (InstantiationException ex) { + throw new IllegalStateException(ex); + } catch (InvocationTargetException ex) { + throw new IllegalStateException(ex); + } catch (IllegalAccessException ex) { + throw new IllegalStateException(ex); + } } }