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"))) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What's our confidence that detecting Dalvik will indeed solve the issue? Should it be a case sensitive compare?

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.

return null;
}
Comment thread
trask marked this conversation as resolved.
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