Skip to content

Commit 96943a5

Browse files
Some Small Code Improvements
I focused on low-hanging fruits that my IDE proposed while making sure, not to change behavior or potentially breaking any API usage. The changes include but are not limited to: - replacing lambdas with method references - Simplifying conditions - Using methods from the JDK over custom ones. - Removing No-Ops
1 parent b7e629c commit 96943a5

31 files changed

Lines changed: 87 additions & 154 deletions

File tree

dbus-java-core/src/main/java/org/freedesktop/dbus/RemoteInvocationHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ public class RemoteInvocationHandler implements InvocationHandler {
3030
private static final Logger LOGGER = LoggerFactory.getLogger(RemoteInvocationHandler.class);
3131

3232
// CHECKSTYLE:OFF
33-
AbstractConnection conn;
34-
RemoteObject remote;
33+
private final AbstractConnection conn;
34+
private final RemoteObject remote;
3535
// CHECKSTYLE:ON
3636

3737
public RemoteInvocationHandler(AbstractConnection _conn, RemoteObject _remote) {

dbus-java-core/src/main/java/org/freedesktop/dbus/StructHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public static <T extends Struct> void convertToStructCollection(Collection<Objec
109109
if (constructorArgClasses.length != object.length) {
110110
throw new IllegalArgumentException("Struct length does not match argument length");
111111
}
112-
T x = createStruct(constructorArgClasses, (Object) object, _structType);
112+
T x = createStruct(constructorArgClasses, object, _structType);
113113
_result.add(x);
114114
}
115115

dbus-java-core/src/main/java/org/freedesktop/dbus/connections/BusAddress.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public static BusAddress of(String _address) {
4949
throw new InvalidBusAddressException("Bus address is blank");
5050
}
5151

52-
BusAddress busAddress = new BusAddress((BusAddress) null);
52+
BusAddress busAddress = new BusAddress(null);
5353

5454
LOGGER.trace("Parsing bus address: {}", _address);
5555

dbus-java-core/src/main/java/org/freedesktop/dbus/connections/SASL.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.io.*;
2121
import java.net.SocketException;
2222
import java.nio.ByteBuffer;
23-
import java.nio.channels.NetworkChannel;
2423
import java.nio.channels.SocketChannel;
2524
import java.nio.charset.Charset;
2625
import java.nio.file.Files;
@@ -600,14 +599,14 @@ public boolean auth(SocketChannel _sock, AbstractTransport _transport) throws IO
600599
switch (state) {
601600
case INITIAL_STATE:
602601
ByteBuffer buf = ByteBuffer.allocate(1);
603-
if (_sock instanceof NetworkChannel) {
602+
if (_sock != null) {
604603
_sock.read(buf); // 0
605604
state = SaslAuthState.WAIT_AUTH;
606605
} else {
607606
try {
608607
int kuid = -1;
609608
if (_transport instanceof AbstractUnixTransport aut) {
610-
kuid = aut.getUid(_sock);
609+
kuid = aut.getUid(null);
611610
}
612611
if (kuid >= 0) {
613612
kernelUid = stupidlyEncode("" + kuid);

dbus-java-core/src/main/java/org/freedesktop/dbus/connections/base/DBusBoundPropertyHandler.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import java.util.*;
2929
import java.util.Map.Entry;
3030
import java.util.concurrent.atomic.AtomicBoolean;
31-
import java.util.stream.Collectors;
3231

3332
/**
3433
* Abstract class containing methods for handling DBus properties and {@link DBusBoundProperty} annotation. <br>
@@ -125,9 +124,9 @@ protected PropHandled handleGetAll(ExportedObject _exportObject, final MethodCal
125124
Object val = invokeMethod(_methodCall, propMeth, object);
126125

127126
// when the value is a collection, array or map, wrap them in a proper variant type
128-
if (val != null && val.getClass().isArray() || Collection.class.isInstance(val) || Map.class.isInstance(val)) {
127+
if (val != null && val.getClass().isArray() || val instanceof Collection || val instanceof Map) {
129128
String[] dataType = Marshalling.getDBusType(propEn.getValue().getGenericReturnType());
130-
String dataTypeStr = Arrays.stream(dataType).collect(Collectors.joining());
129+
String dataTypeStr = String.join("", dataType);
131130
getLogger().trace("Creating embedded Array/Collection/Map of type {}", dataTypeStr);
132131
val = new Variant<>(val, dataTypeStr);
133132
}

dbus-java-core/src/main/java/org/freedesktop/dbus/connections/base/FallbackContainer.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ public synchronized void remove(String _path) {
3030
}
3131

3232
public synchronized ExportedObject get(String _path) {
33-
int best = 0;
3433
ExportedObject bestobject = null;
3534
String[] pathel = _path.split("/");
3635
for (Map.Entry<String[], ExportedObject> entry : fallbacks.entrySet()) {
@@ -48,7 +47,7 @@ public synchronized ExportedObject get(String _path) {
4847
break;
4948
}
5049
}
51-
if (i > 0 && i == fbpath.length && i > best) {
50+
if (i > 0 && i == fbpath.length) {
5251
bestobject = entry.getValue();
5352
}
5453
logger.trace("Matches {} bestobject now {}", i, bestobject);

dbus-java-core/src/main/java/org/freedesktop/dbus/connections/base/IncomingMessageThread.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,11 @@ public void terminate() {
3232
@Override
3333
public void run() {
3434

35-
Message msg;
3635
while (!terminate) {
37-
msg = null;
38-
3936
// read from the wire
4037
try {
4138
// this blocks on outgoing being non-empty or a message being available.
42-
msg = connection.readIncoming();
39+
Message msg = connection.readIncoming();
4340
if (msg != null) {
4441
logger.trace("Read message from {}: {}", connection.getTransport(), msg);
4542

dbus-java-core/src/main/java/org/freedesktop/dbus/connections/impl/DBusConnection.java

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,11 @@ public void register() throws DBusException {
132132
}
133133

134134
/**
135-
* Do some action with the currently registered names in a synchronized manor.
135+
* Do some action with the currently registered names in a synchronized manner.
136136
*
137-
* @param _exClz exception type which may be thrown
138137
* @param _action action to execute
139-
* @param <X> type of exception
140138
*
141139
* @return whatever the action returns
142-
*
143-
* @throws X thrown when action throws
144140
*/
145141
private <T> T doWithBusNamesAndReturn(Function<List<String>, T> _action) {
146142
if (_action == null) {
@@ -152,13 +148,9 @@ private <T> T doWithBusNamesAndReturn(Function<List<String>, T> _action) {
152148
}
153149

154150
/**
155-
* Do some action with the currently registered names in a synchronized manor.
151+
* Do some action with the currently registered names in a synchronized manner.
156152
*
157-
* @param _exClz exception type which may be thrown
158153
* @param _action action to execute
159-
* @param <X> type of exception
160-
*
161-
* @throws X thrown when action throws
162154
*/
163155
private void doWithBusNames(Consumer<List<String>> _action) {
164156
doWithBusNamesAndReturn(bn -> {
@@ -302,7 +294,7 @@ public void requestBusName(String _busname) throws DBusException {
302294
* @return unique name
303295
*/
304296
public String getUniqueName() {
305-
return doWithBusNamesAndReturn(bn -> bn.getFirst());
297+
return doWithBusNamesAndReturn(List::getFirst);
306298
}
307299

308300
/**
@@ -311,11 +303,7 @@ public String getUniqueName() {
311303
* @return connection names
312304
*/
313305
public String[] getNames() {
314-
return doWithBusNamesAndReturn(bn -> {
315-
Set<String> names = new TreeSet<>();
316-
names.addAll(bn);
317-
return names;
318-
}).toArray(String[]::new);
306+
return doWithBusNamesAndReturn(TreeSet::new).toArray(String[]::new);
319307
}
320308

321309
@Override
@@ -514,7 +502,7 @@ public <T extends DBusSignal> AutoCloseable addSigHandler(DBusMatchRule _rule, D
514502
try {
515503
dbus.AddMatch(_rule.toString());
516504
} catch (DBusExecutionException _ex) {
517-
logger.debug("Cannot add match rule: " + _rule.toString(), _ex);
505+
logger.debug("Cannot add match rule: {}", _rule, _ex);
518506
throw new DBusException("Cannot add match rule.", _ex);
519507
}
520508
}

dbus-java-core/src/main/java/org/freedesktop/dbus/connections/impl/DBusConnectionBuilder.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,23 +121,21 @@ private static BusAddress validateTransportAddress(BusAddress _address) {
121121
throw new IllegalArgumentException("No transports found to connect to DBus. Please add at least one transport provider to your classpath");
122122
}
123123

124-
BusAddress address = _address;
125-
126124
// no unix transport but address wants to use a unix socket
127125
if (!TransportBuilder.getRegisteredBusTypes().contains("UNIX")
128-
&& address != null
129-
&& address.isBusType("UNIX")) {
126+
&& _address != null
127+
&& _address.isBusType("UNIX")) {
130128
throw new AddressResolvingException("No transports found to handle UNIX socket connections. Please add a unix-socket transport provider to your classpath");
131129
}
132130

133131
// no tcp transport but TCP address given
134132
if (!TransportBuilder.getRegisteredBusTypes().contains("TCP")
135-
&& address != null
136-
&& address.isBusType("TCP")) {
133+
&& _address != null
134+
&& _address.isBusType("TCP")) {
137135
throw new AddressResolvingException("No transports found to handle TCP connections. Please add a TCP transport provider to your classpath");
138136
}
139137

140-
return address;
138+
return _address;
141139

142140
}
143141

dbus-java-core/src/main/java/org/freedesktop/dbus/connections/impl/IRemoteObjectGetter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ default <I extends DBusInterface> I getPeerRemoteObject(String _busname, DBusPat
6464
* happens, try specifying the interface explicitly.
6565
*
6666
* @param _busname
67-
* The bus name to connect to. Usually a well known bus name name in dot-notation (such as
67+
* The bus name to connect to. Usually a well known bus name in dot-notation (such as
6868
* "org.freedesktop.local") or may be a DBus address such as ":1-16".
6969
* @param _objectpath
7070
* The path on which the process is exporting the object.

0 commit comments

Comments
 (0)