public class JSweetTranspiler extends Object implements JSweetOptions
Instantiate this class to transpile Java to TypeScript (phase 1), and TypeScript to JavaScript (phase 2).
There are 2 phases in JSweet transpilation:
In phase 1, JSweet delegates to Javac and applies the
Java2TypeScriptTranslator AST visitor to print out the TypeScript
code. Before printing out the code, the transpiler first applies AST
visitors: GlobalBeforeTranslationScanner,
StaticInitilializerAnalyzer, and TypeChecker. All external
referenced classes must be in the classpath for this phase to succeed. Note
that this generation is fully customizable with the
org.jsweet.transpiler.extension API.
In phase 2, JSweet delegates to tsc (TypeScript). TypeScript needs to have a TypeScript typing definition for all external classes. Existing JSweet candies (http://www.jsweet.org/jsweet-candies/) are Maven artifacts that come both with the compiled Java implementation/definition in a Jar, and the associated TypeScript definitions for phase 2.
| Modifier and Type | Field and Description |
|---|---|
static String |
EXPORTED_VAR_BEGIN
A constant that is used for exporting variables.
|
static String |
EXPORTED_VAR_END
A constant that is used for exporting variables.
|
static Pattern |
EXPORTED_VAR_REGEXP |
static String |
TMP_WORKING_DIR_NAME
The constant for the name of the directory that stores temporary files.
|
static String |
TSC_VERSION
The TypeScript version to be installed/used with this version of JSweet
(WARNING: so far, having multiple JSweet versions for the same user account
may lead to performance issues - could be fixed if necessary).
|
static String |
TSCROOTFILE
Deprecated.
|
candiesJsOut, declaration, disableSinglePrecisionFloats, dtsout, enableAssertions, extraSystemPath, header, ignoreDefinitions, jsout, module, sourceMap, targetVersion, tsOnly, tsout| Constructor and Description |
|---|
JSweetTranspiler(File baseDirectory,
File configurationFile,
JSweetFactory factory,
File workingDir,
File tsOutputDir,
File jsOutputDir,
File extractedCandiesJavascriptDir,
String classPath)
Creates a JSweet transpiler.
|
JSweetTranspiler(File configurationFile,
JSweetFactory factory,
File workingDir,
File tsOutputDir,
File jsOutputDir,
File extractedCandiesJavascriptDir,
String classPath)
Creates a JSweet transpiler.
|
JSweetTranspiler(JSweetFactory factory)
Creates a JSweet transpiler, with the default values.
|
JSweetTranspiler(JSweetFactory factory,
File workingDir,
File tsOutputDir,
File jsOutputDir,
File extractedCandiesJavascriptDir,
String classPath)
Creates a JSweet transpiler.
|
JSweetTranspiler(JSweetFactory factory,
File tsOutputDir,
File jsOutputDir,
File extractedCandiesJavascriptDir,
String classPath)
Creates a JSweet transpiler.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addJsLibFiles(File... files)
Add JavaScript libraries that are used for the JavaScript evaluation.
|
void |
addTsDefDir(File tsDefDir)
Adds a directory that contains TypeScript definition files (sub-directories
are scanned recursively to find all .d.ts files).
|
void |
clearJsLibFiles()
Clears JavaScript libraries that are used for the JavaScript evaluation.
|
void |
clearTsDefDirs()
Undo previous calls to
setTsDefDirs(File...) and
addTsDefDir(File). |
EvaluationResult |
eval(String engineName,
TranspilationHandler transpilationHandler,
SourceFile... sourceFiles)
Evaluates the given source files with the given evaluation engine.
|
EvaluationResult |
eval(TranspilationHandler transpilationHandler,
SourceFile... sourceFiles)
Evaluates the given Java source files with the default JavaScript engine
(Nashorn).
|
List<String> |
getAdapters()
A list of adapter class names (fully qualified) to be used to extend the
transpiler behavior.
|
CandyProcessor |
getCandiesProcessor()
Gets the candies processor.
|
String |
getClassPath() |
Map<String,Object> |
getConfiguration()
Returns the configuration from the configuration file.
|
File |
getConfigurationFile()
The configuration file, which is by default the
jsweetconfig.json file in the current project. |
JSweetContext |
getContext() |
File |
getDeclarationsOutputDir()
Gets the current .d.ts output directory (only if the declaration option
is set).
|
EcmaScriptComplianceLevel |
getEcmaTargetVersion()
The targeted ECMAScript version.
|
static EcmaScriptComplianceLevel |
getEcmaTargetVersion(String targetVersion)
Gets the target version from a user-friendly descriptive string.
|
String |
getEncoding()
Gets the expected Java source code encoding.
|
File |
getExtractedCandyJavascriptDir()
The directory where the transpiler should put the extracted JavaScript
files from candies.
|
File |
getHeaderFile()
Gets the file containing the header to be added to the generated files.
|
File |
getJsOutputDir()
Gets the current JavaScript output directory.
|
ModuleKind |
getModuleKind()
Gets the module kind when transpiling to code using JavaScript modules.
|
ModuleResolution |
getModuleResolution()
Determines how modules get resolved.
|
com.sun.tools.javac.util.Options |
getOptions() |
File |
getSourceRoot()
Gets the source root for the source map files.
|
File |
getTsOutputDir()
Gets the current TypeScript output directory.
|
SourceFile |
getWatchedFile(File javaFile)
Gets the watched files that corresponds to the given Java file.
|
SourceFile[] |
getWatchedFiles()
Returns the watched files when the transpiler is in watch mode.
|
File |
getWorkingDirectory()
Gets this transpiler working directory (where the temporary files are
stored).
|
void |
initNode(TranspilationHandler transpilationHandler) |
boolean |
isBundle()
Tells if this transpiler generates JavaScript bundles for running in a
Web browser.
|
boolean |
isDebugMode()
If true, the transpiler generates code that can be debugged with
JavaScript.
|
boolean |
isDisableSinglePrecisionFloats()
By default, for a target version >=ES5, JSweet will force Java floats to
be mapped to JavaScript numbers that will be constrained with the
Math.fround function.
|
boolean |
isGenerateDeclarations()
Generates d.ts files along with the js files.
|
boolean |
isGenerateDefinitions()
Generated definitions from def.* packages in d.ts files.
|
boolean |
isGenerateJsFiles()
Tells if the JavaScript generation is enabled/disabled.
|
boolean |
isGenerateSourceMaps()
Tells if the transpiler generates js.map files for Java debugging.
|
boolean |
isGenerateTsFiles()
If false, do not generate TypeScript output (just validate the Java
source code and do not transpile anything).
|
boolean |
isIgnoreAssertions()
Tells if the transpiler should ignore the 'assert' statements or generate
appropriate code.
|
boolean |
isIgnoreCandiesTypeScriptDefinitions() |
boolean |
isIgnoreJavaErrors()
If true, JSweet will ignore any message reported by Java (including
error).
|
boolean |
isIgnoreJavaFileNameError()
Generates output code even if the main class is not placed within a file
of the same name.
|
boolean |
isIgnoreTypeScriptErrors()
If true, JSweet will ignore any message reported by TypeScript (including
error) and the compilation will be successful if no Java error is raised.
|
boolean |
isNoRootDirectories()
Tells if this transpiler skips the root directories (packages annotated
with @jsweet.lang.Root) so that the generated file hierarchy starts at
the root directories rather than including the entire directory
structure.
|
boolean |
isPreserveSourceLineNumbers()
Deprecated.
|
boolean |
isSkipTypeScriptChecks() |
boolean |
isTscWatchMode()
Tells if this transpiler is using a Tsc watch process to automatically
regenerate the javascript when one of the source file changes.
|
boolean |
isUsingModules()
Tells if this transpiler transpiles to code using JavaScript modules.
|
boolean |
isVerbose()
If true, JSweet has been launched in verbose mode.
|
void |
resetTscWatchMode()
Resets the watch mode (clears the watched files and restarts the Tsc process
on the next invocation of
transpile(TranspilationHandler, SourceFile...)). |
void |
setAdapters(List<String> adapters) |
void |
setBundle(boolean bundle)
Sets this transpiler to generate JavaScript bundles for running in a Web
browser.
|
void |
setDebugMode(boolean debugMode) |
void |
setDeclarationsOutputDir(File declarationsOutputDir) |
void |
setDisableSinglePrecisionFloats(boolean disableSinglePrecisionFloats) |
void |
setEcmaTargetVersion(EcmaScriptComplianceLevel ecmaTargetVersion)
Sets target ECMA script version for generated JavaScript
|
void |
setEncoding(String encoding)
Sets the expected Java source code encoding.
|
void |
setGenerateDeclarations(boolean generateDeclarations) |
void |
setGenerateDefinitions(boolean generateDefinitions) |
void |
setGenerateJsFiles(boolean generateJsFiles)
Sets the flag to enable/disable JavaScript generation.
|
void |
setGenerateSourceMaps(boolean generateSourceMaps)
Sets the flag that tells if the transpiler allows for Java debugging through
js.map files.
|
void |
setGenerateTsFiles(boolean generateTsFiles) |
void |
setHeaderFile(File headerFile) |
void |
setIgnoreAssertions(boolean ignoreAssertions)
Sets the transpiler to ignore the 'assert' statements or generate appropriate
code.
|
void |
setIgnoreCandiesTypeScriptDefinitions(boolean ignoreCandiesTypeScriptDefinitions) |
void |
setIgnoreJavaErrors(boolean ignoreJavaErrors) |
void |
setIgnoreJavaFileNameError(boolean ignoreJavaFileNameError) |
void |
setIgnoreTypeScriptErrors(boolean ignoreTypeScriptErrors) |
void |
setJsOutputDir(File jsOutputDir)
Sets the current JavaScript output directory.
|
void |
setModuleKind(ModuleKind moduleKind)
Sets the module kind when transpiling to code using JavaScript modules.
|
void |
setModuleResolution(ModuleResolution moduleResolution)
Sets the module strategy when transpiling to code using JavaScript modules.
|
void |
setNoRootDirectories(boolean noRootDirectories)
Sets this transpiler to skip the root directories (packages annotated
with @jsweet.lang.Root) so that the generated file hierarchy starts at the
root directories rather than including the entire directory structure.
|
void |
setPreserveSourceLineNumbers(boolean preserveSourceLineNumbers)
Deprecated.
use
setGenerateSourceMaps(boolean) instead |
void |
setSkipTypeScriptChecks(boolean skipTypeScriptChecks)
Tells tsc to skip some checks in order to reduce load time, useful in unit
tests where transpiler is invoked many times
|
void |
setSourceRoot(File sourceRoot) |
void |
setTscWatchMode(boolean tscWatchMode)
Enables or disable this transpiler watch mode.
|
void |
setTsDefDirs(File... tsDefDirs)
Sets one or more directories that contain TypeScript definition files
(sub-directories are scanned recursively to find all .d.ts files).
|
void |
setTsOutputDir(File tsOutputDir)
Sets the current TypeScript output directory.
|
com.sun.tools.javac.util.List<com.sun.tools.javac.tree.JCTree.JCCompilationUnit> |
setupCompiler(List<File> files,
ErrorCountTranspilationHandler transpilationHandler) |
void |
setUseTsserver(boolean useTsserver) |
void |
setUsingJavaRuntime(boolean usingJavaRuntime)
Manually sets the transpiler to use (or not use) a Java runtime.
|
void |
setVerbose(boolean verbose) |
String |
toString() |
String |
transpile(ErrorCountTranspilationHandler handler,
com.sun.tools.javac.tree.JCTree tree,
String targetFileName)
Transpiles the given Java AST.
|
void |
transpile(TranspilationHandler transpilationHandler,
SourceFile... files)
Transpiles the given Java source files.
|
public static final String TSC_VERSION
public static final String TMP_WORKING_DIR_NAME
public static final String EXPORTED_VAR_BEGIN
TraceBasedEvaluationResult,
eval(TranspilationHandler, SourceFile...),
Constant Field Valuespublic static final String EXPORTED_VAR_END
TraceBasedEvaluationResult,
eval(TranspilationHandler, SourceFile...),
Constant Field Valuespublic static Pattern EXPORTED_VAR_REGEXP
public static final String TSCROOTFILE
TypeScript2JavaScriptWithTscTranspiler.TSCROOTFILE
insteadpublic JSweetTranspiler(JSweetFactory factory)
TypeScript and JavaScript output directories are set to
System.getProperty("java.io.tmpdir"). The classpath is set to
System.getProperty("java.class.path").
factory - the factory used to create the transpiler objectspublic JSweetTranspiler(JSweetFactory factory, File tsOutputDir, File jsOutputDir, File extractedCandiesJavascriptDir, String classPath)
factory - the factory used to create the transpiler objectstsOutputDir - the directory where TypeScript files are writtenjsOutputDir - the directory where JavaScript files are writtenextractedCandiesJavascriptDir - see getExtractedCandyJavascriptDir()classPath - the classpath as a string (check out system-specific requirements
for Java classpathes)public JSweetTranspiler(JSweetFactory factory, File workingDir, File tsOutputDir, File jsOutputDir, File extractedCandiesJavascriptDir, String classPath)
factory - the factory used to create the transpiler objectsworkingDir - the working directory (uses default one if null)tsOutputDir - the directory where TypeScript files are writtenjsOutputDir - the directory where JavaScript files are writtenextractedCandiesJavascriptDir - see getExtractedCandyJavascriptDir()classPath - the classpath as a string (check out system-specific requirements
for Java classpaths)public JSweetTranspiler(File configurationFile, JSweetFactory factory, File workingDir, File tsOutputDir, File jsOutputDir, File extractedCandiesJavascriptDir, String classPath)
configurationFile - the configurationFile (uses default one if null)factory - the factory used to create the transpiler objectsworkingDir - the working directory (uses default one if null)tsOutputDir - the directory where TypeScript files are writtenjsOutputDir - the directory where JavaScript files are writtenextractedCandiesJavascriptDir - see getExtractedCandyJavascriptDir()classPath - the classpath as a string (check out system-specific requirements
for Java classpaths)public JSweetTranspiler(File baseDirectory, File configurationFile, JSweetFactory factory, File workingDir, File tsOutputDir, File jsOutputDir, File extractedCandiesJavascriptDir, String classPath)
configurationFile - the configurationFile (uses default one if null)factory - the factory used to create the transpiler objectsworkingDir - the working directory (uses default one if null)tsOutputDir - the directory where TypeScript files are writtenjsOutputDir - the directory where JavaScript files are writtenextractedCandiesJavascriptDir - see getExtractedCandyJavascriptDir()classPath - the classpath as a string (check out system-specific requirements
for Java classpaths)public static EcmaScriptComplianceLevel getEcmaTargetVersion(String targetVersion)
public void setUsingJavaRuntime(boolean usingJavaRuntime)
Calling this method is usually not needed since JSweet auto-detects the J4TS candy. Use only to manually force the transpiler in a mode or another.
public File getWorkingDirectory()
public void initNode(TranspilationHandler transpilationHandler) throws Exception
Exceptionpublic void setTsDefDirs(File... tsDefDirs)
tsDefDirs - a list of directories to scan for .d.ts filespublic void addTsDefDir(File tsDefDir)
tsDefDir - a directory to scan for .d.ts filespublic void clearTsDefDirs()
setTsDefDirs(File...) and
addTsDefDir(File).public EvaluationResult eval(TranspilationHandler transpilationHandler, SourceFile... sourceFiles) throws Exception
This function automatically transpile the source files if needed.
transpilationHandler - the transpilation handlersourceFiles - the source files to be evaluatedException - when an internal error occurspublic EvaluationResult eval(String engineName, TranspilationHandler transpilationHandler, SourceFile... sourceFiles) throws Exception
If given engine name is "Java", this function looks up for the classes in the classpath and run the main methods when found.
engineName - the engine name: either "Java" or any valid and installed
JavaScript engine.transpilationHandler - the log handlersourceFiles - the source files to be evaluated (transpiled first if needed)Exception - when an internal error occurspublic com.sun.tools.javac.util.List<com.sun.tools.javac.tree.JCTree.JCCompilationUnit> setupCompiler(List<File> files, ErrorCountTranspilationHandler transpilationHandler) throws IOException
IOExceptionpublic void transpile(TranspilationHandler transpilationHandler, SourceFile... files) throws IOException
setTscWatchMode(boolean)), the first invocation to this method
determines the files to be watched by the Tsc process.transpilationHandler - the log handlerfiles - the files to be transpiledIOExceptionpublic void setUseTsserver(boolean useTsserver)
public SourceFile[] getWatchedFiles()
setTscWatchMode(boolean). The watched file list corresponds to the
one given at the first invocation of
transpile(TranspilationHandler, SourceFile...) after the transpiler
was set to watch mode. All subsequent invocations of
transpile(TranspilationHandler, SourceFile...) will not change the
initial watched files. In order to change the watch files, invoke
resetTscWatchMode() and call
transpile(TranspilationHandler, SourceFile...) with a new file list.public SourceFile getWatchedFile(File javaFile)
setTscWatchMode(boolean).public boolean isTscWatchMode()
isTscWatchMode in interface JSweetOptionspublic void setTscWatchMode(boolean tscWatchMode)
transpile(TranspilationHandler, SourceFile...) will start the Tsc
watch process, which regenerates the JavaScript files when one of the input
file changes.tscWatchMode - true: enables the watch mode (do nothing is already enabled),
false: disables the watch mode and stops the current Tsc watching
processgetWatchedFile(File)@Deprecated public boolean isPreserveSourceLineNumbers()
JSweetOptionsisPreserveSourceLineNumbers in interface JSweetOptionspublic boolean isGenerateSourceMaps()
JSweetOptionsisGenerateSourceMaps in interface JSweetOptions@Deprecated public void setPreserveSourceLineNumbers(boolean preserveSourceLineNumbers)
setGenerateSourceMaps(boolean) insteadpublic void setGenerateSourceMaps(boolean generateSourceMaps)
public File getTsOutputDir()
JSweetOptionsgetTsOutputDir in interface JSweetOptionspublic void setTsOutputDir(File tsOutputDir)
public File getJsOutputDir()
JSweetOptionsgetJsOutputDir in interface JSweetOptionspublic void setJsOutputDir(File jsOutputDir)
public boolean isGenerateJsFiles()
isGenerateJsFiles in interface JSweetOptionspublic void setGenerateJsFiles(boolean generateJsFiles)
public void resetTscWatchMode()
transpile(TranspilationHandler, SourceFile...)).public CandyProcessor getCandiesProcessor()
public void setEcmaTargetVersion(EcmaScriptComplianceLevel ecmaTargetVersion)
ecmaTargetVersion - The target versionpublic EcmaScriptComplianceLevel getEcmaTargetVersion()
JSweetOptionsgetEcmaTargetVersion in interface JSweetOptionspublic ModuleKind getModuleKind()
JSweetOptionsgetModuleKind in interface JSweetOptionspublic void setModuleKind(ModuleKind moduleKind)
public ModuleResolution getModuleResolution()
JSweetOptionsgetModuleResolution in interface JSweetOptionspublic void setModuleResolution(ModuleResolution moduleResolution)
public void setSkipTypeScriptChecks(boolean skipTypeScriptChecks)
public boolean isSkipTypeScriptChecks()
isSkipTypeScriptChecks in interface JSweetOptionspublic boolean isUsingModules()
isUsingModules in interface JSweetOptionspublic boolean isBundle()
JSweetOptionsisBundle in interface JSweetOptionspublic void setBundle(boolean bundle)
public String getEncoding()
JSweetOptionsgetEncoding in interface JSweetOptionspublic void setEncoding(String encoding)
public boolean isNoRootDirectories()
JSweetOptionsisNoRootDirectories in interface JSweetOptionspublic void setNoRootDirectories(boolean noRootDirectories)
public boolean isIgnoreAssertions()
JSweetOptionsisIgnoreAssertions in interface JSweetOptionspublic void setIgnoreAssertions(boolean ignoreAssertions)
public boolean isIgnoreJavaFileNameError()
JSweetOptionsisIgnoreJavaFileNameError in interface JSweetOptionspublic void setIgnoreJavaFileNameError(boolean ignoreJavaFileNameError)
public boolean isGenerateDeclarations()
JSweetOptionsisGenerateDeclarations in interface JSweetOptionspublic void setGenerateDeclarations(boolean generateDeclarations)
public File getDeclarationsOutputDir()
JSweetOptionsBy default, declarations are placed in the JavaScript output directory.
getDeclarationsOutputDir in interface JSweetOptionspublic void setDeclarationsOutputDir(File declarationsOutputDir)
public File getExtractedCandyJavascriptDir()
JSweetOptionsgetExtractedCandyJavascriptDir in interface JSweetOptionspublic void addJsLibFiles(File... files)
public void clearJsLibFiles()
public String transpile(ErrorCountTranspilationHandler handler, com.sun.tools.javac.tree.JCTree tree, String targetFileName) throws IOException
transpilationHandler - the log handlertree - the AST to be transpiledtargetFileName - the name of the file (without any extension) where to put the
transpilation outputIOExceptionpublic boolean isGenerateDefinitions()
JSweetOptionsisGenerateDefinitions in interface JSweetOptionspublic void setGenerateDefinitions(boolean generateDefinitions)
public File getSourceRoot()
JSweetOptionsgetSourceRoot in interface JSweetOptionspublic void setSourceRoot(File sourceRoot)
public Map<String,Object> getConfiguration()
JSweetOptionsgetConfiguration in interface JSweetOptionspublic boolean isIgnoreTypeScriptErrors()
JSweetOptionsisIgnoreTypeScriptErrors in interface JSweetOptionspublic void setIgnoreTypeScriptErrors(boolean ignoreTypeScriptErrors)
public File getHeaderFile()
JSweetOptionsgetHeaderFile in interface JSweetOptionspublic void setHeaderFile(File headerFile)
public boolean isGenerateTsFiles()
JSweetOptionsisGenerateTsFiles in interface JSweetOptionspublic void setGenerateTsFiles(boolean generateTsFiles)
public boolean isIgnoreJavaErrors()
JSweetOptionsisIgnoreJavaErrors in interface JSweetOptionspublic void setIgnoreJavaErrors(boolean ignoreJavaErrors)
public JSweetContext getContext()
public com.sun.tools.javac.util.Options getOptions()
public boolean isDebugMode()
JSweetOptionsisDebugMode in interface JSweetOptionspublic void setDebugMode(boolean debugMode)
public boolean isVerbose()
JSweetOptionsisVerbose in interface JSweetOptionspublic void setVerbose(boolean verbose)
public boolean isDisableSinglePrecisionFloats()
JSweetOptionsisDisableSinglePrecisionFloats in interface JSweetOptionspublic void setDisableSinglePrecisionFloats(boolean disableSinglePrecisionFloats)
public List<String> getAdapters()
JSweetOptionsPrinterAdapter that adapts the TypeScript default printer in
order to tune the generated code. All the adapter classes must be
accessible within the 'jsweet_extension' directory to be created at the
root of the transpiled project. The adapter classes can be provided as
Java class files (*.class) or Java source files (*.java). In the latter
case, they will be on-the-fly compiled by JSweet.getAdapters in interface JSweetOptionsPrinterAdapterpublic File getConfigurationFile()
JSweetOptionsjsweetconfig.json file in the current project.getConfigurationFile in interface JSweetOptionspublic String getClassPath()
public boolean isIgnoreCandiesTypeScriptDefinitions()
public void setIgnoreCandiesTypeScriptDefinitions(boolean ignoreCandiesTypeScriptDefinitions)