Search in sources :

Example 6 with STJSRuntimeException

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

the class JavascriptToJava method getClassName.

private String getClassName(String propertiesFile) {
    InputStream in = null;
    try {
        in = classLoader.getResourceAsStream(propertiesFile.substring(1));
        if (in == null) {
            return null;
        }
        Properties p = new Properties();
        p.load(in);
        return p.getProperty(STJSClass.CLASS_PROP);
    } catch (IOException e) {
        throw new STJSRuntimeException(e);
    } finally {
        if (in != null) {
            Closeables.closeQuietly(in);
        }
    }
}
Also used : STJSRuntimeException(org.stjs.generator.STJSRuntimeException) InputStream(java.io.InputStream) IOException(java.io.IOException) Properties(java.util.Properties)

Example 7 with STJSRuntimeException

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

the class JavascriptToJava method buildStacktraceElement.

/**
 * // the format is the one given by stacktrace.js: // <br>
 * at prototype.method (url) <br>
 * where url is http://localhost:xxxx/org/stjs/TestClass.js:row:col
 *
 * @param stacktraceLine
 * @return
 */
private StackTraceElement buildStacktraceElement(String stacktraceLine) {
    Matcher m = STACKTRACE_UNIVERSAL_JS_PATTERN.matcher(stacktraceLine);
    if (!m.matches()) {
        // wrong pattern !?
        throw new STJSRuntimeException("Unknown location format:" + stacktraceLine);
    }
    try {
        String methodName = m.group(STACKTRACE_GROUP_METHOD);
        URL url = new URL(m.group(STACKTRACE_GROUP_LOCATION));
        String file = url.getFile();
        String[] fileParts = file.split(":");
        // source file
        String jsSourceFile = fileParts[0];
        String sourceFile = jsSourceFile.replaceAll("\\.js$", ".java");
        // java line
        String cleanJsPath = url.getPath().split(":")[0];
        int jsLineNumber = parseInt(fileParts[1]);
        int line = getJavaLine(cleanJsPath, jsLineNumber);
        String stjsPropertyFile = cleanJsPath.replaceAll("\\.js$", ".stjs");
        // class name
        String className = getClassName(stjsPropertyFile);
        if (className == null) {
            className = "<Unknown class>";
            sourceFile = jsSourceFile;
        }
        return new StackTraceElement(className, methodName, sourceFile, line);
    } catch (MalformedURLException e) {
        throw new STJSRuntimeException(e);
    }
}
Also used : STJSRuntimeException(org.stjs.generator.STJSRuntimeException) MalformedURLException(java.net.MalformedURLException) Matcher(java.util.regex.Matcher) URL(java.net.URL)

Example 8 with STJSRuntimeException

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

the class JavascriptToJava method getJavaLine.

/**
 * <p>getJavaLine.</p>
 *
 * @param path a {@link java.lang.String} object.
 * @param lineNumber a int.
 * @return a int.
 */
public int getJavaLine(String path, int lineNumber) {
    String sourceMapFile = path.replaceAll("\\.js$", ".map");
    URL url = classLoader.getResource(sourceMapFile.substring(1));
    if (url == null) {
        return lineNumber;
    }
    String contents;
    try {
        contents = Resources.toString(url, Charsets.UTF_8);
        SourceMapping mapping = SourceMapConsumerFactory.parse(contents);
        return mapping.getMappingForLine(lineNumber, 1).getLineNumber();
    } catch (IOException e) {
        throw new STJSRuntimeException(e);
    } catch (SourceMapParseException e) {
        throw new STJSRuntimeException(e);
    }
}
Also used : STJSRuntimeException(org.stjs.generator.STJSRuntimeException) SourceMapParseException(com.google.debugging.sourcemap.SourceMapParseException) IOException(java.io.IOException) SourceMapping(com.google.debugging.sourcemap.SourceMapping) URL(java.net.URL)

Example 9 with STJSRuntimeException

use of org.stjs.generator.STJSRuntimeException 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 10 with STJSRuntimeException

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

the class RhinoJavaScriptWriter method println.

/**
 * <p>println.</p>
 *
 * @return a {@link org.stjs.generator.javascript.rhino.RhinoJavaScriptWriter} object.
 */
public RhinoJavaScriptWriter println() {
    try {
        writer.append('\n');
    } catch (IOException e) {
        throw new STJSRuntimeException("Writing problem:" + e, e);
    }
    indented = false;
    currentLine++;
    currentColumn = 0;
    addMapping();
    return this;
}
Also used : STJSRuntimeException(org.stjs.generator.STJSRuntimeException) IOException(java.io.IOException)

Aggregations

STJSRuntimeException (org.stjs.generator.STJSRuntimeException)12 IOException (java.io.IOException)9 InputStream (java.io.InputStream)2 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2 Properties (java.util.Properties)2 SourceMapParseException (com.google.debugging.sourcemap.SourceMapParseException)1 SourceMapping (com.google.debugging.sourcemap.SourceMapping)1 SuppressWarnings (edu.umd.cs.findbugs.annotations.SuppressWarnings)1 File (java.io.File)1 JarURLConnection (java.net.JarURLConnection)1 MalformedURLException (java.net.MalformedURLException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 URLClassLoader (java.net.URLClassLoader)1 URLConnection (java.net.URLConnection)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 JarEntry (java.util.jar.JarEntry)1 Matcher (java.util.regex.Matcher)1