Search in sources :

Example 1 with ComMethod

use of com.sun.jna.platform.win32.COM.util.annotation.ComMethod in project jna by java-native-access.

the class ProxyObject method invoke.

// --------------------- InvocationHandler -----------------------------
@Override
public Object invoke(final Object proxy, final java.lang.reflect.Method method, final Object[] args) throws Throwable {
    boolean declaredAsInterface = (method.getAnnotation(ComMethod.class) != null) || (method.getAnnotation(ComProperty.class) != null);
    if ((!declaredAsInterface) && (method.getDeclaringClass().equals(Object.class) || method.getDeclaringClass().equals(IRawDispatchHandle.class) || method.getDeclaringClass().equals(com.sun.jna.platform.win32.COM.util.IUnknown.class) || method.getDeclaringClass().equals(com.sun.jna.platform.win32.COM.util.IDispatch.class) || method.getDeclaringClass().equals(IConnectionPoint.class))) {
        try {
            return method.invoke(this, args);
        } catch (InvocationTargetException ex) {
            throw ex.getCause();
        }
    }
    Class<?> returnType = method.getReturnType();
    boolean isVoid = Void.TYPE.equals(returnType);
    ComProperty prop = method.getAnnotation(ComProperty.class);
    if (null != prop) {
        int dispId = prop.dispId();
        if (isVoid) {
            if (dispId != -1) {
                this.setProperty(new DISPID(dispId), args[0]);
                return null;
            } else {
                String propName = this.getMutatorName(method, prop);
                this.setProperty(propName, args[0]);
                return null;
            }
        } else {
            if (dispId != -1) {
                return this.getProperty(returnType, new DISPID(dispId), args);
            } else {
                String propName = this.getAccessorName(method, prop);
                return this.getProperty(returnType, propName, args);
            }
        }
    }
    ComMethod meth = method.getAnnotation(ComMethod.class);
    if (null != meth) {
        Object[] fullLengthArgs = unfoldWhenVarargs(method, args);
        int dispId = meth.dispId();
        if (dispId != -1) {
            return this.invokeMethod(returnType, new DISPID(dispId), fullLengthArgs);
        } else {
            String methName = this.getMethodName(method, meth);
            return this.invokeMethod(returnType, methName, fullLengthArgs);
        }
    }
    return null;
}
Also used : ComProperty(com.sun.jna.platform.win32.COM.util.annotation.ComProperty) DISPID(com.sun.jna.platform.win32.OaIdl.DISPID) WString(com.sun.jna.WString) InvocationTargetException(java.lang.reflect.InvocationTargetException) ConnectionPoint(com.sun.jna.platform.win32.COM.ConnectionPoint) ComMethod(com.sun.jna.platform.win32.COM.util.annotation.ComMethod)

Aggregations

WString (com.sun.jna.WString)1 ConnectionPoint (com.sun.jna.platform.win32.COM.ConnectionPoint)1 ComMethod (com.sun.jna.platform.win32.COM.util.annotation.ComMethod)1 ComProperty (com.sun.jna.platform.win32.COM.util.annotation.ComProperty)1 DISPID (com.sun.jna.platform.win32.OaIdl.DISPID)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1