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);
}
}
}
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);
}
}
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);
}
}
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");
}
}
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;
}
Aggregations