Skip to content

Commit 0710c64

Browse files
authored
bugfix: support jdk9+ compile code (#4410)
1 parent c0c8725 commit 0710c64

File tree

11 files changed

+145
-35
lines changed

11 files changed

+145
-35
lines changed

changes/en-us/develop.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Add changes here for all PR submitted to the develop branch.
1010
- [[#5991](https://github.com/seata/seata/pull/5991)] fix the issue that the Lua script is not synchronized when the redis sentinel master node is down
1111
- [[#6025](https://github.com/seata/seata/pull/6025)] fix the white screen after click the "View Global Lock" button on the transaction info page in the console
1212
- [[#6026](https://github.com/seata/seata/pull/6026)] fix incorrect metric report
13+
- [[#4410](https://github.com/seata/seata/pull/4410)] support jdk9+ compile code
1314

1415
### optimize:
1516
- [[#6044](https://github.com/seata/seata/pull/6044)] optimize derivative product check base on mysql
@@ -27,6 +28,7 @@ Thanks to these contributors for their code commits. Please report an unintended
2728
- [jsbxyyx](https://github.com/jsbxyyx)
2829
- [liuqiufeng](https://github.com/liuqiufeng)
2930
- [ptyin](https://github.com/ptyin)
31+
- [funky-eyes](https://github.com/funky-eyes)
3032

3133

3234
Also, we receive many valuable issues, questions and advices from our community. Thanks for you all.

changes/zh-cn/develop.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- [[#5991](https://github.com/seata/seata/pull/5991)] 修复redis sentinel master node 宕机时,lua脚本未同步的问题
1111
- [[#6025](https://github.com/seata/seata/pull/6025)] 修复控制台点击事务信息页面中的"查看全局锁"按钮之后白屏的问题
1212
- [[#6026](https://github.com/seata/seata/pull/6026)] 修复异常的打点
13+
- [[#4410](https://github.com/seata/seata/pull/4410)] 修复jdk9+版本编译后,引入后ByteBuffer#flip NoSuchMethodError的问题
1314

1415
### optimize:
1516
- [[#6044](https://github.com/seata/seata/pull/6044)] 优化MySQL衍生数据库判断逻辑
@@ -27,5 +28,6 @@
2728
- [jsbxyyx](https://github.com/jsbxyyx)
2829
- [liuqiufeng](https://github.com/liuqiufeng)
2930
- [ptyin](https://github.com/ptyin)
31+
- [funky-eyes](https://github.com/funky-eyes)
3032

3133
同时,我们收到了社区反馈的很多有价值的issue和建议,非常感谢大家。
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright 1999-2019 Seata.io Group.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
package io.seata.common.util;
14+
15+
import java.nio.Buffer;
16+
17+
/**
18+
* Explicit cast to {@link Buffer} parent buffer type. It resolves issues with covariant return types in Java 9+ for
19+
* {@link java.nio.ByteBuffer} and {@link java.nio.CharBuffer}. Explicit casting resolves the NoSuchMethodErrors (e.g
20+
* java.lang.NoSuchMethodError: java.nio.ByteBuffer.flip(I)Ljava/nio/ByteBuffer) when the project is compiled with
21+
* newer Java version and run on Java 8.
22+
* <p/>
23+
* <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/ByteBuffer.html">Java 8</a> doesn't provide override the
24+
* following Buffer methods in subclasses:
25+
*
26+
* <pre>
27+
* Buffer clear()
28+
* Buffer flip()
29+
* Buffer limit(int newLimit)
30+
* Buffer mark()
31+
* Buffer position(int newPosition)
32+
* Buffer reset()
33+
* Buffer rewind()
34+
* </pre>
35+
*
36+
* <a href="https://docs.oracle.com/javase/9/docs/api/java/nio/ByteBuffer.html">Java 9</a> introduces the overrides in
37+
* child classes (e.g the ByteBuffer), but the return type is the specialized one and not the abstract {@link Buffer}.
38+
* So the code compiled with newer Java is not working on Java 8 unless a workaround with explicit casting is used.
39+
*
40+
* @author funkye
41+
*/
42+
public class BufferUtils {
43+
44+
/**
45+
* @param buffer byteBuffer
46+
*/
47+
public static void flip(Buffer buffer) {
48+
buffer.flip();
49+
}
50+
51+
/**
52+
* @param buffer byteBuffer
53+
*/
54+
public static void clear(Buffer buffer) {
55+
buffer.clear();
56+
}
57+
58+
/**
59+
* @param buffer byteBuffer
60+
*/
61+
public static void limit(Buffer buffer, int newLimit) {
62+
buffer.limit(newLimit);
63+
}
64+
65+
/**
66+
* @param buffer byteBuffer
67+
*/
68+
public static void mark(Buffer buffer) {
69+
buffer.mark();
70+
}
71+
72+
/**
73+
* @param buffer byteBuffer
74+
*/
75+
public static void position(Buffer buffer, int newPosition) {
76+
buffer.position(newPosition);
77+
}
78+
79+
/**
80+
* @param buffer byteBuffer
81+
*/
82+
public static void rewind(Buffer buffer) {
83+
buffer.rewind();
84+
}
85+
86+
/**
87+
* @param buffer byteBuffer
88+
*/
89+
public static void reset(Buffer buffer) {
90+
buffer.reset();
91+
}
92+
93+
}

integration/http/src/test/java/io/seata/integration/http/HttpTest.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,6 @@
1515
*/
1616
package io.seata.integration.http;
1717

18-
import com.alibaba.fastjson.JSON;
19-
import com.alibaba.fastjson.JSONObject;
20-
import io.seata.core.context.RootContext;
21-
import org.apache.http.HttpResponse;
22-
import org.junit.jupiter.api.Assertions;
23-
import org.junit.jupiter.api.Test;
24-
2518
import java.io.ByteArrayOutputStream;
2619
import java.io.IOException;
2720
import java.io.InputStream;
@@ -31,6 +24,14 @@
3124
import java.nio.channels.WritableByteChannel;
3225
import java.util.HashMap;
3326
import java.util.Map;
27+
import com.alibaba.fastjson.JSON;
28+
import com.alibaba.fastjson.JSONObject;
29+
import io.seata.common.util.BufferUtils;
30+
import io.seata.core.context.RootContext;
31+
import org.apache.http.HttpResponse;
32+
import org.junit.jupiter.api.Assertions;
33+
import org.junit.jupiter.api.Test;
34+
3435

3536
import static io.seata.integration.http.AbstractHttpExecutor.convertParamOfBean;
3637
import static io.seata.integration.http.AbstractHttpExecutor.convertParamOfJsonString;
@@ -205,7 +206,7 @@ public static String readStreamAsStr(InputStream is) throws IOException {
205206
ByteBuffer bb = ByteBuffer.allocate(4096);
206207

207208
while (src.read(bb) != -1) {
208-
bb.flip();
209+
BufferUtils.flip(bb);
209210
dest.write(bb);
210211
bb.clear();
211212
}

rm-datasource/src/main/java/io/seata/rm/datasource/undo/parser/ProtostuffUndoLogParser.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
import java.sql.Timestamp;
2121
import java.util.List;
2222

23+
import org.slf4j.Logger;
24+
import org.slf4j.LoggerFactory;
25+
2326
import io.protostuff.Input;
2427
import io.protostuff.LinkedBuffer;
2528
import io.protostuff.Output;
@@ -36,11 +39,10 @@
3639
import io.seata.common.loader.EnhancedServiceNotFoundException;
3740
import io.seata.common.loader.LoadLevel;
3841
import io.seata.common.util.CollectionUtils;
42+
import io.seata.common.util.BufferUtils;
3943
import io.seata.rm.datasource.undo.BranchUndoLog;
4044
import io.seata.rm.datasource.undo.UndoLogParser;
4145
import io.seata.rm.datasource.undo.parser.spi.ProtostuffDelegate;
42-
import org.slf4j.Logger;
43-
import org.slf4j.LoggerFactory;
4446

4547
/**
4648
* The type protostuff based undo log parser.
@@ -132,7 +134,7 @@ public java.sql.Timestamp readFrom(Input input) throws IOException {
132134
ByteBuffer buffer = input.readByteBuffer();
133135
long time = buffer.getLong();
134136
int nanos = buffer.getInt();
135-
buffer.flip();
137+
BufferUtils.flip(buffer);
136138
java.sql.Timestamp timestamp = new Timestamp(time);
137139
timestamp.setNanos(nanos);
138140
return timestamp;
@@ -143,7 +145,7 @@ public void writeTo(Output output, int number, java.sql.Timestamp value, boolean
143145
ByteBuffer buffer = ByteBuffer.allocate(12);
144146
buffer.putLong(value.getTime());
145147
buffer.putInt(value.getNanos());
146-
buffer.flip();
148+
BufferUtils.flip(buffer);
147149
output.writeBytes(number, buffer, repeated);
148150
}
149151

serializer/seata-serializer-protobuf/src/main/java/io/seata/serializer/protobuf/ProtobufSerializer.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,18 @@
1515
*/
1616
package io.seata.serializer.protobuf;
1717

18+
import java.nio.ByteBuffer;
19+
import java.nio.charset.Charset;
20+
import java.nio.charset.StandardCharsets;
21+
1822
import com.google.protobuf.GeneratedMessageV3;
23+
1924
import io.seata.common.loader.LoadLevel;
25+
import io.seata.common.util.BufferUtils;
2026
import io.seata.core.serializer.Serializer;
2127
import io.seata.serializer.protobuf.convertor.PbConvertor;
2228
import io.seata.serializer.protobuf.manager.ProtobufConvertManager;
2329

24-
import java.nio.ByteBuffer;
25-
import java.nio.charset.Charset;
26-
import java.nio.charset.StandardCharsets;
27-
2830
/**
2931
* The type Protobuf codec.
3032
*
@@ -53,7 +55,7 @@ public <T> byte[] serialize(T t) {
5355
byteBuffer.putInt(nameBytes.length);
5456
byteBuffer.put(nameBytes);
5557
byteBuffer.put(body);
56-
byteBuffer.flip();
58+
BufferUtils.flip(byteBuffer);
5759
byte[] content = new byte[byteBuffer.limit()];
5860
byteBuffer.get(content);
5961
return content;

serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/SeataSerializer.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@
1515
*/
1616
package io.seata.serializer.seata;
1717

18+
import java.nio.ByteBuffer;
19+
1820
import io.netty.buffer.ByteBuf;
1921
import io.netty.buffer.Unpooled;
2022
import io.seata.common.loader.LoadLevel;
23+
import io.seata.common.util.BufferUtils;
2124
import io.seata.core.protocol.AbstractMessage;
2225
import io.seata.core.serializer.Serializer;
2326

24-
import java.nio.ByteBuffer;
25-
2627
/**
2728
* The Seata codec.
2829
*
@@ -53,7 +54,7 @@ public <T> byte[] serialize(T t) {
5354
byteBuffer.putShort(typecode);
5455
byteBuffer.put(body);
5556

56-
byteBuffer.flip();
57+
BufferUtils.flip(byteBuffer);
5758
byte[] content = new byte[byteBuffer.limit()];
5859
byteBuffer.get(content);
5960
return content;

server/src/main/java/io/seata/server/session/BranchSession.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.concurrent.ConcurrentHashMap;
2222
import java.util.concurrent.ConcurrentMap;
2323
import io.seata.common.util.CompressUtil;
24+
import io.seata.common.util.BufferUtils;
2425
import io.seata.core.exception.TransactionException;
2526
import io.seata.core.model.BranchStatus;
2627
import io.seata.core.model.BranchType;
@@ -350,7 +351,7 @@ public byte[] encode() {
350351

351352
ByteBuffer byteBuffer = byteBufferThreadLocal.get();
352353
//recycle
353-
byteBuffer.clear();
354+
BufferUtils.clear(byteBuffer);
354355

355356
byteBuffer.putLong(transactionId);
356357
byteBuffer.putLong(branchId);
@@ -394,7 +395,7 @@ public byte[] encode() {
394395

395396
byteBuffer.put((byte)status.getCode());
396397
byteBuffer.put((byte)lockStatus.getCode());
397-
byteBuffer.flip();
398+
BufferUtils.flip(byteBuffer);
398399
byte[] result = new byte[byteBuffer.limit()];
399400
byteBuffer.get(result);
400401
return result;

server/src/main/java/io/seata/server/session/GlobalSession.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import io.seata.common.Constants;
3030
import io.seata.common.DefaultValues;
3131
import io.seata.common.XID;
32+
import io.seata.common.util.BufferUtils;
3233
import io.seata.common.util.StringUtils;
3334
import io.seata.config.ConfigurationFactory;
3435
import io.seata.core.constants.ConfigurationKeys;
@@ -46,6 +47,7 @@
4647
import org.slf4j.Logger;
4748
import org.slf4j.LoggerFactory;
4849

50+
4951
import static io.seata.core.model.GlobalStatus.AsyncCommitting;
5052
import static io.seata.core.model.GlobalStatus.CommitRetrying;
5153
import static io.seata.core.model.GlobalStatus.Committing;
@@ -584,7 +586,7 @@ public byte[] encode() {
584586
}
585587
ByteBuffer byteBuffer = byteBufferThreadLocal.get();
586588
//recycle
587-
byteBuffer.clear();
589+
BufferUtils.clear(byteBuffer);
588590

589591
byteBuffer.putLong(transactionId);
590592
byteBuffer.putInt(timeout);
@@ -621,7 +623,7 @@ public byte[] encode() {
621623

622624
byteBuffer.putLong(beginTime);
623625
byteBuffer.put((byte)status.getCode());
624-
byteBuffer.flip();
626+
BufferUtils.flip(byteBuffer);
625627
byte[] result = new byte[byteBuffer.limit()];
626628
byteBuffer.get(result);
627629
return result;

server/src/main/java/io/seata/server/storage/file/store/FileTransactionStoreManager.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@
3131
import java.util.concurrent.TimeUnit;
3232
import java.util.concurrent.atomic.AtomicLong;
3333
import java.util.concurrent.locks.ReentrantLock;
34-
3534
import io.seata.common.exception.StoreException;
3635
import io.seata.common.thread.NamedThreadFactory;
3736
import io.seata.common.util.CollectionUtils;
37+
import io.seata.common.util.BufferUtils;
3838
import io.seata.server.session.BranchSession;
3939
import io.seata.server.session.GlobalSession;
4040
import io.seata.server.session.SessionCondition;
@@ -50,6 +50,7 @@
5050
import org.slf4j.LoggerFactory;
5151
import org.slf4j.MDC;
5252

53+
5354
import static io.seata.core.context.RootContext.MDC_KEY_BRANCH_ID;
5455

5556
/**
@@ -263,11 +264,11 @@ private boolean writeDataFrame(byte[] data) {
263264
}
264265

265266
private boolean flushWriteBuffer(ByteBuffer writeBuffer) {
266-
writeBuffer.flip();
267+
BufferUtils.flip(writeBuffer);
267268
if (!writeDataFileByBuffer(writeBuffer)) {
268269
return false;
269270
}
270-
writeBuffer.clear();
271+
BufferUtils.clear(writeBuffer);
271272
return true;
272273
}
273274

@@ -397,12 +398,12 @@ private List<TransactionWriteStore> parseDataFile(File file, int readSize, long
397398
ByteBuffer buffSize = ByteBuffer.allocate(MARK_SIZE);
398399
while (fileChannel.position() < size) {
399400
try {
400-
buffSize.clear();
401+
BufferUtils.clear(buffSize);
401402
int avilReadSize = fileChannel.read(buffSize);
402403
if (avilReadSize != MARK_SIZE) {
403404
break;
404405
}
405-
buffSize.flip();
406+
BufferUtils.flip(buffSize);
406407
int bodySize = buffSize.getInt();
407408
byte[] byBody = new byte[bodySize];
408409
ByteBuffer buffBody = ByteBuffer.wrap(byBody);

0 commit comments

Comments
 (0)