@Override public void setUp() throws Exception { super.setUp(); CompilationState state = CompilationStateBuilder.buildFrom(createCompileLogger(), UiJavaResources.getUiResources()); types = state.getTypeOracle(); }
/** * Given the source code to a Java class named <code>test.EntryPoint</code>, compiles it with * emulated stack traces turned on and returns the JavaScript. */ private JsProgram compileClass(String... lines) throws UnableToCompleteException { // Gather the Java source code to compile. final String code = Joiner.on("\n").join(lines); MockResourceOracle sourceOracle = new MockResourceOracle(); sourceOracle.addOrReplace( new MockJavaResource("test.EntryPoint") { @Override public CharSequence getContent() { return code; } }); sourceOracle.add(JavaAstConstructor.getCompilerTypes()); PrecompileTaskOptions options = new PrecompileTaskOptionsImpl(); options.setOutput(JsOutputOption.PRETTY); options.setRunAsyncEnabled(false); CompilerContext context = new CompilerContext.Builder() .options(options) .minimalRebuildCache(new MinimalRebuildCache()) .build(); ConfigurationProperties config = new ConfigurationProperties(Arrays.asList(recordFileNamesProp, recordLineNumbersProp)); CompilationState state = CompilationStateBuilder.buildFrom(logger, context, sourceOracle.getResources(), null); JProgram jProgram = AstConstructor.construct(logger, state, options, config); jProgram.addEntryMethod(findMethod(jProgram, "onModuleLoad")); if (inline) { MethodInliner.exec(jProgram); } CatchBlockNormalizer.exec(jProgram); // Construct the JavaScript AST. // These passes are needed by GenerateJavaScriptAST. ComputeCastabilityInformation.exec(jProgram, false); ImplementCastsAndTypeChecks.exec(jProgram, false); ArrayNormalizer.exec(jProgram); StringTypeMapper typeMapper = new StringTypeMapper(jProgram); ResolveRuntimeTypeReferences.exec(jProgram, typeMapper, TypeOrder.FREQUENCY); Map<StandardSymbolData, JsName> symbolTable = new TreeMap<StandardSymbolData, JsName>(new SymbolData.ClassIdentComparator()); BindingProperty stackMode = new BindingProperty("compiler.stackMode"); stackMode.addDefinedValue(new ConditionNone(), "EMULATED"); PermutationProperties properties = new PermutationProperties( Arrays.asList( new BindingProperties( new BindingProperty[] {stackMode}, new String[] {"EMULATED"}, config))); JsProgram jsProgram = new JsProgram(); JavaToJavaScriptMap jjsmap = GenerateJavaScriptAST.exec( logger, jProgram, jsProgram, context, typeMapper, symbolTable, properties) .getLeft(); // Finally, run the pass we care about. JsStackEmulator.exec(jProgram, jsProgram, properties, jjsmap); return jsProgram; }