Skip to content

Commit

Permalink
Intercept Cipher#getCurrentSpi to avoid running error
Browse files Browse the repository at this point in the history
  • Loading branch information
alixwar committed Jun 1, 2023
1 parent cf9c4b0 commit c735405
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
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();
}
};
}

@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

0 comments on commit c735405

Please sign in to comment.