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

org.eclipse.jetty.websocket.common.events.annotated.CallableMethod Maven / Gradle / Ivy

There is a newer version: 11.0.0.beta1
Show newest version
//
//  ========================================================================
//  Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//

package org.eclipse.jetty.websocket.common.events.annotated;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Objects;

import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.websocket.api.WebSocketException;
import org.eclipse.jetty.websocket.common.util.ReflectUtils;

/**
 * A Callable Method
 */
public class CallableMethod
{
    private static final Logger LOG = Log.getLogger(CallableMethod.class);
    protected final Class pojo;
    protected final Method method;
    protected Class[] paramTypes;

    public CallableMethod(Class pojo, Method method)
    {
        Objects.requireNonNull(pojo, "Pojo cannot be null");
        Objects.requireNonNull(method, "Method cannot be null");
        this.pojo = pojo;
        this.method = method;
        this.paramTypes = method.getParameterTypes();
    }

    public Object call(Object obj, Object... args)
    {
        if ((this.pojo == null) || (this.method == null))
        {
            LOG.warn("Cannot execute call: pojo={}, method={}",pojo,method);
            return null; // no call event method determined
        }

        if (obj == null)
        {
            LOG.warn("Cannot call {} on null object",this.method);
            return null;
        }

        if (args.length < paramTypes.length)
        {
            throw new IllegalArgumentException("Call arguments length [" + args.length + "] must always be greater than or equal to captured args length ["
                    + paramTypes.length + "]");
        }

        try
        {
            return this.method.invoke(obj,args);
        }
        catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e)
        {
            StringBuilder err = new StringBuilder();
            err.append("Cannot call method ");
            err.append(ReflectUtils.toString(pojo,method));
            err.append(" with args: [");

            boolean delim = false;
            for (Object arg : args)
            {
                if (delim)
                {
                    err.append(", ");
                }
                if (arg == null)
                {
                    err.append("");
                }
                else
                {
                    err.append(arg.getClass().getName());
                }
                delim = true;
            }
            err.append("]");

            throw new WebSocketException(err.toString(),e);
        }
    }

    public Method getMethod()
    {
        return method;
    }

    public Class[] getParamTypes()
    {
        return paramTypes;
    }

    public Class getPojo()
    {
        return pojo;
    }

    @Override
    public String toString()
    {
        return String.format("%s[%s]",this.getClass().getSimpleName(),method.toGenericString());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy