简体   繁体   English

Java库生成JavaScript代码

[英]Java Library to generate JavaScript Code

I need to generate JavaScript (ECMAScript) code from inside a Java program. 我需要从Java程序内部生成JavaScript(ECMAScript)代码。 For that, I am looking for something like JavaPoet , but producing JavaScript as output. 为此,我正在寻找类似JavaPoet的东西,但是将JavaScript作为输出。

I cannot use one of these transpilers that translates another language into JavaScript (eg GWT is not the answer) nor a tool that generates JavaScript from a syntax tree (only when there is a library that helps building that syntax tree ...). 我不能使用其中一种将另一种语言转换为JavaScript的编译器(例如,GWT不能解决问题),也不能使用从语法树生成JavaScript的工具(仅当存在帮助构建该语法树的库时)。

Something like the already mentioned JavaPoet would be the answer because it has a very small footprint both in memory usage and in code size. 像已经提到的JavaPoet之类的东西将是答案,因为它在内存使用和代码大小方面的占用空间都非常小。

Target for the resulting JavaScript code is Java/JSR 223 (Nashorn), if this would be relevant. 如果可能,生成的JavaScript代码的目标是Java / JSR 223(Nashorn)。

To specify the requirements: JavaPoet uses this code 要指定要求:JavaPoet使用此代码

MethodSpec main = MethodSpec.methodBuilder("main")
    .addModifiers(Modifier.PUBLIC, Modifier.STATIC)
    .returns(void.class)
    .addParameter(String[].class, "args")
    .addStatement("$T.out.println($S)", System.class, "Hello, JavaPoet!")
    .build();

TypeSpec helloWorld = TypeSpec.classBuilder("HelloWorld")
    .addModifiers(Modifier.PUBLIC, Modifier.FINAL)
    .addMethod(main)
    .build();

JavaFile javaFile = JavaFile.builder("com.example.helloworld", helloWorld)
    .build();
javaFile.writeTo(System.out);

to create this Java code 创建此Java代码

package com.example.helloworld;

public final class HelloWorld {
  public static void main(String[] args) {
    System.out.println("Hello, JavaPoet!");
  }
}

(I've copied the sample from the JavaPoet project web site.) (我已经从JavaPoet项目网站复制了该示例。)

I would like to have something similar that creates JavaScript code instead. 我想使用类似的方法来创建JavaScript代码。

To my current knowledge, StringBuilder is in fact closest to this requirement. 就我目前所知,StringBuilder实际上最接近此要求。 First generating Java to transform it to JavaScript should work, but looks really odd to me - and is all, but not really lightweight. 首先生成Java以将其转换为JavaScript应该可以,但是对我来说真的很奇怪-就是全部,但并不是真正的轻量级。 Same as first generate Kotlin or Ceylon code and then transforming it to JavaScript. 与首先生成Kotlin或Ceylon代码,然后将其转换为JavaScript相同。

I know you don't want a transpiler, but since it seems that there is no javascript generator for what you want, probably a light-weight java-js transpiler could do the job. 我知道您不希望使用编译器,但是由于似乎没有想要的javascript生成器,因此轻量级的Java-js编译器可以完成此工作。

Try to produce some java with javapoet as you mentioned and then make the output pass through the jsweet transpiler, there is an online sandbox that you can use to see the output before deciding whether it is the best option. 如前所述,尝试使用javapoet生成一些Java,然后使输出通过jsweet Transpiler,有一个在线沙箱可用于查看输出,然后再确定是否是最佳选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM