Example #1
0
  public ReplInterpreter(
      @NotNull Disposable disposable, @NotNull CompilerConfiguration configuration) {
    KotlinCoreEnvironment environment =
        KotlinCoreEnvironment.createForProduction(
            disposable, configuration, EnvironmentConfigFiles.JVM_CONFIG_FILES);
    Project project = environment.getProject();
    this.psiFileFactory = (PsiFileFactoryImpl) PsiFileFactory.getInstance(project);
    this.trace = new CliLightClassGenerationSupport.NoScopeRecordCliBindingTrace();
    MutableModuleContext moduleContext =
        TopDownAnalyzerFacadeForJVM.createContextWithSealedModule(project);
    this.module = moduleContext.getModule();

    scriptDeclarationFactory = new ScriptMutableDeclarationProviderFactory();

    FileScopeProvider.AdditionalScopes scopeProvider =
        new FileScopeProvider.AdditionalScopes() {
          @NotNull
          @Override
          public List<JetScope> scopes(@NotNull JetFile file) {
            return lastLineScope != null
                ? new SmartList<JetScope>(lastLineScope)
                : Collections.<JetScope>emptyList();
          }
        };

    ContainerForReplWithJava container =
        DiPackage.createContainerForReplWithJava(
            moduleContext,
            trace,
            scriptDeclarationFactory,
            ProjectScope.getAllScope(project),
            scopeProvider);

    this.topDownAnalysisContext =
        new TopDownAnalysisContext(
            TopDownAnalysisMode.LocalDeclarations,
            DataFlowInfo.EMPTY,
            container.getResolveSession().getDeclarationScopeProvider());
    this.topDownAnalyzer = container.getLazyTopDownAnalyzerForTopLevel();
    this.resolveSession = container.getResolveSession();

    moduleContext.initializeModuleContents(
        new CompositePackageFragmentProvider(
            Arrays.asList(
                container.getResolveSession().getPackageFragmentProvider(),
                container.getJavaDescriptorResolver().getPackageFragmentProvider())));

    List<URL> classpath = Lists.newArrayList();
    for (File file : getJvmClasspathRoots(configuration)) {
      try {
        classpath.add(file.toURI().toURL());
      } catch (MalformedURLException e) {
        throw UtilsPackage.rethrow(e);
      }
    }

    this.classLoader =
        new ReplClassLoader(new URLClassLoader(classpath.toArray(new URL[classpath.size()]), null));
  }
Example #2
0
 @NotNull
 public static List<KtFile> loadToJetFiles(
     @NotNull KotlinCoreEnvironment environment, @NotNull List<File> files) throws IOException {
   List<KtFile> jetFiles = Lists.newArrayList();
   for (File file : files) {
     jetFiles.add(loadJetFile(environment.getProject(), file));
   }
   return jetFiles;
 }
Example #3
0
 @NotNull
 public static KotlinCoreEnvironment createEnvironmentWithJdkAndNullabilityAnnotationsFromIdea(
     @NotNull Disposable disposable,
     @NotNull ConfigurationKind configurationKind,
     @NotNull TestJdkKind jdkKind) {
   return KotlinCoreEnvironment.createForTests(
       disposable,
       compilerConfigurationForTests(configurationKind, jdkKind, getAnnotationsJar()),
       EnvironmentConfigFiles.JVM_CONFIG_FILES);
 }
  protected void doTest(String path) throws Exception {
    File ktFile = new File(path);
    assertTrue("Cannot find a file " + ktFile.getAbsolutePath(), ktFile.exists());

    String fileText = FileUtil.loadFile(ktFile, true);

    KtFile psiFile =
        KotlinTestUtils.createFile(ktFile.getName(), fileText, jetCoreEnvironment.getProject());

    OutputFileCollection outputFiles =
        GenerationUtils.compileFileGetClassFileFactoryForTest(psiFile, jetCoreEnvironment);

    List<TestedObject> testedObjects = parseExpectedTestedObject(fileText);
    for (TestedObject testedObject : testedObjects) {
      String className = null;
      for (OutputFile outputFile : outputFiles.asList()) {
        String filePath = outputFile.getRelativePath();
        if (testedObject.isFullContainingClassName
            && filePath.equals(testedObject.containingClass + ".class")) {
          className = filePath;
        } else if (!testedObject.isFullContainingClassName
            && filePath.startsWith(testedObject.containingClass)) {
          className = filePath;
        }
      }

      assertNotNull(
          "Couldn't find a class file with name " + testedObject.containingClass, className);

      OutputFile outputFile = outputFiles.get(className);
      assertNotNull(outputFile);

      ClassReader cr = new ClassReader(outputFile.asByteArray());
      TestClassVisitor classVisitor = getClassVisitor(testedObject.kind, testedObject.name, false);
      cr.accept(classVisitor, ClassReader.SKIP_CODE);

      if (!classVisitor.isExists()) {
        classVisitor = getClassVisitor(testedObject.kind, testedObject.name, true);
        cr.accept(classVisitor, ClassReader.SKIP_CODE);
      }

      boolean isObjectExists =
          !Boolean.valueOf(findStringWithPrefixes(testedObject.textData, "// ABSENT: "));
      assertEquals(
          "Wrong object existence state: " + testedObject, isObjectExists, classVisitor.isExists());

      if (isObjectExists) {
        assertEquals(
            "Wrong access flag for " + testedObject + " \n" + outputFile.asText(),
            getExpectedFlags(testedObject.textData),
            classVisitor.getAccess());
      }
    }
  }
Example #5
0
 @NotNull
 protected String loadFileByFullPath(@NotNull String fullPath) {
   try {
     File file = new File(fullPath);
     String content = FileUtil.loadFile(file, Charsets.UTF_8.name(), true);
     myFiles = CodegenTestFiles.create(file.getName(), content, myEnvironment.getProject());
     return content;
   } catch (IOException e) {
     throw new RuntimeException(e);
   }
 }
 @Override
 protected KotlinCoreEnvironment createEnvironment() {
   File javaFilesDir = createJavaFilesDir();
   return KotlinCoreEnvironment.createForTests(
       getTestRootDisposable(),
       KotlinTestUtils.compilerConfigurationForTests(
           ConfigurationKind.ALL,
           TestJdkKind.FULL_JDK,
           Arrays.asList(KotlinTestUtils.getAnnotationsJar()),
           Arrays.asList(javaFilesDir)),
       EnvironmentConfigFiles.JVM_CONFIG_FILES);
 }
  @NotNull
  private KotlinCoreEnvironment createEnvironment(@NotNull List<File> extraClassPath) {
    List<File> extras = new ArrayList<File>();
    extras.addAll(extraClassPath);
    extras.add(KotlinTestUtils.getAnnotationsJar());

    CompilerConfiguration configuration =
        KotlinTestUtils.compilerConfigurationForTests(
            ConfigurationKind.ALL, TestJdkKind.MOCK_JDK, extras.toArray(new File[extras.size()]));
    return KotlinCoreEnvironment.createForTests(
        getTestRootDisposable(), configuration, EnvironmentConfigFiles.JVM_CONFIG_FILES);
  }
Example #8
0
  @NotNull
  private URL[] getClassPathURLs() {
    List<URL> urls = Lists.newArrayList();
    for (File file : JvmContentRootsKt.getJvmClasspathRoots(myEnvironment.getConfiguration())) {
      try {
        urls.add(file.toURI().toURL());
      } catch (MalformedURLException e) {
        throw new RuntimeException(e);
      }
    }

    return urls.toArray(new URL[urls.size()]);
  }
Example #9
0
  public static void resolveAllKotlinFiles(KotlinCoreEnvironment environment) throws IOException {
    List<ContentRoot> paths =
        environment.getConfiguration().get(CommonConfigurationKeys.CONTENT_ROOTS);
    if (paths == null) return;
    List<KtFile> jetFiles = Lists.newArrayList();
    for (ContentRoot root : paths) {
      if (!(root instanceof KotlinSourceRoot)) continue;

      String path = ((KotlinSourceRoot) root).getPath();
      File file = new File(path);
      if (file.isFile()) {
        jetFiles.add(loadJetFile(environment.getProject(), file));
      } else {
        //noinspection ConstantConditions
        for (File childFile : file.listFiles()) {
          if (childFile.getName().endsWith(".kt")) {
            jetFiles.add(loadJetFile(environment.getProject(), childFile));
          }
        }
      }
    }
    LazyResolveTestUtil.resolve(environment.getProject(), jetFiles, environment);
  }
Example #10
0
  protected final void createEnvironmentWithMockJdkAndIdeaAnnotations(
      @NotNull ConfigurationKind configurationKind, File... javaSourceRoot) {
    if (myEnvironment != null) {
      throw new IllegalStateException("must not set up myEnvironment twice");
    }

    CompilerConfiguration configuration =
        compilerConfigurationForTests(
            configurationKind,
            TestJdkKind.MOCK_JDK,
            Collections.singletonList(getAnnotationsJar()),
            new SmartList<File>(javaSourceRoot));

    myEnvironment =
        KotlinCoreEnvironment.createForTests(
            getTestRootDisposable(), configuration, EnvironmentConfigFiles.JVM_CONFIG_FILES);
  }
Example #11
0
 protected void loadFiles(@NotNull String... names) {
   myFiles = CodegenTestFiles.create(myEnvironment.getProject(), names);
 }
Example #12
0
 protected void loadText(@NotNull String text) {
   myFiles =
       CodegenTestFiles.create(DEFAULT_TEST_FILE_NAME + ".kt", text, myEnvironment.getProject());
 }