Skip to content

Commit 9999659

Browse files
committed
Fix Object Factory to check class type when instantiating an object from class name
1 parent 0c956c7 commit 9999659

File tree

4 files changed

+7
-7
lines changed

4 files changed

+7
-7
lines changed

src/main/java/com/amazon/redshift/core/SocketFactoryFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static SocketFactory getSocketFactory(Properties info) throws RedshiftExc
3838
return SocketFactory.getDefault();
3939
}
4040
try {
41-
return (SocketFactory) ObjectFactory.instantiate(socketFactoryClassName, info, true,
41+
return ObjectFactory.instantiate(SocketFactory.class, socketFactoryClassName, info, true,
4242
RedshiftProperty.SOCKET_FACTORY_ARG.get(info));
4343
} catch (Exception e) {
4444
throw new RedshiftException(
@@ -66,7 +66,7 @@ public static SSLSocketFactory getSslSocketFactory(Properties info) throws Redsh
6666
if (classname.equals(RedshiftConnectionImpl.NON_VALIDATING_SSL_FACTORY))
6767
classname = NonValidatingFactory.class.getName();
6868

69-
return (SSLSocketFactory) ObjectFactory.instantiate(classname, info, true,
69+
return ObjectFactory.instantiate(SSLSocketFactory.class, classname, info, true,
7070
RedshiftProperty.SSL_FACTORY_ARG.get(info));
7171
} catch (Exception e) {
7272
throw new RedshiftException(

src/main/java/com/amazon/redshift/ssl/LibPQFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ private CallbackHandler getCallbackHandler(Properties info) throws RedshiftExcep
6161
String sslpasswordcallback = RedshiftProperty.SSL_PASSWORD_CALLBACK.get(info);
6262
if (sslpasswordcallback != null) {
6363
try {
64-
cbh = (CallbackHandler) ObjectFactory.instantiate(sslpasswordcallback, info, false, null);
64+
cbh = ObjectFactory.instantiate(CallbackHandler.class,sslpasswordcallback, info, false, null);
6565
} catch (Exception e) {
6666
throw new RedshiftException(
6767
GT.tr("The password callback class provided {0} could not be instantiated.",

src/main/java/com/amazon/redshift/ssl/MakeSSL.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ private static void verifyPeerName(RedshiftStream stream, Properties info, SSLSo
5959
sslhostnameverifier = "RedshiftjdbcHostnameVerifier";
6060
} else {
6161
try {
62-
hvn = (HostnameVerifier) instantiate(sslhostnameverifier, info, false, null);
62+
hvn = instantiate(HostnameVerifier.class, sslhostnameverifier, info, false, null);
6363
} catch (Exception e) {
6464
throw new RedshiftException(
6565
GT.tr("The HostnameVerifier class provided {0} could not be instantiated.",

src/main/java/com/amazon/redshift/util/ObjectFactory.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ public class ObjectFactory {
3434
* @throws IllegalAccessException if something goes wrong
3535
* @throws InvocationTargetException if something goes wrong
3636
*/
37-
public static Object instantiate(String classname, Properties info, boolean tryString,
37+
public static <T> T instantiate(Class<T> expectedClass, String classname, Properties info, boolean tryString,
3838
String stringarg) throws ClassNotFoundException, SecurityException, NoSuchMethodException,
3939
IllegalArgumentException, InstantiationException, IllegalAccessException,
4040
InvocationTargetException {
4141
Object[] args = {info};
42-
Constructor<?> ctor = null;
43-
Class<?> cls = Class.forName(classname);
42+
Constructor<? extends T> ctor = null;
43+
Class<? extends T> cls = Class.forName(classname).asSubclass(expectedClass);
4444
try {
4545
ctor = cls.getConstructor(Properties.class);
4646
} catch (NoSuchMethodException nsme) {

0 commit comments

Comments
 (0)