Пример #1
0
  public ReplInterpreter(
      @NotNull Disposable disposable, @NotNull CompilerConfiguration configuration) {
    jetCoreEnvironment = JetCoreEnvironment.createForProduction(disposable, configuration);
    Project project = jetCoreEnvironment.getProject();
    trace = new BindingTraceContext();
    module = AnalyzerFacadeForJVM.createJavaModule("<repl>");
    TopDownAnalysisParameters topDownAnalysisParameters =
        TopDownAnalysisParameters.createForLocalDeclarations(
            new LockBasedStorageManager(),
            new ExceptionTracker(), // dummy
            Predicates.<PsiFile>alwaysTrue());
    injector =
        new InjectorForTopDownAnalyzerForJvm(
            project, topDownAnalysisParameters, trace, module, MemberFilter.ALWAYS_TRUE);
    topDownAnalysisContext = new TopDownAnalysisContext(topDownAnalysisParameters);
    module.addFragmentProvider(SOURCES, injector.getTopDownAnalyzer().getPackageFragmentProvider());
    module.addFragmentProvider(
        BUILT_INS, KotlinBuiltIns.getInstance().getBuiltInsModule().getPackageFragmentProvider());
    module.addFragmentProvider(
        BINARIES, injector.getJavaDescriptorResolver().getPackageFragmentProvider());

    List<URL> classpath = Lists.newArrayList();

    for (File file : configuration.getList(JVMConfigurationKeys.CLASSPATH_KEY)) {
      try {
        classpath.add(file.toURI().toURL());
      } catch (MalformedURLException e) {
        throw UtilsPackage.rethrow(e);
      }
    }

    classLoader = new ReplClassLoader(new URLClassLoader(classpath.toArray(new URL[0])));
  }
Пример #2
0
  public static AnalyzeExhaust analyzeFilesWithJavaIntegration(
      Project project,
      Collection<JetFile> files,
      BindingTrace trace,
      List<AnalyzerScriptParameter> scriptParameters,
      Predicate<PsiFile> filesToAnalyzeCompletely,
      boolean storeContextForBodiesResolve) {
    final ModuleDescriptor owner = new ModuleDescriptor(Name.special("<module>"));

    TopDownAnalysisParameters topDownAnalysisParameters =
        new TopDownAnalysisParameters(filesToAnalyzeCompletely, false, false, scriptParameters);

    InjectorForTopDownAnalyzerForJvm injector =
        new InjectorForTopDownAnalyzerForJvm(
            project, topDownAnalysisParameters, new ObservableBindingTrace(trace), owner);
    try {
      injector.getTopDownAnalyzer().analyzeFiles(files, scriptParameters);
      BodiesResolveContext bodiesResolveContext =
          storeContextForBodiesResolve
              ? new CachedBodiesResolveContext(injector.getTopDownAnalysisContext())
              : null;
      return AnalyzeExhaust.success(
          trace.getBindingContext(), bodiesResolveContext, injector.getModuleConfiguration());
    } finally {
      injector.destroy();
    }
  }
Пример #3
0
    public void doTest(String path) throws Exception {
      File dir = new File(path);

      CompilerConfiguration configuration =
          JetTestUtils.compilerConfigurationForTests(
              ConfigurationKind.JDK_ONLY, TestJdkKind.MOCK_JDK, new File(dir, "java"));
      configuration.put(
          CommonConfigurationKeys.SOURCE_ROOTS_KEY,
          Arrays.asList(new File(dir, "kotlin").getAbsolutePath()));
      JetCoreEnvironment environment =
          new JetCoreEnvironment(getTestRootDisposable(), configuration);

      ModuleDescriptor moduleDescriptor = new ModuleDescriptor(Name.special("<test module>"));

      // we need the same binding trace for resolve from Java and Kotlin
      BindingTrace trace =
          CliLightClassGenerationSupport.getInstanceForCli(environment.getProject()).getTrace();

      InjectorForJavaDescriptorResolver injectorForJava =
          new InjectorForJavaDescriptorResolver(environment.getProject(), trace, moduleDescriptor);

      InjectorForTopDownAnalyzerForJvm injectorForAnalyzer =
          new InjectorForTopDownAnalyzerForJvm(
              environment.getProject(),
              new TopDownAnalysisParameters(
                  Predicates.<PsiFile>alwaysFalse(),
                  false,
                  false,
                  Collections.<AnalyzerScriptParameter>emptyList()),
              trace,
              moduleDescriptor);

      injectorForAnalyzer
          .getTopDownAnalyzer()
          .analyzeFiles(
              environment.getSourceFiles(), Collections.<AnalyzerScriptParameter>emptyList());

      JavaDescriptorResolver javaDescriptorResolver = injectorForJava.getJavaDescriptorResolver();
      NamespaceDescriptor namespaceDescriptor =
          javaDescriptorResolver.resolveNamespace(
              LoadDescriptorUtil.TEST_PACKAGE_FQNAME, DescriptorSearchRule.INCLUDE_KOTLIN);
      assert namespaceDescriptor != null;

      compareNamespaceWithFile(
          namespaceDescriptor,
          NamespaceComparator.DONT_INCLUDE_METHODS_OF_OBJECT,
          new File(dir, "expected.txt"));

      ExpectedLoadErrorsUtil.checkForLoadErrors(namespaceDescriptor, trace.getBindingContext());
    }
Пример #4
0
  public static AnalyzeExhaust analyzeFilesWithJavaIntegration(
      Project project,
      Collection<JetFile> files,
      Predicate<PsiFile> filesToAnalyzeCompletely,
      JetControlFlowDataTraceFactory flowDataTraceFactory) {
    BindingTraceContext bindingTraceContext = new BindingTraceContext();

    final ModuleDescriptor owner = new ModuleDescriptor("<module>");

    TopDownAnalysisParameters topDownAnalysisParameters =
        new TopDownAnalysisParameters(filesToAnalyzeCompletely, false, false);

    InjectorForTopDownAnalyzerForJvm injector =
        new InjectorForTopDownAnalyzerForJvm(
            project,
            topDownAnalysisParameters,
            new ObservableBindingTrace(bindingTraceContext),
            owner,
            flowDataTraceFactory);

    injector.getTopDownAnalyzer().analyzeFiles(files);
    return new AnalyzeExhaust(
        bindingTraceContext.getBindingContext(), JetStandardLibrary.getInstance());
  }
Пример #5
0
  @Nullable
  private ScriptDescriptor doAnalyze(
      @NotNull JetFile psiFile, @NotNull MessageCollector messageCollector) {
    WritableScope scope =
        new WritableScopeImpl(
            JetScope.EMPTY,
            module,
            new TraceBasedRedeclarationHandler(trace),
            "Root scope in analyzePackage");

    scope.changeLockLevel(WritableScope.LockLevel.BOTH);

    // Import a scope that contains all top-level packages that come from dependencies
    // This makes the packages visible at all, does not import themselves
    scope.importScope(module.getPackage(FqName.ROOT).getMemberScope());

    if (lastLineScope != null) {
      scope.importScope(lastLineScope);
    }

    scope.changeLockLevel(WritableScope.LockLevel.READING);

    // dummy builder is used because "root" is module descriptor,
    // packages added to module explicitly in
    injector
        .getTopDownAnalyzer()
        .doProcess(
            topDownAnalysisContext,
            scope,
            new PackageLikeBuilderDummy(),
            Collections.singletonList(psiFile));

    boolean hasErrors =
        AnalyzerWithCompilerReport.reportDiagnostics(trace.getBindingContext(), messageCollector);
    if (hasErrors) {
      return null;
    }

    ScriptDescriptor scriptDescriptor =
        topDownAnalysisContext.getScripts().get(psiFile.getScript());
    lastLineScope = trace.get(BindingContext.SCRIPT_SCOPE, scriptDescriptor);
    if (lastLineScope == null) {
      throw new IllegalStateException("last line scope is not initialized");
    }

    return scriptDescriptor;
  }