Skip to content

Commit

Permalink
🦄 refactor: Remove duplicated get() from reflection proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Jan 31, 2024
1 parent e5455d8 commit 67868d5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,12 @@ protected void addMethod(Method method, int startIndex, Map<String, List<Method>
}
}

@Override
public V8Value get(V8Value target, V8Value property, V8Value receiver) throws JavetException, E {
V8Value v8Value = internalGet(target, property);
return v8Value == null ? v8Runtime.createV8ValueUndefined() : v8Value;
}

/**
* Gets class descriptor cache.
*
Expand Down Expand Up @@ -324,7 +330,7 @@ protected int getGetterPrefixLength(Method method) {
public V8ValueObject getOwnPropertyDescriptor(V8Value target, V8Value property) throws JavetException, E {
V8Value v8Value = null;
try {
v8Value = get(target, property, null);
v8Value = internalGet(target, property);
return V8ValueUtils.createV8ValueObject(getV8Runtime(),
getV8Runtime().createV8ValueString(PROXY_PROPERTY_CONFIGURABLE),
getV8Runtime().createV8ValueBoolean(true),
Expand Down Expand Up @@ -540,6 +546,18 @@ protected void initializePublicMethods(
}
}

/**
* Internal get.
*
* @param target the target
* @param property the property
* @return the V8 value
* @throws JavetException the javet exception
* @throws E the custom exception
* @since 3.0.4
*/
protected abstract V8Value internalGet(V8Value target, V8Value property) throws JavetException, E;

/**
* Is allowed.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,6 @@ public V8Value construct(V8Value target, V8ValueArray arguments, V8Value newTarg
return v8Runtime.createV8ValueUndefined();
}

@Override
public V8Value get(V8Value target, V8Value property, V8Value receiver) throws JavetException {
V8Value v8Value = getFromField(property);
v8Value = v8Value == null ? getFromMethod(target, property) : v8Value;
v8Value = v8Value == null ? getFromGetter(property) : v8Value;
return v8Value == null ? v8Runtime.createV8ValueUndefined() : v8Value;
}

@Override
public JavetCallbackContext[] getCallbackContexts() {
if (callbackContexts == null) {
Expand Down Expand Up @@ -186,6 +178,14 @@ protected void initializeFieldsAndMethods(Class<?> currentClass, boolean staticM
} while (currentClass != null);
}

@Override
protected V8Value internalGet(V8Value target, V8Value property) throws JavetException {
V8Value v8Value = getFromField(property);
v8Value = v8Value == null ? getFromMethod(target, property) : v8Value;
v8Value = v8Value == null ? getFromGetter(property) : v8Value;
return v8Value;
}

@Override
public V8ValueArray ownKeys(V8Value target) throws JavetException {
return V8ValueUtils.createV8ValueArray(v8Runtime, classDescriptor.getUniqueKeySet().toArray());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,17 +131,6 @@ public V8ValueBoolean deleteProperty(V8Value target, V8Value property) throws Ja
return super.deleteProperty(target, property);
}

@Override
public V8Value get(V8Value target, V8Value property, V8Value receiver) throws JavetException, E {
V8Value v8Value = getFromCollection(property);
v8Value = v8Value == null ? getFromField(property) : v8Value;
v8Value = v8Value == null ? getFromMethod(target, property) : v8Value;
v8Value = v8Value == null ? getFromSymbol(property) : v8Value;
v8Value = v8Value == null ? getFromGetter(property) : v8Value;
v8Value = v8Value == null ? getFromPolyfill(property) : v8Value;
return v8Value == null ? v8Runtime.createV8ValueUndefined() : v8Value;
}

@Override
public JavetCallbackContext[] getCallbackContexts() {
if (callbackContexts == null) {
Expand Down Expand Up @@ -368,6 +357,17 @@ protected void initializeOverrideMethods() {
}
}

@Override
protected V8Value internalGet(V8Value target, V8Value property) throws JavetException, E {
V8Value v8Value = getFromCollection(property);
v8Value = v8Value == null ? getFromField(property) : v8Value;
v8Value = v8Value == null ? getFromMethod(target, property) : v8Value;
v8Value = v8Value == null ? getFromSymbol(property) : v8Value;
v8Value = v8Value == null ? getFromGetter(property) : v8Value;
v8Value = v8Value == null ? getFromPolyfill(property) : v8Value;
return v8Value;
}

@Override
public V8ValueArray ownKeys(V8Value target) throws JavetException {
Object[] keys = null;
Expand Down

0 comments on commit 67868d5

Please sign in to comment.