Exemplo n.º 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));
  }
Exemplo n.º 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;
 }
Exemplo n.º 3
0
  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());
      }
    }
  }
Exemplo n.º 4
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);
   }
 }
Exemplo n.º 5
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);
  }
Exemplo n.º 6
0
 protected void loadFiles(@NotNull String... names) {
   myFiles = CodegenTestFiles.create(myEnvironment.getProject(), names);
 }
Exemplo n.º 7
0
 protected void loadText(@NotNull String text) {
   myFiles =
       CodegenTestFiles.create(DEFAULT_TEST_FILE_NAME + ".kt", text, myEnvironment.getProject());
 }