Skip to content

Commit 309b694

Browse files
slievrlyralf0131
authored andcommitted
optimize array code style (#4031)
Signed-off-by: jimin.jm <slievrly@163.com>
1 parent a1e5c8e commit 309b694

File tree

11 files changed

+69
-51
lines changed

11 files changed

+69
-51
lines changed

dubbo-common/src/main/java/org/apache/dubbo/common/io/StreamUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public int read() throws IOException {
4040
}
4141

4242
@Override
43-
public int read(byte b[], int off, int len) throws IOException {
43+
public int read(byte[] b, int off, int len) throws IOException {
4444
if (b == null) {
4545
throw new NullPointerException();
4646
}

dubbo-common/src/main/java/org/apache/dubbo/common/io/UnsafeByteArrayInputStream.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,19 @@
2323
* UnsafeByteArrayInputStream.
2424
*/
2525
public class UnsafeByteArrayInputStream extends InputStream {
26-
protected byte mData[];
26+
protected byte[] mData;
2727

2828
protected int mPosition, mLimit, mMark = 0;
2929

30-
public UnsafeByteArrayInputStream(byte buf[]) {
30+
public UnsafeByteArrayInputStream(byte[] buf) {
3131
this(buf, 0, buf.length);
3232
}
3333

34-
public UnsafeByteArrayInputStream(byte buf[], int offset) {
34+
public UnsafeByteArrayInputStream(byte[] buf, int offset) {
3535
this(buf, offset, buf.length - offset);
3636
}
3737

38-
public UnsafeByteArrayInputStream(byte buf[], int offset, int length) {
38+
public UnsafeByteArrayInputStream(byte[] buf, int offset, int length) {
3939
mData = buf;
4040
mPosition = mMark = offset;
4141
mLimit = Math.min(offset + length, buf.length);
@@ -47,7 +47,7 @@ public int read() {
4747
}
4848

4949
@Override
50-
public int read(byte b[], int off, int len) {
50+
public int read(byte[] b, int off, int len) {
5151
if (b == null) {
5252
throw new NullPointerException();
5353
}

dubbo-common/src/main/java/org/apache/dubbo/common/io/UnsafeByteArrayOutputStream.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
* UnsafeByteArrayOutputStream.
2626
*/
2727
public class UnsafeByteArrayOutputStream extends OutputStream {
28-
protected byte mBuffer[];
28+
protected byte[] mBuffer;
2929

3030
protected int mCount;
3131

@@ -51,7 +51,7 @@ public void write(int b) {
5151
}
5252

5353
@Override
54-
public void write(byte b[], int off, int len) {
54+
public void write(byte[] b, int off, int len) {
5555
if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) > b.length) || ((off + len) < 0)) {
5656
throw new IndexOutOfBoundsException();
5757
}

dubbo-common/src/main/java/org/apache/dubbo/common/json/GenericJSONConverter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ public void writeValue(Object obj, JSONWriter jb, boolean writeClass) throws IOE
474474
jb.objectBegin();
475475

476476
Wrapper w = Wrapper.getWrapper(c);
477-
String pns[] = w.getPropertyNames();
477+
String[] pns = w.getPropertyNames();
478478

479479
for (String pn : pns) {
480480
if ((obj instanceof Throwable) && (

dubbo-common/src/main/java/org/apache/dubbo/common/json/J2oVisitor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ private static Object toArray(Class<?> c, Stack<Object> list, int len) throws Pa
8383
return EMPTY_STRING_ARRAY;
8484
} else {
8585
Object o;
86-
String ss[] = new String[len];
86+
String[] ss = new String[len];
8787
for (int i = len - 1; i >= 0; i--) {
8888
o = list.pop();
8989
ss[i] = (o == null ? null : o.toString());

dubbo-common/src/main/java/org/apache/dubbo/common/json/Yylex.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public class Yylex {
4545
* at the beginning of a line
4646
* l is of the form l = 2*k, k a non negative integer
4747
*/
48-
private static final int ZZ_LEXSTATE[] = {
48+
private static final int[] ZZ_LEXSTATE = {
4949
0, 0, 1, 1, 2, 2
5050
};
5151

@@ -92,7 +92,7 @@ public class Yylex {
9292
/**
9393
* The transition table of the DFA
9494
*/
95-
private static final int ZZ_TRANS[] = {
95+
private static final int[] ZZ_TRANS = {
9696
3, 4, 5, 5, 6, 3, 5, 3, 7, 8,
9797
3, 9, 3, 5, 10, 11, 5, 12, 5, 5,
9898
13, 5, 5, 5, 5, 5, 14, 5, 5, 5,
@@ -247,7 +247,7 @@ public class Yylex {
247247
private static final int ZZ_NO_MATCH = 1;
248248
private static final int ZZ_PUSHBACK_2BIG = 2;
249249
/* error messages for the codes above */
250-
private static final String ZZ_ERROR_MSG[] = {
250+
private static final String[] ZZ_ERROR_MSG = {
251251
"Unkown internal scanner error",
252252
"Error: could not match input",
253253
"Error: pushback value was too large"
@@ -276,7 +276,7 @@ public class Yylex {
276276
* this buffer contains the current text to be matched and is
277277
* the source of the yytext() string
278278
*/
279-
private char zzBuffer[] = new char[ZZ_BUFFERSIZE];
279+
private char[] zzBuffer = new char[ZZ_BUFFERSIZE];
280280
/**
281281
* the textposition at the last accepting state
282282
*/
@@ -447,7 +447,7 @@ private boolean zzRefill() throws java.io.IOException {
447447
/* is the buffer big enough? */
448448
if (zzCurrentPos >= zzBuffer.length) {
449449
/* if not: blow it up */
450-
char newBuffer[] = new char[zzCurrentPos * 2];
450+
char[] newBuffer = new char[zzCurrentPos * 2];
451451
System.arraycopy(zzBuffer, 0, newBuffer, 0, zzBuffer.length);
452452
zzBuffer = newBuffer;
453453
}

dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/support/AbortPolicyWithReport.java

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,21 @@
1616
*/
1717
package org.apache.dubbo.common.threadpool.support;
1818

19-
import org.apache.dubbo.common.Constants;
20-
import org.apache.dubbo.common.URL;
21-
import org.apache.dubbo.common.logger.Logger;
22-
import org.apache.dubbo.common.logger.LoggerFactory;
23-
import org.apache.dubbo.common.utils.JVMUtil;
24-
2519
import java.io.File;
2620
import java.io.FileOutputStream;
2721
import java.text.SimpleDateFormat;
2822
import java.util.Date;
23+
import java.util.concurrent.ExecutorService;
2924
import java.util.concurrent.Executors;
3025
import java.util.concurrent.RejectedExecutionException;
3126
import java.util.concurrent.Semaphore;
3227
import java.util.concurrent.ThreadPoolExecutor;
33-
import java.util.concurrent.ExecutorService;
28+
29+
import org.apache.dubbo.common.Constants;
30+
import org.apache.dubbo.common.URL;
31+
import org.apache.dubbo.common.logger.Logger;
32+
import org.apache.dubbo.common.logger.LoggerFactory;
33+
import org.apache.dubbo.common.utils.JVMUtil;
3434

3535
/**
3636
* Abort Policy.
@@ -46,6 +46,16 @@ public class AbortPolicyWithReport extends ThreadPoolExecutor.AbortPolicy {
4646

4747
private static volatile long lastPrintTime = 0;
4848

49+
private static final long TEN_MINUTES_MILLS = 10 * 60 * 1000;
50+
51+
private static final String OS_WIN_PREFIX = "win";
52+
53+
private static final String OS_NAME_KEY = "os.name";
54+
55+
private static final String WIN_DATETIME_FORMAT = "yyyy-MM-dd_HH-mm-ss";
56+
57+
private static final String DEFAULT_DATETIME_FORMAT = "yyyy-MM-dd_HH:mm:ss";
58+
4959
private static Semaphore guard = new Semaphore(1);
5060

5161
public AbortPolicyWithReport(String threadName, URL url) {
@@ -56,11 +66,13 @@ public AbortPolicyWithReport(String threadName, URL url) {
5666
@Override
5767
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
5868
String msg = String.format("Thread pool is EXHAUSTED!" +
59-
" Thread Name: %s, Pool Size: %d (active: %d, core: %d, max: %d, largest: %d), Task: %d (completed: %d)," +
60-
" Executor status:(isShutdown:%s, isTerminated:%s, isTerminating:%s), in %s://%s:%d!",
61-
threadName, e.getPoolSize(), e.getActiveCount(), e.getCorePoolSize(), e.getMaximumPoolSize(), e.getLargestPoolSize(),
62-
e.getTaskCount(), e.getCompletedTaskCount(), e.isShutdown(), e.isTerminated(), e.isTerminating(),
63-
url.getProtocol(), url.getIp(), url.getPort());
69+
" Thread Name: %s, Pool Size: %d (active: %d, core: %d, max: %d, largest: %d), Task: %d (completed: "
70+
+ "%d)," +
71+
" Executor status:(isShutdown:%s, isTerminated:%s, isTerminating:%s), in %s://%s:%d!",
72+
threadName, e.getPoolSize(), e.getActiveCount(), e.getCorePoolSize(), e.getMaximumPoolSize(),
73+
e.getLargestPoolSize(),
74+
e.getTaskCount(), e.getCompletedTaskCount(), e.isShutdown(), e.isTerminated(), e.isTerminating(),
75+
url.getProtocol(), url.getIp(), url.getPort());
6476
logger.warn(msg);
6577
dumpJStack();
6678
throw new RejectedExecutionException(msg);
@@ -70,7 +82,7 @@ private void dumpJStack() {
7082
long now = System.currentTimeMillis();
7183

7284
//dump every 10 minutes
73-
if (now - lastPrintTime < 10 * 60 * 1000) {
85+
if (now - lastPrintTime < TEN_MINUTES_MILLS) {
7486
return;
7587
}
7688

@@ -84,18 +96,19 @@ private void dumpJStack() {
8496

8597
SimpleDateFormat sdf;
8698

87-
String os = System.getProperty("os.name").toLowerCase();
99+
String os = System.getProperty(OS_NAME_KEY).toLowerCase();
88100

89101
// window system don't support ":" in file name
90-
if (os.contains("win")) {
91-
sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
102+
if (os.contains(OS_WIN_PREFIX)) {
103+
sdf = new SimpleDateFormat(WIN_DATETIME_FORMAT);
92104
} else {
93-
sdf = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");
105+
sdf = new SimpleDateFormat(DEFAULT_DATETIME_FORMAT);
94106
}
95107

96108
String dateStr = sdf.format(new Date());
97109
//try-with-resources
98-
try (FileOutputStream jStackStream = new FileOutputStream(new File(dumpPath, "Dubbo_JStack.log" + "." + dateStr))) {
110+
try (FileOutputStream jStackStream = new FileOutputStream(
111+
new File(dumpPath, "Dubbo_JStack.log" + "." + dateStr))) {
99112
JVMUtil.jstack(jStackStream);
100113
} catch (Throwable t) {
101114
logger.error("dump jStack error", t);

dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/telnet/support/command/LogTelnetHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public String telnet(Channel channel, String message) {
4848
if (message == null || message.trim().length() == 0) {
4949
buf.append("EXAMPLE: log error / log 100");
5050
} else {
51-
String str[] = message.split(" ");
51+
String[] str = message.split(" ");
5252
if (!StringUtils.isInteger(str[0])) {
5353
LoggerFactory.setLevel(Level.valueOf(message.toUpperCase()));
5454
} else {

dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractCodec.java

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
*/
1717
package org.apache.dubbo.remoting.transport;
1818

19+
import java.io.IOException;
20+
import java.net.InetSocketAddress;
21+
1922
import org.apache.dubbo.common.Constants;
2023
import org.apache.dubbo.common.URL;
2124
import org.apache.dubbo.common.constants.RemotingConstants;
@@ -26,23 +29,25 @@
2629
import org.apache.dubbo.remoting.Channel;
2730
import org.apache.dubbo.remoting.Codec2;
2831

29-
import java.io.IOException;
30-
import java.net.InetSocketAddress;
31-
3232
/**
3333
* AbstractCodec
3434
*/
3535
public abstract class AbstractCodec implements Codec2 {
3636

3737
private static final Logger logger = LoggerFactory.getLogger(AbstractCodec.class);
3838

39+
private static final String CLIENT_SIDE = "client";
40+
41+
private static final String SERVER_SIDE = "server";
42+
3943
protected static void checkPayload(Channel channel, long size) throws IOException {
4044
int payload = RemotingConstants.DEFAULT_PAYLOAD;
4145
if (channel != null && channel.getUrl() != null) {
4246
payload = channel.getUrl().getParameter(RemotingConstants.PAYLOAD_KEY, RemotingConstants.DEFAULT_PAYLOAD);
4347
}
4448
if (payload > 0 && size > payload) {
45-
ExceedPayloadLimitException e = new ExceedPayloadLimitException("Data length too large: " + size + ", max payload: " + payload + ", channel: " + channel);
49+
ExceedPayloadLimitException e = new ExceedPayloadLimitException(
50+
"Data length too large: " + size + ", max payload: " + payload + ", channel: " + channel);
4651
logger.error(e);
4752
throw e;
4853
}
@@ -53,21 +58,21 @@ protected Serialization getSerialization(Channel channel) {
5358
}
5459

5560
protected boolean isClientSide(Channel channel) {
56-
String side = (String) channel.getAttribute(Constants.SIDE_KEY);
57-
if ("client".equals(side)) {
61+
String side = (String)channel.getAttribute(Constants.SIDE_KEY);
62+
if (CLIENT_SIDE.equals(side)) {
5863
return true;
59-
} else if ("server".equals(side)) {
64+
} else if (SERVER_SIDE.equals(side)) {
6065
return false;
6166
} else {
6267
InetSocketAddress address = channel.getRemoteAddress();
6368
URL url = channel.getUrl();
64-
boolean client = url.getPort() == address.getPort()
65-
&& NetUtils.filterLocalHost(url.getIp()).equals(
66-
NetUtils.filterLocalHost(address.getAddress()
67-
.getHostAddress()));
68-
channel.setAttribute(Constants.SIDE_KEY, client ? "client"
69-
: "server");
70-
return client;
69+
boolean isClient = url.getPort() == address.getPort()
70+
&& NetUtils.filterLocalHost(url.getIp()).equals(
71+
NetUtils.filterLocalHost(address.getAddress()
72+
.getHostAddress()));
73+
channel.setAttribute(Constants.SIDE_KEY, isClient ? CLIENT_SIDE
74+
: SERVER_SIDE);
75+
return isClient;
7176
}
7277
}
7378

dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/LogTelnetHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public String telnet(Channel channel, String message) {
4848
if (message == null || message.trim().length() == 0) {
4949
buf.append("EXAMPLE: log error / log 100");
5050
} else {
51-
String str[] = message.split(" ");
51+
String[] str = message.split(" ");
5252
if (!StringUtils.isInteger(str[0])) {
5353
LoggerFactory.setLevel(Level.valueOf(message.toUpperCase()));
5454
} else {

0 commit comments

Comments
 (0)