Skip to content
This repository was archived by the owner on Jun 19, 2024. It is now read-only.

Commit 9a62aa8

Browse files
committed
Fix docs, cache classes of JavaScript functions
1 parent 52e15c8 commit 9a62aa8

4 files changed

Lines changed: 16 additions & 10 deletions

File tree

ultralight-java-databind/src/main/java/com/labymedia/ultralight/databind/DatabindConfiguration.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,11 @@ public final class DatabindConfiguration {
4343
*
4444
* @param classCache The class cache used by this configuration
4545
* @param methodChooser The method chooser used by this configuration
46-
* @param propertyCallerFactory The factory creating the property caller used for calling properties on java objects and classes
46+
* @param propertyCallerFactory The factory creating the property caller used for calling properties on
47+
* java objects and classes
4748
* @param automaticPrototype If {@code true}, automatic prototyping is enabled
4849
* @param contextProviderFactory The factory for binding context providers, or {@code null}, if this feature
50+
* is not required
4951
*/
5052
private DatabindConfiguration(
5153
JavascriptClassCache classCache,
@@ -198,7 +200,8 @@ public Builder contextProviderFactory(ContextProviderFactory contextProviderFact
198200
* @return The built {@link DatabindConfiguration}
199201
*/
200202
public DatabindConfiguration build() {
201-
return new DatabindConfiguration(classCache, methodChooser, propertyCallerFactory, automaticPrototype, contextProviderFactory);
203+
return new DatabindConfiguration(
204+
classCache, methodChooser, propertyCallerFactory, automaticPrototype, contextProviderFactory);
202205
}
203206
}
204207
}

ultralight-java-databind/src/main/java/com/labymedia/ultralight/databind/DatabindJavascriptClass.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ public final class DatabindJavascriptClass {
6363
private final Map<String, Set<Method>> methods = new HashMap<>();
6464
private final Map<String, Field> fields = new HashMap<>();
6565

66+
private final Map<String, JavascriptClass> methodClassCache = new HashMap<>();
67+
6668
/**
6769
* Constructs a new {@link DatabindJavascriptClass}.
6870
*
@@ -242,12 +244,13 @@ private JavascriptValue onGetProperty(
242244
}
243245

244246
return context.makeObject(
245-
DatabindJavascriptMethodHandler.create(
247+
// caching classes of methods to avoid creating a new class on every call
248+
methodClassCache.computeIfAbsent(propertyName, key -> DatabindJavascriptMethodHandler.create(
246249
configuration,
247250
conversionUtils,
248251
propertyCaller,
249252
methodSet,
250-
propertyName).bake(),
253+
propertyName).bake()),
251254
new DatabindJavascriptMethodHandler.Data(privateData.instance, null));
252255
}
253256

ultralight-java-databind/src/main/java/com/labymedia/ultralight/databind/DatabindJavascriptMethodHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,14 @@ private JavascriptValue onCallAsFunction(
119119
arguments
120120
);
121121

122-
Object ret = this.propertyCaller.callMethod(privateData.instance(), method, parameters.toArray());
122+
// Invoke method with constructed arguments
123+
Object ret = propertyCaller.callMethod(privateData.instance(), method, parameters.toArray());
123124
Class<?> suggestedReturnType = method.getReturnType();
124125

125126
if (ret != null) {
126127
suggestedReturnType = ret.getClass();
127128
}
128129

129-
// Invoke method with constructed arguments
130130
return conversionUtils.toJavascript(context, ret, suggestedReturnType);
131131
}
132132

ultralight-java-databind/src/main/java/com/labymedia/ultralight/databind/call/property/PropertyCaller.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ public interface PropertyCaller {
3333
/**
3434
* Calls a method on a certain instance.
3535
*
36-
* @param instance The instance the method belongs to or {@code null}, if static
36+
* @param instance The instance the method belongs to, or {@code null}, if static
3737
* @param method The method which should be called
3838
* @param parameters The parameters the method should be called with
39-
* @return The return value of the method or {@code null} if void
39+
* @return The return value of the method, or {@code null}, if void
4040
* @throws JavascriptInteropException If an error occurred while calling the method
4141
*/
4242
Object callMethod(Object instance, Method method, Object[] parameters) throws JavascriptInteropException;
@@ -54,7 +54,7 @@ public interface PropertyCaller {
5454
/**
5555
* Returns the value from a certain field.
5656
*
57-
* @param instance The instance the field belongs to or {@code null} if static
57+
* @param instance The instance the field belongs to, or {@code null}, if static
5858
* @param field The field whose value should be returned
5959
* @return The value of the field
6060
* @throws JavascriptInteropException If an error occurred while getting the value from the field
@@ -64,7 +64,7 @@ public interface PropertyCaller {
6464
/**
6565
* Sets a value of a certain field.
6666
*
67-
* @param instance The instance the field belongs to or {@code null} if static
67+
* @param instance The instance the field belongs to, or {@code null}, if static
6868
* @param field The field whose value should be set
6969
* @param value The value to be set
7070
* @throws JavascriptInteropException If an error occurred while setting the value of the field

0 commit comments

Comments
 (0)