Re: [PR] Fix SocketFactory creation (geronimo-mail)

rzo1 (via GitHub) <[email protected]>
Newsgroups gmane.comp.java.geronimo.devel
Message-ID <PR_kwDONrBQJs6yGL-T-e7b23cc0-91ed-418f-a22a-b314b4f59da3@gitbox.apache.org>
rzo1 commented on code in PR #2:
URL: https://github.com/apache/geronimo-mail/pull/2#discussion_r2508013106


##########
geronimo-mail_2.1_impl/geronimo-mail_2.1_provider/src/main/java/org/apache/geronimo/mail/util/MailConnection.java:
##########
@@ -354,10 +354,22 @@ private boolean createSocketFromFactory(boolean ssl, boolean layer) throws IOExc
                 // done indirectly, we need to invoke the method using reflection.
                 // This retrieves a factory instance.
                 //Method getDefault = factoryClass.getMethod("getDefault", new Class[0]); //TODO check instantiation of socket factory
-                Object defFactory = factoryClass.newInstance();// getDefault.invoke(new Object(), new Object[0]);
+                // Object defFactory = factoryClass.newInstance();// getDefault.invoke(new Object(), new Object[0]);
                 // now that we have the factory, there are two different createSocket() calls we use,
                 // depending on whether we have a localAddress override.
 
+                Object defFactory = null;
+                try {
+                	defFactory = factoryClass.newInstance();
+                } catch (Throwable t) {
+                	Method getDefault = factoryClass.getMethod("getDefault", new Class[0]); //TODO check instantiation of socket factory

Review Comment:
   I would interpret it as 
   
   ```java
      Object defFactory;
                   try {
                   	defFactory = factoryClass.newInstance();
                   } catch (Throwable t) {
                       try {
                           final Method getDefault = factoryClass.getMethod("getDefault");
                           defFactory = getDefault.invoke(null); // static method
                           if (defFactory == null) {
                               throw new Exception("Factory class '" + factoryClass.getName() + "' returned null from getDefault()", t);
                           }
                       } catch (Throwable e) {
                           throw new Exception("Cannot create factory class '" + factoryClass.getName() + "' either by newInstance() or using getDefault()", t);
                       }
                   }
   ```
   
   since `getMethod(...)` and `invoke(...)` can throw.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe-nmThH5wfGryY1pf4d8vP8EB+6BGkLq7r@public.gmane.org

For queries about this service, please contact Infrastructure at:
users-pF6z+fRhj9EyzMRdD/[email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.