@Override public boolean isModule() { checkErrorManager(); try { regenerateDependencyInfoIfNecessary(); return isModuleFile; } catch (IOException e) { compiler .getErrorManager() .report(CheckLevel.ERROR, JSError.make(AbstractCompiler.READ_ERROR, getName())); return false; } }
/** Gets a list of types provided by this input. */ @Override public Collection<String> getProvides() { checkErrorManager(); try { regenerateDependencyInfoIfNecessary(); return Collections.unmodifiableSet(provides); } catch (IOException e) { compiler .getErrorManager() .report(CheckLevel.ERROR, JSError.make(AbstractCompiler.READ_ERROR, getName())); return ImmutableList.of(); } }
/** Regenerates the provides/requires if we need to do so. */ private void regenerateDependencyInfoIfNecessary() throws IOException { // If the code is NOT a JsAst, then it was not originally JS code. // Look at the Ast for dependency info. if (!(ast instanceof JsAst)) { Preconditions.checkNotNull(compiler, "Expected setCompiler to be called first"); DepsFinder finder = new DepsFinder(); Node root = getAstRoot(compiler); if (root == null) { return; } finder.visitTree(getAstRoot(compiler)); // TODO(nicksantos|user): This caching behavior is a bit // odd, and only works if you assume the exact call flow that // clients are currently using. In that flow, they call // getProvides(), then remove the goog.provide calls from the // AST, and then call getProvides() again. // // This won't work for any other call flow, or any sort of incremental // compilation scheme. The API needs to be fixed so callers aren't // doing weird things like this, and then we should get rid of the // multiple-scan strategy. isModuleFile = finder.isModuleFile; provides.addAll(finder.provides); requires.addAll(finder.requires); } else { // Otherwise, look at the source code. if (!generatedDependencyInfoFromSource) { // Note: it's OK to use getName() instead of // getPathRelativeToClosureBase() here because we're not using // this to generate deps files. (We're only using it for // symbol dependencies.) DependencyInfo info = (new JsFileParser(compiler.getErrorManager())) .setIncludeGoogBase(true) .parseFile(getName(), getName(), getCode()); isModuleFile = info.isModule(); provides.addAll(info.getProvides()); requires.addAll(info.getRequires()); generatedDependencyInfoFromSource = true; } } }
private void checkErrorManager() { Preconditions.checkNotNull(compiler, "Expected setCompiler to be called first: " + this); Preconditions.checkNotNull( compiler.getErrorManager(), "Expected compiler to call an error manager: " + this); }