All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.freedesktop.dbus.utils.DBusNamingUtil Maven / Gradle / Ivy

Go to download

Improved version of the DBus-Java library provided by freedesktop.org (https://dbus.freedesktop.org/doc/dbus-java/).

There is a newer version: 5.1.0
Show newest version
package org.freedesktop.dbus.utils;

import org.freedesktop.dbus.annotations.DBusInterfaceName;
import org.freedesktop.dbus.annotations.DBusMemberName;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Objects;
import java.util.regex.Pattern;

/**
 * DBus name Util class for internal and external use.
 */
public final class DBusNamingUtil {
    private static final Pattern DOLLAR_PATTERN = Pattern.compile("[$]");

    private DBusNamingUtil() {
    }

    /**
     * Get DBus interface name for specified interface class
     *
     * @param clazz input DBus interface class
     * @return interface name
     * @see DBusInterfaceName
     */
    public static String getInterfaceName(Class clazz) {
        Objects.requireNonNull(clazz, "clazz must not be null");

        if (clazz.isAnnotationPresent(DBusInterfaceName.class)) {
            return clazz.getAnnotation(DBusInterfaceName.class).value();
        }
        return DOLLAR_PATTERN.matcher(clazz.getName()).replaceAll(".");
    }

    /**
     * Get DBus method name for specified method object.
     *
     * @param method input method
     * @return method name
     * @see DBusMemberName
     */
    public static String getMethodName(Method method) {
        Objects.requireNonNull(method, "method must not be null");

        if (method.isAnnotationPresent(DBusMemberName.class)) {
            return method.getAnnotation(DBusMemberName.class).value();
        }
        return method.getName();
    }

    /**
     * Get DBus signal name for specified signal class.
     *
     * @param clazz input DBus signal class
     * @return signal name
     * @see DBusMemberName
     */
    public static String getSignalName(Class clazz) {
        Objects.requireNonNull(clazz, "clazz must not be null");

        if (clazz.isAnnotationPresent(DBusMemberName.class)) {
            return clazz.getAnnotation(DBusMemberName.class).value();
        }
        return clazz.getSimpleName();
    }

    /**
     * Get DBus name for specified annotation class
     *
     * @param clazz input DBus annotation
     * @return interface name
     * @see DBusInterfaceName
     */
    public static String getAnnotationName(Class clazz) {
        Objects.requireNonNull(clazz, "clazz must not be null");

        if (clazz.isAnnotationPresent(DBusInterfaceName.class)) {
            return clazz.getAnnotation(DBusInterfaceName.class).value();
        }
        return DOLLAR_PATTERN.matcher(clazz.getName()).replaceAll(".");
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy