Repository navigation
Description
I have a situation where I am sharing some code across a few modules, and my DBusInterface interface and implementation extend a generic interface that is used outside of the dbus components.
Something like this ..
public interface RootItem {
}
public interface Root<ITEM_TYPE extends RootItem> {
public ITEM_TYPE getItem(long itemId);
}
public interface DBusItem extends RootItem {
}
public interface DBusAPI extends Root<DBusItem>, DBusInterface {
@Override
public DBusItem getItem(long itemId);
}
public class DBusAPIImpl implements DBusAPI {
@Override
public DBusItem getItem(long itemId) {
/// do stuff to get item
return theItem;
}
}This will fail to export the interface.
org.freedesktop.dbus.exceptions.DBusException: Exporting non-exportable type: interface com.logonbox.vpn.client.common.api.IVPNConnection
at [email protected]/org.freedesktop.dbus.Marshalling.recursiveGetDBusType(Marshalling.java:281)
at [email protected]/org.freedesktop.dbus.Marshalling.getDBusType(Marshalling.java:118)
at [email protected]/org.freedesktop.dbus.Marshalling.getDBusType(Marshalling.java:103)
at [email protected]/org.freedesktop.dbus.messages.ExportedObject.generateMethodsXml(ExportedObject.java:227)
at [email protected]/org.freedesktop.dbus.messages.ExportedObject.generateIntrospectionXml(ExportedObject.java:341)
at [email protected]/org.freedesktop.dbus.messages.ExportedObject.<init>(ExportedObject.java:37)
at [email protected]/org.freedesktop.dbus.connections.AbstractConnection.exportObject(AbstractConnection.java:329)
What appears to be happening, is that the getItem() method is being found twice. What is is interesting, is that one occurrence is being identified as a default method even though it's not, while the other is being identified as abstract (like all the other methods in the interface that do not have this generic type).
Doing a System.out of the two methods that are found results in ...
public default com.acme.root.RootItem com.acme.impl.DBusAPI.getItemlong)
public abstract com.acme.impl.DBusItem com.acme.impl.DBusAPI.getItem(long)I could fix this by adding a @DBusIgnore to Root, but Root is in a module that does not have dbus-java on the CLASSPATH.
So instead, I changed dbus-javas ExportedObject.isExcluded() method.
public static boolean isExcluded(Method _meth) {
return !Modifier.isPublic(_meth.getModifiers())
|| _meth.isDefault() /* <--- Added this */
|| _meth.getAnnotation(DBusIgnore.class) != null
|| _meth.getAnnotation(DBusBoundProperty.class) != null
|| _meth.getName().equals("getObjectPath") && _meth.getReturnType().equals(String.class)
&& _meth.getParameterCount() == 0;
}And now it all works again.
This doesn't seem to have any other ill effects to the DBus API. Everything else is still getting exported correctly. It seems that it is only identified as default with this particular arrangement of interfaces, OR if you do actually use a default method in the DBusInterface.
I think this may be a valid fix, because using a default method doesn't make sense anyway. If you are acting as a client, accessing an external DBus service from Java, then default methods don't work anyway (RemoteInvocationHandler tries to run it as if it were a remote method). It might be valid if you are exporting an API from Java, but then you cannot share those interface definitions with client code.
TLDR; I think default methods in DBus interfaces should be excluded by default
Activity
hypfvieh commentedon Oct 22, 2023
Can you create a example demonstrating this? I would like to understand where the "default" implementation comes from...
brett-smith commentedon Oct 22, 2023
That is what is strange, because I haven't provided one. I'll get together a self contained example.
brett-smith commentedon Oct 22, 2023
If you put a
System.out.println(meth)inExportedObject.generateMethodsXmljust after theisExcluded(), you'd see ...As you can see, there are no
defaultmethods.Issue #240: Do not allow exporting of default, bridge or synthetic me…
hypfvieh commentedon Oct 23, 2023
So after digging into this, I guess you stumbled across a bridging method generated by the compiler.
You can see that by asking if the method
isSynthetic(or by asking forisBridge), which are both true in your example code.Those methods will be created when multiple methods will override one another from the language perspective.
You may take a look at this stack overflow article
See also the Javadoc of
Method.isBridge().So we got three things which needs to be taken care of: default implementations in interfaces, synthetic methods and bridge methods.
I think the
isExcluded()method should return true in all cases.brett-smith commentedon Oct 23, 2023
Ohh interesting. Today I learned something new about Java :)
All the above makes sense.
One further thing ... I was thinking about
defaultmethods, and that they may be actually valid in a client only interface. I don't think there would be any harm in changingRemoteInvocationHandler, so that if it encounters a default method, then it should just execute it locally. If this is being ignored now on the server side, that means the interfaces can safely be shared.hypfvieh commentedon Oct 23, 2023
Me too. Most of the time one does not really care what is happening under the hood.. but in some rare cases these details make the difference.
I'm really not so sure what's the best way to deal with default methods.
There may be some cases where they may work depending what the user does.
If you have a good idea, please provide a PR.
brett-smith commentedon Oct 23, 2023
Heh yeh, I'm thinking they may be useful for something like this this ..
I'm pretty sure it's no more than a handful of lines of code in
RemoteInvocationHandler. I'll get together a PR later.