/** * Constructor. * * @throws LanguageAdapterException * In case of an initialization error */ public RhinoAdapter() throws LanguageAdapterException { super( "Rhino", getImplementationVersion(), "JavaScript", getLanguageVersion(), Arrays.asList( "js", "javascript", "rhino" ), "js", Arrays.asList( "javascript", "js", "rhino" ), "rhino" ); CompilerEnvirons compilerEnvirons = new CompilerEnvirons(); compilerEnvirons.setOptimizationLevel( getOptimizationLevel() ); classCompiler = new ClassCompiler( compilerEnvirons ); Context context = enterContext(); try { generatedClassLoader = context.createClassLoader( RhinoAdapter.class.getClassLoader() ); } finally { Context.exit(); } }
public Main() { reporter = new ToolErrorReporter(true); compilerEnv = new CompilerEnvirons(); compilerEnv.setErrorReporter(reporter); compiler = new ClassCompiler(compilerEnv); }
protected void compileScripts(JRCompilationUnit unit, CompilerEnvirons compilerEnv, CompileSources compileSources, JavaScriptCompiledData compiledData) { List<String> scripts = compileSources.getScripts(); int scriptIndex = 0; for (String scriptSource : scripts) { String scriptClassName = unit.getName() + "_" + scriptIndex; if (log.isTraceEnabled()) { log.trace("compiling script with name " + scriptClassName + "\n" + scriptSource); } ClassCompiler compiler = new ClassCompiler(compilerEnv); // this should not fail since we've already separately compiled the default expression Object[] compilationResult = compiler.compileToClassFiles(scriptSource, unit.getName(), 0, scriptClassName); if (compilationResult.length != 2) { throw new JRRuntimeException( EXCEPTION_MESSAGE_KEY_UNEXPECTED_CLASSES_LENGTH, new Object[]{compilationResult.length}); } if (!scriptClassName.equals(compilationResult[0])) { throw new JRRuntimeException( EXCEPTION_MESSAGE_KEY_UNEXPECTED_CLASS_NAME, new Object[]{compilationResult[0], scriptClassName}); } byte[] compiledClass = (byte[]) compilationResult[1]; compiledData.addClass(scriptClassName, compiledClass); ++scriptIndex; } }