Exemplo n.º 1
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());
    }
Exemplo n.º 2
0
 @SuppressWarnings("ConstantConditions")
 private WritableScopeImpl addImports(JetScope scope) {
   WritableScopeImpl writableScope =
       new WritableScopeImpl(
           scope,
           scope.getContainingDeclaration(),
           RedeclarationHandler.DO_NOTHING,
           "JetTypeCheckerTest.addImports");
   InjectorForJavaDescriptorResolver injector =
       new InjectorForJavaDescriptorResolver(getProject(), new BindingTraceContext());
   JavaDescriptorResolver javaDescriptorResolver = injector.getJavaDescriptorResolver();
   writableScope.importScope(
       javaDescriptorResolver
           .resolveNamespace(FqName.ROOT, INCLUDE_KOTLIN_SOURCES)
           .getMemberScope());
   writableScope.importScope(
       javaDescriptorResolver
           .resolveNamespace(new FqName("java.lang"), IGNORE_KOTLIN_SOURCES)
           .getMemberScope());
   writableScope.changeLockLevel(WritableScope.LockLevel.BOTH);
   writableScope.importScope(builtIns.getBuiltInsScope());
   return writableScope;
 }
Exemplo n.º 3
0
  @NotNull
  @Override
  public ResolveSession getLazyResolveSession(
      @NotNull final Project fileProject, @NotNull Collection<JetFile> files) {
    ModuleDescriptor javaModule = new ModuleDescriptor(Name.special("<java module>"));

    BindingTraceContext javaResolverTrace = new BindingTraceContext();
    InjectorForJavaDescriptorResolver injector =
        new InjectorForJavaDescriptorResolver(fileProject, javaResolverTrace, javaModule);

    final PsiClassFinder psiClassFinder = injector.getPsiClassFinder();

    // TODO: Replace with stub declaration provider
    LockBasedStorageManager storageManager = new LockBasedStorageManager();
    final FileBasedDeclarationProviderFactory declarationProviderFactory =
        new FileBasedDeclarationProviderFactory(
            storageManager,
            files,
            new Predicate<FqName>() {
              @Override
              public boolean apply(FqName fqName) {
                return psiClassFinder.findPsiPackage(fqName) != null
                    || new FqName("jet").equals(fqName);
              }
            });

    final JavaDescriptorResolver javaDescriptorResolver = injector.getJavaDescriptorResolver();

    ModuleConfiguration moduleConfiguration =
        new ModuleConfiguration() {
          @Override
          public List<ImportPath> getDefaultImports() {
            LinkedHashSet<ImportPath> imports =
                Sets.newLinkedHashSet(JavaBridgeConfiguration.DEFAULT_JAVA_IMPORTS);
            imports.addAll(DefaultModuleConfiguration.DEFAULT_JET_IMPORTS);
            return Lists.newArrayList(imports);
          }

          @Override
          public void extendNamespaceScope(
              @NotNull BindingTrace trace,
              @NotNull NamespaceDescriptor namespaceDescriptor,
              @NotNull WritableScope namespaceMemberScope) {
            FqName fqName = DescriptorUtils.getFQName(namespaceDescriptor).toSafe();
            if (new FqName("jet").equals(fqName)) {
              namespaceMemberScope.importScope(KotlinBuiltIns.getInstance().getBuiltInsScope());
            }
            if (psiClassFinder.findPsiPackage(fqName) != null) {
              JetScope javaPackageScope =
                  javaDescriptorResolver.getJavaPackageScope(namespaceDescriptor);
              assert javaPackageScope != null;
              namespaceMemberScope.importScope(javaPackageScope);
            }
          }

          @NotNull
          @Override
          public PlatformToKotlinClassMap getPlatformToKotlinClassMap() {
            return JavaToKotlinClassMap.getInstance();
          }
        };

    ModuleDescriptor lazyModule = new ModuleDescriptor(Name.special("<lazy module>"));

    return new ResolveSession(
        fileProject,
        storageManager,
        lazyModule,
        moduleConfiguration,
        declarationProviderFactory,
        javaResolverTrace);
  }