/** Initialize this index with information from the user libraries. */
 private boolean indexUserLibraries() {
   boolean librariesIndexed = true;
   try {
     AnalysisServer analysisServer = PackageLibraryManagerProvider.getDefaultAnalysisServer();
     SavedContext savedContext = analysisServer.getSavedContext();
     DartModel model = DartCore.create(ResourcesPlugin.getWorkspace().getRoot());
     for (DartProject project : model.getDartProjects()) {
       for (DartLibrary library : project.getDartLibraries()) {
         CompilationUnit compilationUnit = library.getDefiningCompilationUnit();
         if (compilationUnit == null) {
           continue;
         }
         IResource libraryResource = compilationUnit.getResource();
         if (libraryResource == null) {
           continue;
         }
         IPath libraryLocation = libraryResource.getLocation();
         if (libraryLocation == null) {
           continue;
         }
         File libraryFile = libraryLocation.toFile();
         savedContext.resolve(libraryFile, null);
       }
     }
   } catch (Exception exception) {
     librariesIndexed = false;
     DartCore.logError("Could not index user libraries", exception);
   }
   return librariesIndexed;
 }
 /**
  * Initialize this index with information from the bundled libraries.
  *
  * @return <code>true</code> if the bundled libraries were successfully indexed
  */
 private boolean indexBundledLibraries() {
   boolean librariesIndexed = true;
   long startTime = System.currentTimeMillis();
   PackageLibraryManager libraryManager = PackageLibraryManagerProvider.getPackageLibraryManager();
   ArrayList<String> librarySpecs = new ArrayList<String>(libraryManager.getAllLibrarySpecs());
   if (librarySpecs.remove("dart:html")) {
     librarySpecs.add("dart:html");
   }
   AnalysisServer analysisServer = PackageLibraryManagerProvider.getDefaultAnalysisServer();
   analysisServer.reanalyze();
   SavedContext savedContext = analysisServer.getSavedContext();
   for (String urlSpec : librarySpecs) {
     try {
       URI libraryUri = new URI(urlSpec);
       File libraryFile = new File(libraryManager.resolveDartUri(libraryUri));
       savedContext.resolve(libraryFile, null);
     } catch (URISyntaxException exception) {
       librariesIndexed = false;
       DartCore.logError(
           "Invalid URI returned from the system library manager: \"" + urlSpec + "\"", exception);
     } catch (Exception exception) {
       librariesIndexed = false;
       DartCore.logError("Could not index bundled libraries", exception);
     }
   }
   if (DartCoreDebug.PERF_INDEX) {
     long endTime = System.currentTimeMillis();
     DartCore.logInformation(
         "Initializing the index with information from bundled libraries took "
             + (endTime - startTime)
             + " ms ("
             + initIndexingTime
             + " ms indexing)");
   }
   return librariesIndexed;
 }
Example #3
0
  /** A synchronized call to {@link DartCompiler#analyzeLibrary} */
  public static LibraryUnit secureAnalyzeLibrary(
      LibrarySource librarySource,
      final Map<URI, DartUnit> parsedUnits,
      final CompilerConfiguration config,
      DartArtifactProvider provider,
      DartCompilerListener listener)
      throws IOException {

    long start = System.currentTimeMillis();

    if (!DartSdkManager.getManager().hasSdk()) {
      return null;
    }

    final PackageLibraryManager manager = PackageLibraryManagerProvider.getPackageLibraryManager();
    AnalysisServer server = PackageLibraryManagerProvider.getDefaultAnalysisServer();

    URI librarySourceUri = librarySource.getUri();

    if (parsedUnits == null && !(librarySource instanceof LibraryWithSuppliedSources)) {

      // Resolve dart:<libname> to file URI before calling AnalysisServer
      URI libraryFileUri = manager.resolveDartUri(librarySourceUri);
      File libraryFile = new File(libraryFileUri.getPath());

      LibraryUnit ret = server.getSavedContext().resolve(libraryFile, 30000);

      long elapsed = System.currentTimeMillis() - start;
      Instrumentation.metric("DartCompilerUtils-secureAnalyzeLibrary", elapsed).log();
      Instrumentation.operation("DartCompilerUtils-secureAnalyzeLibrary", elapsed)
          .with("librarySource.Name", librarySource.getName())
          .with("librarySource.LastModified", librarySource.getLastModified())
          .log();

      return ret;
    }

    // Resolve the specified library against all currently cached libraries
    final Map<URI, LibraryUnit> resolvedLibs = server.getSavedContext().getResolvedLibraries(50);
    resolvedLibs.remove(librarySourceUri);

    // Construct the selective cache
    SelectiveCache selectiveCache =
        new DartCompiler.SelectiveCache() {
          @Override
          public Map<URI, LibraryUnit> getResolvedLibraries() {
            return resolvedLibs;
          }

          @Override
          public DartUnit getUnresolvedDartUnit(DartSource dartSrc) {
            if (parsedUnits == null) {
              return null;
            }
            URI srcUri = dartSrc.getUri();
            DartUnit parsedUnit = parsedUnits.remove(srcUri);
            if (parsedUnit != null) {
              return parsedUnit;
            }
            URI fileUri = manager.resolveDartUri(srcUri);
            return parsedUnits.remove(fileUri);
          }
        };

    Map<URI, LibraryUnit> libMap;
    // All calls to DartC must be synchronized
    synchronized (compilerLock) {
      libMap =
          DartCompiler.analyzeLibraries(
              librarySource, selectiveCache, config, provider, listener, false);
    }

    long elapsed = System.currentTimeMillis() - start;
    Instrumentation.metric("DartCompilerUtils-secureAnalyzeLibrary", elapsed)
        .with("libMapHasResult", Boolean.toString(libMap != null))
        .log();

    Instrumentation.operation("DartCompilerUtils-secureAnalyzeLibrary", elapsed)
        .with("librarySource.Name", librarySource.getName())
        .with("libMapHasResult", Boolean.toString(libMap != null))
        .with("librarySource.LastModified", librarySource.getLastModified())
        .log();

    return libMap != null ? libMap.get(librarySourceUri) : null;
  }