Exemplo n.º 1
0
  /**
   * Process the given resource within the context of the given working set in order to record the
   * data and relationships found within the resource.
   */
  public void indexResource(File libraryFile, File sourceFile, DartUnit dartUnit)
      throws DartModelException {

    // Get the LibrarySource

    LibrarySource librarySource = dartUnit.getLibrary().getSource();

    // Get the DartLibrary

    DartLibraryImpl library;
    IResource resource = ResourceUtil.getResource(libraryFile);
    if (resource == null) {
      library = new DartLibraryImpl(librarySource);
    } else {
      DartElement element = DartCore.create(resource);
      if (element instanceof CompilationUnitImpl) {
        element = ((CompilationUnitImpl) element).getLibrary();
      }
      if (!(element instanceof DartLibrary)) {
        DartCore.logError("Expected library to be associated with " + libraryFile);
        return;
      }
      library = (DartLibraryImpl) element;
    }

    // Get the CompilationUnit

    DartSource unitSource = (DartSource) dartUnit.getSourceInfo().getSource();
    CompilationUnit compilationUnit;
    IResource res = ResourceUtil.getResource(sourceFile);
    if (res != null) {
      DefaultWorkingCopyOwner workingCopy = DefaultWorkingCopyOwner.getInstance();
      compilationUnit = new CompilationUnitImpl(library, (IFile) res, workingCopy);
    } else {
      String relPath = unitSource.getRelativePath();
      compilationUnit = new ExternalCompilationUnitImpl(library, relPath, unitSource);
    }

    URI unitUri = unitSource.getUri();
    Resource indexerResource;
    if (PackageLibraryManager.isDartUri(unitUri)) {
      indexerResource =
          new Resource(
              ResourceFactory.composeResourceId(
                  librarySource.getUri().toString(), unitUri.toString()));
    } else if (PackageLibraryManager.isPackageUri(unitUri)) {
      indexerResource =
          new Resource(
              ResourceFactory.composeResourceId(
                  libraryFile.toURI().toString(), sourceFile.toURI().toString()));
    } else {
      indexerResource = ResourceFactory.getResource(compilationUnit);
    }

    // Queue the resource to be indexed

    indexResource(indexerResource, libraryFile, compilationUnit, dartUnit);
  }
Exemplo n.º 2
0
 @Override
 public DartSource getSourceFor(String relPath) {
   URI uri;
   try {
     uri = wrappedSource.getUri().resolve(new URI(null, null, relPath, null));
   } catch (URISyntaxException e) {
     uri = null;
   }
   if (uri != null) {
     for (URI key : suppliedSources.keySet()) {
       if (equalUris(libraryManager, key, uri)) {
         String source = suppliedSources.get(key);
         return new DartURIStringSource(this, uri, relPath, source);
       }
     }
   }
   return wrappedSource.getSourceFor(relPath);
 }
Exemplo n.º 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;
  }
Exemplo n.º 4
0
 @Override
 public URI getUri() {
   return wrappedSource.getUri();
 }