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; }
/** Create ViewHolder for adapters with injections */ protected void generateAdapter() { // view holder class String holderClassName = Utils.getViewHolderClassName(); StringBuilder holderBuilder = new StringBuilder(); // generator of view holder class StringBuilder generator = new StringBuilder(); generator.append("public " + holderClassName + "(android.view.View rootView) {\n"); // rootView String rootViewName = "rootView"; holderBuilder.append("public " + "android.view.View " + rootViewName + ";\n"); generator.append("this." + rootViewName + " = " + rootViewName + ";\n"); for (Element element : mElements) { if (!element.used) { continue; } // field holderBuilder.append("public " + element.name + " " + element.getFieldName() + ";\n"); // findViewById in generator generator.append( "this." + element.getFieldName() + " = (" + element.name + ") " + rootViewName + ".findViewById(" + element.getFullID() + ");\n"); } generator.append("}\n"); holderBuilder.append(generator.toString()); PsiClass viewHolder = mFactory.createClassFromText(holderBuilder.toString(), mClass); viewHolder.setName(holderClassName); mClass.add(viewHolder); mClass.addBefore( mFactory.createKeyword("public", mClass), mClass.findInnerClassByName(holderClassName, true)); mClass.addBefore( mFactory.createKeyword("static", mClass), mClass.findInnerClassByName(holderClassName, true)); }