在Telosys模板中是否可以调用专门创建的函数? [英] Is it possible in a Telosys template to call a function created specifically?

查看:58
本文介绍了在Telosys模板中是否可以调用专门创建的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Telosys( https://www.telosys.org )生成Python源代码及其工作良好.但是我有一个特定的需求,可以通过调用特定的转换函数来解决.

I use Telosys (https://www.telosys.org) to generate Python source code and it works fine. But I have a specific need that could be solved by calling a specific conversion function.

是否可以创建一个特定的函数并在Telosys模板中调用它?

Is it possible to create a specific function and to call it inside a Telosys template?

例如: myFunction("abc") $ something.myFunction("abc")或其他任何内容

For example: myFunction("abc") or $something.myFunction("abc") or anything else

如有必要,我可以用Java,Python或JavaScript等不同语言创建此功能.

If necessary it's possible for me to create this function in different languages ​​like Java, Python or JavaScript.

推荐答案

Telosys被设计为可扩展的,因此您可以创建自己的函数并在模板中调用它们.由于Telosys是用Java编写的,因此您必须使用Java创建这些功能,然后使用" loader "".vm"中的对象文件以加载您的类并调用该类中定义的方法.

Telosys is designed to be extensible, so yes you can create your own functions and call them in your templates. As Telosys is written in Java you will have to create these functions in Java, then use the "loader" object in the ".vm" file to load your class and call the methods defined in this class.

这是逐步进行操作的方法:

Here's how to do that step by step:

  1. 使用首选的IDE创建一个Java类,以定义您的特定方法.此类可以在任何包中(包括默认/未命名的包"),方法可以是静态"的,也可以是静态"的.如果不需要该类的实例.

  1. Use your preferred IDE to create a Java class defining your specific method(s). This class can be in any package (including the "default / unnamed package"), the method(s) can be "static" if you don't need an instance of the class.

编译此类(目标是生成一个简单的" .class "文件或一个" .jar "文件).)

Compile this class (the goal is to produce a simple ".class" file or a ".jar" file if you prefer)

将类(或罐子)放入模板包文件夹中:

Put the class (or the jar) in the templates bundle folder :

  • 如果您有" .class "文件放在""中;文件夹
  • 如果您有" .jar "文件放入" lib "文件夹
    • if you have a ".class" file put it in "classes" folder
    • if you have a ".jar" file put it in the "lib" folder
    • 示例:

      TelosysTools/templates/my-bundle/classes/MyClass.class
      TelosysTools/templates/my-bundle/lib/my-lib.jar
      

      1. 在模板文件(".vm")中,使用" $ loader "对象以加载您的Java类并调用其任何方法请参见"$ loader"在这里参考: http://www.telosys.org/templates-doc/objects/loader.html
      1. In the template file (".vm") use the "$loader" object to load your Java class and call any of its methods See "$loader" reference here : http://www.telosys.org/templates-doc/objects/loader.html

      如果所有方法都是静态"的,则不需要实例,因此只需使用" $ loader.loadClass()".例子:

      If all your methods are "static" you don’t need an instance so just use "$loader.loadClass()". Example :

      ## load the class and keep it in a new "$Math" object (no instance created)
      #set( $Math = $loader.loadClass("java.lang.Math")
      ## use the static methods of this class
      $Math.random()
      

      如果您的方法不是静态"的,那么您需要一个实例,然后使用" $ loader.newInstance()".例子:

      If your methods are not "static" so you need an instance, then use "$loader.newInstance()". Examples :

      ## create an instance of StringBuilder and put it in the context with #set
      #set( $strBuilder = $loader.newInstance('java.lang.StringBuilder') )
      ## use the instance to call a method
      $strBuilder.append('aa')
             
      ## create new instance of a specific class : MyTool.class
      #set( $tool = $loader.newInstance('MyTool') )
      ## use the instance to call a method
      $tool.myFunction()
      

      总而言之,您可以使用Java-JRE提供的任何类(例如"Math","StringBuilder"),也可以通过添加".jar"文件来重用现有的库(不要忘记添加如果jar文件不是独立的,则需要依赖项)或仅添加一个".class"文件.

      So to sum up, you can use any class provided by Java-JRE (eg "Math", "StringBuilder"), you can reuse existing libraries by adding a ".jar" file (don't forget to add dependencies required if the jar file is not stand-alone) or just add a single ".class" file.

      这篇关于在Telosys模板中是否可以调用专门创建的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆