From 5d22553a4ddd50b332da7543caf74613036559f9 Mon Sep 17 00:00:00 2001 From: fingerfrings Date: Sat, 16 Nov 2024 02:07:19 +0800 Subject: [PATCH 1/2] (feat): Add BusName and ObjectPath annotation support for core - Add BusName and ObjectPath annotations to mark interface bus names and object paths - Implement annotation-based remote object retrieval in DBusConnection - Add validation for annotation presence and uniqueness with improved error messages --- .../freedesktop/dbus/annotations/BusName.java | 11 +++++++++ .../dbus/annotations/ObjectPath.java | 10 ++++++++ .../dbus/connections/impl/DBusConnection.java | 24 +++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 dbus-java-core/src/main/java/org/freedesktop/dbus/annotations/BusName.java create mode 100644 dbus-java-core/src/main/java/org/freedesktop/dbus/annotations/ObjectPath.java diff --git a/dbus-java-core/src/main/java/org/freedesktop/dbus/annotations/BusName.java b/dbus-java-core/src/main/java/org/freedesktop/dbus/annotations/BusName.java new file mode 100644 index 00000000..d343bda8 --- /dev/null +++ b/dbus-java-core/src/main/java/org/freedesktop/dbus/annotations/BusName.java @@ -0,0 +1,11 @@ +package org.freedesktop.dbus.annotations; + + +import java.lang.annotation.*; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +@Inherited +public @interface BusName { + String value(); +} diff --git a/dbus-java-core/src/main/java/org/freedesktop/dbus/annotations/ObjectPath.java b/dbus-java-core/src/main/java/org/freedesktop/dbus/annotations/ObjectPath.java new file mode 100644 index 00000000..fb274fda --- /dev/null +++ b/dbus-java-core/src/main/java/org/freedesktop/dbus/annotations/ObjectPath.java @@ -0,0 +1,10 @@ +package org.freedesktop.dbus.annotations; + +import java.lang.annotation.*; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +@Inherited +public @interface ObjectPath { + String value(); +} diff --git a/dbus-java-core/src/main/java/org/freedesktop/dbus/connections/impl/DBusConnection.java b/dbus-java-core/src/main/java/org/freedesktop/dbus/connections/impl/DBusConnection.java index ca042fdf..6eeb1cbf 100644 --- a/dbus-java-core/src/main/java/org/freedesktop/dbus/connections/impl/DBusConnection.java +++ b/dbus-java-core/src/main/java/org/freedesktop/dbus/connections/impl/DBusConnection.java @@ -5,6 +5,9 @@ import org.freedesktop.dbus.DBusMatchRule; import org.freedesktop.dbus.RemoteInvocationHandler; import org.freedesktop.dbus.RemoteObject; +import org.freedesktop.dbus.annotations.BusName; +import org.freedesktop.dbus.annotations.DBusInterfaceName; +import org.freedesktop.dbus.annotations.ObjectPath; import org.freedesktop.dbus.connections.AbstractConnection; import org.freedesktop.dbus.connections.IDisconnectAction; import org.freedesktop.dbus.connections.config.ReceivingServiceConfig; @@ -455,6 +458,27 @@ public I getRemoteObject(String _busname, String _obje return getRemoteObject(_busname, _objectpath, _type, true); } + public I getRemoteObject(Class type) throws DBusException{ + BusName[] busNameTypes + = type.getAnnotationsByType(BusName.class); + if(busNameTypes.length == 0){ + throw new DBusException("No BusName annotation found on class " + type.getName()); + } + if(busNameTypes.length > 1){ + throw new DBusException("Multiple BusName annotations found on class " + type.getName()); + } + String busName= busNameTypes[0].value(); + ObjectPath[] objectPathTypes = type.getAnnotationsByType(ObjectPath.class); + if(objectPathTypes.length == 0){ + throw new DBusException("No DBusInterfaceName annotation found on class " + type.getName()); + } + if(objectPathTypes.length > 1){ + throw new DBusException("Multiple DBusInterfaceName annotations found on class " + type.getName()); + } + String interfaceName = objectPathTypes[0].value(); + return getRemoteObject(busName, interfaceName, type); + } + /** * Return a reference to a remote object. This method will always refer to the well known name (if given) rather * than resolving it to a unique bus name. In particular this means that if a process providing the well known name From 23e7d56b2d8b87fab838047262e795a563b9169c Mon Sep 17 00:00:00 2001 From: fingerfrings Date: Sat, 16 Nov 2024 02:15:57 +0800 Subject: [PATCH 2/2] (feat): Add BusName and ObjectPath annotation support for core - Add BusName and ObjectPath annotations to mark interface bus names and object paths - Implement annotation-based remote object retrieval in DBusConnection - Add validation for annotation presence and uniqueness with improved error messages --- .../freedesktop/dbus/annotations/BusName.java | 11 +++++++++ .../dbus/annotations/ObjectPath.java | 10 ++++++++ .../dbus/connections/impl/DBusConnection.java | 24 +++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 dbus-java-core/src/main/java/org/freedesktop/dbus/annotations/BusName.java create mode 100644 dbus-java-core/src/main/java/org/freedesktop/dbus/annotations/ObjectPath.java diff --git a/dbus-java-core/src/main/java/org/freedesktop/dbus/annotations/BusName.java b/dbus-java-core/src/main/java/org/freedesktop/dbus/annotations/BusName.java new file mode 100644 index 00000000..d343bda8 --- /dev/null +++ b/dbus-java-core/src/main/java/org/freedesktop/dbus/annotations/BusName.java @@ -0,0 +1,11 @@ +package org.freedesktop.dbus.annotations; + + +import java.lang.annotation.*; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +@Inherited +public @interface BusName { + String value(); +} diff --git a/dbus-java-core/src/main/java/org/freedesktop/dbus/annotations/ObjectPath.java b/dbus-java-core/src/main/java/org/freedesktop/dbus/annotations/ObjectPath.java new file mode 100644 index 00000000..fb274fda --- /dev/null +++ b/dbus-java-core/src/main/java/org/freedesktop/dbus/annotations/ObjectPath.java @@ -0,0 +1,10 @@ +package org.freedesktop.dbus.annotations; + +import java.lang.annotation.*; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +@Inherited +public @interface ObjectPath { + String value(); +} diff --git a/dbus-java-core/src/main/java/org/freedesktop/dbus/connections/impl/DBusConnection.java b/dbus-java-core/src/main/java/org/freedesktop/dbus/connections/impl/DBusConnection.java index ca042fdf..6eeb1cbf 100644 --- a/dbus-java-core/src/main/java/org/freedesktop/dbus/connections/impl/DBusConnection.java +++ b/dbus-java-core/src/main/java/org/freedesktop/dbus/connections/impl/DBusConnection.java @@ -5,6 +5,9 @@ import org.freedesktop.dbus.DBusMatchRule; import org.freedesktop.dbus.RemoteInvocationHandler; import org.freedesktop.dbus.RemoteObject; +import org.freedesktop.dbus.annotations.BusName; +import org.freedesktop.dbus.annotations.DBusInterfaceName; +import org.freedesktop.dbus.annotations.ObjectPath; import org.freedesktop.dbus.connections.AbstractConnection; import org.freedesktop.dbus.connections.IDisconnectAction; import org.freedesktop.dbus.connections.config.ReceivingServiceConfig; @@ -455,6 +458,27 @@ public I getRemoteObject(String _busname, String _obje return getRemoteObject(_busname, _objectpath, _type, true); } + public I getRemoteObject(Class type) throws DBusException{ + BusName[] busNameTypes + = type.getAnnotationsByType(BusName.class); + if(busNameTypes.length == 0){ + throw new DBusException("No BusName annotation found on class " + type.getName()); + } + if(busNameTypes.length > 1){ + throw new DBusException("Multiple BusName annotations found on class " + type.getName()); + } + String busName= busNameTypes[0].value(); + ObjectPath[] objectPathTypes = type.getAnnotationsByType(ObjectPath.class); + if(objectPathTypes.length == 0){ + throw new DBusException("No DBusInterfaceName annotation found on class " + type.getName()); + } + if(objectPathTypes.length > 1){ + throw new DBusException("Multiple DBusInterfaceName annotations found on class " + type.getName()); + } + String interfaceName = objectPathTypes[0].value(); + return getRemoteObject(busName, interfaceName, type); + } + /** * Return a reference to a remote object. This method will always refer to the well known name (if given) rather * than resolving it to a unique bus name. In particular this means that if a process providing the well known name