Search in sources :

Example 1 with RhinoExecutor

use of org.stjs.generator.executor.RhinoExecutor in project st-js by st-js.

the class AbstractStjsTest method executeOrGenerate.

/**
 * @return the javascript code generator from the given class
 */
private Object executeOrGenerate(Class<?> clazz, boolean execute, boolean withSourceMap, GeneratorConfiguration extraConfig) {
    File generationPath = new File("target", TEMP_GENERATION_PATH);
    // which fucks up URI.resolve
    if (!generationPath.exists() && !generationPath.mkdirs()) {
        throw new STJSRuntimeException("Unable to create generation directory");
    }
    GenerationDirectory generationFolder = new GenerationDirectory(generationPath, new File(TEMP_GENERATION_PATH), generationPath.toURI());
    final File sourcePath = new File("src/test/java");
    File resourcePath = new File("src/test/resources");
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    GeneratorConfiguration config = // 
    new GeneratorConfigurationBuilder(extraConfig).allowedPackage(// 
    "org.stjs.javascript").allowedPackage(// 
    "org.stjs.generator").allowedPackage(// 
    clazz.getPackage().getName()).sourceEncoding("UTF-8").generateSourceMap(// 
    withSourceMap).stjsClassLoader(// 
    classLoader).generationFolder(generationFolder).targetFolder(new File("target", "test-classes")).classResolver(new LazyGenerationClassResolver(classLoader, new LazyGenerator() {

        @Override
        public ClassWithJavascript generateJavaScript(String className) {
            return generator.generateJavascript(className, sourcePath);
        }
    })).build();
    // 
    this.generator = new Generator(config);
    ClassWithJavascript stjsClass = this.generator.generateJavascript(clazz.getName(), sourcePath);
    Timers.start("js-exec");
    List<File> javascriptFiles = new ArrayList<File>();
    try {
        File jsFile = new File(stjsClass.getJavascriptFiles().get(0).getPath());
        String content = Files.toString(jsFile, Charset.defaultCharset());
        List<ClassWithJavascript> allDeps = new DependencyCollector().orderAllDependencies(stjsClass);
        for (ClassWithJavascript dep : allDeps) {
            for (URI js : dep.getJavascriptFiles()) {
                if (dep instanceof BridgeClass) {
                    javascriptFiles.add(new File(resourcePath, js.getPath()));
                } else {
                    javascriptFiles.add(new File(js.getPath()));
                }
            }
        }
        ExecutionResult execResult = new RhinoExecutor().run(javascriptFiles, !execute);
        if (execute) {
            return execResult.getResult();
        }
        return content;
    } catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    } catch (ScriptException ex) {
        // display the generated code in case of exception
        for (File file : javascriptFiles) {
            displayWithLines(file);
        }
        throw new STJSRuntimeException(ex);
    } finally {
        Timers.end("js-exec");
    }
}
Also used : BridgeClass(org.stjs.generator.BridgeClass) STJSRuntimeException(org.stjs.generator.STJSRuntimeException) ArrayList(java.util.ArrayList) LazyGenerator(org.stjs.generator.utils.LazyGenerationClassResolver.LazyGenerator) ExecutionResult(org.stjs.generator.executor.ExecutionResult) GenerationDirectory(org.stjs.generator.GenerationDirectory) IOException(java.io.IOException) RhinoExecutor(org.stjs.generator.executor.RhinoExecutor) URI(java.net.URI) ScriptException(javax.script.ScriptException) STJSRuntimeException(org.stjs.generator.STJSRuntimeException) URLClassLoader(java.net.URLClassLoader) ClassWithJavascript(org.stjs.generator.ClassWithJavascript) GeneratorConfiguration(org.stjs.generator.GeneratorConfiguration) GeneratorConfigurationBuilder(org.stjs.generator.GeneratorConfigurationBuilder) File(java.io.File) DependencyCollector(org.stjs.generator.DependencyCollector) Generator(org.stjs.generator.Generator) LazyGenerator(org.stjs.generator.utils.LazyGenerationClassResolver.LazyGenerator)

Example 2 with RhinoExecutor

use of org.stjs.generator.executor.RhinoExecutor in project st-js by st-js.

the class AbstractStjsTest method execute.

/**
 * <p>execute.</p>
 *
 * @param preGeneratedJs a {@link java.lang.String} object.
 * @return a {@link java.lang.Object} object.
 */
public Object execute(String preGeneratedJs) {
    try {
        File jsfile = new File(preGeneratedJs);
        ExecutionResult execResult = new RhinoExecutor().run(Collections.singletonList(jsfile), false);
        return convert(execResult.getResult());
    } catch (ScriptException se) {
        throw new RuntimeException(se);
    }
}
Also used : ScriptException(javax.script.ScriptException) STJSRuntimeException(org.stjs.generator.STJSRuntimeException) ExecutionResult(org.stjs.generator.executor.ExecutionResult) RhinoExecutor(org.stjs.generator.executor.RhinoExecutor) File(java.io.File)

Aggregations

File (java.io.File)2 ScriptException (javax.script.ScriptException)2 STJSRuntimeException (org.stjs.generator.STJSRuntimeException)2 ExecutionResult (org.stjs.generator.executor.ExecutionResult)2 RhinoExecutor (org.stjs.generator.executor.RhinoExecutor)2 IOException (java.io.IOException)1 URI (java.net.URI)1 URLClassLoader (java.net.URLClassLoader)1 ArrayList (java.util.ArrayList)1 BridgeClass (org.stjs.generator.BridgeClass)1 ClassWithJavascript (org.stjs.generator.ClassWithJavascript)1 DependencyCollector (org.stjs.generator.DependencyCollector)1 GenerationDirectory (org.stjs.generator.GenerationDirectory)1 Generator (org.stjs.generator.Generator)1 GeneratorConfiguration (org.stjs.generator.GeneratorConfiguration)1 GeneratorConfigurationBuilder (org.stjs.generator.GeneratorConfigurationBuilder)1 LazyGenerator (org.stjs.generator.utils.LazyGenerationClassResolver.LazyGenerator)1