From e28c769e07e76f71c54e2900b96efdcc0e89a6c2 Mon Sep 17 00:00:00 2001 From: "jimin.jm" Date: Sun, 12 May 2019 00:22:50 +0800 Subject: [PATCH] optimize array code style Signed-off-by: jimin.jm --- .../apache/dubbo/common/io/StreamUtils.java | 2 +- .../common/io/UnsafeByteArrayInputStream.java | 10 ++-- .../io/UnsafeByteArrayOutputStream.java | 4 +- .../common/json/GenericJSONConverter.java | 2 +- .../apache/dubbo/common/json/J2oVisitor.java | 2 +- .../org/apache/dubbo/common/json/Yylex.java | 10 ++-- .../support/AbortPolicyWithReport.java | 49 ++++++++++++------- .../support/command/LogTelnetHandler.java | 2 +- .../remoting/transport/AbstractCodec.java | 33 +++++++------ .../dubbo/telnet/LogTelnetHandler.java | 2 +- .../io/RandomAccessByteArrayOutputStream.java | 4 +- 11 files changed, 69 insertions(+), 51 deletions(-) diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/io/StreamUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/io/StreamUtils.java index 2a463c3e0986..ab2836ef9130 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/io/StreamUtils.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/io/StreamUtils.java @@ -40,7 +40,7 @@ public int read() throws IOException { } @Override - public int read(byte b[], int off, int len) throws IOException { + public int read(byte[] b, int off, int len) throws IOException { if (b == null) { throw new NullPointerException(); } diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/io/UnsafeByteArrayInputStream.java b/dubbo-common/src/main/java/org/apache/dubbo/common/io/UnsafeByteArrayInputStream.java index c5b72bdc1992..df96a1c1d012 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/io/UnsafeByteArrayInputStream.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/io/UnsafeByteArrayInputStream.java @@ -23,19 +23,19 @@ * UnsafeByteArrayInputStream. */ public class UnsafeByteArrayInputStream extends InputStream { - protected byte mData[]; + protected byte[] mData; protected int mPosition, mLimit, mMark = 0; - public UnsafeByteArrayInputStream(byte buf[]) { + public UnsafeByteArrayInputStream(byte[] buf) { this(buf, 0, buf.length); } - public UnsafeByteArrayInputStream(byte buf[], int offset) { + public UnsafeByteArrayInputStream(byte[] buf, int offset) { this(buf, offset, buf.length - offset); } - public UnsafeByteArrayInputStream(byte buf[], int offset, int length) { + public UnsafeByteArrayInputStream(byte[] buf, int offset, int length) { mData = buf; mPosition = mMark = offset; mLimit = Math.min(offset + length, buf.length); @@ -47,7 +47,7 @@ public int read() { } @Override - public int read(byte b[], int off, int len) { + public int read(byte[] b, int off, int len) { if (b == null) { throw new NullPointerException(); } diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/io/UnsafeByteArrayOutputStream.java b/dubbo-common/src/main/java/org/apache/dubbo/common/io/UnsafeByteArrayOutputStream.java index 1ac43c893ddd..6e75eb831090 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/io/UnsafeByteArrayOutputStream.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/io/UnsafeByteArrayOutputStream.java @@ -25,7 +25,7 @@ * UnsafeByteArrayOutputStream. */ public class UnsafeByteArrayOutputStream extends OutputStream { - protected byte mBuffer[]; + protected byte[] mBuffer; protected int mCount; @@ -51,7 +51,7 @@ public void write(int b) { } @Override - public void write(byte b[], int off, int len) { + public void write(byte[] b, int off, int len) { if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) > b.length) || ((off + len) < 0)) { throw new IndexOutOfBoundsException(); } diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/json/GenericJSONConverter.java b/dubbo-common/src/main/java/org/apache/dubbo/common/json/GenericJSONConverter.java index 74cf09a142bb..9082eb155cb0 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/json/GenericJSONConverter.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/json/GenericJSONConverter.java @@ -474,7 +474,7 @@ public void writeValue(Object obj, JSONWriter jb, boolean writeClass) throws IOE jb.objectBegin(); Wrapper w = Wrapper.getWrapper(c); - String pns[] = w.getPropertyNames(); + String[] pns = w.getPropertyNames(); for (String pn : pns) { if ((obj instanceof Throwable) && ( diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/json/J2oVisitor.java b/dubbo-common/src/main/java/org/apache/dubbo/common/json/J2oVisitor.java index fdcf674c5904..3aa3fd9ed1ce 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/json/J2oVisitor.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/json/J2oVisitor.java @@ -83,7 +83,7 @@ private static Object toArray(Class c, Stack list, int len) throws Pa return EMPTY_STRING_ARRAY; } else { Object o; - String ss[] = new String[len]; + String[] ss = new String[len]; for (int i = len - 1; i >= 0; i--) { o = list.pop(); ss[i] = (o == null ? null : o.toString()); diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/json/Yylex.java b/dubbo-common/src/main/java/org/apache/dubbo/common/json/Yylex.java index 822175e60315..32a73a7589ec 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/json/Yylex.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/json/Yylex.java @@ -45,7 +45,7 @@ public class Yylex { * at the beginning of a line * l is of the form l = 2*k, k a non negative integer */ - private static final int ZZ_LEXSTATE[] = { + private static final int[] ZZ_LEXSTATE = { 0, 0, 1, 1, 2, 2 }; @@ -92,7 +92,7 @@ public class Yylex { /** * The transition table of the DFA */ - private static final int ZZ_TRANS[] = { + private static final int[] ZZ_TRANS = { 3, 4, 5, 5, 6, 3, 5, 3, 7, 8, 3, 9, 3, 5, 10, 11, 5, 12, 5, 5, 13, 5, 5, 5, 5, 5, 14, 5, 5, 5, @@ -247,7 +247,7 @@ public class Yylex { private static final int ZZ_NO_MATCH = 1; private static final int ZZ_PUSHBACK_2BIG = 2; /* error messages for the codes above */ - private static final String ZZ_ERROR_MSG[] = { + private static final String[] ZZ_ERROR_MSG = { "Unkown internal scanner error", "Error: could not match input", "Error: pushback value was too large" @@ -276,7 +276,7 @@ public class Yylex { * this buffer contains the current text to be matched and is * the source of the yytext() string */ - private char zzBuffer[] = new char[ZZ_BUFFERSIZE]; + private char[] zzBuffer = new char[ZZ_BUFFERSIZE]; /** * the textposition at the last accepting state */ @@ -447,7 +447,7 @@ private boolean zzRefill() throws java.io.IOException { /* is the buffer big enough? */ if (zzCurrentPos >= zzBuffer.length) { /* if not: blow it up */ - char newBuffer[] = new char[zzCurrentPos * 2]; + char[] newBuffer = new char[zzCurrentPos * 2]; System.arraycopy(zzBuffer, 0, newBuffer, 0, zzBuffer.length); zzBuffer = newBuffer; } diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/support/AbortPolicyWithReport.java b/dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/support/AbortPolicyWithReport.java index 8a137613f17f..cb368db895cd 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/support/AbortPolicyWithReport.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/support/AbortPolicyWithReport.java @@ -16,21 +16,21 @@ */ package org.apache.dubbo.common.threadpool.support; -import org.apache.dubbo.common.Constants; -import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.logger.Logger; -import org.apache.dubbo.common.logger.LoggerFactory; -import org.apache.dubbo.common.utils.JVMUtil; - import java.io.File; import java.io.FileOutputStream; import java.text.SimpleDateFormat; import java.util.Date; +import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.RejectedExecutionException; import java.util.concurrent.Semaphore; import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.ExecutorService; + +import org.apache.dubbo.common.Constants; +import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.logger.Logger; +import org.apache.dubbo.common.logger.LoggerFactory; +import org.apache.dubbo.common.utils.JVMUtil; /** * Abort Policy. @@ -46,6 +46,16 @@ public class AbortPolicyWithReport extends ThreadPoolExecutor.AbortPolicy { private static volatile long lastPrintTime = 0; + private static final long TEN_MINUTES_MILLS = 10 * 60 * 1000; + + private static final String OS_WIN_PREFIX = "win"; + + private static final String OS_NAME_KEY = "os.name"; + + private static final String WIN_DATETIME_FORMAT = "yyyy-MM-dd_HH-mm-ss"; + + private static final String DEFAULT_DATETIME_FORMAT = "yyyy-MM-dd_HH:mm:ss"; + private static Semaphore guard = new Semaphore(1); public AbortPolicyWithReport(String threadName, URL url) { @@ -56,11 +66,13 @@ public AbortPolicyWithReport(String threadName, URL url) { @Override public void rejectedExecution(Runnable r, ThreadPoolExecutor e) { String msg = String.format("Thread pool is EXHAUSTED!" + - " Thread Name: %s, Pool Size: %d (active: %d, core: %d, max: %d, largest: %d), Task: %d (completed: %d)," + - " Executor status:(isShutdown:%s, isTerminated:%s, isTerminating:%s), in %s://%s:%d!", - threadName, e.getPoolSize(), e.getActiveCount(), e.getCorePoolSize(), e.getMaximumPoolSize(), e.getLargestPoolSize(), - e.getTaskCount(), e.getCompletedTaskCount(), e.isShutdown(), e.isTerminated(), e.isTerminating(), - url.getProtocol(), url.getIp(), url.getPort()); + " Thread Name: %s, Pool Size: %d (active: %d, core: %d, max: %d, largest: %d), Task: %d (completed: " + + "%d)," + + " Executor status:(isShutdown:%s, isTerminated:%s, isTerminating:%s), in %s://%s:%d!", + threadName, e.getPoolSize(), e.getActiveCount(), e.getCorePoolSize(), e.getMaximumPoolSize(), + e.getLargestPoolSize(), + e.getTaskCount(), e.getCompletedTaskCount(), e.isShutdown(), e.isTerminated(), e.isTerminating(), + url.getProtocol(), url.getIp(), url.getPort()); logger.warn(msg); dumpJStack(); throw new RejectedExecutionException(msg); @@ -70,7 +82,7 @@ private void dumpJStack() { long now = System.currentTimeMillis(); //dump every 10 minutes - if (now - lastPrintTime < 10 * 60 * 1000) { + if (now - lastPrintTime < TEN_MINUTES_MILLS) { return; } @@ -84,18 +96,19 @@ private void dumpJStack() { SimpleDateFormat sdf; - String os = System.getProperty("os.name").toLowerCase(); + String os = System.getProperty(OS_NAME_KEY).toLowerCase(); // window system don't support ":" in file name - if (os.contains("win")) { - sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss"); + if (os.contains(OS_WIN_PREFIX)) { + sdf = new SimpleDateFormat(WIN_DATETIME_FORMAT); } else { - sdf = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss"); + sdf = new SimpleDateFormat(DEFAULT_DATETIME_FORMAT); } String dateStr = sdf.format(new Date()); //try-with-resources - try (FileOutputStream jStackStream = new FileOutputStream(new File(dumpPath, "Dubbo_JStack.log" + "." + dateStr))) { + try (FileOutputStream jStackStream = new FileOutputStream( + new File(dumpPath, "Dubbo_JStack.log" + "." + dateStr))) { JVMUtil.jstack(jStackStream); } catch (Throwable t) { logger.error("dump jStack error", t); diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/telnet/support/command/LogTelnetHandler.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/telnet/support/command/LogTelnetHandler.java index db531dc626f8..2d39c23b73b6 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/telnet/support/command/LogTelnetHandler.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/telnet/support/command/LogTelnetHandler.java @@ -48,7 +48,7 @@ public String telnet(Channel channel, String message) { if (message == null || message.trim().length() == 0) { buf.append("EXAMPLE: log error / log 100"); } else { - String str[] = message.split(" "); + String[] str = message.split(" "); if (!StringUtils.isInteger(str[0])) { LoggerFactory.setLevel(Level.valueOf(message.toUpperCase())); } else { diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractCodec.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractCodec.java index 346f54ca320a..e5f76964370b 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractCodec.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractCodec.java @@ -16,6 +16,9 @@ */ package org.apache.dubbo.remoting.transport; +import java.io.IOException; +import java.net.InetSocketAddress; + import org.apache.dubbo.common.Constants; import org.apache.dubbo.common.URL; import org.apache.dubbo.common.constants.RemotingConstants; @@ -26,9 +29,6 @@ import org.apache.dubbo.remoting.Channel; import org.apache.dubbo.remoting.Codec2; -import java.io.IOException; -import java.net.InetSocketAddress; - /** * AbstractCodec */ @@ -36,13 +36,18 @@ public abstract class AbstractCodec implements Codec2 { private static final Logger logger = LoggerFactory.getLogger(AbstractCodec.class); + private static final String CLIENT_SIDE = "client"; + + private static final String SERVER_SIDE = "server"; + protected static void checkPayload(Channel channel, long size) throws IOException { int payload = RemotingConstants.DEFAULT_PAYLOAD; if (channel != null && channel.getUrl() != null) { payload = channel.getUrl().getParameter(RemotingConstants.PAYLOAD_KEY, RemotingConstants.DEFAULT_PAYLOAD); } if (payload > 0 && size > payload) { - ExceedPayloadLimitException e = new ExceedPayloadLimitException("Data length too large: " + size + ", max payload: " + payload + ", channel: " + channel); + ExceedPayloadLimitException e = new ExceedPayloadLimitException( + "Data length too large: " + size + ", max payload: " + payload + ", channel: " + channel); logger.error(e); throw e; } @@ -53,21 +58,21 @@ protected Serialization getSerialization(Channel channel) { } protected boolean isClientSide(Channel channel) { - String side = (String) channel.getAttribute(Constants.SIDE_KEY); - if ("client".equals(side)) { + String side = (String)channel.getAttribute(Constants.SIDE_KEY); + if (CLIENT_SIDE.equals(side)) { return true; - } else if ("server".equals(side)) { + } else if (SERVER_SIDE.equals(side)) { return false; } else { InetSocketAddress address = channel.getRemoteAddress(); URL url = channel.getUrl(); - boolean client = url.getPort() == address.getPort() - && NetUtils.filterLocalHost(url.getIp()).equals( - NetUtils.filterLocalHost(address.getAddress() - .getHostAddress())); - channel.setAttribute(Constants.SIDE_KEY, client ? "client" - : "server"); - return client; + boolean isClient = url.getPort() == address.getPort() + && NetUtils.filterLocalHost(url.getIp()).equals( + NetUtils.filterLocalHost(address.getAddress() + .getHostAddress())); + channel.setAttribute(Constants.SIDE_KEY, isClient ? CLIENT_SIDE + : SERVER_SIDE); + return isClient; } } diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/LogTelnetHandler.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/LogTelnetHandler.java index b62198d11d43..86bcd9b26a87 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/LogTelnetHandler.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/LogTelnetHandler.java @@ -48,7 +48,7 @@ public String telnet(Channel channel, String message) { if (message == null || message.trim().length() == 0) { buf.append("EXAMPLE: log error / log 100"); } else { - String str[] = message.split(" "); + String[] str = message.split(" "); if (!StringUtils.isInteger(str[0])) { LoggerFactory.setLevel(Level.valueOf(message.toUpperCase())); } else { diff --git a/dubbo-rpc/dubbo-rpc-thrift/src/main/java/org/apache/dubbo/rpc/protocol/thrift/io/RandomAccessByteArrayOutputStream.java b/dubbo-rpc/dubbo-rpc-thrift/src/main/java/org/apache/dubbo/rpc/protocol/thrift/io/RandomAccessByteArrayOutputStream.java index a5d0d119370c..660dd8031dbd 100644 --- a/dubbo-rpc/dubbo-rpc-thrift/src/main/java/org/apache/dubbo/rpc/protocol/thrift/io/RandomAccessByteArrayOutputStream.java +++ b/dubbo-rpc/dubbo-rpc-thrift/src/main/java/org/apache/dubbo/rpc/protocol/thrift/io/RandomAccessByteArrayOutputStream.java @@ -25,7 +25,7 @@ @Deprecated public class RandomAccessByteArrayOutputStream extends OutputStream { - protected byte buffer[]; + protected byte[] buffer; protected int count; @@ -54,7 +54,7 @@ public void write(int b) { } @Override - public void write(byte b[], int off, int len) { + public void write(byte[] b, int off, int len) { if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) > b.length) || ((off + len) < 0)) { throw new IndexOutOfBoundsException();