Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt at fixing https://github.com/robolectric/robolectric/issues/8242 #8250

Merged
merged 1 commit into from Jun 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -27,7 +27,8 @@ public void allMethodRefs() throws Exception {
new MethodRef("java.util.Locale", "adjustLanguageCode"),
new MethodRef("java.io.FileDescriptor", "release$"),
new MethodRef("java.io.FileDescriptor", "getInt$"),
new MethodRef("java.io.FileDescriptor", "setInt$"));
new MethodRef("java.io.FileDescriptor", "setInt$"),
new MethodRef("javax.crypto.Cipher", "getCurrentSpi"));
}

@Test
Expand Down
Expand Up @@ -24,6 +24,8 @@
import java.util.List;
import java.util.Locale;
import javax.annotation.Nullable;
import javax.crypto.Cipher;
import javax.crypto.CipherSpi;
import org.robolectric.fakes.CleanerCompat;
import org.robolectric.internal.bytecode.Interceptor;
import org.robolectric.internal.bytecode.MethodRef;
Expand All @@ -46,6 +48,7 @@ public static Collection<Interceptor> all() {
new FileDescriptorInterceptor(),
new NoOpInterceptor(),
new SocketInterceptor(),
new CipherInterceptor(),
new ReferenceRefersToInterceptor()));

if (Util.getJavaVersion() >= 9) {
Expand Down Expand Up @@ -465,6 +468,35 @@ public MethodHandle getMethodHandle(String methodName, MethodType type)
}
}

/** Intercepts calls to methods in {@link javax.crypto.Cipher} not present in the OpenJDK. */
public static class CipherInterceptor extends Interceptor {
public CipherInterceptor() {
super(new MethodRef(Cipher.class, "getCurrentSpi"));
}

@Nullable
static CipherSpi getCurrentSpi() {
return null;
}

@Override
public Function<Object, Object> handle(MethodSignature methodSignature) {
return new Function<Object, Object>() {
@Override
public Object call(Class<?> theClass, Object value, Object[] params) {
return getCurrentSpi();
alwa marked this conversation as resolved.
Show resolved Hide resolved
}
};
}

@Override
public MethodHandle getMethodHandle(String methodName, MethodType type)
throws NoSuchMethodException, IllegalAccessException {
return lookup.findStatic(
getClass(), "getCurrentSpi", methodType(CipherSpi.class, void.class));
}
}

/** AndroidInterceptor for Reference.refersTo which is not available until JDK 16. */
public static class ReferenceRefersToInterceptor extends Interceptor {
private static final String METHOD = "refersTo";
Expand Down