From ffc79172289d0a23bf0e8c1cb5a92e182ec4dbb2 Mon Sep 17 00:00:00 2001 From: Matthew Johnson Date: Fri, 13 Jul 2007 23:00:50 +0100 Subject: * add DBusInterfaceName annotation to override the Java interface name as the name exported on the bus. --- TODO | 1 - changelog | 2 ++ dbus-java.tex | 30 ++++++++++++++++++++++ debug.conf | 6 ++--- org/freedesktop/dbus/DBusInterfaceName.java | 27 +++++++++++++++++++ org/freedesktop/dbus/DBusMatchRule.java | 20 ++++++++++++--- org/freedesktop/dbus/DBusSignal.java | 5 +++- org/freedesktop/dbus/ExportedObject.java | 5 +++- org/freedesktop/dbus/RemoteInvocationHandler.java | 8 ++++-- .../dbus/test/TestRemoteInterface2.java | 2 ++ org/freedesktop/dbus/test/test.java | 2 +- 11 files changed, 95 insertions(+), 13 deletions(-) create mode 100644 org/freedesktop/dbus/DBusInterfaceName.java diff --git a/TODO b/TODO index e07402a..8a47efa 100644 --- a/TODO +++ b/TODO @@ -1,5 +1,4 @@ * Javadoc - * add annotation to override the interface or method name on the bus * don't respond to Introspect/Ping except on the right interface Serkan Kaba * document that we handle Introspect for you * add header checks to daemon diff --git a/changelog b/changelog index f5e4803..f0d2981 100644 --- a/changelog +++ b/changelog @@ -9,6 +9,8 @@ Version 2.3: * Fix arrays-of-structs bug (spotted by Daniel Machado ) * Fix bashism in Makefile + * add DBusInterfaceName annotation to override the Java interface name as + the name exported on the bus. Version 2.2: diff --git a/dbus-java.tex b/dbus-java.tex index 8900128..abc1e46 100644 --- a/dbus-java.tex +++ b/dbus-java.tex @@ -290,6 +290,36 @@ public class DBusImpl implements DBus \label{fig:class} \end{figure} +\subsection{Interface name overriding} + +It is highly recommended that the Java interface and package name match the +D-Bus interface. However, if, for some reason, this is not possible then the +name can be overridden by use of an Annotation. + +To override the Java interface name you should add an annotation to the +interface of {\tt +DBusInterfaceName\footnote{\url{\javadocroot/org/freedesktop/dbus/DBusInterfaceName.html}}} +with a value of the desired D-Bus interface name. An example of this can be seen in +figure \ref{fig:interfacename}. + +\begin{figure}[htb] +\begin{center} +\begin{verbatim} +package my.package; +import org.freedesktop.dbus.DBusInterface; +import org.freedesktop.dbus.DBusInterfaceName; + +@DBusInterfaceName("my.otherpackage.Remote") +public interface Remote extends DBusInterface +{ + ... +} +\end{verbatim} +\end{center} +\caption{Overloading the name of an interface.} +\label{fig:interfacename} +\end{figure} + \section{DBusSignal} Signals are also declared as part of an interface. The Java API diff --git a/debug.conf b/debug.conf index 904302a..4a6b81a 100644 --- a/debug.conf +++ b/debug.conf @@ -1,5 +1,5 @@ -org.freedesktop.dbus.MessageReader = ERR -org.freedesktop.dbus.MessageWriter = ERR +org.freedesktop.dbus.MessageReader = INFO +org.freedesktop.dbus.MessageWriter = INFO org.freedesktop.dbus.Message = ERR org.freedesktop.dbus.MethodCall = ERR org.freedesktop.dbus.MethodTuple = ERR @@ -14,7 +14,7 @@ org.freedesktop.dbus.EfficientQueue = ERR org.freedesktop.dbus.Transport = ERR org.freedesktop.dbus.Transport$SASL = ERR org.freedesktop.dbus.Transport$SASL$Command = ERR -org.freedesktop.dbus.Marshalling = VERBOSE +org.freedesktop.dbus.Marshalling = ERR org.freedesktop.dbus.BusAddress = ERR org.freedesktop.dbus.RemoteInvocationHandler = ERR org.freedesktop.dbus.test.cross_test_client = ERR diff --git a/org/freedesktop/dbus/DBusInterfaceName.java b/org/freedesktop/dbus/DBusInterfaceName.java new file mode 100644 index 0000000..c6f76bf --- /dev/null +++ b/org/freedesktop/dbus/DBusInterfaceName.java @@ -0,0 +1,27 @@ +/* + D-Bus Java Implementation + Copyright (c) 2005-2006 Matthew Johnson + + This program is free software; you can redistribute it and/or modify it + under the terms of either the GNU General Public License Version 2 or the + Academic Free Licence Version 2.1. + + Full licence texts are included in the COPYING file with this program. +*/ +package org.freedesktop.dbus; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Force the interface name to be different to the Java class name. + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +public @interface DBusInterfaceName +{ + /** The replacement interface name. */ + String value(); +} diff --git a/org/freedesktop/dbus/DBusMatchRule.java b/org/freedesktop/dbus/DBusMatchRule.java index b231e1f..6ab5801 100644 --- a/org/freedesktop/dbus/DBusMatchRule.java +++ b/org/freedesktop/dbus/DBusMatchRule.java @@ -63,14 +63,20 @@ public class DBusMatchRule public DBusMatchRule(Class c) throws DBusException { if (DBusInterface.class.isAssignableFrom(c)) { - iface = AbstractConnection.dollar_pattern.matcher(c.getName()).replaceAll("."); + if (null != c.getAnnotation(DBusInterfaceName.class)) + iface = ((DBusInterfaceName) c.getAnnotation(DBusInterfaceName.class)).value(); + else + iface = AbstractConnection.dollar_pattern.matcher(c.getName()).replaceAll("."); if (!iface.matches(".*\\..*")) throw new DBusException("DBusInterfaces must be defined in a package."); member = null; type = null; } else if (DBusSignal.class.isAssignableFrom(c)) { - iface = AbstractConnection.dollar_pattern.matcher(c.getEnclosingClass().getName()).replaceAll("."); + if (null != c.getEnclosingClass().getAnnotation(DBusInterfaceName.class)) + iface = ((DBusInterfaceName) c.getEnclosingClass().getAnnotation(DBusInterfaceName.class)).value(); + else + iface = AbstractConnection.dollar_pattern.matcher(c.getEnclosingClass().getName()).replaceAll("."); // Don't export things which are invalid D-Bus interfaces if (!iface.matches(".*\\..*")) throw new DBusException("DBusInterfaces must be defined in a package."); @@ -78,14 +84,20 @@ public class DBusMatchRule type = "signal"; } else if (Error.class.isAssignableFrom(c)) { - iface = AbstractConnection.dollar_pattern.matcher(c.getName()).replaceAll("."); + if (null != c.getAnnotation(DBusInterfaceName.class)) + iface = ((DBusInterfaceName) c.getAnnotation(DBusInterfaceName.class)).value(); + else + iface = AbstractConnection.dollar_pattern.matcher(c.getName()).replaceAll("."); if (!iface.matches(".*\\..*")) throw new DBusException("DBusInterfaces must be defined in a package."); member = null; type = "error"; } else if (DBusExecutionException.class.isAssignableFrom(c)) { - iface = AbstractConnection.dollar_pattern.matcher(c.getClass().getName()).replaceAll("."); + if (null != c.getClass().getAnnotation(DBusInterfaceName.class)) + iface = c.getClass().getAnnotation(DBusInterfaceName.class).value(); + else + iface = AbstractConnection.dollar_pattern.matcher(c.getClass().getName()).replaceAll("."); if (!iface.matches(".*\\..*")) throw new DBusException("DBusInterfaces must be defined in a package."); member = null; diff --git a/org/freedesktop/dbus/DBusSignal.java b/org/freedesktop/dbus/DBusSignal.java index 2582a43..ee3e49e 100644 --- a/org/freedesktop/dbus/DBusSignal.java +++ b/org/freedesktop/dbus/DBusSignal.java @@ -160,7 +160,10 @@ public class DBusSignal extends Message enc.getName().equals(enc.getSimpleName())) throw new DBusException("Signals must be declared as a member of a class implementing DBusInterface which is the member of a package."); else - iface = AbstractConnection.dollar_pattern.matcher(enc.getName()).replaceAll("."); + if (null != enc.getAnnotation(DBusInterfaceName.class)) + iface = ((DBusInterfaceName) enc.getAnnotation(DBusInterfaceName.class)).value(); + else + iface = AbstractConnection.dollar_pattern.matcher(enc.getName()).replaceAll("."); headers.put(Message.HeaderField.PATH,objectpath); headers.put(Message.HeaderField.MEMBER,member); diff --git a/org/freedesktop/dbus/ExportedObject.java b/org/freedesktop/dbus/ExportedObject.java index ecd3d7b..81222c7 100644 --- a/org/freedesktop/dbus/ExportedObject.java +++ b/org/freedesktop/dbus/ExportedObject.java @@ -56,7 +56,10 @@ class ExportedObject // add this class's public methods if (c.getName().length() > DBusConnection.MAX_NAME_LENGTH) throw new DBusException("Introspected interface name exceeds 255 characters. Cannot export objects of type "+c.getName()+"."); - introspectiondata += " \n"; + if (null != c.getAnnotation(DBusInterfaceName.class)) + introspectiondata += " \n"; + else + introspectiondata += " \n"; introspectiondata += getAnnotations(c); for (Method meth: c.getDeclaredMethods()) if (Modifier.isPublic(meth.getModifiers())) { diff --git a/org/freedesktop/dbus/RemoteInvocationHandler.java b/org/freedesktop/dbus/RemoteInvocationHandler.java index 89e60e8..04c73ba 100644 --- a/org/freedesktop/dbus/RemoteInvocationHandler.java +++ b/org/freedesktop/dbus/RemoteInvocationHandler.java @@ -96,8 +96,12 @@ class RemoteInvocationHandler implements InvocationHandler try { if (null == ro.iface) call = new MethodCall(ro.busname, ro.objectpath, null, m.getName(),flags, sig, args); - else - call = new MethodCall(ro.busname, ro.objectpath, AbstractConnection.dollar_pattern.matcher(ro.iface.getName()).replaceAll("."), m.getName(), flags, sig, args); + else { + if (null != ro.iface.getAnnotation(DBusInterfaceName.class)) { + call = new MethodCall(ro.busname, ro.objectpath, ro.iface.getAnnotation(DBusInterfaceName.class).value(), m.getName(), flags, sig, args); + } else + call = new MethodCall(ro.busname, ro.objectpath, AbstractConnection.dollar_pattern.matcher(ro.iface.getName()).replaceAll("."), m.getName(), flags, sig, args); + } } catch (DBusException DBe) { if (AbstractConnection.EXCEPTION_DEBUG && Debug.debug) Debug.print(Debug.ERR, DBe); throw new DBusExecutionException("Failed to construct outgoing method call: "+DBe.getMessage()); diff --git a/org/freedesktop/dbus/test/TestRemoteInterface2.java b/org/freedesktop/dbus/test/TestRemoteInterface2.java index 16cfb0c..e09c9fc 100644 --- a/org/freedesktop/dbus/test/TestRemoteInterface2.java +++ b/org/freedesktop/dbus/test/TestRemoteInterface2.java @@ -11,12 +11,14 @@ package org.freedesktop.dbus.test; import org.freedesktop.dbus.DBusInterface; +import org.freedesktop.dbus.DBusInterfaceName; import org.freedesktop.dbus.Variant; import org.freedesktop.DBus.Description; import java.util.List; @Description("An example remote interface") +@DBusInterfaceName("org.freedesktop.dbus.test.AlternateTestInterface") public interface TestRemoteInterface2 extends DBusInterface { @Description("Test multiple return values and implicit variant parameters.") diff --git a/org/freedesktop/dbus/test/test.java b/org/freedesktop/dbus/test/test.java index c37e40b..75259d2 100644 --- a/org/freedesktop/dbus/test/test.java +++ b/org/freedesktop/dbus/test/test.java @@ -236,7 +236,7 @@ class testclass implements TestRemoteInterface, TestRemoteInterface2, TestSignal public int overload() { DBusCallInfo info = DBusConnection.getCallInfo(); - if ("org.freedesktop.dbus.test.TestRemoteInterface2".equals(info.getInterface())) + if ("org.freedesktop.dbus.test.AlternateTestInterface".equals(info.getInterface())) return 3; else if ("org.freedesktop.dbus.test.TestRemoteInterface".equals(info.getInterface())) return 4; -- cgit v1.2.3