From 0400cca3236de1ca303af38bf81eab332d042b7c Mon Sep 17 00:00:00 2001 From: Adam Cozzette Date: Tue, 13 Mar 2018 16:37:29 -0700 Subject: Integrated internal changes from Google --- .../src/main/java/com/google/protobuf/UnsafeUtil.java | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'java/core/src/main/java/com/google/protobuf/UnsafeUtil.java') diff --git a/java/core/src/main/java/com/google/protobuf/UnsafeUtil.java b/java/core/src/main/java/com/google/protobuf/UnsafeUtil.java index b09ff673..c9e2904b 100644 --- a/java/core/src/main/java/com/google/protobuf/UnsafeUtil.java +++ b/java/core/src/main/java/com/google/protobuf/UnsafeUtil.java @@ -33,6 +33,7 @@ package com.google.protobuf; import java.lang.reflect.Field; import java.nio.Buffer; import java.nio.ByteBuffer; +import java.nio.ByteOrder; import java.security.AccessController; import java.security.PrivilegedExceptionAction; import java.util.logging.Level; @@ -83,6 +84,7 @@ final class UnsafeUtil { return HAS_UNSAFE_BYTEBUFFER_OPERATIONS; } + static long objectFieldOffset(Field field) { return MEMORY_ACCESSOR.objectFieldOffset(field); } @@ -287,7 +289,7 @@ final class UnsafeUtil { /** * Gets the {@code sun.misc.Unsafe} instance, or {@code null} if not available on this platform. */ - private static sun.misc.Unsafe getUnsafe() { + static sun.misc.Unsafe getUnsafe() { sun.misc.Unsafe unsafe = null; try { unsafe = @@ -367,6 +369,10 @@ final class UnsafeUtil { clazz.getMethod("objectFieldOffset", Field.class); clazz.getMethod("getLong", Object.class, long.class); + if (bufferAddressField() == null) { + return false; + } + clazz.getMethod("getByte", long.class); clazz.getMethod("putByte", long.class, byte.class); clazz.getMethod("getInt", long.class); @@ -387,12 +393,14 @@ final class UnsafeUtil { /** Finds the address field within a direct {@link Buffer}. */ private static Field bufferAddressField() { - return field(Buffer.class, "address", long.class); + Field field = field(Buffer.class, "address"); + return field != null && field.getType() == long.class ? field : null; } /** Finds the value field within a {@link String}. */ private static Field stringValueField() { - return field(String.class, "value", char[].class); + Field field = field(String.class, "value"); + return field != null && field.getType() == char[].class ? field : null; } /** @@ -407,14 +415,11 @@ final class UnsafeUtil { * Gets the field with the given name within the class, or {@code null} if not found. If found, * the field is made accessible. */ - private static Field field(Class clazz, String fieldName, Class expectedType) { + private static Field field(Class clazz, String fieldName) { Field field; try { field = clazz.getDeclaredField(fieldName); field.setAccessible(true); - if (!field.getType().equals(expectedType)) { - return null; - } } catch (Throwable t) { // Failed to access the fields. field = null; -- cgit v1.2.3