@Test void addsArrayItemsToTheOdBuilderWhenReturnTypeIsArray() { JsApiCache.Builder builder = new JsApiCache.Builder(() -> odBuilder, () -> entryBuilder); builder.addClass(Foo.class, mock(ClassEntry.class)); ObjectEntry objectEntry = mock(ObjectEntry.class); Foo foo1 = mock(Foo.class); Foo foo2 = mock(Foo.class); FooArray array = new FooArray(List.of(foo1, foo2)); when(objectEntry.call(any(), any())).thenReturn(array); builder.add("foo", objectEntry); JsApiCache cache = builder.build(); Object[] args = new Object[] {"x", 5}; FunctionCall functionCall = new FunctionCall("foo", "x", args); ObjectDescription result = cache.call(functionCall); verify(odBuilder).setArray(any(),eq(array), eq(Foo.class)); verify(adBuilder).addItem(any(), eq(foo1)); verify(adBuilder).addItem(any(), eq(foo2)); }
ObjectDescription call(FunctionCall functionCall) { ObjectEntry objectEntry = (ObjectEntry) this.entries.get(functionCall.getObject()); if(objectEntry == null) { objectEntry = this.sessionEntries.get(SessionCacheKey.of(functionCall.getObject())); } Object result = objectEntry.call(functionCall.getFunction(), functionCall.getArgs()); if(result instanceof JsArray<?>) { Class<?> elementType = null; try { ParameterizedType genericSuperclass = (ParameterizedType) result.getClass().getGenericSuperclass(); String typeName = genericSuperclass.getActualTypeArguments()[0].getTypeName(); elementType = Class.forName(typeName); } catch (ClassNotFoundException e) { throw new IllegalStateException("Could not get element type of JsArray", e); } JsArray<?> jsArray = (JsArray<?>) result; ObjectDescription.ArrayDescriptionBuilder arrayDescriptionBuilder = odBuilderSupplier.get() .setArray(new SessionCacheKey(), jsArray, elementType); for(Object item : jsArray.getItems()) { SessionCacheKey cacheKey = new SessionCacheKey(); ObjectEntry.Builder oeBuilder = entryBuilderSupplier.get(); oeBuilder.setProxiedObject(item); oeBuilder.setClass(item.getClass().getName(), (ClassEntry) this.entries.get("--classes").get(item.getClass().getName())); sessionEntries.put(cacheKey, oeBuilder.build()); arrayDescriptionBuilder.addItem(cacheKey, item); } return arrayDescriptionBuilder .build(); } else if(result == null) { return odBuilderSupplier.get().empty(); } return odBuilderSupplier.get().setObject(null, result).build(); }
JsApiCache extends ApiEntry { ObjectDescription call(FunctionCall functionCall) { ObjectEntry objectEntry = (ObjectEntry) this.entries.get(functionCall.getObject()); if(objectEntry == null) { objectEntry = this.sessionEntries.get(SessionCacheKey.of(functionCall.getObject())); } Object result = objectEntry.call(functionCall.getFunction(), functionCall.getArgs()); if(result instanceof JsArray<?>) { Class<?> elementType = null; try { ParameterizedType genericSuperclass = (ParameterizedType) result.getClass().getGenericSuperclass(); String typeName = genericSuperclass.getActualTypeArguments()[0].getTypeName(); elementType = Class.forName(typeName); } catch (ClassNotFoundException e) { throw new IllegalStateException("Could not get element type of JsArray", e); } JsArray<?> jsArray = (JsArray<?>) result; ObjectDescription.ArrayDescriptionBuilder arrayDescriptionBuilder = odBuilderSupplier.get() .setArray(new SessionCacheKey(), jsArray, elementType); for(Object item : jsArray.getItems()) { SessionCacheKey cacheKey = new SessionCacheKey(); ObjectEntry.Builder oeBuilder = entryBuilderSupplier.get(); oeBuilder.setProxiedObject(item); oeBuilder.setClass(item.getClass().getName(), (ClassEntry) this.entries.get("--classes").get(item.getClass().getName())); sessionEntries.put(cacheKey, oeBuilder.build()); arrayDescriptionBuilder.addItem(cacheKey, item); } return arrayDescriptionBuilder .build(); } else if(result == null) { return odBuilderSupplier.get().empty(); } return odBuilderSupplier.get().setObject(null, result).build(); } }
JsApiCache extends ApiEntry { ObjectDescription call(FunctionCall functionCall) { ObjectEntry objectEntry = (ObjectEntry) this.entries.get(functionCall.getObject()); if(objectEntry == null) { objectEntry = this.sessionEntries.get(SessionCacheKey.of(functionCall.getObject())); } Object result = objectEntry.call(functionCall.getFunction(), functionCall.getArgs()); if(result instanceof JsArray<?>) { Class<?> elementType = null; try { ParameterizedType genericSuperclass = (ParameterizedType) result.getClass().getGenericSuperclass(); String typeName = genericSuperclass.getActualTypeArguments()[0].getTypeName(); elementType = Class.forName(typeName); } catch (ClassNotFoundException e) { throw new IllegalStateException("Could not get element type of JsArray", e); } JsArray<?> jsArray = (JsArray<?>) result; ObjectDescription.ArrayDescriptionBuilder arrayDescriptionBuilder = odBuilderSupplier.get() .setArray(new SessionCacheKey(), jsArray, elementType); for(Object item : jsArray.getItems()) { SessionCacheKey cacheKey = new SessionCacheKey(); ObjectEntry.Builder oeBuilder = entryBuilderSupplier.get(); oeBuilder.setProxiedObject(item); oeBuilder.setClass(item.getClass().getName(), (ClassEntry) this.entries.get("--classes").get(item.getClass().getName())); sessionEntries.put(cacheKey, oeBuilder.build()); arrayDescriptionBuilder.addItem(cacheKey, item); } return arrayDescriptionBuilder .build(); } else if(result == null) { return odBuilderSupplier.get().empty(); } return odBuilderSupplier.get().setObject(null, result).build(); } private  JsApiCache(); }
JsApiCache extends ApiEntry { ObjectDescription call(FunctionCall functionCall) { ObjectEntry objectEntry = (ObjectEntry) this.entries.get(functionCall.getObject()); if(objectEntry == null) { objectEntry = this.sessionEntries.get(SessionCacheKey.of(functionCall.getObject())); } Object result = objectEntry.call(functionCall.getFunction(), functionCall.getArgs()); if(result instanceof JsArray<?>) { Class<?> elementType = null; try { ParameterizedType genericSuperclass = (ParameterizedType) result.getClass().getGenericSuperclass(); String typeName = genericSuperclass.getActualTypeArguments()[0].getTypeName(); elementType = Class.forName(typeName); } catch (ClassNotFoundException e) { throw new IllegalStateException("Could not get element type of JsArray", e); } JsArray<?> jsArray = (JsArray<?>) result; ObjectDescription.ArrayDescriptionBuilder arrayDescriptionBuilder = odBuilderSupplier.get() .setArray(new SessionCacheKey(), jsArray, elementType); for(Object item : jsArray.getItems()) { SessionCacheKey cacheKey = new SessionCacheKey(); ObjectEntry.Builder oeBuilder = entryBuilderSupplier.get(); oeBuilder.setProxiedObject(item); oeBuilder.setClass(item.getClass().getName(), (ClassEntry) this.entries.get("--classes").get(item.getClass().getName())); sessionEntries.put(cacheKey, oeBuilder.build()); arrayDescriptionBuilder.addItem(cacheKey, item); } return arrayDescriptionBuilder .build(); } else if(result == null) { return odBuilderSupplier.get().empty(); } return odBuilderSupplier.get().setObject(null, result).build(); } private  JsApiCache();  }
JsApiCache extends ApiEntry { ObjectDescription call(FunctionCall functionCall) { ObjectEntry objectEntry = (ObjectEntry) this.entries.get(functionCall.getObject()); if(objectEntry == null) { objectEntry = this.sessionEntries.get(SessionCacheKey.of(functionCall.getObject())); } Object result = objectEntry.call(functionCall.getFunction(), functionCall.getArgs()); if(result instanceof JsArray<?>) { Class<?> elementType = null; try { ParameterizedType genericSuperclass = (ParameterizedType) result.getClass().getGenericSuperclass(); String typeName = genericSuperclass.getActualTypeArguments()[0].getTypeName(); elementType = Class.forName(typeName); } catch (ClassNotFoundException e) { throw new IllegalStateException("Could not get element type of JsArray", e); } JsArray<?> jsArray = (JsArray<?>) result; ObjectDescription.ArrayDescriptionBuilder arrayDescriptionBuilder = odBuilderSupplier.get() .setArray(new SessionCacheKey(), jsArray, elementType); for(Object item : jsArray.getItems()) { SessionCacheKey cacheKey = new SessionCacheKey(); ObjectEntry.Builder oeBuilder = entryBuilderSupplier.get(); oeBuilder.setProxiedObject(item); oeBuilder.setClass(item.getClass().getName(), (ClassEntry) this.entries.get("--classes").get(item.getClass().getName())); sessionEntries.put(cacheKey, oeBuilder.build()); arrayDescriptionBuilder.addItem(cacheKey, item); } return arrayDescriptionBuilder .build(); } else if(result == null) { return odBuilderSupplier.get().empty(); } return odBuilderSupplier.get().setObject(null, result).build(); } private  JsApiCache();   }