Delay SSLSocketImpl instantiation until needed DO NOT MERGE

Class preloading will create an instance of objects if they are in
static fields, so put the ones we don't want instantiated into a holder
class that is not preloaded.

(cherry picked from commit da5b7116b58795b169961cbd63c2b21bac741d9a)

Bug: 9984058
Change-Id: Ifbf7b7a3668f19ca21fa66a486ae557086a195b5
diff --git a/luni/src/main/java/javax/net/ssl/HttpsURLConnection.java b/luni/src/main/java/javax/net/ssl/HttpsURLConnection.java
index 9803f3d..ab86a9b 100644
--- a/luni/src/main/java/javax/net/ssl/HttpsURLConnection.java
+++ b/luni/src/main/java/javax/net/ssl/HttpsURLConnection.java
@@ -104,11 +104,16 @@
  * connection will be retried with SSLv3 only.
  */
 public abstract class HttpsURLConnection extends HttpURLConnection {
+    /*
+     * Holds default instances so class preloading doesn't create an instance of
+     * it.
+     */
+    private static class NoPreloadHolder {
+        public static HostnameVerifier defaultHostnameVerifier = new DefaultHostnameVerifier();
 
-    private static HostnameVerifier defaultHostnameVerifier = new DefaultHostnameVerifier();
-
-    private static SSLSocketFactory defaultSSLSocketFactory = (SSLSocketFactory) SSLSocketFactory
-            .getDefault();
+        public static SSLSocketFactory defaultSSLSocketFactory = (SSLSocketFactory) SSLSocketFactory
+                .getDefault();
+    }
 
     /**
      * Sets the default hostname verifier to be used by new instances.
@@ -122,7 +127,7 @@
         if (v == null) {
             throw new IllegalArgumentException("HostnameVerifier is null");
         }
-        defaultHostnameVerifier = v;
+        NoPreloadHolder.defaultHostnameVerifier = v;
     }
 
     /**
@@ -131,7 +136,7 @@
      * @return the default hostname verifier.
      */
     public static HostnameVerifier getDefaultHostnameVerifier() {
-        return defaultHostnameVerifier;
+        return NoPreloadHolder.defaultHostnameVerifier;
     }
 
     /**
@@ -146,7 +151,7 @@
         if (sf == null) {
             throw new IllegalArgumentException("SSLSocketFactory is null");
         }
-        defaultSSLSocketFactory = sf;
+        NoPreloadHolder.defaultSSLSocketFactory = sf;
     }
 
     /**
@@ -155,7 +160,7 @@
      * @return the default SSL socket factory for new instances.
      */
     public static SSLSocketFactory getDefaultSSLSocketFactory() {
-        return defaultSSLSocketFactory;
+        return NoPreloadHolder.defaultSSLSocketFactory;
     }
 
     /**
@@ -176,8 +181,8 @@
      */
     protected HttpsURLConnection(URL url) {
         super(url);
-        hostnameVerifier = defaultHostnameVerifier;
-        sslSocketFactory = defaultSSLSocketFactory;
+        hostnameVerifier = NoPreloadHolder.defaultHostnameVerifier;
+        sslSocketFactory = NoPreloadHolder.defaultSSLSocketFactory;
     }
 
     /**
diff --git a/luni/src/main/java/org/apache/harmony/xnet/provider/jsse/OpenSSLSocketImpl.java b/luni/src/main/java/org/apache/harmony/xnet/provider/jsse/OpenSSLSocketImpl.java
index ced6310..ea56afb 100644
--- a/luni/src/main/java/org/apache/harmony/xnet/provider/jsse/OpenSSLSocketImpl.java
+++ b/luni/src/main/java/org/apache/harmony/xnet/provider/jsse/OpenSSLSocketImpl.java
@@ -726,7 +726,7 @@
             } catch (IOException e) {
                 // return an invalid session with
                 // invalid cipher suite of "SSL_NULL_WITH_NULL_NULL"
-                return SSLSessionImpl.NULL_SESSION;
+                return SSLSessionImpl.getNullSession();
             }
         }
         return sslSession;
diff --git a/luni/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLEngineImpl.java b/luni/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLEngineImpl.java
index af03325..7f01c8a 100644
--- a/luni/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLEngineImpl.java
+++ b/luni/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLEngineImpl.java
@@ -370,7 +370,7 @@
         if (session != null) {
             return session;
         }
-        return SSLSessionImpl.NULL_SESSION;
+        return SSLSessionImpl.getNullSession();
     }
 
     /**
diff --git a/luni/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLSessionImpl.java b/luni/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLSessionImpl.java
index 247c6ee..46834ad 100644
--- a/luni/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLSessionImpl.java
+++ b/luni/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLSessionImpl.java
@@ -33,8 +33,13 @@
 
 public final class SSLSessionImpl implements SSLSession, Cloneable  {
 
-    /** Session object reporting an invalid cipher suite of "SSL_NULL_WITH_NULL_NULL" */
-    public static final SSLSessionImpl NULL_SESSION = new SSLSessionImpl(null);
+    /*
+     * Holds default instances so class preloading doesn't create an instance of
+     * it.
+     */
+    private static class DefaultHolder {
+        public static final SSLSessionImpl NULL_SESSION = new SSLSessionImpl(null);
+    }
 
     private long creationTime;
     private boolean isValid = true;
@@ -54,6 +59,10 @@
     byte[] serverRandom;
     final boolean isServer;
 
+    public static SSLSessionImpl getNullSession() {
+        return DefaultHolder.NULL_SESSION;
+    }
+
     public SSLSessionImpl(CipherSuite cipher_suite, SecureRandom secureRandom) {
         creationTime = System.currentTimeMillis();
         lastAccessedTime = creationTime;
diff --git a/luni/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLSocketImpl.java b/luni/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLSocketImpl.java
index 2cd2cf5..3e72aeb 100644
--- a/luni/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLSocketImpl.java
+++ b/luni/src/main/java/org/apache/harmony/xnet/provider/jsse/SSLSocketImpl.java
@@ -391,7 +391,7 @@
             } catch (IOException e) {
                 // return an invalid session with
                 // invalid cipher suite of "SSL_NULL_WITH_NULL_NULL"
-                return SSLSessionImpl.NULL_SESSION;
+                return SSLSessionImpl.getNullSession();
             }
         }
         return session;