Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package org.robolectric.interceptors;
import static java.lang.Math.min;
import static java.lang.invoke.MethodHandles.constant;
import static java.lang.invoke.MethodHandles.dropArguments;
import static java.lang.invoke.MethodType.methodType;
import static java.util.Arrays.asList;
import static org.robolectric.util.ReflectionHelpers.callStaticMethod;
import com.google.common.base.Throwables;
import java.io.FileDescriptor;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.ref.PhantomReference;
import java.lang.ref.Reference;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.lang.reflect.Field;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Locale;
import javax.annotation.Nullable;
import org.robolectric.fakes.CleanerCompat;
import org.robolectric.internal.bytecode.Interceptor;
import org.robolectric.internal.bytecode.MethodRef;
import org.robolectric.internal.bytecode.MethodSignature;
import org.robolectric.util.Function;
public class AndroidInterceptors {
private static final MethodHandles.Lookup lookup = MethodHandles.lookup();
public static Collection all() {
return new ArrayList<>(
asList(
new LinkedHashMapEldestInterceptor(),
new SystemTimeInterceptor(),
new SystemArrayCopyInterceptor(),
new LocaleAdjustLanguageCodeInterceptor(),
new SystemLogInterceptor(),
new FileDescriptorInterceptor(),
new NoOpInterceptor(),
new SocketInterceptor(),
new ReferenceRefersToInterceptor(),
new NioUtilsFreeDirectBufferInterceptor(),
new NioUtilsUnsafeArrayInterceptor(),
new NioUtilsUnsafeArrayOffsetInterceptor(),
new CleanerInterceptor()));
}
/**
* Intercepts calls to libcore-extensions to {@link FileDescriptor}.
*
*
libcore implements extensions to {@link FileDescriptor} that support ownership tracking of
* unix FDs, which are not part of the Java API. This intercepts calls to these and maps them to
* the OpenJDK API.
*/
public static class FileDescriptorInterceptor extends Interceptor {
public FileDescriptorInterceptor() {
super(
new MethodRef(FileDescriptor.class, "release$"),
new MethodRef(FileDescriptor.class, "getInt$"),
new MethodRef(FileDescriptor.class, "setInt$"));
}
private static void moveField(
FileDescriptor out, FileDescriptor in, String fieldName, Object movedOutValue)
throws ReflectiveOperationException {
Field fieldAccessor = FileDescriptor.class.getDeclaredField(fieldName);
fieldAccessor.setAccessible(true);
fieldAccessor.set(out, fieldAccessor.get(in));
fieldAccessor.set(in, movedOutValue);
}
static Object setInt(FileDescriptor input, int value) {
try {
final Object obj =
Class.forName("jdk.internal.access.SharedSecrets")
.getMethod("getJavaIOFileDescriptorAccess")
.invoke(null);
Class.forName("jdk.internal.access.JavaIOFileDescriptorAccess")
.getMethod("set", FileDescriptor.class, int.class)
.invoke(obj, input, value);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(
"Failed to interact with raw FileDescriptor internals;" + " perhaps JRE has changed?",
e);
}
return null;
}
static int getInt(FileDescriptor input) {
try {
Field fieldAccessor = input.getClass().getDeclaredField("fd");
fieldAccessor.setAccessible(true);
return fieldAccessor.getInt(input);
} catch (IllegalAccessException e) {
// Should not happen since we set this to be accessible
return -1;
} catch (NoSuchFieldException e) {
return -2;
}
}
static FileDescriptor release(FileDescriptor input) {
synchronized (input) {
try {
FileDescriptor ret = new FileDescriptor();
moveField(ret, input, "fd", /* movedOutValue= */ -1);
// "closed" is irrelevant if the fd is already -1.
moveField(ret, input, "closed", /* movedOutValue= */ false);
// N.B.: FileDescriptor.attach() is not implemented in libcore (yet), so these won't be
// used.
moveField(ret, input, "parent", /* movedOutValue= */ null);
moveField(ret, input, "otherParents", /* movedOutValue= */ null);
// These only exist on Windows.
try {
moveField(ret, input, "handle", /* movedOutValue= */ -1);
moveField(ret, input, "append", /* movedOutValue= */ false);
} catch (ReflectiveOperationException ex) {
// Ignore.
}
return ret;
} catch (ReflectiveOperationException ex) {
throw new RuntimeException(ex);
}
}
}
@Override
public Function