package org.robolectric.shadows;

import static android.os.Build.VERSION_CODES.LOLLIPOP;
import static android.os.Build.VERSION_CODES.LOLLIPOP_MR1;
import static android.os.Build.VERSION_CODES.M;

import android.net.Network;
import java.io.FileDescriptor;
import java.net.DatagramSocket;
import java.net.Socket;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.shadow.api.Shadow;

@Implements(value = Network.class, minSdk = LOLLIPOP)
public class ShadowNetwork {
  private int netId;

  /**
   * Creates new instance of {@link Network}, because its constructor is hidden.
   *
   * @param netId The netId.
   * @return The Network instance.
   */
  public static Network newInstance(int netId) {
    return Shadow.newInstance(Network.class, new Class[] {int.class}, new Object[] {netId});
  }

  @Implementation
  protected void __constructor__(int netId) {
    this.netId = netId;
  }

  /**
   * No-ops. We cannot assume that a Network represents a real network interface on the device
   * running this test, so we have nothing to bind the socket to.
   */
  @Implementation(minSdk = LOLLIPOP_MR1)
  protected void bindSocket(DatagramSocket socket) {}

  /**
   * No-ops. We cannot assume that a Network represents a real network interface on the device
   * running this test, so we have nothing to bind the socket to.
   */
  @Implementation
  protected void bindSocket(Socket socket) {}

  /**
   * No-ops. We cannot assume that a Network represents a real network interface on the device
   * running this test, so we have nothing to bind the socket to.
   */
  @Implementation(minSdk = M)
  protected void bindSocket(FileDescriptor fd) {}

  /**
   * Allows to get the stored netId.
   *
   * @return The netId.
   */
  public int getNetId() {
    return netId;
  }
}

@@ -428,25 +428,118 @@
 ric.
-shadow.api.Shadow
+annotation.RealObject;%0Aimport org.robolectric.shadow.api.Shadow;%0Aimport org.robolectric.util.ReflectionHelpers
 ;%0A%0A@
@@ -624,27 +624,49 @@
 k %7B%0A
+%0A
   
-private int netId
+@RealObject private Network realObject
 ;%0A%0A 
@@ -977,101 +977,8 @@
  %7D%0A%0A
-  @Implementation%0A  protected void __constructor__(int netId) %7B%0A    this.netId = netId;%0A  %7D%0A%0A
   /*
@@ -1849,21 +1849,63 @@
  return 
+ReflectionHelpers.getField(realObject, %22
 netId
+%22)
 ;%0A  %7D%0A%7D%0A

Use real netId in ShadowNetwork, which enables hashCode() and equals().