Design of `exerciseArchive` in Java bindings
- Tamas_KalczaOP
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
exerciseArchivenot defined on a generic/super interface, e.g.ContractId? - Why does the
exerciseArchivemethod have a parameter? - Why is that parameter in the generated code and not in some generic code?
- Why is the
- Stephen
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, andUpdate, which among other things enable typed exercise and typed ACS/tx stream decoding. With even functions like this 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
exerciseArchivemethod 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 avoidsexerciseArchiveentirely, just relying on the generatedArchiveclass.// 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 - Tamas_Kalcza
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
exerciseArchiveup tocom.daml.ledger.javaapi.data.codegen.ContractIdand/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
Archiveis 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.
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); } } - Stephen
Tamas_Kalcza:Thanks for the code snippet for generic archive approach. The issue here is really that
Archiveis 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
Choicedoesn’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: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
vis a record, but because you don’t need any data, ignoring it is fine here. - Stephen
You may wish to
github.com/digital-asset/daml
Subscribe to this relevant PR:generic `exerciseArchive` for Java codegen
digital-asset:main←digital-asset:s11/15540-generic-exerciseArchiveFixes #15540 with two related features: 1. This was skipped before by the fla…
- Stephen
The above should be included in Daml 2.7.0.