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

bionic.js.BjsNativeWrapper Maven / Gradle / Ivy

The newest version!
package bionic.js;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * The super type of every Java class which binds a {@link BjsExport} class to the associated JavaScript native
 * interface.
 *
 * @param  the type of the Java class bound to the JavaScript one
 */
public abstract class BjsNativeWrapper
{
    protected final Class realImplementation;

    protected static  Class getClass(Class extending, String fullClassName)
    {
        try
        {
            Class clazz = Class.forName(fullClassName);
            return clazz.asSubclass(extending);
        }
        catch (ClassNotFoundException e)
        {
            throw new RuntimeException(e);
        }
    }

    protected BjsNativeWrapper(Class realImplementation)
    {
        this.realImplementation = realImplementation;
    }

    protected final  R invokeStatic(String name, Class[] types, Object[] args)
    {
        try
        {
            Method method = realImplementation.getMethod(name, types);
            @SuppressWarnings("unchecked")
            R invoke = (R) method.invoke(realImplementation, args);
            return invoke;
        }
        catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e)
        {
            throw new RuntimeException(e);
        }
    }

    protected final T invokeConstructor(Class[] types, Object[] args)
    {
        try
        {
            Constructor constructor = realImplementation.getConstructor(types);
            return constructor.newInstance(args);
        }
        catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e)
        {
            throw new RuntimeException(e);
        }
    }
}