diff --git a/dbus-java-core/src/main/java/org/freedesktop/dbus/DBusPath.java b/dbus-java-core/src/main/java/org/freedesktop/dbus/DBusPath.java index c4f83b360..218741069 100644 --- a/dbus-java-core/src/main/java/org/freedesktop/dbus/DBusPath.java +++ b/dbus-java-core/src/main/java/org/freedesktop/dbus/DBusPath.java @@ -1,58 +1,21 @@ package org.freedesktop.dbus; -import java.util.Objects; - -public class DBusPath implements Comparable { - private final String source; - private final String path; - - public DBusPath(String _source, String _path) { - source = _source; - path = _path; - } - +public record DBusPath(String source, String path) implements Comparable { public DBusPath(String _path) { this(null, _path); } - public String getPath() { - return path; - } - - public String getSource() { - return source; - } - @Override public String toString() { - return getPath(); - } - - @Override - public int hashCode() { - return Objects.hash(source, path); - } - - @Override - public boolean equals(Object _obj) { - if (this == _obj) { - return true; - } else if (_obj == null || getClass() != _obj.getClass()) { - return false; - } - - DBusPath dbusPath = (DBusPath) _obj; - - return Objects.equals(path, dbusPath.path) - && Objects.equals(source, dbusPath.source); + return path(); } @Override public int compareTo(DBusPath _that) { - if (getPath() == null || _that == null) { + if (path() == null || _that == null) { return 0; } - return getPath().compareTo(_that.getPath()); + return path().compareTo(_that.path()); } /** diff --git a/dbus-java-core/src/main/java/org/freedesktop/dbus/FileDescriptor.java b/dbus-java-core/src/main/java/org/freedesktop/dbus/FileDescriptor.java index e8aef9d02..dd0d31007 100644 --- a/dbus-java-core/src/main/java/org/freedesktop/dbus/FileDescriptor.java +++ b/dbus-java-core/src/main/java/org/freedesktop/dbus/FileDescriptor.java @@ -11,14 +11,7 @@ * Can be created from either an integer (gotten through some JNI/JNA/JNR call) or from a * {@link java.io.FileDescriptor}. */ -public final class FileDescriptor { - - private final int fd; - - public FileDescriptor(int _fd) { - fd = _fd; - } - +public record FileDescriptor(int intFileDescriptor) { /** * Converts this DBus {@link FileDescriptor} to a {@link java.io.FileDescriptor}.
* Tries to use the provided ISocketProvider if present first.
@@ -31,43 +24,17 @@ public FileDescriptor(int _fd) { */ public java.io.FileDescriptor toJavaFileDescriptor(ISocketProvider _provider) throws MarshallingException { if (_provider != null) { - Optional result = _provider.createFileDescriptor(fd); + Optional result = _provider.createFileDescriptor(intFileDescriptor); if (result.isPresent()) { return result.get(); } } return ReflectionFileDescriptorHelper.getInstance() - .flatMap(helper -> helper.createFileDescriptor(fd)) + .flatMap(helper -> helper.createFileDescriptor(intFileDescriptor)) .orElseThrow(() -> new MarshallingException("Could not create new FileDescriptor instance")); } - public int getIntFileDescriptor() { - return fd; - } - - @Override - public boolean equals(Object _o) { - if (this == _o) { - return true; - } - if (_o == null || getClass() != _o.getClass()) { - return false; - } - FileDescriptor that = (FileDescriptor) _o; - return fd == that.fd; - } - - @Override - public int hashCode() { - return fd; - } - - @Override - public String toString() { - return FileDescriptor.class.getSimpleName() + "[fd=" + fd + "]"; - } - /** * Utility method to create a DBus {@link FileDescriptor} from a {@link java.io.FileDescriptor}.
* Tries to use the provided ISocketProvider if present first.
diff --git a/dbus-java-core/src/main/java/org/freedesktop/dbus/Marshalling.java b/dbus-java-core/src/main/java/org/freedesktop/dbus/Marshalling.java index 547647c9a..4bef84cdf 100644 --- a/dbus-java-core/src/main/java/org/freedesktop/dbus/Marshalling.java +++ b/dbus-java-core/src/main/java/org/freedesktop/dbus/Marshalling.java @@ -586,9 +586,9 @@ static Object deSerializeParameter(Object _parameter, Type _type, AbstractConnec if (parameter instanceof DBusPath op) { LOGGER.trace("Parameter is DBusPath"); if (_type instanceof Class tClazz && DBusInterface.class.isAssignableFrom(tClazz)) { - parameter = _conn.getExportedObject(op.getSource(), op.getPath(), (Class) _type); + parameter = _conn.getExportedObject(op.source(), op.path(), (Class) _type); } else { - parameter = new DBusPath(op.getPath()); + parameter = new DBusPath(op.path()); } } diff --git a/dbus-java-core/src/main/java/org/freedesktop/dbus/MethodTuple.java b/dbus-java-core/src/main/java/org/freedesktop/dbus/MethodTuple.java index 6d52b3c0d..049b67d61 100644 --- a/dbus-java-core/src/main/java/org/freedesktop/dbus/MethodTuple.java +++ b/dbus-java-core/src/main/java/org/freedesktop/dbus/MethodTuple.java @@ -3,45 +3,13 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.Objects; +public record MethodTuple(String name, String sig) { + private static final Logger LOGGER = LoggerFactory.getLogger(MethodTuple.class); -public class MethodTuple { - private final Logger logger = LoggerFactory.getLogger(getClass()); - - private final String name; - private final String sig; - - public MethodTuple(String _name, String _sig) { - name = _name; - sig = Objects.requireNonNullElse(_sig, ""); - logger.trace("new MethodTuple({}, {})", name, sig); - } - - @Override - public int hashCode() { - return Objects.hash(name, sig); - } - - @Override - public boolean equals(Object _obj) { - if (this == _obj) { - return true; - } - if (!(_obj instanceof MethodTuple other)) { - return false; + public MethodTuple { + if (sig == null) { + sig = ""; } - return Objects.equals(name, other.name) && Objects.equals(sig, other.sig); - } - - public Logger getLogger() { - return logger; - } - - public String getName() { - return name; - } - - public String getSig() { - return sig; + LOGGER.trace("new MethodTuple({}, {})", name, sig); } } diff --git a/dbus-java-core/src/main/java/org/freedesktop/dbus/connections/base/DBusBoundPropertyHandler.java b/dbus-java-core/src/main/java/org/freedesktop/dbus/connections/base/DBusBoundPropertyHandler.java index 963822bab..200418c4d 100644 --- a/dbus-java-core/src/main/java/org/freedesktop/dbus/connections/base/DBusBoundPropertyHandler.java +++ b/dbus-java-core/src/main/java/org/freedesktop/dbus/connections/base/DBusBoundPropertyHandler.java @@ -118,7 +118,7 @@ protected PropHandled handleGetAll(ExportedObject _exportObject, final MethodCal Map resultMap = new HashMap<>(); for (Entry propEn : allPropertyMethods) { Method propMeth = propEn.getValue(); - if (propEn.getKey().getAccess() == Access.READ) { + if (propEn.getKey().access() == Access.READ) { try { _methodCall.setArgs(new Object[0]); Object val = invokeMethod(_methodCall, propMeth, object); @@ -131,7 +131,7 @@ protected PropHandled handleGetAll(ExportedObject _exportObject, final MethodCal val = new Variant<>(val, dataTypeStr); } - resultMap.put(propEn.getKey().getName(), val); + resultMap.put(propEn.getKey().name(), val); } catch (Throwable _ex) { getLogger().debug("Error executing method {} on method call {}", propMeth, _methodCall, _ex); handleException(_methodCall, new UnknownMethod("Failure in de-serializing message: " + _ex)); diff --git a/dbus-java-core/src/main/java/org/freedesktop/dbus/connections/impl/IRemoteObjectGetter.java b/dbus-java-core/src/main/java/org/freedesktop/dbus/connections/impl/IRemoteObjectGetter.java index 53a781a92..ba8d8eb5e 100644 --- a/dbus-java-core/src/main/java/org/freedesktop/dbus/connections/impl/IRemoteObjectGetter.java +++ b/dbus-java-core/src/main/java/org/freedesktop/dbus/connections/impl/IRemoteObjectGetter.java @@ -20,7 +20,7 @@ default I getPeerRemoteObject(String _busname, String default I getPeerRemoteObject(String _busname, DBusPath _objectpath, Class _type) throws DBusException { DBusObjects.requireObjectPath(_objectpath); - return getPeerRemoteObject(_busname, _objectpath.getPath(), _type); + return getPeerRemoteObject(_busname, _objectpath.path(), _type); } /** @@ -134,7 +134,7 @@ default I getPeerRemoteObject(String _busname, DBusPat DBusObjects.requireBusNameOrConnectionId(_busname); DBusObjects.requireObjectPath(_objectpath); - return getRemoteObject(getDBusOwnerName(_busname), _objectpath.getPath(), _type, _autostart); + return getRemoteObject(getDBusOwnerName(_busname), _objectpath.path(), _type, _autostart); } /** @@ -174,7 +174,7 @@ default I getRemoteObject(String _busname, String _obj default I getRemoteObject(String _busname, DBusPath _objectpath, Class _type) throws DBusException { DBusObjects.requireObjectPath(_objectpath); - return getRemoteObject(_busname, _objectpath.getPath(), _type, true); + return getRemoteObject(_busname, _objectpath.path(), _type, true); } /** * Return a reference to a remote object. This method will always refer to the well known name (if given) rather @@ -215,7 +215,7 @@ I getRemoteObject(String _busname, String _objectpath, default I getRemoteObject(String _busname, DBusPath _objectpath, Class _type, boolean _autostart) throws DBusException { DBusObjects.requireObjectPath(_objectpath); - return getRemoteObject(_busname, _objectpath.getPath(), _type, _autostart); + return getRemoteObject(_busname, _objectpath.path(), _type, _autostart); } /** diff --git a/dbus-java-core/src/main/java/org/freedesktop/dbus/matchrules/MatchRuleMatcher.java b/dbus-java-core/src/main/java/org/freedesktop/dbus/matchrules/MatchRuleMatcher.java index 867b10ac8..87864cd4a 100644 --- a/dbus-java-core/src/main/java/org/freedesktop/dbus/matchrules/MatchRuleMatcher.java +++ b/dbus-java-core/src/main/java/org/freedesktop/dbus/matchrules/MatchRuleMatcher.java @@ -110,7 +110,7 @@ static boolean matchArg0123Path(Message _msg, Map _compare) { if (clz.isAssignableFrom(String.class)) { matchVal = (String) parameters[i]; } else if (clz.isAssignableFrom(DBusPath.class)) { - matchVal = ((DBusPath) parameters[i]).getPath(); + matchVal = ((DBusPath) parameters[i]).path(); } else { continue; // not String or DBusPath, do not try to match } diff --git a/dbus-java-core/src/main/java/org/freedesktop/dbus/messages/ExportedObject.java b/dbus-java-core/src/main/java/org/freedesktop/dbus/messages/ExportedObject.java index 5b07496b4..0de29e924 100644 --- a/dbus-java-core/src/main/java/org/freedesktop/dbus/messages/ExportedObject.java +++ b/dbus-java-core/src/main/java/org/freedesktop/dbus/messages/ExportedObject.java @@ -168,7 +168,7 @@ protected String generatePropertiesXml(Class _clz) throws DBusException { propertyMethods.put(ref, method); if (map.containsKey(name)) { PropertyRef existing = map.get(name); - if (access.equals(existing.getAccess())) { + if (access.equals(existing.access())) { throw new DBusException(MessageFormat.format( "Property ''{0}'' has access mode ''{1}'' defined multiple times.", name, access)); } else { @@ -188,7 +188,7 @@ protected String generatePropertiesXml(Class _clz) throws DBusException { } for (PropertyRef ref : map.values()) { - xml.append(" ").append(generatePropertyXml(ref.getName(), ref.getType(), ref.getAccess(), ref.getEmitChangeSignal())).append("\n"); + xml.append(" ").append(generatePropertyXml(ref.name(), ref.type(), ref.access(), ref.emitChangeSignal())).append("\n"); } return xml.toString(); @@ -277,10 +277,9 @@ private void createReturnArguments(Method _meth, StringBuilder _sb) throws DBusE } else if (_meth.getGenericReturnType() instanceof Class clz) { argTypes = Arrays.stream(clz.getDeclaredFields()) .filter(f -> f.isAnnotationPresent(Position.class)) - .filter(Objects::nonNull) .map(f -> Map.entry(f.getAnnotation(Position.class).value(), f)) - .sorted(Comparator.comparingInt(k -> k.getKey())) - .map(f -> f.getValue()) + .sorted(Comparator.comparingInt(Map.Entry::getKey)) + .map(Map.Entry::getValue) .map(Field::getType) .map(Type.class::cast) .toList(); diff --git a/dbus-java-core/src/main/java/org/freedesktop/dbus/propertyref/PropertyRef.java b/dbus-java-core/src/main/java/org/freedesktop/dbus/propertyref/PropertyRef.java index 65962da24..16da16ea9 100644 --- a/dbus-java-core/src/main/java/org/freedesktop/dbus/propertyref/PropertyRef.java +++ b/dbus-java-core/src/main/java/org/freedesktop/dbus/propertyref/PropertyRef.java @@ -16,27 +16,9 @@ * @author Brett Smith * @since 5.0.0 - 2023-10-20 */ -public final class PropertyRef { - - private final String name; - private final Class type; - private final DBusProperty.Access access; - private final EmitChangeSignal emitChangeSignal; - +public record PropertyRef(String name, Class type, Access access, EmitChangeSignal emitChangeSignal) { public PropertyRef(String _name, Class _type, Access _access) { - super(); - this.name = _name; - this.type = _type; - this.access = _access; - this.emitChangeSignal = EmitChangeSignal.TRUE; - } - - public PropertyRef(String _name, Class _type, Access _access, EmitChangeSignal _emitChangeSignal) { - super(); - this.name = _name; - this.type = _type; - this.access = _access; - this.emitChangeSignal = _emitChangeSignal; + this(_name, _type, _access, EmitChangeSignal.TRUE); } public PropertyRef(DBusProperty _property) { @@ -63,22 +45,6 @@ public boolean equals(Object _obj) { return access == other.access && Objects.equals(name, other.name); } - public String getName() { - return name; - } - - public Class getType() { - return type; - } - - public DBusProperty.Access getAccess() { - return access; - } - - public EmitChangeSignal getEmitChangeSignal() { - return emitChangeSignal; - } - public static Access accessForMethod(Method _method) { DBusBoundProperty annotation = _method.getAnnotation(DBusBoundProperty.class); Access access = _method.getName().toLowerCase().startsWith("set") ? Access.WRITE : Access.READ; diff --git a/dbus-java-core/src/main/java/org/freedesktop/dbus/utils/DBusObjects.java b/dbus-java-core/src/main/java/org/freedesktop/dbus/utils/DBusObjects.java index 395be793a..1f435df5d 100644 --- a/dbus-java-core/src/main/java/org/freedesktop/dbus/utils/DBusObjects.java +++ b/dbus-java-core/src/main/java/org/freedesktop/dbus/utils/DBusObjects.java @@ -223,7 +223,7 @@ public static String requireObjectPath(String _objectPath, String _customMsg) th * @throws InvalidObjectPathException when input is not a valid object path */ public static DBusPath requireObjectPath(DBusPath _dbusPath, String _customMsg) throws InvalidObjectPathException { - return requireBase(_dbusPath, x -> validateObjectPath(x.getPath()), InvalidObjectPathException::new, _customMsg); + return requireBase(_dbusPath, x -> validateObjectPath(x.path()), InvalidObjectPathException::new, _customMsg); } /** diff --git a/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/BaseFunctionsTest.java b/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/BaseFunctionsTest.java index 2e3363d4a..501f298cc 100644 --- a/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/BaseFunctionsTest.java +++ b/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/BaseFunctionsTest.java @@ -94,10 +94,10 @@ public void testExportPath() throws DBusException { @Test public void testGetProperties() throws DBusException { Properties prop = clientconn.getRemoteObject(getTestBusName(), getTestObjectPath(), Properties.class); - DBusPath prv = (DBusPath) prop.Get("foo.bar", "foo"); + DBusPath prv = prop.Get("foo.bar", "foo"); logger.debug("Got path " + prv); - assertEquals("/nonexistant/path", prv.getPath()); + assertEquals("/nonexistant/path", prv.path()); } diff --git a/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/FileDescriptorsTest.java b/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/FileDescriptorsTest.java index 945e7153d..755807c0d 100644 --- a/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/FileDescriptorsTest.java +++ b/dbus-java-tests/src/test/java/org/freedesktop/dbus/test/FileDescriptorsTest.java @@ -63,7 +63,7 @@ public void fileDescriptorPassing() throws DBusException { FDPassing remoteObject = clientconn.getRemoteObject("foo.bar.Test", TEST_OBJECT_PATH, FDPassing.class); Stream.of(0, 1, 2).map(FileDescriptor::new).forEach(fd -> { // that's not a mistake of using NotEquals here, as fd passing make a new copy with a new value - assertNotEquals(fd.getIntFileDescriptor(), remoteObject.doNothing(fd).getIntFileDescriptor()); + assertNotEquals(fd.intFileDescriptor(), remoteObject.doNothing(fd).intFileDescriptor()); }); } diff --git a/dbus-java-tests/src/test/java/org/freedesktop/dbus/utils/LoggingHelperTest.java b/dbus-java-tests/src/test/java/org/freedesktop/dbus/utils/LoggingHelperTest.java index 642bfc8a6..fec51b280 100644 --- a/dbus-java-tests/src/test/java/org/freedesktop/dbus/utils/LoggingHelperTest.java +++ b/dbus-java-tests/src/test/java/org/freedesktop/dbus/utils/LoggingHelperTest.java @@ -68,15 +68,7 @@ private static Stream getTestData() { ); } - private static class TestValues { - private final Object input; - private final String expected; - - TestValues(Object _input, String _expected) { - input = _input; - expected = _expected; - } - + private record TestValues(Object input, String expected) { @Override public String toString() { return input.getClass().getTypeName().replace("[]", "Array") + "Test"; diff --git a/dbus-java-utils/src/main/java/org/freedesktop/dbus/utils/generator/ClassBuilderInfo.java b/dbus-java-utils/src/main/java/org/freedesktop/dbus/utils/generator/ClassBuilderInfo.java index e0720fe70..728ee5a4e 100644 --- a/dbus-java-utils/src/main/java/org/freedesktop/dbus/utils/generator/ClassBuilderInfo.java +++ b/dbus-java-utils/src/main/java/org/freedesktop/dbus/utils/generator/ClassBuilderInfo.java @@ -154,7 +154,6 @@ public String createClassFileContent() { * Create the Java source for the class information provided. * * @param _staticClass this is static inner class - * @param _argumentPrefix use given prefix for generated method arguments (null/blank if not needed) * @param _otherImports this class needs additional imports (e.g. due to inner class) * @return */ @@ -187,10 +186,10 @@ private List createClassFileContent(boolean _staticClass, Set _o } for (AnnotationInfo annotation : annotations) { - allImports.add(annotation.getAnnotationClass().getName()); - String annotationCode = classIndent + "@" + annotation.getAnnotationClass().getSimpleName(); - if (annotation.getAnnotationParams() != null) { - annotationCode += "(" + annotation.getAnnotationParams() + ")"; + allImports.add(annotation.annotationClass().getName()); + String annotationCode = classIndent + "@" + annotation.annotationClass().getSimpleName(); + if (annotation.annotationParams() != null) { + annotationCode += "(" + annotation.annotationParams() + ")"; } content.add(annotationCode); } @@ -370,27 +369,12 @@ static Set getImportsForType(String _type) { /** * Contains information about annotation to place on classes, members or methods. * + * @param annotationClass Annotation class. + * @param annotationParams Annotation params (e.g. value = "foo", key = "bar"). * @author hypfvieh * @since v3.2.1 - 2019-11-13 */ - public static class AnnotationInfo { - /** Annotation class. */ - private final Class annotationClass; - /** Annotation params (e.g. value = "foo", key = "bar"). */ - private final String annotationParams; - - public AnnotationInfo(Class _annotationClass, String _annotationParams) { - annotationClass = _annotationClass; - annotationParams = _annotationParams; - } - - public Class getAnnotationClass() { - return annotationClass; - } - - public String getAnnotationParams() { - return annotationParams; - } + public record AnnotationInfo(Class annotationClass, String annotationParams) { } /** diff --git a/dbus-java-utils/src/main/java/org/freedesktop/dbus/viewer/FileSaver.java b/dbus-java-utils/src/main/java/org/freedesktop/dbus/viewer/FileSaver.java index 4254a4c20..27c0d3fbf 100644 --- a/dbus-java-utils/src/main/java/org/freedesktop/dbus/viewer/FileSaver.java +++ b/dbus-java-utils/src/main/java/org/freedesktop/dbus/viewer/FileSaver.java @@ -52,7 +52,7 @@ private void saveFiles() { final Iterator iterator = textFiles.iterator(); while (iterator.hasNext()) { final TextFile textFile = iterator.next(); - String fileName = textFile.getFileName(); + String fileName = textFile.fileName(); File fileToSave = new File(parentDirectory, fileName); File parentFile = fileToSave.getParentFile(); if (parentFile.exists() || parentFile.mkdirs()) { @@ -82,7 +82,7 @@ private void saveFiles() { } if (doSave) { try { - String contents = textFile.getContents(); + String contents = textFile.contents(); writeFile(fileToSave, contents); } catch (IOException _ex) { /* Can't access parent directory for saving */ diff --git a/dbus-java-utils/src/main/java/org/freedesktop/dbus/viewer/TextFile.java b/dbus-java-utils/src/main/java/org/freedesktop/dbus/viewer/TextFile.java index 05f66c886..600ab5621 100644 --- a/dbus-java-utils/src/main/java/org/freedesktop/dbus/viewer/TextFile.java +++ b/dbus-java-utils/src/main/java/org/freedesktop/dbus/viewer/TextFile.java @@ -10,39 +10,14 @@ */ package org.freedesktop.dbus.viewer; -/** A Text file abstraction +/** + * A Text file abstraction * + * @param fileName The file name + * @param contents The contents * * @author pete * @since 10/02/2006 */ -class TextFile { - private final String fileName; - private final String contents; - - /** Create the TextFile - * - * @param _fileName The file name - * @param _contents The contents - */ - TextFile(String _fileName, String _contents) { - this.fileName = _fileName; - this.contents = _contents; - } - - /** Retrieve the fileName - * - * @return The fileName. - */ - String getFileName() { - return fileName; - } - - /** Retrieve the contents - * - * @return The contents. - */ - String getContents() { - return contents; - } +record TextFile(String fileName, String contents) { }