use of com.theoryinpractice.testng.TestNGFramework in project intellij-community by JetBrains.
the class TestNGDataProviderInspection method createMethodFix.
private static CreateMethodQuickFix createMethodFix(PsiAnnotationMemberValue provider, @NotNull PsiClass providerClass, PsiClass topLevelClass) {
final String name = StringUtil.unquoteString(provider.getText());
FileTemplateDescriptor templateDesc = new TestNGFramework().getParametersMethodFileTemplateDescriptor();
assert templateDesc != null;
final FileTemplate fileTemplate = FileTemplateManager.getInstance(provider.getProject()).getCodeTemplate(templateDesc.getFileName());
String body = "";
try {
final Properties attributes = new Properties();
attributes.put(FileTemplate.ATTRIBUTE_NAME, name);
body = fileTemplate.getText(attributes);
body = body.replace("${BODY}\n", "");
final PsiMethod methodFromTemplate = JavaPsiFacade.getElementFactory(providerClass.getProject()).createMethodFromText(body, providerClass);
final PsiCodeBlock methodBody = methodFromTemplate.getBody();
if (methodBody != null) {
body = StringUtil.trimEnd(StringUtil.trimStart(methodBody.getText(), "{"), "}");
} else {
body = "";
}
} catch (Exception ignored) {
}
if (StringUtil.isEmptyOrSpaces(body)) {
body = "return new Object[][]{};";
}
String signature = "@" + DataProvider.class.getName() + " public ";
if (providerClass == topLevelClass) {
signature += "static ";
}
signature += "Object[][] " + name + "()";
return CreateMethodQuickFix.createFix(providerClass, signature, body);
}
Aggregations