Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ public static StringEncoder createFallbackEncoder() {
*/
@Nullable
public static StringEncoder createUnsafeEncoder() {
// Android exposes sun.misc.Unsafe, but String does not have the OpenJDK internal fields used by
// UnsafeStringEncoder. ART logs an error when these missing fields are queried, even though the
// exception is caught.
if ("Dalvik".equals(System.getProperty("java.vm.name"))) {
return null;
}
Comment on lines +42 to +47

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was torn between adding the check here or in UnsafeStringEncoder.createIfAvailable(). Happy to move if you have a preference.

return UnsafeStringEncoder.createIfAvailable();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledOnJre;
import org.junit.jupiter.api.condition.JRE;
import org.junitpioneer.jupiter.SetSystemProperty;

class StringEncoderTest {

Expand Down Expand Up @@ -57,6 +58,12 @@ void testUtf8SizeLatin1_VarHandle() {
testUtf8SizeLatin1(varHandleStringEncoder);
}

@Test
@SetSystemProperty(key = "java.vm.name", value = "Dalvik")
void unsafeStringEncoderNotAvailableOnAndroid() {
assertThat(StringEncoderHolder.createUnsafeEncoder()).isNull();
}

@SuppressWarnings("AvoidEscapedUnicodeCharacters")
private static void testUtf8Encoding(StringEncoder stringEncoder) {
assertThat(stringEncoder).isNotNull();
Expand Down
Loading