native icon indicating copy to clipboard operation
native copied to clipboard

[jnigen] Add a templating language for custom methods in transformers

Open HosseinYousefi opened this issue 6 months ago • 0 comments

Currently we can add a custom method to an existing JNIgen generated class, however the experience is clunky. We might want to use other Java classes, that exist in different packages, hardcoding all this can be flaky, we would like to write our custom code in a templating language that allows us to reference other Java types, methods, fields, ... These references will be replaced at the generation time with the correct values after all the renaming is done. They also add the necessary import statements to import these types.

An example syntax of such language:

r'''
  @{{dart:core/override}}
  @{{dart:core/StackTrace}} get stackTrace {
    final stringWriter = {{java.io.StringWriter}}();
    {{java.lang.Throwable#printStackTrace(Ljava.io.PrintWriter;)V}}(stringWriter);
    final stackTrace = @{{dart:core/StackTrace}}.fromString(
      stringWriter.toString().toDartString(releaseOriginal: true),
    );
    stringWriter.release();
    return stackTrace;
  }
'''

HosseinYousefi avatar May 12 '25 11:05 HosseinYousefi