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

org.freedesktop.dbus.ExportedObject 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: 3.3.2
Show newest version
/*
   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 Lesser 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 static org.freedesktop.dbus.Gettext.t;

import java.lang.annotation.Annotation;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import org.freedesktop.dbus.exceptions.DBusException;
import org.freedesktop.dbus.exceptions.DBusExecutionException;

class ExportedObject {
    private String getAnnotations(AnnotatedElement c) {
        String ans = "";
        for (Annotation a : c.getDeclaredAnnotations()) {
            Class t = a.annotationType();
            String value = "";
            try {
                Method m = t.getMethod("value");
                value = m.invoke(a).toString();
            } catch (NoSuchMethodException exNsm) {
            } catch (InvocationTargetException exIt) {
            } catch (IllegalAccessException exIa) {
            }

            ans += "  \n";
        }
        return ans;
    }

    private Map getExportedMethods(Class c) throws DBusException {
        if (DBusInterface.class.equals(c)) {
            return new HashMap();
        }
        Map m = new HashMap();
        for (Class i : c.getInterfaces()) {
            if (DBusInterface.class.equals(i)) {
                // add this class's public methods
                if (null != c.getAnnotation(DBusInterfaceName.class)) {
                    String name = ((DBusInterfaceName) c.getAnnotation(DBusInterfaceName.class)).value();
                    introspectiondata += " \n";
                    DBusSignal.addInterfaceMap(c.getName(), name);
                } else {
                    // don't let people export things which don't have a
                    // valid D-Bus interface name
                    if (c.getName().equals(c.getSimpleName())) {
                        throw new DBusException(t("DBusInterfaces cannot be declared outside a package"));
                    }
                    if (c.getName().length() > AbstractConnection.MAX_NAME_LENGTH) {
                        throw new DBusException(t("Introspected interface name exceeds 255 characters. Cannot export objects of type ") + c.getName());
                    } else {
                        introspectiondata += " \n";
                    }
                }
                introspectiondata += getAnnotations(c);
                for (Method meth : c.getDeclaredMethods()) {
                    if (Modifier.isPublic(meth.getModifiers())) {
                        String ms = "";
                        String name;
                        if (meth.isAnnotationPresent(DBusMemberName.class)) {
                            name = meth.getAnnotation(DBusMemberName.class).value();
                        } else {
                            name = meth.getName();
                        }
                        if (name.length() > AbstractConnection.MAX_NAME_LENGTH) {
                            throw new DBusException(t("Introspected method name exceeds 255 characters. Cannot export objects with method ") + name);
                        }
                        introspectiondata += "  \n";
                        introspectiondata += getAnnotations(meth);
                        for (Class ex : meth.getExceptionTypes()) {
                            if (DBusExecutionException.class.isAssignableFrom(ex)) {
                                introspectiondata += "   \n";
                            }
                        }
                        for (Type pt : meth.getGenericParameterTypes()) {
                            for (String s : Marshalling.getDBusType(pt)) {
                                introspectiondata += "   \n";
                                ms += s;
                            }
                        }
                        if (!Void.TYPE.equals(meth.getGenericReturnType())) {
                            if (Tuple.class.isAssignableFrom(meth.getReturnType())) {
                                ParameterizedType tc = (ParameterizedType) meth.getGenericReturnType();
                                Type[] ts = tc.getActualTypeArguments();

                                for (Type t : ts) {
                                    if (t != null) {
                                        for (String s : Marshalling.getDBusType(t)) {
                                            introspectiondata += "   \n";
                                        }
                                    }
                                }
                            } else if (Object[].class.equals(meth.getGenericReturnType())) {
                                throw new DBusException(t("Return type of Object[] cannot be introspected properly"));
                            } else {
                                for (String s : Marshalling.getDBusType(meth.getGenericReturnType())) {
                                    introspectiondata += "   \n";
                                }
                            }
                        }
                        introspectiondata += "  \n";
                        m.put(new MethodTuple(name, ms), meth);
                    }
                }
                for (Class sig : c.getDeclaredClasses()) {
                    if (DBusSignal.class.isAssignableFrom(sig)) {
                        String name;
                        if (sig.isAnnotationPresent(DBusMemberName.class)) {
                            name = ((DBusMemberName) sig.getAnnotation(DBusMemberName.class)).value();
                            DBusSignal.addSignalMap(sig.getSimpleName(), name);
                        } else {
                            name = sig.getSimpleName();
                        }
                        if (name.length() > AbstractConnection.MAX_NAME_LENGTH) {
                            throw new DBusException(t("Introspected signal name exceeds 255 characters. Cannot export objects with signals of type ") + name);
                        }
                        introspectiondata += "  \n";
                        Constructor con = sig.getConstructors()[0];
                        Type[] ts = con.getGenericParameterTypes();
                        for (int j = 1; j < ts.length; j++) {
                            for (String s : Marshalling.getDBusType(ts[j])) {
                                introspectiondata += "   \n";
                            }
                        }
                        introspectiondata += getAnnotations(sig);
                        introspectiondata += "  \n";

                    }
                }
                introspectiondata += " \n";
            } else {
                // recurse
                m.putAll(getExportedMethods(i));
            }
        }
        return m;
    }
    // CHECKSTYLE:OFF
    Map methods;
    Reference object;
    String                   introspectiondata;
    // CHECKSTYLE:ON

    ExportedObject(DBusInterface _object, boolean _weakreferences) throws DBusException {
        if (_weakreferences) {
            this.object = new WeakReference(_object);
        } else {
            this.object = new StrongReference(_object);
        }
        introspectiondata = "";
        methods = getExportedMethods(_object.getClass());
        introspectiondata += " \n" + "  \n" + "   \n" + "  \n" + " \n";
        introspectiondata += " \n" + "  \n" + "  \n" + " \n";
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy