@Before public void setUp() throws Exception { project = mock(Project.class); psiElementFactory = mock(PsiElementFactory.class); application = mock(Application.class); mockStatic(PsiElementFactory.SERVICE.class, ApplicationManager.class); when(PsiElementFactory.SERVICE.getInstance(project)).thenReturn(psiElementFactory); when(ApplicationManager.getApplication()).thenReturn(application); addImportService = new AddImportServiceImpl(); }
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; }
/** * Instantiates a new Abstract java doc generator. * * @param project the Project */ public AbstractJavaDocGenerator(@NotNull Project project) { docTemplateManager = ServiceManager.getService(project, DocTemplateManager.class); docTemplateProcessor = ServiceManager.getService(project, DocTemplateProcessor.class); psiElementFactory = PsiElementFactory.SERVICE.getInstance(project); settings = ServiceManager.getService(project, JavaDocConfiguration.class); }