public static String getResult(String code) {
    Project project = EnvironmentManager.getEnvironment().getProject();
    JavaToKotlinConverter converter =
        new JavaToKotlinConverter(
            project,
            ConverterSettings.Companion.getDefaultSettings(),
            EmptyJavaToKotlinServices.INSTANCE);
    PsiElementFactory instance = PsiElementFactory.SERVICE.getInstance(project);

    List<PsiElement> inputElements = null;
    PsiFile javaFile =
        PsiFileFactory.getInstance(project)
            .createFileFromText("test.java", JavaLanguage.INSTANCE, code);

    // To create a module
    ResolveUtils.getBindingContext(Collections.EMPTY_LIST, project, false);

    for (PsiElement element : javaFile.getChildren()) {
      if (element instanceof PsiClass) {
        inputElements = Collections.<PsiElement>singletonList(javaFile);
      }
    }

    if (inputElements == null) {
      PsiClass psiClass = instance.createClassFromText(code, javaFile);
      boolean errorsFound = false;
      for (PsiElement element : psiClass.getChildren()) {
        if (element instanceof PsiErrorElement) {
          errorsFound = true;
        }
      }
      if (!errorsFound) {
        inputElements = Arrays.asList(psiClass.getChildren());
      }
    }

    if (inputElements == null) {
      PsiCodeBlock codeBlock = instance.createCodeBlockFromText("{" + code + "}", javaFile);
      PsiElement[] childrenWithoutBraces =
          Arrays.copyOfRange(codeBlock.getChildren(), 1, codeBlock.getChildren().length - 1);
      inputElements = Arrays.asList(childrenWithoutBraces);
    }

    List<JavaToKotlinConverter.ElementResult> resultFormConverter =
        converter.elementsToKotlin(inputElements).getResults();
    String textResult = "";
    for (JavaToKotlinConverter.ElementResult it : resultFormConverter) {
      if (it == null) continue;
      textResult = textResult + it.getText() + "\n";
    }
    textResult = JavaToKotlinTranslator.INSTANCE.prettify(textResult);
    return textResult;
  }
 public void checkSourceClassConflicts() {
   final PsiElement[] children = myClass.getChildren();
   for (PsiElement child : children) {
     if (child instanceof PsiMember && !myMovedMembers.contains(child)) {
       child.accept(new UsedMovedMembersConflictsCollector(child));
     }
   }
 }
Esempio n. 3
0
 @NotNull
 private List<Member> getMembers(@NotNull PsiClass psiClass) {
   List<Member> members = new LinkedList<Member>();
   for (PsiElement e : psiClass.getChildren()) {
     if (e instanceof PsiMethod) {
       members.add(methodToFunction((PsiMethod) e, true));
     } else if (e instanceof PsiField) {
       members.add(fieldToField((PsiField) e, psiClass));
     } else if (e instanceof PsiClass) {
       members.add(classToClass((PsiClass) e));
     } else if (e instanceof PsiClassInitializer) {
       members.add(initializerToInitializer((PsiClassInitializer) e));
     } else if (e instanceof PsiMember) {
       // System.out.println(e.getClass() + " " + e.getText());
     }
   }
   return members;
 }