Skip to content

Commit 16d171b

Browse files
authored
Merge pull request #292 from hypfvieh/jdk21-improvements
Applied improvements possible with Java 21
2 parents 6b4b5ca + 44fca65 commit 16d171b

71 files changed

Lines changed: 202 additions & 92 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ private static String[] recursiveGetDBusType(StringBuffer[] _out, Type _dataType
214214
throw new DBusException(ERROR_MULTI_VALUED_ARRAY);
215215
}
216216
_out[_level].append(s[0]);
217-
} else if (_dataType instanceof Class<?> && DBusSerializable.class.isAssignableFrom((Class<?>) _dataType)
217+
} else if (_dataType instanceof Class<?> dtClazz && DBusSerializable.class.isAssignableFrom(dtClazz)
218218
|| _dataType instanceof ParameterizedType pt
219219
&& DBusSerializable.class.isAssignableFrom((Class<?>) pt.getRawType())) {
220220
// it's a custom serializable type
@@ -395,12 +395,12 @@ public static int getJavaType(String _dbusType, List<Type> _resultValue, int _li
395395
if (ArgumentType.DICT_ENTRY1 == _dbusType.charAt(idx + 1)) {
396396
contained = new ArrayList<>();
397397
int javaType = getJavaType(_dbusType.substring(idx + 2), contained, 2);
398-
_resultValue.add(new DBusMapType(contained.get(0), contained.get(1)));
398+
_resultValue.add(new DBusMapType(contained.getFirst(), contained.get(1)));
399399
idx += javaType + 2;
400400
} else {
401401
contained = new ArrayList<>();
402402
int javaType = getJavaType(_dbusType.substring(idx + 1), contained, 1);
403-
_resultValue.add(new DBusListType(contained.get(0)));
403+
_resultValue.add(new DBusListType(contained.getFirst()));
404404
idx += javaType;
405405
}
406406
break;
@@ -452,7 +452,7 @@ public static int getJavaType(String _dbusType, List<Type> _resultValue, int _li
452452
case ArgumentType.DICT_ENTRY1:
453453
contained = new ArrayList<>();
454454
int javaType = getJavaType(_dbusType.substring(idx + 1), contained, 2);
455-
_resultValue.add(new DBusMapType(contained.get(0), contained.get(1)));
455+
_resultValue.add(new DBusMapType(contained.getFirst(), contained.get(1)));
456456
idx += javaType + 1;
457457
break;
458458
default:
@@ -577,33 +577,33 @@ static Object deSerializeParameter(Object _parameter, Type _type, AbstractConnec
577577
}
578578

579579
// Turn a signature into a Type[]
580-
if (_type instanceof Class && ((Class<?>) _type).isArray() && ((Class<?>) _type).getComponentType().equals(Type.class) && parameter instanceof String) {
580+
if (_type instanceof Class<?> tClazz && tClazz.isArray() && tClazz.getComponentType().equals(Type.class) && parameter instanceof String str) {
581581
List<Type> rv = new ArrayList<>();
582-
getJavaType((String) parameter, rv, -1);
582+
getJavaType(str, rv, -1);
583583
parameter = rv.toArray(EMPTY_TYPE_ARRAY);
584584
}
585585

586586
if (parameter instanceof DBusPath op) {
587587
LOGGER.trace("Parameter is DBusPath");
588-
if (_type instanceof Class && DBusInterface.class.isAssignableFrom((Class<?>) _type)) {
588+
if (_type instanceof Class<?> tClazz && DBusInterface.class.isAssignableFrom(tClazz)) {
589589
parameter = _conn.getExportedObject(op.getSource(), op.getPath(), (Class<DBusInterface>) _type);
590590
} else {
591591
parameter = new DBusPath(op.getPath());
592592
}
593593
}
594594

595595
// its an enum, parse either as the string name or the ordinal
596-
if (parameter instanceof String str && _type instanceof Class && Enum.class.isAssignableFrom((Class<?>) _type)) {
596+
if (parameter instanceof String str && _type instanceof Class<?> tClazz && Enum.class.isAssignableFrom(tClazz)) {
597597
LOGGER.trace("Type seems to be an enum");
598598
parameter = Enum.valueOf((Class<Enum>) _type, str);
599599
}
600600

601601
// it should be a struct. create it
602-
if (parameter instanceof Object[] objArr && _type instanceof Class && Struct.class.isAssignableFrom((Class<?>) _type)) {
602+
if (parameter instanceof Object[] objArr && _type instanceof Class<?> tClazz && Struct.class.isAssignableFrom(tClazz)) {
603603
LOGGER.trace("Creating Struct {} from {}", _type, parameter);
604604
Type[] ts = Container.getTypeCache(_type);
605605
if (ts == null) {
606-
Field[] fs = ((Class<?>) _type).getDeclaredFields();
606+
Field[] fs = tClazz.getDeclaredFields();
607607
ts = new Type[fs.length];
608608
for (Field f : fs) {
609609
Position p = f.getAnnotation(Position.class);
@@ -617,7 +617,7 @@ static Object deSerializeParameter(Object _parameter, Type _type, AbstractConnec
617617

618618
// recurse over struct contents
619619
parameter = deSerializeParameters(objArr, ts, _conn);
620-
for (Constructor<?> con : ((Class<?>) _type).getDeclaredConstructors()) {
620+
for (Constructor<?> con : tClazz.getDeclaredConstructors()) {
621621
try {
622622
LOGGER.trace("Trying to create instance of {} using constructor {} with parameters: {}", _type, con, objArr);
623623
parameter = con.newInstance(objArr);
@@ -778,8 +778,8 @@ public static Object[] deSerializeParameters(Object[] _parameters, Type[] _types
778778
continue;
779779
}
780780

781-
if (types[i] instanceof Class
782-
&& DBusSerializable.class.isAssignableFrom((Class<? extends Object>) types[i])
781+
if (types[i] instanceof Class<? extends Object> tClazz
782+
&& DBusSerializable.class.isAssignableFrom(tClazz)
783783
|| types[i] instanceof ParameterizedType pt
784784
&& DBusSerializable.class.isAssignableFrom((Class<? extends Object>) pt.getRawType())) {
785785
Class<? extends DBusSerializable> dsc;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,8 @@ protected PropHandled handleGetAll(ExportedObject _exportObject, final MethodCal
155155
} catch (Throwable _ex) {
156156
getLogger().debug("Failed to invoke method call", _ex);
157157
handleException(_methodCall,
158-
new DBusExecutionException(String.format("Error Executing Method %s.%s: %s",
159-
_methodCall.getInterface(), _methodCall.getName(), _ex.getMessage()), _ex));
158+
new DBusExecutionException("Error Executing Method %s.%s: %s".formatted(
159+
_methodCall.getInterface(), _methodCall.getName(), _ex.getMessage()), _ex));
160160
}
161161
});
162162
return PropHandled.HANDLED;

dbus-java-core/src/main/java/org/freedesktop/dbus/connections/config/ReceivingServiceConfigBuilder.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public ReceivingServiceConfigBuilder<R> withMethodReturnThreadPriority(int _prio
165165
/**
166166
* Use virtual threads for the created signal thread(s).
167167
* <p>
168-
* Default: {@value false};
168+
* Default: {@code false}
169169
*
170170
* @param _virtual true to use virtual threads, false to use native threads
171171
* @return this
@@ -180,7 +180,7 @@ public ReceivingServiceConfigBuilder<R> withVirtualSignalThreads(boolean _virtua
180180
/**
181181
* Use virtual threads for the created error thread(s).
182182
* <p>
183-
* Default: {@value false};
183+
* Default: {@code false}
184184
*
185185
* @param _virtual true to use virtual threads, false to use native threads
186186
* @return this
@@ -195,7 +195,7 @@ public ReceivingServiceConfigBuilder<R> withVirtualErrorThreads(boolean _virtual
195195
/**
196196
* Use virtual threads for the created method call thread(s).
197197
* <p>
198-
* Default: {@value false};
198+
* Default: {@code false}
199199
*
200200
* @param _virtual true to use virtual threads, false to use native threads
201201
* @return this
@@ -210,7 +210,7 @@ public ReceivingServiceConfigBuilder<R> withMethodCallSignalThreads(boolean _vir
210210
/**
211211
* Use virtual threads for the created method return thread(s).
212212
* <p>
213-
* Default: {@value false};
213+
* Default: {@code false}
214214
*
215215
* @param _virtual true to use virtual threads, false to use native threads
216216
* @return this
@@ -225,7 +225,7 @@ public ReceivingServiceConfigBuilder<R> withVirtualMethodReturnThreads(boolean _
225225
/**
226226
* Use virtual threads for all created thread(s).
227227
* <p>
228-
* Default: {@value false};
228+
* Default: {@code false}
229229
*
230230
* @param _virtual true to use virtual threads, false to use native threads
231231
* @return this

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ public <T extends DBusInterface> T dynamicProxy(String _source, String _path, Cl
212212
}
213213

214214
RemoteObject ro = new RemoteObject(_source, _path, _type, false);
215-
DBusInterface newi = (DBusInterface) Proxy.newProxyInstance(ifcs.get(0).getClassLoader(),
215+
DBusInterface newi = (DBusInterface) Proxy.newProxyInstance(ifcs.getFirst().getClassLoader(),
216216
ifcs.toArray(Class[]::new), new RemoteInvocationHandler(this, ro));
217217
getImportedObjects().put(newi, ro);
218218

@@ -302,7 +302,7 @@ public void requestBusName(String _busname) throws DBusException {
302302
* @return unique name
303303
*/
304304
public String getUniqueName() {
305-
return doWithBusNamesAndReturn(bn -> bn.get(0));
305+
return doWithBusNamesAndReturn(bn -> bn.getFirst());
306306
}
307307

308308
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ <T extends DBusInterface> T dynamicProxy(String _path, Class<T> _type) throws DB
8787
}
8888

8989
RemoteObject ro = new RemoteObject(null, _path, _type, false);
90-
DBusInterface newi = (DBusInterface) Proxy.newProxyInstance(ifcs.get(0).getClassLoader(), ifcs.toArray(EMPTY_CLASS_ARRAY), new RemoteInvocationHandler(this, ro));
90+
DBusInterface newi = (DBusInterface) Proxy.newProxyInstance(ifcs.getFirst().getClassLoader(), ifcs.toArray(EMPTY_CLASS_ARRAY), new RemoteInvocationHandler(this, ro));
9191
getImportedObjects().put(newi, ro);
9292
return (T) newi;
9393
} catch (Exception _ex) {

dbus-java-core/src/main/java/org/freedesktop/dbus/errors/AccessDenied.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
import org.freedesktop.dbus.exceptions.DBusExecutionException;
44

5+
import java.io.Serial;
6+
57
/**
68
* Thrown if a message is denied due to a security policy
79
*/
810
public class AccessDenied extends DBusExecutionException {
11+
@Serial
912
private static final long serialVersionUID = 368173196466740803L;
1013

1114
public AccessDenied(String _message) {

dbus-java-core/src/main/java/org/freedesktop/dbus/errors/InvalidMethodArgument.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
import org.freedesktop.dbus.exceptions.DBusExecutionException;
44

5+
import java.io.Serial;
6+
57
/**
68
* Thrown if a arguments passed to the method are invalid
79
*/
810
public class InvalidMethodArgument extends DBusExecutionException {
11+
@Serial
912
private static final long serialVersionUID = 2504012938615867394L;
1013

1114
public InvalidMethodArgument(String _message) {

dbus-java-core/src/main/java/org/freedesktop/dbus/errors/MatchRuleInvalid.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
import org.freedesktop.dbus.exceptions.DBusExecutionException;
44

5+
import java.io.Serial;
6+
57
/**
68
* Thrown if the match rule is invalid
79
*/
810
public class MatchRuleInvalid extends DBusExecutionException {
11+
@Serial
912
private static final long serialVersionUID = 6922529529288327323L;
1013

1114
public MatchRuleInvalid(String _message) {

dbus-java-core/src/main/java/org/freedesktop/dbus/errors/NoReply.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
import org.freedesktop.dbus.exceptions.DBusExecutionException;
44

5+
import java.io.Serial;
6+
57
/**
68
* Thrown if there is no reply to a method call
79
*/
810
public class NoReply extends DBusExecutionException {
11+
@Serial
912
private static final long serialVersionUID = 5280031560938871837L;
1013

1114
public NoReply(String _message) {

dbus-java-core/src/main/java/org/freedesktop/dbus/errors/NotSupported.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
import org.freedesktop.dbus.exceptions.DBusExecutionException;
44

5+
import java.io.Serial;
6+
57
/**
68
* Thrown if a called operation is not supported
79
*/
810
public class NotSupported extends DBusExecutionException {
11+
@Serial
912
private static final long serialVersionUID = -3937521136197720266L;
1013

1114
public NotSupported(String _message) {

0 commit comments

Comments
 (0)