Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 4 additions & 41 deletions dbus-java-core/src/main/java/org/freedesktop/dbus/DBusPath.java
Original file line number Diff line number Diff line change
@@ -1,58 +1,21 @@
package org.freedesktop.dbus;

import java.util.Objects;

public class DBusPath implements Comparable<DBusPath> {
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<DBusPath> {
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());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}.<br>
* Tries to use the provided ISocketProvider if present first. <br>
Expand All @@ -31,43 +24,17 @@ public FileDescriptor(int _fd) {
*/
public java.io.FileDescriptor toJavaFileDescriptor(ISocketProvider _provider) throws MarshallingException {
if (_provider != null) {
Optional<java.io.FileDescriptor> result = _provider.createFileDescriptor(fd);
Optional<java.io.FileDescriptor> 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}.<br>
* Tries to use the provided ISocketProvider if present first.<br>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<DBusInterface>) _type);
parameter = _conn.getExportedObject(op.source(), op.path(), (Class<DBusInterface>) _type);
} else {
parameter = new DBusPath(op.getPath());
parameter = new DBusPath(op.path());
}
}

Expand Down
44 changes: 6 additions & 38 deletions dbus-java-core/src/main/java/org/freedesktop/dbus/MethodTuple.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ protected PropHandled handleGetAll(ExportedObject _exportObject, final MethodCal
Map<String, Object> resultMap = new HashMap<>();
for (Entry<PropertyRef, Method> 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);
Expand All @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ default <I extends DBusInterface> I getPeerRemoteObject(String _busname, String
default <I extends DBusInterface> I getPeerRemoteObject(String _busname, DBusPath _objectpath, Class<I> _type)
throws DBusException {
DBusObjects.requireObjectPath(_objectpath);
return getPeerRemoteObject(_busname, _objectpath.getPath(), _type);
return getPeerRemoteObject(_busname, _objectpath.path(), _type);
}

/**
Expand Down Expand Up @@ -134,7 +134,7 @@ default <I extends DBusInterface> 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);
}

/**
Expand Down Expand Up @@ -174,7 +174,7 @@ default <I extends DBusInterface> I getRemoteObject(String _busname, String _obj
default <I extends DBusInterface> I getRemoteObject(String _busname, DBusPath _objectpath, Class<I> _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
Expand Down Expand Up @@ -215,7 +215,7 @@ <I extends DBusInterface> I getRemoteObject(String _busname, String _objectpath,
default <I extends DBusInterface> I getRemoteObject(String _busname, DBusPath _objectpath, Class<I> _type,
boolean _autostart) throws DBusException {
DBusObjects.requireObjectPath(_objectpath);
return getRemoteObject(_busname, _objectpath.getPath(), _type, _autostart);
return getRemoteObject(_busname, _objectpath.path(), _type, _autostart);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ static boolean matchArg0123Path(Message _msg, Map<Integer, String> _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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,7 @@ private static Stream<TestValues> 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";
Expand Down
Loading
Loading