use of com.intellij.refactoring.replaceConstructorWithBuilder.ParameterData in project intellij-community by JetBrains.
the class ReplaceConstructorWithBuilderTest method doTest.
private void doTest(final boolean createNewBuilderClass, final Map<String, String> expectedDefaults, final String conflicts, final String packageName) throws Exception {
doTest((rootDir, rootAfter) -> {
final PsiClass aClass = myJavaFacade.findClass("Test", GlobalSearchScope.projectScope(getProject()));
assertNotNull("Class Test not found", aClass);
final LinkedHashMap<String, ParameterData> map = new LinkedHashMap<>();
final PsiMethod[] constructors = aClass.getConstructors();
for (PsiMethod constructor : constructors) {
ParameterData.createFromConstructor(constructor, "set", map);
}
if (expectedDefaults != null) {
for (Map.Entry<String, String> entry : expectedDefaults.entrySet()) {
final ParameterData parameterData = map.get(entry.getKey());
assertNotNull(parameterData);
assertEquals(entry.getValue(), parameterData.getDefaultValue());
}
}
try {
new ReplaceConstructorWithBuilderProcessor(getProject(), constructors, map, "Builder", packageName, null, createNewBuilderClass).run();
if (conflicts != null) {
fail("Conflicts were not detected:" + conflicts);
}
} catch (BaseRefactoringProcessor.ConflictsInTestsException e) {
if (conflicts == null) {
fail("Conflict detected:" + e.getMessage());
}
}
LocalFileSystem.getInstance().refresh(false);
FileDocumentManager.getInstance().saveAllDocuments();
});
}
use of com.intellij.refactoring.replaceConstructorWithBuilder.ParameterData in project intellij-community by JetBrains.
the class ReplaceConstructorWithSettersChainInfo method fixUsage.
public void fixUsage() throws IncorrectOperationException {
final PsiNewExpression expr = (PsiNewExpression) getElement();
if (expr != null) {
final PsiElementFactory elementFactory = JavaPsiFacade.getInstance(expr.getProject()).getElementFactory();
final PsiMethod constructor = expr.resolveConstructor();
if (constructor != null) {
StringBuffer buf = new StringBuffer();
final PsiExpressionList argumentList = expr.getArgumentList();
if (argumentList != null) {
final PsiExpression[] args = argumentList.getExpressions();
final PsiParameter[] parameters = constructor.getParameterList().getParameters();
final JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(constructor.getProject());
for (int i = 0; i < Math.min(constructor.getParameterList().getParametersCount(), args.length); i++) {
String arg = args[i].getText();
if (parameters[i].isVarArgs()) {
for (int ia = i + 1; ia < args.length; ia++) {
arg += ", " + args[ia].getText();
}
}
final String pureParamName = styleManager.variableNameToPropertyName(parameters[i].getName(), VariableKind.PARAMETER);
final ParameterData data = myParametersMap.get(pureParamName);
if (!Comparing.strEqual(arg, data.getDefaultValue()) || data.isInsertSetter()) {
buf.append(data.getSetterName()).append("(").append(arg).append(").");
}
}
final PsiExpression settersChain = elementFactory.createExpressionFromText("new " + myBuilderClass + "()." + buf.toString() + "create" + StringUtil.capitalize(constructor.getName()) + "()", null);
styleManager.shortenClassReferences(expr.replace(settersChain));
}
}
}
}
Aggregations