Search in sources :

Example 11 with VariableData

use of com.intellij.refactoring.util.VariableData in project intellij-community by JetBrains.

the class IntroduceParameterObjectDialog method createParametersPanel.

protected ParameterTablePanel createParametersPanel() {
    final PsiParameterList parameterList = mySourceMethod.getParameterList();
    final PsiParameter[] parameters = parameterList.getParameters();
    VariableData[] parameterInfo = new VariableData[parameters.length];
    for (int i = 0; i < parameterInfo.length; i++) {
        parameterInfo[i] = new VariableData(parameters[i]);
        parameterInfo[i].name = parameters[i].getName();
        parameterInfo[i].passAsParameter = true;
    }
    return new ParameterTablePanel(myProject, parameterInfo) {

        protected void updateSignature() {
        }

        protected void doEnterAction() {
        }

        protected void doCancelAction() {
            IntroduceParameterObjectDialog.this.doCancelAction();
        }
    };
}
Also used : VariableData(com.intellij.refactoring.util.VariableData) ParameterTablePanel(com.intellij.refactoring.util.ParameterTablePanel)

Example 12 with VariableData

use of com.intellij.refactoring.util.VariableData in project intellij-community by JetBrains.

the class ExtractMethodDialog method checkMethodConflicts.

protected void checkMethodConflicts(MultiMap<PsiElement, String> conflicts) {
    PsiMethod prototype;
    try {
        PsiElementFactory factory = JavaPsiFacade.getInstance(myProject).getElementFactory();
        prototype = factory.createMethod(myNameField.getEnteredName().trim(), myReturnType);
        if (myTypeParameterList != null)
            prototype.getTypeParameterList().replace(myTypeParameterList);
        for (VariableData data : myInputVariables) {
            if (data.passAsParameter) {
                prototype.getParameterList().add(factory.createParameter(data.name, data.type));
            }
        }
        // set the modifiers with which the method is supposed to be created
        PsiUtil.setModifierProperty(prototype, PsiModifier.PRIVATE, true);
    } catch (IncorrectOperationException e) {
        return;
    }
    ConflictsUtil.checkMethodConflicts(myTargetClass, null, prototype, conflicts);
}
Also used : IncorrectOperationException(com.intellij.util.IncorrectOperationException) VariableData(com.intellij.refactoring.util.VariableData)

Example 13 with VariableData

use of com.intellij.refactoring.util.VariableData in project intellij-community by JetBrains.

the class ExtractMethodDialog method createOptionsPanel.

protected JPanel createOptionsPanel() {
    final JPanel optionsPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 5));
    if (myStaticFlag || myCanBeStatic) {
        myMakeStatic.setEnabled(!myStaticFlag);
        myMakeStatic.setSelected(myStaticFlag);
        if (myVariableData.hasInstanceFields()) {
            myMakeStatic.setText(RefactoringBundle.message("declare.static.pass.fields.checkbox"));
        }
        myMakeStatic.addItemListener(e -> {
            if (myVariableData.hasInstanceFields()) {
                myVariableData.setPassFields(myMakeStatic.isSelected());
                myInputVariables = myVariableData.getInputVariables().toArray(new VariableData[myVariableData.getInputVariables().size()]);
                updateVarargsEnabled();
                createParametersPanel();
            }
            updateSignature();
        });
        optionsPanel.add(myMakeStatic);
    } else {
        myMakeStatic.setSelected(false);
        myMakeStatic.setEnabled(false);
    }
    final Border emptyBorder = IdeBorderFactory.createEmptyBorder(5, 0, 5, 4);
    myMakeStatic.setBorder(emptyBorder);
    myFoldParameters.setSelected(myVariableData.isFoldingSelectedByDefault());
    myFoldParameters.setVisible(myVariableData.isFoldable());
    myVariableData.setFoldingAvailable(myFoldParameters.isSelected());
    myInputVariables = myVariableData.getInputVariables().toArray(new VariableData[myVariableData.getInputVariables().size()]);
    myFoldParameters.addActionListener(e -> {
        myVariableData.setFoldingAvailable(myFoldParameters.isSelected());
        myInputVariables = myVariableData.getInputVariables().toArray(new VariableData[myVariableData.getInputVariables().size()]);
        updateVarargsEnabled();
        createParametersPanel();
        updateSignature();
    });
    optionsPanel.add(myFoldParameters);
    myFoldParameters.setBorder(emptyBorder);
    boolean canBeVarargs = false;
    for (VariableData data : myInputVariables) {
        canBeVarargs |= data.type instanceof PsiArrayType;
    }
    if (myVariableData.isFoldable()) {
        canBeVarargs |= myVariableData.isFoldingSelectedByDefault();
    }
    if (canBeVarargs) {
        myMakeVarargs = new NonFocusableCheckBox(RefactoringBundle.message("declare.varargs.checkbox"));
        myMakeVarargs.setBorder(emptyBorder);
        updateVarargsEnabled();
        myMakeVarargs.addItemListener(e -> updateSignature());
        myMakeVarargs.setSelected(false);
        optionsPanel.add(myMakeVarargs);
    }
    if (myNullness != null && myNullness != Nullness.UNKNOWN) {
        final boolean isSelected = PropertiesComponent.getInstance(myProject).getBoolean(EXTRACT_METHOD_GENERATE_ANNOTATIONS, true);
        myGenerateAnnotations = new JCheckBox(RefactoringBundle.message("declare.generated.annotations"), isSelected);
        myGenerateAnnotations.addItemListener(e -> updateSignature());
        optionsPanel.add(myGenerateAnnotations);
    }
    if (myCbChainedConstructor != null) {
        optionsPanel.add(myCbChainedConstructor);
        myCbChainedConstructor.setBorder(emptyBorder);
        myCbChainedConstructor.addItemListener(e -> {
            if (myDefaultVisibility) {
                myChangingVisibility = true;
                try {
                    if (isChainedConstructor()) {
                        myVisibilityPanel.setVisibility(VisibilityUtil.getVisibilityModifier(myTargetClass.getModifierList()));
                    } else {
                        myVisibilityPanel.setVisibility(PsiModifier.PRIVATE);
                    }
                } finally {
                    myChangingVisibility = false;
                }
            }
            update();
        });
    }
    return optionsPanel;
}
Also used : NonFocusableCheckBox(com.intellij.ui.NonFocusableCheckBox) VariableData(com.intellij.refactoring.util.VariableData) Border(javax.swing.border.Border)

Example 14 with VariableData

use of com.intellij.refactoring.util.VariableData in project intellij-community by JetBrains.

the class ExtractMethodDialog method doOKAction.

@Override
protected void doOKAction() {
    MultiMap<PsiElement, String> conflicts = new MultiMap<>();
    checkMethodConflicts(conflicts);
    if (!conflicts.isEmpty()) {
        final ConflictsDialog conflictsDialog = new ConflictsDialog(myProject, conflicts);
        if (!conflictsDialog.showAndGet()) {
            if (conflictsDialog.isShowConflicts())
                close(CANCEL_EXIT_CODE);
            return;
        }
    }
    if (myMakeVarargs != null && myMakeVarargs.isSelected()) {
        final VariableData data = myInputVariables[myInputVariables.length - 1];
        if (data.type instanceof PsiArrayType) {
            data.type = new PsiEllipsisType(((PsiArrayType) data.type).getComponentType());
        }
    }
    final PsiMethod containingMethod = getContainingMethod();
    if (containingMethod != null && containingMethod.hasModifierProperty(PsiModifier.PUBLIC)) {
        PropertiesComponent.getInstance(myProject).setValue(EXTRACT_METHOD_DEFAULT_VISIBILITY, getVisibility());
    }
    if (myGenerateAnnotations != null && myGenerateAnnotations.isEnabled()) {
        PropertiesComponent.getInstance(myProject).setValue(EXTRACT_METHOD_GENERATE_ANNOTATIONS, myGenerateAnnotations.isSelected(), true);
    }
    super.doOKAction();
}
Also used : MultiMap(com.intellij.util.containers.MultiMap) VariableData(com.intellij.refactoring.util.VariableData)

Example 15 with VariableData

use of com.intellij.refactoring.util.VariableData in project intellij-community by JetBrains.

the class ExtractMethodSignatureSuggester method restoreRenamedParams.

private void restoreRenamedParams(List<PsiExpression> copies, ParametersFolder folder) {
    final Map<String, String> renameMap = new HashMap<>();
    for (VariableData data : myVariableData) {
        final String replacement = folder.getGeneratedCallArgument(data);
        if (!data.name.equals(replacement)) {
            renameMap.put(data.name, replacement);
        }
    }
    if (!renameMap.isEmpty()) {
        for (PsiExpression currentExpression : copies) {
            final Map<PsiReferenceExpression, String> params = new HashMap<>();
            currentExpression.accept(new JavaRecursiveElementWalkingVisitor() {

                @Override
                public void visitReferenceExpression(PsiReferenceExpression expression) {
                    super.visitReferenceExpression(expression);
                    final PsiElement resolve = expression.resolve();
                    if (resolve instanceof PsiParameter && myExtractedMethod.equals(((PsiParameter) resolve).getDeclarationScope())) {
                        final String name = ((PsiParameter) resolve).getName();
                        final String variable = renameMap.get(name);
                        if (renameMap.containsKey(name)) {
                            params.put(expression, variable);
                        }
                    }
                }
            });
            for (PsiReferenceExpression expression : params.keySet()) {
                final String var = params.get(expression);
                expression.replace(myElementFactory.createExpressionFromText(var, expression));
            }
        }
    }
}
Also used : THashMap(gnu.trove.THashMap) VariableData(com.intellij.refactoring.util.VariableData)

Aggregations

VariableData (com.intellij.refactoring.util.VariableData)20 ArrayList (java.util.ArrayList)4 PsiElement (com.intellij.psi.PsiElement)3 JavaCodeStyleManager (com.intellij.psi.codeStyle.JavaCodeStyleManager)2 Settings (com.intellij.refactoring.makeStatic.Settings)2 MultiMap (com.intellij.util.containers.MultiMap)2 THashMap (gnu.trove.THashMap)2 NullableNotNullManager (com.intellij.codeInsight.NullableNotNullManager)1 Editor (com.intellij.openapi.editor.Editor)1 DocumentAdapter (com.intellij.openapi.editor.event.DocumentAdapter)1 DocumentEvent (com.intellij.openapi.editor.event.DocumentEvent)1 Project (com.intellij.openapi.project.Project)1 PsiClass (com.intellij.psi.PsiClass)1 PsiExpression (com.intellij.psi.PsiExpression)1 PsiFile (com.intellij.psi.PsiFile)1 PsiMethod (com.intellij.psi.PsiMethod)1 CodeStyleSettings (com.intellij.psi.codeStyle.CodeStyleSettings)1 ParameterInfoImpl (com.intellij.refactoring.changeSignature.ParameterInfoImpl)1 ExtractMethodProcessor (com.intellij.refactoring.extractMethod.ExtractMethodProcessor)1 MakeClassStaticProcessor (com.intellij.refactoring.makeStatic.MakeClassStaticProcessor)1