Search in sources :

Example 1 with DefaultJavaScriptNameProvider

use of org.stjs.generator.name.DefaultJavaScriptNameProvider in project st-js by st-js.

the class Generator method generateJavascript.

/**
 * <p>generateJavascript.</p>
 *
 * @return the list of imports needed by the generated class
 * @param className a {@link java.lang.String} object.
 * @param sourceFolder a {@link java.io.File} object.
 * @throws org.stjs.generator.JavascriptFileGenerationException if any.
 * @throws org.stjs.generator.JavascriptFileGenerationException if any.
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public ClassWithJavascript generateJavascript(String className, File sourceFolder) throws JavascriptFileGenerationException {
    Class<?> clazz = ClassUtils.getClazz(config.getStjsClassLoader(), className);
    if (ClassUtils.isBridge(config.getStjsClassLoader(), clazz)) {
        return new BridgeClass(config.getClassResolver(), clazz);
    }
    File inputFile = getInputFile(sourceFolder, className);
    File outputFile = getOutputFile(config.getGenerationFolder().getGeneratedSourcesAbsolutePath(), className);
    JavaScriptNameProvider names = new DefaultJavaScriptNameProvider();
    GenerationPlugins<Object> currentClassPlugins = plugins.forClass(clazz);
    GenerationContext<Object> context = new GenerationContext<Object>(inputFile, config, names, null, cacheAnnotations, getJavaScriptBuilder());
    CompilationUnitTree cu = parseAndResolve(inputFile, context, config.getStjsClassLoader(), config.getSourceEncoding());
    // check the code
    Timers.start("check-java");
    currentClassPlugins.getCheckVisitor().scan(cu, (GenerationContext) context);
    context.getChecks().check();
    Timers.end("check-java");
    // generate the javascript code
    Timers.start("write-js-ast");
    Object javascriptRoot = currentClassPlugins.getWriterVisitor().scan(cu, context);
    // check for any error arriving during writing
    context.getChecks().check();
    Timers.end("write-js-ast");
    Class<?> javaClass = config.getClassResolver().resolveJavaClass(className);
    STJSClass stjsClass = new STJSClass(config.getClassResolver(), config.getTargetFolder(), javaClass);
    Map<String, DependencyType> resolvedClasses = new LinkedHashMap<String, DependencyType>(names.getResolvedTypes());
    resolvedClasses.remove(className);
    stjsClass.setDependencies(resolvedClasses);
    stjsClass.setGeneratedJavascriptFile(getRuntimeUri(className));
    TypeElement classElement = context.getElements().getTypeElement(clazz.getCanonicalName());
    stjsClass.setJavascriptNamespace(context.wrap(classElement).getNamespace());
    // dump the ast to a file
    taskExecutor.execute(new DumpFilesTask<>(outputFile, context, javascriptRoot, stjsClass));
    return stjsClass;
}
Also used : DefaultJavaScriptNameProvider(org.stjs.generator.name.DefaultJavaScriptNameProvider) JavaScriptNameProvider(org.stjs.generator.name.JavaScriptNameProvider) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) TypeElement(javax.lang.model.element.TypeElement) LinkedHashMap(java.util.LinkedHashMap) DefaultJavaScriptNameProvider(org.stjs.generator.name.DefaultJavaScriptNameProvider) DependencyType(org.stjs.generator.name.DependencyType) JavaFileObject(javax.tools.JavaFileObject) File(java.io.File)

Aggregations

CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)1 File (java.io.File)1 LinkedHashMap (java.util.LinkedHashMap)1 TypeElement (javax.lang.model.element.TypeElement)1 JavaFileObject (javax.tools.JavaFileObject)1 DefaultJavaScriptNameProvider (org.stjs.generator.name.DefaultJavaScriptNameProvider)1 DependencyType (org.stjs.generator.name.DependencyType)1 JavaScriptNameProvider (org.stjs.generator.name.JavaScriptNameProvider)1