# Design of \`exerciseArchive\` in Java bindings

**URL:** <https://forum.canton.network/t/design-of-exercisearchive-in-java-bindings/6648>\
**Category:** App Development\
**Tags:** bindings, daml, rxjava, java\
**Created:** [June 6, 2023, 11:07am UTC](https://forum.canton.network/t/design-of-exercisearchive-in-java-bindings/6648 "2023-06-06T11:07:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tamas\_Kalcza](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/tamas_kalcza/32/1788_2.png) [@Tamas\_Kalcza](https://forum.canton.network/u/Tamas_Kalcza)\
**Post date:** [June 6, 2023, 11:07am UTC](https://forum.canton.network/t/design-of-exercisearchive-in-java-bindings/6648/1 "2023-06-06T11:07:38Z")

</div>

I wanted to create a generic code that given a contract ID from the generated codegen code returns the corresponding archive command. During this I have noticed that it seems to be extremely difficult (even considering using reflection).

Can someone explain some the design decisions behind the archival in the Java bindings? I have the following questions in particular:

- Why is the `exerciseArchive` not defined on a generic/super interface, e.g. `ContractId`?
- Why does the `exerciseArchive` method have a parameter?
- Why is that parameter in the _generated_ code and not in some generic code?

---

<div class="post-metadata">

**Author:** ![Stephen](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/stephen/32/102_2.png) [@Stephen](https://forum.canton.network/u/Stephen)\
**Post date:** [June 6, 2023, 3:22pm UTC](https://forum.canton.network/t/design-of-exercisearchive-in-java-bindings/6648/2 "2023-06-06T15:22:16Z")

</div>

The past several releases of Java bindings/codegen have seen numerous abstractions introduced to allow more kinds of abstract utilities to be written against them, chiefly `ContractTypeCompanion`, `ValueDecoder`, and `Update`, which among other things enable typed exercise and typed ACS/tx stream decoding. With even [functions like this](https://github.com/digital-asset/daml/blob/39f331ec7fad27f4923a6b887c19ffa20064b4d6/language-support/java/bindings/src/main/java/com/daml/ledger/javaapi/data/codegen/PrimitiveValueDecoders.java#L118-L131) having been generated inline instead of handwritten and called, there are a lot of potential further developments in this area.

> [@Tamas\_Kalcza](#):
>
> Why does the `exerciseArchive` method have a parameter?

The rule that lets Java codegen generate flattened overloads for choice argument record types doesn’t work on `Archive`. It’s definitely because it’s a strange argument, but I’m not exactly sure why.

> [@Tamas\_Kalcza](#):
>
> I wanted to create a generic code that given a contract ID from the generated codegen code returns the corresponding archive command.

Given a `javaapi.data.codegen.ContractId<?>`, I would take an approach that avoids `exerciseArchive` entirely, just relying on the generated `Archive` class.

```haskell
// this is constant; you can stash it somewhere
// it can be copied from any codegenned CHOICE_Archive
Choice<Object, Archive, Unit> choiceArchive = Choice.create(
    "Archive", value -> value.toValue(),
    Archive.valueDecoder(),
    PrimitiveValueDecoders.fromUnit);

// exactly what exerciseArchive does, but supplying the argument
var update = cid.makeExerciseCmd(choiceArchive, new Archive());

// update is the same as what exerciseArchive returns

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Kalcza](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/tamas_kalcza/32/1788_2.png) [@Tamas\_Kalcza](https://forum.canton.network/u/Tamas_Kalcza)\
**Post date:** [June 7, 2023, 5:51am UTC](https://forum.canton.network/t/design-of-exercisearchive-in-java-bindings/6648/3 "2023-06-07T05:51:50Z")

</div>

Those are great additions and I use them regularly.

Are you saying there is technical reason (imposed by some Java codegen library) that prevents pulling `exerciseArchive` up to `com.daml.ledger.javaapi.data.codegen.ContractId` and/or remove the argument? Maybe my mind tricks me, however I remember there used to be a variant of this method without the argument. If that was possible I think it should not be difficult to pull up the method.

Thanks for the code snippet for generic archive approach. The issue here is really that `Archive` is generated. So that code only works if such a snippet lives in a codebase that has access to the generated code. I wanted to create code that relies only the bindings itself.

I solved my problem using reflection, however it is not very nice.

```java
static <Data extends Template, Id extends ContractId<Data>> Update<?> archive(Id contractId) {
  try {
    var exerciseMethods = contractId.getClass().getInterfaces()[0].getDeclaredMethods();
    var exerciseArchive = Arrays
      .stream(exerciseMethods)
      .filter(x -> Objects.equals("exerciseArchive", x.getName()))
      .findFirst().get();
    var archiveType = exerciseArchive.getParameterTypes()[0];
    var archiveArgument = archiveType.getDeclaredConstructor().newInstance();
    return (Update<?>) exerciseArchive.invoke(contractId, archiveArgument);
  } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
    throw new RuntimeException(e);
  }
}

```

---

<div class="post-metadata">

**Author:** ![Stephen](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/stephen/32/102_2.png) [@Stephen](https://forum.canton.network/u/Stephen)\
**Post date:** [June 7, 2023, 3:35pm UTC](https://forum.canton.network/t/design-of-exercisearchive-in-java-bindings/6648/4 "2023-06-07T15:35:27Z")

</div>

> [@Tamas\_Kalcza](#):
>
> Thanks for the code snippet for generic archive approach. The issue here is really that `Archive` is generated. So that code only works if such a snippet lives in a codebase that has access to the generated code.

That can be worked around by the fact that `Choice` doesn’t require that its typed forms (every type parameter) are codegenned or “canonical” in any way. The code snippet I wrote can be adapted as follows to avoid codegen:

```haskell
class MyArchive {}

Choice<Object, MyArchive, Unit> choiceArchive = Choice.create(
    "Archive", value -> new /*data.*/DamlRecord(),
    v -> new MyArchive(),
    PrimitiveValueDecoders.fromUnit);

```

You could add stuff to confirm that `v` is a record, but because you don’t need any data, ignoring it is fine here.

---

<div class="post-metadata">

**Author:** ![Stephen](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/stephen/32/102_2.png) [@Stephen](https://forum.canton.network/u/Stephen)\
**Post date:** [July 13, 2023, 4:10pm UTC](https://forum.canton.network/t/design-of-exercisearchive-in-java-bindings/6648/5 "2023-07-13T16:10:27Z")

</div>

You may wish to 🔔 Subscribe to this relevant PR:

> <https://github.com/digital-asset/daml/pull/17113>
>
> Fixes #15540 with two related features:
> 
> 1. This was skipped before by the fla…ttening rules because \`Archive\` is defined in a separate package. By flattening across packages in this PR, \`Archive\` is now a valid candidate.
> 2. The new \`Exercises.Archive\` interface is a superinterface of \_most\_ generated \`exercise\*\` method sets, and therefore generated \`ContractId\`s, \`CreateAnd\`s, and \`ByKey\`s, giving a LUB to different contract types that contains the \`exerciseArchive\` method.
> \* This is \_most\_, not all, because the Daml-LF rules determine what codegen ought to do, and \_Daml-LF does not mandate an Archive method of any kind\_, never mind one in the exact shape that we happen to generate.
> \* If an \`Archive\` is present, \_and\_ it has an empty record as its parameter and \`Unit\` as its result type, then we can safely assume that the \`exerciseArchive\` method is sensible to include in the type.
> \* However, this \_still\_ excludes the generic \`ContractId\` type, because \_generic ContractId does not have enough data to generate a Daml-LF-correct \`Archive\` command\_ given the unknowns mentioned above. So we simply exclude it from the type instead of guessing the argument and hoping the guess is close enough. That's why the \`Exercises.Archive\` type is available to users who want access to the method, which is a supertype of codegenned contract IDs among other things mentioned above:
> 
> https://github.com/digital-asset/daml/blob/a87fe85930a66943ae2cc3e4d6f7175908b87621/language-support/java/codegen/src/it/java/com/daml/TemplateMethodTest.java#L88-L91

---

<div class="post-metadata">

**Author:** ![Stephen](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/stephen/32/102_2.png) [@Stephen](https://forum.canton.network/u/Stephen)\
**Post date:** [July 18, 2023, 1:55pm UTC](https://forum.canton.network/t/design-of-exercisearchive-in-java-bindings/6648/6 "2023-07-18T13:55:26Z")

</div>

The above should be included in Daml 2.7.0.
