Skip to content

Commit f36bed9

Browse files
CrazyHZMralf0131
authored andcommitted
Code optimization (#3118)
* code optimization * useless import * optimization
1 parent ba27bc5 commit f36bed9

File tree

15 files changed

+97
-60
lines changed

15 files changed

+97
-60
lines changed

dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/directory/StaticDirectory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929

3030
/**
3131
* StaticDirectory
32-
*
3332
*/
3433
public class StaticDirectory<T> extends AbstractDirectory<T> {
3534
private static final Logger logger = LoggerFactory.getLogger(StaticDirectory.class);
@@ -50,8 +49,9 @@ public StaticDirectory(URL url, List<Invoker<T>> invokers) {
5049

5150
public StaticDirectory(URL url, List<Invoker<T>> invokers, RouterChain<T> routerChain) {
5251
super(url == null && invokers != null && !invokers.isEmpty() ? invokers.get(0).getUrl() : url, routerChain);
53-
if (invokers == null || invokers.isEmpty())
52+
if (invokers == null || invokers.isEmpty()) {
5453
throw new IllegalArgumentException("invokers == null");
54+
}
5555
this.invokers = invokers;
5656
}
5757

dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/AbstractRouter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public int compareTo(Router o) {
7070
return (this.getPriority() >= o.getPriority()) ? 1 : -1;
7171
}
7272

73+
@Override
7374
public int getPriority() {
7475
return priority;
7576
}

dubbo-common/src/main/java/org/apache/dubbo/common/logger/Logger.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,144 +28,144 @@ public interface Logger {
2828
*
2929
* @param msg log this message
3030
*/
31-
public void trace(String msg);
31+
void trace(String msg);
3232

3333
/**
3434
* Logs an error with trace log level.
3535
*
3636
* @param e log this cause
3737
*/
38-
public void trace(Throwable e);
38+
void trace(Throwable e);
3939

4040
/**
4141
* Logs an error with trace log level.
4242
*
4343
* @param msg log this message
4444
* @param e log this cause
4545
*/
46-
public void trace(String msg, Throwable e);
46+
void trace(String msg, Throwable e);
4747

4848
/**
4949
* Logs a message with debug log level.
5050
*
5151
* @param msg log this message
5252
*/
53-
public void debug(String msg);
53+
void debug(String msg);
5454

5555
/**
5656
* Logs an error with debug log level.
5757
*
5858
* @param e log this cause
5959
*/
60-
public void debug(Throwable e);
60+
void debug(Throwable e);
6161

6262
/**
6363
* Logs an error with debug log level.
6464
*
6565
* @param msg log this message
6666
* @param e log this cause
6767
*/
68-
public void debug(String msg, Throwable e);
68+
void debug(String msg, Throwable e);
6969

7070
/**
7171
* Logs a message with info log level.
7272
*
7373
* @param msg log this message
7474
*/
75-
public void info(String msg);
75+
void info(String msg);
7676

7777
/**
7878
* Logs an error with info log level.
7979
*
8080
* @param e log this cause
8181
*/
82-
public void info(Throwable e);
82+
void info(Throwable e);
8383

8484
/**
8585
* Logs an error with info log level.
8686
*
8787
* @param msg log this message
8888
* @param e log this cause
8989
*/
90-
public void info(String msg, Throwable e);
90+
void info(String msg, Throwable e);
9191

9292
/**
9393
* Logs a message with warn log level.
9494
*
9595
* @param msg log this message
9696
*/
97-
public void warn(String msg);
97+
void warn(String msg);
9898

9999
/**
100100
* Logs a message with warn log level.
101101
*
102102
* @param e log this message
103103
*/
104-
public void warn(Throwable e);
104+
void warn(Throwable e);
105105

106106
/**
107107
* Logs a message with warn log level.
108108
*
109109
* @param msg log this message
110110
* @param e log this cause
111111
*/
112-
public void warn(String msg, Throwable e);
112+
void warn(String msg, Throwable e);
113113

114114
/**
115115
* Logs a message with error log level.
116116
*
117117
* @param msg log this message
118118
*/
119-
public void error(String msg);
119+
void error(String msg);
120120

121121
/**
122122
* Logs an error with error log level.
123123
*
124124
* @param e log this cause
125125
*/
126-
public void error(Throwable e);
126+
void error(Throwable e);
127127

128128
/**
129129
* Logs an error with error log level.
130130
*
131131
* @param msg log this message
132132
* @param e log this cause
133133
*/
134-
public void error(String msg, Throwable e);
134+
void error(String msg, Throwable e);
135135

136136
/**
137137
* Is trace logging currently enabled?
138138
*
139139
* @return true if trace is enabled
140140
*/
141-
public boolean isTraceEnabled();
141+
boolean isTraceEnabled();
142142

143143
/**
144144
* Is debug logging currently enabled?
145145
*
146146
* @return true if debug is enabled
147147
*/
148-
public boolean isDebugEnabled();
148+
boolean isDebugEnabled();
149149

150150
/**
151151
* Is info logging currently enabled?
152152
*
153153
* @return true if info is enabled
154154
*/
155-
public boolean isInfoEnabled();
155+
boolean isInfoEnabled();
156156

157157
/**
158158
* Is warn logging currently enabled?
159159
*
160160
* @return true if warn is enabled
161161
*/
162-
public boolean isWarnEnabled();
162+
boolean isWarnEnabled();
163163

164164
/**
165165
* Is error logging currently enabled?
166166
*
167167
* @return true if error is enabled
168168
*/
169-
public boolean isErrorEnabled();
169+
boolean isErrorEnabled();
170170

171171
}

dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package org.apache.dubbo.common.utils;
1818

19+
import org.apache.dubbo.common.Constants;
1920
import org.apache.dubbo.common.URL;
2021
import org.apache.dubbo.common.logger.Logger;
2122
import org.apache.dubbo.common.logger.LoggerFactory;
@@ -37,8 +38,6 @@
3738
*/
3839
public class NetUtils {
3940

40-
public static final String LOCALHOST = "127.0.0.1";
41-
public static final String ANYHOST = "0.0.0.0";
4241
private static final Logger logger = LoggerFactory.getLogger(NetUtils.class);
4342
private static final int RND_PORT_START = 30000;
4443

@@ -109,18 +108,18 @@ public static boolean isValidAddress(String address) {
109108
public static boolean isLocalHost(String host) {
110109
return host != null
111110
&& (LOCAL_IP_PATTERN.matcher(host).matches()
112-
|| host.equalsIgnoreCase("localhost"));
111+
|| host.equalsIgnoreCase(Constants.LOCALHOST_KEY));
113112
}
114113

115114
public static boolean isAnyHost(String host) {
116-
return "0.0.0.0".equals(host);
115+
return Constants.ANYHOST_VALUE.equals(host);
117116
}
118117

119118
public static boolean isInvalidLocalHost(String host) {
120119
return host == null
121120
|| host.length() == 0
122-
|| host.equalsIgnoreCase("localhost")
123-
|| host.equals("0.0.0.0")
121+
|| host.equalsIgnoreCase(Constants.LOCALHOST_KEY)
122+
|| host.equals(Constants.ANYHOST_VALUE)
124123
|| (LOCAL_IP_PATTERN.matcher(host).matches());
125124
}
126125

@@ -139,13 +138,14 @@ static boolean isValidAddress(InetAddress address) {
139138
}
140139
String name = address.getHostAddress();
141140
return (name != null
142-
&& !ANYHOST.equals(name)
143-
&& !LOCALHOST.equals(name)
144-
&& IP_PATTERN.matcher(name).matches());
141+
&& IP_PATTERN.matcher(name).matches()
142+
&& !Constants.ANYHOST_VALUE.equals(name)
143+
&& !Constants.LOCALHOST_VALUE.equals(name));
145144
}
146145

147146
/**
148147
* Check if an ipv6 address is reachable.
148+
*
149149
* @param address the given address
150150
* @return true if it is reachable
151151
*/
@@ -166,12 +166,13 @@ static boolean isValidV6Address(Inet6Address address) {
166166
* normalize the ipv6 Address, convert scope name to scope id.
167167
* e.g.
168168
* convert
169-
* fe80:0:0:0:894:aeec:f37d:23e1%en0
169+
* fe80:0:0:0:894:aeec:f37d:23e1%en0
170170
* to
171-
* fe80:0:0:0:894:aeec:f37d:23e1%5
172-
*
171+
* fe80:0:0:0:894:aeec:f37d:23e1%5
172+
* <p>
173173
* The %5 after ipv6 address is called scope id.
174174
* see java doc of {@link Inet6Address} for more details.
175+
*
175176
* @param address the input address
176177
* @return the normalized address, with scope id converted to int
177178
*/
@@ -191,7 +192,7 @@ static InetAddress normalizeV6Address(Inet6Address address) {
191192

192193
public static String getLocalHost() {
193194
InetAddress address = getLocalAddress();
194-
return address == null ? LOCALHOST : address.getHostAddress();
195+
return address == null ? Constants.LOCALHOST_VALUE : address.getHostAddress();
195196
}
196197

197198
public static String filterLocalHost(String host) {
@@ -236,7 +237,7 @@ private static InetAddress getLocalAddress0() {
236237
localAddress = InetAddress.getLocalHost();
237238
if (localAddress instanceof Inet6Address) {
238239
Inet6Address address = (Inet6Address) localAddress;
239-
if (isValidV6Address(address)){
240+
if (isValidV6Address(address)) {
240241
return normalizeV6Address(address);
241242
}
242243
} else if (isValidAddress(localAddress)) {
@@ -259,7 +260,7 @@ private static InetAddress getLocalAddress0() {
259260
InetAddress address = addresses.nextElement();
260261
if (address instanceof Inet6Address) {
261262
Inet6Address v6Address = (Inet6Address) address;
262-
if (isValidV6Address(v6Address)){
263+
if (isValidV6Address(v6Address)) {
263264
return normalizeV6Address(v6Address);
264265
}
265266
} else if (isValidAddress(address)) {

dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractConfig.java

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -304,9 +304,9 @@ protected static void checkExtension(Class<?> type, String property, String valu
304304
* Check whether there is a <code>Extension</code> who's name (property) is <code>value</code> (special treatment is
305305
* required)
306306
*
307-
* @param type The Extension type
307+
* @param type The Extension type
308308
* @param property The extension key
309-
* @param value The Extension name
309+
* @param value The Extension name
310310
*/
311311
protected static void checkMultiExtension(Class<?> type, String property, String value) {
312312
checkMultiName(property, value);
@@ -485,12 +485,7 @@ public Map<String, String> getMetaData() {
485485
for (Method method : methods) {
486486
try {
487487
String name = method.getName();
488-
if ((name.startsWith("get") || name.startsWith("is"))
489-
&& !name.equals("get")
490-
&& !"getClass".equals(name)
491-
&& Modifier.isPublic(method.getModifiers())
492-
&& method.getParameterTypes().length == 0
493-
&& ClassHelper.isPrimitive(method.getReturnType())) {
488+
if (isMetaMethod(method)) {
494489
String prop = calculateAttributeFromGetter(name);
495490
String key;
496491
Parameter parameter = method.getAnnotation(Parameter.class);
@@ -620,4 +615,27 @@ public boolean isValid() {
620615
return true;
621616
}
622617

618+
private boolean isMetaMethod(Method method) {
619+
String name = method.getName();
620+
if (!(name.startsWith("get") || name.startsWith("is"))) {
621+
return false;
622+
}
623+
if ("get".equals(name)) {
624+
return false;
625+
}
626+
if ("getClass".equals(name)) {
627+
return false;
628+
}
629+
if (!Modifier.isPublic(method.getModifiers())) {
630+
return false;
631+
}
632+
if (method.getParameterTypes().length != 0) {
633+
return false;
634+
}
635+
if (!ClassHelper.isPrimitive(method.getReturnType())) {
636+
return false;
637+
}
638+
return true;
639+
}
640+
623641
}

dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public class ReferenceConfig<T> extends AbstractReferenceConfig {
7272
*
7373
* <li>when the url is dubbo://224.5.6.7:1234/org.apache.dubbo.config.api.DemoService?application=dubbo-sample, then
7474
* the protocol is <b>DubboProtocol</b></li>
75-
*
75+
* <p>
7676
* Actually,when the {@link ExtensionLoader} init the {@link Protocol} instants,it will automatically wraps two
7777
* layers, and eventually will get a <b>ProtocolFilterWrapper</b> or <b>ProtocolListenerWrapper</b>
7878
*/
@@ -327,7 +327,7 @@ private T createProxy(Map<String, String> map) {
327327
}
328328

329329
if (isJvmRefer) {
330-
URL url = new URL(Constants.LOCAL_PROTOCOL, NetUtils.LOCALHOST, 0, interfaceClass.getName()).addParameters(map);
330+
URL url = new URL(Constants.LOCAL_PROTOCOL, Constants.LOCALHOST_VALUE, 0, interfaceClass.getName()).addParameters(map);
331331
invoker = refprotocol.refer(interfaceClass, url);
332332
if (logger.isInfoEnabled()) {
333333
logger.info("Using injvm service " + interfaceClass.getName());

dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
import java.util.concurrent.ScheduledExecutorService;
5858
import java.util.concurrent.TimeUnit;
5959

60-
import static org.apache.dubbo.common.utils.NetUtils.LOCALHOST;
60+
import static org.apache.dubbo.common.Constants.LOCALHOST_VALUE;
6161
import static org.apache.dubbo.common.utils.NetUtils.getAvailablePort;
6262
import static org.apache.dubbo.common.utils.NetUtils.getLocalHost;
6363
import static org.apache.dubbo.common.utils.NetUtils.isInvalidLocalHost;
@@ -82,7 +82,7 @@ public class ServiceConfig<T> extends AbstractServiceConfig {
8282
*
8383
* <li>when the url is dubbo://224.5.6.7:1234/org.apache.dubbo.config.api.DemoService?application=dubbo-sample, then
8484
* the protocol is <b>DubboProtocol</b></li>
85-
*
85+
* <p>
8686
* Actually,when the {@link ExtensionLoader} init the {@link Protocol} instants,it will automatically wraps two
8787
* layers, and eventually will get a <b>ProtocolFilterWrapper</b> or <b>ProtocolListenerWrapper</b>
8888
*/
@@ -583,7 +583,7 @@ private void exportLocal(URL url) {
583583
if (!Constants.LOCAL_PROTOCOL.equalsIgnoreCase(url.getProtocol())) {
584584
URL local = URL.valueOf(url.toFullString())
585585
.setProtocol(Constants.LOCAL_PROTOCOL)
586-
.setHost(LOCALHOST)
586+
.setHost(LOCALHOST_VALUE)
587587
.setPort(0);
588588
Exporter<?> exporter = protocol.export(
589589
proxyFactory.getInvoker(ref, (Class) interfaceClass, local));

0 commit comments

Comments
 (0)