com.caoccao.javet.interop.proxy.JavetDynamicProxyObjectHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of javet-macos Show documentation
Show all versions of javet-macos Show documentation
Javet is Java + V8 (JAVa + V + EighT). It is an awesome way of embedding Node.js and V8 in Java.
/*
* Copyright (c) 2021-2022. 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.annotations.V8Function;
import com.caoccao.javet.enums.V8ConversionMode;
import com.caoccao.javet.enums.V8ProxyMode;
import com.caoccao.javet.exceptions.JavetException;
import com.caoccao.javet.interop.V8Runtime;
import com.caoccao.javet.interop.V8Scope;
import com.caoccao.javet.interop.binding.ClassDescriptor;
import com.caoccao.javet.utils.JavetStringUtils;
import com.caoccao.javet.utils.ThreadSafeMap;
import com.caoccao.javet.values.V8Value;
import com.caoccao.javet.values.primitive.V8ValueBoolean;
import com.caoccao.javet.values.primitive.V8ValueString;
import com.caoccao.javet.values.reference.V8ValueArray;
import com.caoccao.javet.values.reference.V8ValueSymbol;
import com.caoccao.javet.values.reference.builtin.V8ValueBuiltInSymbol;
import java.lang.reflect.Array;
import java.util.*;
/**
* The type Javet dynamic proxy object handler.
*
* @param the type parameter
* @since 0.9.6
*/
@SuppressWarnings("unchecked")
public class JavetDynamicProxyObjectHandler extends BaseJavetProxyHandler {
/**
* The constant FUNCTION_NAME_LENGTH.
*
* @since 1.0.6
*/
protected static final String FUNCTION_NAME_LENGTH = "length";
/**
* The constant classDescriptorMap.
*
* @since 1.1.7
*/
protected static final ThreadSafeMap, ClassDescriptor> classDescriptorMap = new ThreadSafeMap<>();
/**
* Instantiates a new Javet dynamic proxy object handler.
*
* @param v8Runtime the V8 runtime
* @param dynamicObjectFactory the dynamic object factory
* @param targetObject the target object
* @since 0.9.6
*/
public JavetDynamicProxyObjectHandler(
V8Runtime v8Runtime,
IJavetDynamicObjectFactory dynamicObjectFactory,
T targetObject) {
super(v8Runtime, dynamicObjectFactory, Objects.requireNonNull(targetObject));
}
@V8Function
@Override
public V8Value get(V8Value target, V8Value property, V8Value receiver) throws JavetException {
V8Value result = getFromCollection(property);
result = result == null ? getFromField(property) : result;
result = result == null ? getFromMethod(target, property) : result;
result = result == null ? getFromSymbol(property) : result;
result = result == null ? getFromGetter(property) : result;
return result == null ? v8Runtime.createV8ValueUndefined() : result;
}
@Override
public ThreadSafeMap, ClassDescriptor> getClassDescriptorCache() {
return classDescriptorMap;
}
/**
* Gets from collection.
*
* @param property the property
* @return the V8 value
* @throws JavetException the javet exception
* @since 1.1.7
*/
protected V8Value getFromCollection(V8Value property) throws JavetException {
if (property instanceof V8ValueString) {
String propertyString = ((V8ValueString) property).toPrimitive();
if (JavetStringUtils.isDigital(propertyString)) {
final int index = Integer.parseInt(propertyString);
if (index >= 0) {
if (classDescriptor.getTargetClass().isArray()) {
if (index < Array.getLength(targetObject)) {
return v8Runtime.toV8Value(Array.get(targetObject, index));
}
} else if (List.class.isAssignableFrom(classDescriptor.getTargetClass())) {
List> list = (List>) targetObject;
if (index < list.size()) {
return v8Runtime.toV8Value(list.get(index));
}
}
}
} else if (classDescriptor.getTargetClass().isArray() && FUNCTION_NAME_LENGTH.equals(propertyString)) {
return v8Runtime.toV8Value(Array.getLength(targetObject));
}
}
return null;
}
/**
* Gets from symbol.
*
* @param property the property
* @return the V8 value
* @throws JavetException the javet exception
* @since 1.1.7
*/
protected V8Value getFromSymbol(V8Value property) throws JavetException {
if (property instanceof V8ValueSymbol) {
V8ValueSymbol propertySymbol = (V8ValueSymbol) property;
String description = propertySymbol.getDescription();
if (V8ValueBuiltInSymbol.SYMBOL_PROPERTY_TO_PRIMITIVE.equals(description)) {
return new JavetProxySymbolToPrimitiveConverter<>(v8Runtime, targetObject).getV8ValueFunction();
} else if (V8ValueBuiltInSymbol.SYMBOL_PROPERTY_ITERATOR.equals(description)
&& (targetObject instanceof Iterable || classDescriptor.getTargetClass().isArray())) {
return new JavetProxySymbolIterableConverter<>(v8Runtime, targetObject).getV8ValueFunction();
}
}
return null;
}
@V8Function
@Override
public V8ValueBoolean has(V8Value target, V8Value property) throws JavetException {
boolean isFound = hasFromCollection(property);
isFound = isFound || hasFromRegular(property);
isFound = isFound || hasFromGeneric(property);
return v8Runtime.createV8ValueBoolean(isFound);
}
/**
* Has from collection.
*
* @param property the property
* @return true : has, false: not has
* @throws JavetException the javet exception
*/
protected boolean hasFromCollection(V8Value property) throws JavetException {
if (classDescriptor.isTargetTypeMap()) {
return ((Map, ?>) targetObject).containsKey(v8Runtime.toObject(property));
} else if (classDescriptor.isTargetTypeSet()) {
return ((Set>) targetObject).contains(v8Runtime.toObject(property));
} else if (property instanceof V8ValueString) {
String indexString = ((V8ValueString) property).toPrimitive();
if (JavetStringUtils.isDigital(indexString)) {
final int index = Integer.parseInt(indexString);
if (index >= 0) {
if (classDescriptor.getTargetClass().isArray()) {
return index < Array.getLength(targetObject);
} else if (List.class.isAssignableFrom(classDescriptor.getTargetClass())) {
return index < ((List>) targetObject).size();
}
}
}
}
return false;
}
@Override
protected void initialize() {
Class targetClass = (Class) targetObject.getClass();
classDescriptor = classDescriptorMap.get(targetClass);
if (classDescriptor == null) {
classDescriptor = new ClassDescriptor(V8ProxyMode.Object, targetClass);
if (targetObject instanceof Class) {
initializeFieldsAndMethods((Class>) targetObject, true);
}
initializeCollection();
initializeFieldsAndMethods(targetClass, false);
classDescriptorMap.put(targetClass, classDescriptor);
}
}
/**
* Initialize collection.
*
* @since 1.1.7
*/
protected void initializeCollection() {
if (classDescriptor.isTargetTypeMap()) {
((Map