Search in sources :

Example 1 with ParameterData

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();
    });
}
Also used : ReplaceConstructorWithBuilderProcessor(com.intellij.refactoring.replaceConstructorWithBuilder.ReplaceConstructorWithBuilderProcessor) PsiMethod(com.intellij.psi.PsiMethod) PsiClass(com.intellij.psi.PsiClass) ParameterData(com.intellij.refactoring.replaceConstructorWithBuilder.ParameterData) HashMap(com.intellij.util.containers.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with ParameterData

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));
            }
        }
    }
}
Also used : ParameterData(com.intellij.refactoring.replaceConstructorWithBuilder.ParameterData) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager)

Aggregations

ParameterData (com.intellij.refactoring.replaceConstructorWithBuilder.ParameterData)2 PsiClass (com.intellij.psi.PsiClass)1 PsiMethod (com.intellij.psi.PsiMethod)1 JavaCodeStyleManager (com.intellij.psi.codeStyle.JavaCodeStyleManager)1 ReplaceConstructorWithBuilderProcessor (com.intellij.refactoring.replaceConstructorWithBuilder.ReplaceConstructorWithBuilderProcessor)1 HashMap (com.intellij.util.containers.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1