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

com.caoccao.javet.interop.proxy.JavetDynamicProxyV8ValueFunctionInvocationHandler Maven / Gradle / Ivy

Go to download

Javet is Java + V8 (JAVa + V + EighT). It is an awesome way of embedding Node.js and V8 in Java.

There is a newer version: 4.0.0
Show newest version
/*
 * Copyright (c) 2021-2023. caoccao.com Sam Cao
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.caoccao.javet.interop.proxy;

import com.caoccao.javet.exceptions.JavetException;
import com.caoccao.javet.interfaces.IJavetClosable;
import com.caoccao.javet.utils.JavetResourceUtils;
import com.caoccao.javet.values.reference.V8ValueFunction;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * The type Javet dynamic proxy V8 value function invocation handler.
 *
 * @since 0.9.10
 */
public final class JavetDynamicProxyV8ValueFunctionInvocationHandler implements InvocationHandler, IJavetClosable {
    private static final String METHOD_NAME_CLOSE = "close";
    private V8ValueFunction v8ValueFunction;

    /**
     * Instantiates a new Javet dynamic proxy V8 value function invocation handler.
     *
     * @param v8ValueFunction the V8 value function
     * @since 0.9.10
     */
    public JavetDynamicProxyV8ValueFunctionInvocationHandler(V8ValueFunction v8ValueFunction) {
        this.v8ValueFunction = v8ValueFunction;
    }

    @Override
    public void close() throws JavetException {
        JavetResourceUtils.safeClose(v8ValueFunction);
        v8ValueFunction = null;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object result = null;
        if (args == null) {
            args = new Object[0];
        }
        String methodName = method.getName();
        if (methodName.equals(METHOD_NAME_CLOSE) && args.length == 0) {
            close();
        } else if (v8ValueFunction != null && !v8ValueFunction.isClosed()) {
            result = v8ValueFunction.callObject(null, args);
        }
        return result;
    }

    @Override
    public boolean isClosed() {
        return v8ValueFunction == null || v8ValueFunction.isClosed();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy