@@ -48,8 +48,7 @@ public class ProfilerSharedMemoryWriter {
4848 static ByteBuffer generateProcessCorrelationStorage (String serviceName , @ Nullable String environment , String socketFilePath ) {
4949 ByteBuffer buffer = ByteBuffer .allocateDirect (4096 );
5050 buffer .order (ByteOrder .nativeOrder ());
51- //To make it java 8 compatible regardless of how it's compiled, need to explicitly use Buffer.position(int)
52- ((Buffer ) buffer ).position (0 );
51+ setBufferPosition (buffer , 0 );
5352
5453 buffer .putChar ((char ) 1 ); // layout-minor-version
5554 writeUtf8Str (buffer , serviceName );
@@ -64,6 +63,19 @@ private static void writeUtf8Str(ByteBuffer buffer, String str) {
6463 buffer .put (utf8 );
6564 }
6665
66+ /**
67+ * Set {@link ByteBuffer} position in a way that is compatible with Java8.
68+ *
69+ * @param buffer byte buffer
70+ * @param position new position to set
71+ */
72+ private static void setBufferPosition (ByteBuffer buffer , int position ) {
73+ //To make it java 8 compatible regardless of how it's compiled, need to explicitly use Buffer.position(int)
74+ // a covariant return type override was introduced in ByteBuffer.position(int) in Java 9 which makes it break
75+ // binary compatibility with Java 8.
76+ ((Buffer ) buffer ).position (position );
77+ }
78+
6779 /**
6880 * This method ensures that all writes which happened prior to this method call are not moved
6981 * after the method call due to reordering.
@@ -96,11 +108,11 @@ static void updateThreadCorrelationStorage(@Nullable AbstractSpanImpl<?> newSpan
96108 TransactionImpl tx = newSpan .getParentTransaction ();
97109 tls .put (TLS_TRACE_PRESENT_OFFSET , (byte ) 1 );
98110 tls .put (TLS_TRACE_FLAGS_OFFSET , newSpan .getTraceContext ().getFlags ());
99- tls . position ( TLS_TRACE_ID_OFFSET );
111+ setBufferPosition ( tls , TLS_TRACE_ID_OFFSET );
100112 newSpan .getTraceContext ().getTraceId ().writeToBuffer (tls );
101- tls . position ( TLS_SPAN_ID_OFFSET );
113+ setBufferPosition ( tls , TLS_SPAN_ID_OFFSET );
102114 newSpan .getTraceContext ().getId ().writeToBuffer (tls );
103- tls . position ( TLS_LOCAL_ROOT_SPAN_ID_OFFSET );
115+ setBufferPosition ( tls , TLS_LOCAL_ROOT_SPAN_ID_OFFSET );
104116 tx .getTraceContext ().getId ().writeToBuffer (tls );
105117 } else {
106118 tls .put (TLS_TRACE_PRESENT_OFFSET , (byte ) 0 );
0 commit comments