Skip to content

Commit 635a8c6

Browse files
authored
A series of small fixes that Improve Documentation and Code Quality (SonarQube recommendations) (#424)
* Fixing Javadoc Errors * SonarQube Fix: Performance - Method invokes inefficient Number constructor; use static valueOf instead * SonarQube: Security - Array is stored directly * Fixing null pointer issues with new clone method Signed-off-by: James Sutton <james.sutton@uk.ibm.com>
1 parent adc1160 commit 635a8c6

27 files changed

Lines changed: 376 additions & 340 deletions

File tree

org.eclipse.paho.client.mqttv3/src/main/java-templates/org/eclipse/paho/client/mqttv3/internal/ClientComms.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,11 @@ public void sendNoWait(MqttWireMessage message, MqttToken token) throws MqttExce
203203

204204
/**
205205
* Removes the message corresponding to the token from the outbound queue and persistence.
206-
* @param token The {@link IMqttDeliveryMqttToken} to remove
206+
* @param token The {@link IMqttDeliveryToken} to remove
207207
* @return if the message is removed, then true, otherwise false
208208
* @throws MqttException if an error occurs sending the message
209209
*/
210210
public boolean removeMessage(IMqttDeliveryToken token) throws MqttException {
211-
final String methodName = "removeMessage";
212211
return this.clientState.removeMessage(token);
213212
}
214213

@@ -217,6 +216,7 @@ public boolean removeMessage(IMqttDeliveryToken token) throws MqttException {
217216
*
218217
* Call each main class and let it tidy up e.g. releasing the token
219218
* store which normally survives a disconnect.
219+
* @param force - whether to force the connection to close.
220220
* @throws MqttException if not disconnected
221221
*/
222222
public void close(boolean force) throws MqttException {
@@ -293,7 +293,7 @@ public void connect(MqttConnectOptions options, MqttToken token) throws MqttExce
293293
}
294294
else {
295295
// @TRACE 207=connect failed: not disconnected {0}
296-
log.fine(CLASS_NAME,methodName,"207", new Object[] {new Byte(conState)});
296+
log.fine(CLASS_NAME,methodName,"207", new Object[] {Byte.valueOf(conState)});
297297
if (isClosed() || closePending) {
298298
throw new MqttException(MqttException.REASON_CODE_CLIENT_CLOSED);
299299
} else if (isConnecting()) {
@@ -322,7 +322,7 @@ public void connectComplete( MqttConnack cack, MqttException mex) throws MqttExc
322322
}
323323

324324
// @TRACE 204=connect failed: rc={0}
325-
log.fine(CLASS_NAME,methodName,"204", new Object[]{new Integer(rc)});
325+
log.fine(CLASS_NAME,methodName,"204", new Object[]{Integer.valueOf(rc)});
326326
throw mex;
327327
}
328328

@@ -426,7 +426,7 @@ public void shutdownConnection(MqttToken token, MqttException reason) {
426426
// it now. This is done at the end to allow a new connect
427427
// to be processed and now throw a currently disconnecting error.
428428
// any outstanding tokens and unblock any waiters
429-
if (endToken != null & callback != null) {
429+
if (endToken != null && callback != null) {
430430
callback.asyncOperationComplete(endToken);
431431
}
432432

@@ -627,7 +627,7 @@ public NetworkModule[] getNetworkModules() {
627627
return networkModules;
628628
}
629629
public void setNetworkModules(NetworkModule[] networkModules) {
630-
this.networkModules = networkModules;
630+
this.networkModules = networkModules.clone();
631631
}
632632
public MqttDeliveryToken[] getPendingDeliveryTokens() {
633633
return tokenStore.getOutstandingDelTokens();
@@ -659,7 +659,7 @@ public MqttConnectOptions getConOptions() {
659659

660660
public Properties getDebug() {
661661
Properties props = new Properties();
662-
props.put("conState", new Integer(conState));
662+
props.put("conState", Integer.valueOf(conState));
663663
props.put("serverURI", getClient().getServerURI());
664664
props.put("callback", callback);
665665
props.put("stoppingComms", new Boolean(stoppingComms));

org.eclipse.paho.client.mqttv3/src/main/java/org/eclipse/paho/client/mqttv3/MqttAsyncClient.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,7 @@ public IMqttToken connect(MqttConnectOptions options, Object userContext, IMqttA
750750
// userName={3} password={4} will={5} userContext={6} callback={7}
751751
log.fine(CLASS_NAME, methodName, "103",
752752
new Object[] { Boolean.valueOf(options.isCleanSession()), new Integer(options.getConnectionTimeout()),
753-
new Integer(options.getKeepAliveInterval()), options.getUserName(),
753+
Integer.valueOf(options.getKeepAliveInterval()), options.getUserName(),
754754
((null == options.getPassword()) ? "[null]" : "[notnull]"),
755755
((null == options.getWillMessage()) ? "[null]" : "[notnull]"), userContext, callback });
756756
comms.setNetworkModules(createNetworkModules(serverURI, options));
@@ -815,7 +815,7 @@ public IMqttToken disconnect(long quiesceTimeout, Object userContext, IMqttActio
815815
throws MqttException {
816816
final String methodName = "disconnect";
817817
// @TRACE 104=> quiesceTimeout={0} userContext={1} callback={2}
818-
log.fine(CLASS_NAME, methodName, "104", new Object[] { new Long(quiesceTimeout), userContext, callback });
818+
log.fine(CLASS_NAME, methodName, "104", new Object[] { Long.valueOf(quiesceTimeout), userContext, callback });
819819

820820
MqttToken token = new MqttToken(getClientId());
821821
token.setActionCallback(callback);
@@ -1427,7 +1427,7 @@ private void attemptReconnect() {
14271427
private void startReconnectCycle() {
14281428
String methodName = "startReconnectCycle";
14291429
// @Trace 503=Start reconnect timer for client: {0}, delay: {1}
1430-
log.fine(CLASS_NAME, methodName, "503", new Object[] { this.clientId, new Long(reconnectDelay) });
1430+
log.fine(CLASS_NAME, methodName, "503", new Object[] { this.clientId, Long.valueOf(reconnectDelay) });
14311431
reconnectTimer = new Timer("MQTT Reconnect: " + clientId);
14321432
reconnectTimer.schedule(new ReconnectTask(), reconnectDelay);
14331433
}

org.eclipse.paho.client.mqttv3/src/main/java/org/eclipse/paho/client/mqttv3/MqttConnectOptions.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public class MqttConnectOptions {
7878
private boolean cleanSession = CLEAN_SESSION_DEFAULT;
7979
private int connectionTimeout = CONNECTION_TIMEOUT_DEFAULT;
8080
private String[] serverURIs = null;
81-
private int MqttVersion = MQTT_VERSION_DEFAULT;
81+
private int mqttVersion = MQTT_VERSION_DEFAULT;
8282
private boolean automaticReconnect = false;
8383

8484
/**
@@ -97,6 +97,7 @@ public class MqttConnectOptions {
9797
* More information about these values can be found in the setter methods.
9898
*/
9999
public MqttConnectOptions() {
100+
// Initialise Base MqttConnectOptions Object
100101
}
101102

102103
/**
@@ -112,7 +113,7 @@ public char[] getPassword() {
112113
* @param password A Char Array of the password
113114
*/
114115
public void setPassword(char[] password) {
115-
this.password = password;
116+
this.password = password.clone();
116117
}
117118

118119
/**
@@ -213,7 +214,7 @@ public int getKeepAliveInterval() {
213214
* @return the MQTT version.
214215
*/
215216
public int getMqttVersion() {
216-
return MqttVersion;
217+
return mqttVersion;
217218
}
218219

219220
/**
@@ -515,7 +516,7 @@ public void setServerURIs(String[] array) {
515516
for (int i = 0; i < array.length; i++) {
516517
validateURI(array[i]);
517518
}
518-
this.serverURIs = array;
519+
this.serverURIs = array.clone();
519520
}
520521

521522
/**
@@ -563,16 +564,16 @@ else if ("local".equals(vURI.getScheme())) {
563564
* Version 3.1.1 or 3.1 can be selected specifically, with no fall back,
564565
* by using the MQTT_VERSION_3_1_1 or MQTT_VERSION_3_1 options respectively.
565566
*
566-
* @param MqttVersion the version of the MQTT protocol.
567+
* @param mqttVersion the version of the MQTT protocol.
567568
* @throws IllegalArgumentException If the MqttVersion supplied is invalid
568569
*/
569-
public void setMqttVersion(int MqttVersion)throws IllegalArgumentException {
570-
if (MqttVersion != MQTT_VERSION_DEFAULT &&
571-
MqttVersion != MQTT_VERSION_3_1 &&
572-
MqttVersion != MQTT_VERSION_3_1_1) {
570+
public void setMqttVersion(int mqttVersion)throws IllegalArgumentException {
571+
if (mqttVersion != MQTT_VERSION_DEFAULT &&
572+
mqttVersion != MQTT_VERSION_3_1 &&
573+
mqttVersion != MQTT_VERSION_3_1_1) {
573574
throw new IllegalArgumentException();
574575
}
575-
this.MqttVersion = MqttVersion;
576+
this.mqttVersion = mqttVersion;
576577
}
577578

578579
/**
@@ -608,10 +609,10 @@ public void setAutomaticReconnect(boolean automaticReconnect) {
608609
public Properties getDebug() {
609610
final String strNull="null";
610611
Properties p = new Properties();
611-
p.put("MqttVersion", new Integer(getMqttVersion()));
612+
p.put("MqttVersion", Integer.valueOf(getMqttVersion()));
612613
p.put("CleanSession", Boolean.valueOf(isCleanSession()));
613-
p.put("ConTimeout", new Integer(getConnectionTimeout()));
614-
p.put("KeepAliveInterval", new Integer(getKeepAliveInterval()));
614+
p.put("ConTimeout", Integer.valueOf(getConnectionTimeout()));
615+
p.put("KeepAliveInterval", Integer.valueOf(getKeepAliveInterval()));
615616
p.put("UserName", (getUserName() == null) ? strNull : getUserName());
616617
p.put("WillDestination", (getWillDestination() == null) ? strNull : getWillDestination());
617618
if (getSocketFactory()==null) {

org.eclipse.paho.client.mqttv3/src/main/java/org/eclipse/paho/client/mqttv3/MqttMessage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public void setPayload(byte[] payload) {
9595
if (payload == null) {
9696
throw new NullPointerException();
9797
}
98-
this.payload = payload;
98+
this.payload = payload.clone();
9999
}
100100

101101
/**

org.eclipse.paho.client.mqttv3/src/main/java/org/eclipse/paho/client/mqttv3/MqttTopic.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public static void validate(String topicString, boolean wildcardAllowed)
165165
// NOT encode to more than 65535 bytes
166166
if (topicLen < MIN_TOPIC_LEN || topicLen > MAX_TOPIC_LEN) {
167167
throw new IllegalArgumentException(String.format("Invalid topic length, should be in range[%d, %d]!",
168-
new Object[] { new Integer(MIN_TOPIC_LEN), new Integer(MAX_TOPIC_LEN) }));
168+
new Object[] { Integer.valueOf(MIN_TOPIC_LEN), Integer.valueOf(MAX_TOPIC_LEN) }));
169169
}
170170

171171
// *******************************************************************************

org.eclipse.paho.client.mqttv3/src/main/java/org/eclipse/paho/client/mqttv3/ScheduledExecutorPingSender.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
/**
2626
* Default ping sender implementation
2727
*
28-
* <p>This class implements the {@link IMqttPingSender} pinger interface
28+
* <p>This class implements the {@link MqttPingSender} pinger interface
2929
* allowing applications to send ping packet to server every keep alive interval.
3030
* </p>
3131
*
@@ -84,7 +84,7 @@ public void run() {
8484
String originalThreadName = Thread.currentThread().getName();
8585
Thread.currentThread().setName("MQTT Ping: " + clientid);
8686
//@Trace 660=Check schedule at {0}
87-
log.fine(CLASS_NAME, methodName, "660", new Object[]{ new Long(System.currentTimeMillis()) });
87+
log.fine(CLASS_NAME, methodName, "660", new Object[]{ Long.valueOf(System.currentTimeMillis()) });
8888
comms.checkForActivity();
8989
Thread.currentThread().setName(originalThreadName);
9090
}

org.eclipse.paho.client.mqttv3/src/main/java/org/eclipse/paho/client/mqttv3/TimerPingSender.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ private class PingTask extends TimerTask {
7373

7474
public void run() {
7575
//@Trace 660=Check schedule at {0}
76-
log.fine(CLASS_NAME, methodName, "660", new Object[]{new Long(System.currentTimeMillis())});
76+
log.fine(CLASS_NAME, methodName, "660", new Object[]{Long.valueOf(System.currentTimeMillis())});
7777
comms.checkForActivity();
7878
}
7979
}

0 commit comments

Comments
 (0)